From 7440bfb6f14365067df51ff9a598a4c0b2dc5338 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 23 Jan 2020 14:29:02 +0100 Subject: [PATCH 1/2] Removed obsolete TODOS --- src/cosmoscodec.ts | 1 - src/cosmosconnection.ts | 9 ++++----- src/decode.ts | 6 ------ src/types.ts | 3 ++- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/cosmoscodec.ts b/src/cosmoscodec.ts index 9e122a8f9d..944737ea08 100644 --- a/src/cosmoscodec.ts +++ b/src/cosmoscodec.ts @@ -91,7 +91,6 @@ export class CosmosCodec implements TxCodec { throw new Error("Nonce is required"); } const parsed = unmarshalTx(bytes); - // TODO: this needs access to token list return parseTx(parsed, chainId, nonce, this.tokens); } diff --git a/src/cosmosconnection.ts b/src/cosmosconnection.ts index 76f67f459f..5948e52862 100644 --- a/src/cosmosconnection.ts +++ b/src/cosmosconnection.ts @@ -89,14 +89,13 @@ export class CosmosConnection implements BlockchainConnection { private readonly restClient: RestClient; private readonly chainData: ChainData; - private readonly primaryToken: Token; - - // TODO: deprecate this??? - private readonly supportedTokens: readonly Token[]; - private readonly _prefix: CosmosBech32Prefix; private readonly tokenInfo: TokenInfos; + // these are derived from arguments (cached for use in multiple functions) + private readonly primaryToken: Token; + private readonly supportedTokens: readonly Token[]; + private get prefix(): CosmosBech32Prefix { return this._prefix; } diff --git a/src/decode.ts b/src/decode.ts index af0f2187ec..ad5a828480 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -45,9 +45,6 @@ export function decodeFullSignature(signature: amino.StdSignature, nonce: number }; } -// TODO: this needs access to token list - we need something more like amountToCoin and coinToAmount here -// and wire that info all the way from both connection and codec. - // TODO: return null vs throw exception for undefined??? export const decodeAmount = (tokens: TokenInfos) => (coin: amino.Coin): Amount => { return coinToAmount(tokens, coin); @@ -69,7 +66,6 @@ export function parseMsg(msg: amino.Msg, chainId: ChainId, tokens: TokenInfos): chainId: chainId, sender: msgValue.from_address as Address, recipient: msgValue.to_address as Address, - // TODO: this needs access to token list amount: decodeAmount(tokens)(msgValue.amount[0]), }; } @@ -94,9 +90,7 @@ export function parseTx(tx: amino.Tx, chainId: ChainId, nonce: Nonce, tokens: To } const [primarySignature] = txValue.signatures.map(signature => decodeFullSignature(signature, nonce)); - // TODO: this needs access to token list const msg = parseMsg(txValue.msg[0], chainId, tokens); - // TODO: this needs access to token list const fee = parseFee(txValue.fee, tokens); const transaction = { diff --git a/src/types.ts b/src/types.ts index 039964130a..5e44a030a1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,7 +16,7 @@ export interface TokenInfo extends Token { export type TokenInfos = ReadonlyArray; -// TODO: alias amino types +// TODO: return null vs throw exception for undefined??? export function amountToCoin(lookup: ReadonlyArray, amount: Amount): amino.Coin { const match = lookup.find(({ tokenTicker }) => tokenTicker === amount.tokenTicker); if (!match) { @@ -28,6 +28,7 @@ export function amountToCoin(lookup: ReadonlyArray, amount: Amount): }; } +// TODO: return null vs throw exception for undefined??? export function coinToAmount(tokens: TokenInfos, coin: amino.Coin): Amount { const match = tokens.find(({ denom }) => denom === coin.denom); if (!match) { From 870d9acb66cc34339efdec2a1365a567a4ba91f1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 23 Jan 2020 14:36:36 +0100 Subject: [PATCH 2/2] throw error on failed post, plus format and lint --- package.json | 1 + src/cosmoscodec.ts | 2 +- src/cosmosconnection.spec.ts | 7 ++----- src/cosmosconnection.ts | 6 ++++-- src/cosmosconnector.ts | 2 +- src/decode.ts | 5 +---- src/encode.ts | 2 +- types/cosmosconnection.d.ts | 4 ++-- types/cosmosconnector.d.ts | 2 +- 9 files changed, 14 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 8ddeb511cd..98e3622811 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "docs": "shx rm -rf docs && typedoc --options typedoc.js", "format": "prettier --write --loglevel warn \"./src/**/*.ts\"", "lint": "eslint --max-warnings 0 \"**/*.{js,ts}\" && tslint -t verbose --project .", + "autolint": "eslint --max-warnings 0 \"**/*.{js,ts}\" --fix", "move-types": "shx rm -rf ./types/* && shx mv build/types/* ./types && rm -rf ./types/testdata && shx rm -f ./types/*.spec.d.ts", "format-types": "prettier --write --loglevel warn \"./types/**/*.d.ts\"", "build": "shx rm -rf ./build && tsc && yarn move-types && yarn format-types", diff --git a/src/cosmoscodec.ts b/src/cosmoscodec.ts index 944737ea08..88a74ea86a 100644 --- a/src/cosmoscodec.ts +++ b/src/cosmoscodec.ts @@ -18,7 +18,7 @@ import { Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { marshalTx, unmarshalTx } from "@tendermint/amino-js"; -import { isValidAddress, pubkeyToAddress, CosmosBech32Prefix } from "./address"; +import { CosmosBech32Prefix, isValidAddress, pubkeyToAddress } from "./address"; import { Caip5 } from "./caip5"; import { parseTx } from "./decode"; import { buildSignedTx, buildUnsignedTx } from "./encode"; diff --git a/src/cosmosconnection.spec.ts b/src/cosmosconnection.spec.ts index 0e5aa64a89..7d14072650 100644 --- a/src/cosmosconnection.spec.ts +++ b/src/cosmosconnection.spec.ts @@ -14,9 +14,9 @@ import { Secp256k1 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol"; -import { cosmosCodec, CosmosCodec } from "./cosmoscodec"; -import { CosmosConnection } from "./cosmosconnection"; import { CosmosBech32Prefix } from "./address"; +import { CosmosCodec, cosmosCodec } from "./cosmoscodec"; +import { CosmosConnection } from "./cosmosconnection"; import { TokenInfos } from "./types"; const { fromBase64, toHex } = Encoding; @@ -197,11 +197,9 @@ describe("CosmosConnection", () => { const nonce = await connection.getNonce({ address: faucetAddress }); // TODO: we need to use custom codecs everywhere const codec = new CosmosCodec(defaultPrefix, defaultTokens); - console.log("nonce:", nonce); const signed = await profile.signTransaction(faucet, unsigned, codec, nonce); const postableBytes = codec.bytesToPost(signed); const response = await connection.postTx(postableBytes); - console.log(response); const { transactionId } = response; const blockInfo = await response.blockInfo.waitFor(info => !isBlockInfoPending(info)); expect(blockInfo.state).toEqual(TransactionState.Succeeded); @@ -263,7 +261,6 @@ describe("CosmosConnection", () => { const signed = await profile.signTransaction(faucet, unsigned, codec, nonce); const postableBytes = codec.bytesToPost(signed); const response = await connection.postTx(postableBytes); - console.log(response); const { transactionId } = response; const blockInfo = await response.blockInfo.waitFor(info => !isBlockInfoPending(info)); expect(blockInfo.state).toEqual(TransactionState.Succeeded); diff --git a/src/cosmosconnection.ts b/src/cosmosconnection.ts index 5948e52862..d882d7d79e 100644 --- a/src/cosmosconnection.ts +++ b/src/cosmosconnection.ts @@ -213,8 +213,10 @@ export class CosmosConnection implements BlockchainConnection { } public async postTx(tx: PostableBytes): Promise { - // TODO: we need to check errors here... bad chain-id breaks this - const { txhash, raw_log } = await this.restClient.postTx(tx); + const { code, txhash, raw_log } = await this.restClient.postTx(tx); + if (code !== 0) { + throw new Error(raw_log); + } const transactionId = txhash as TransactionId; const firstEvent: BlockInfo = { state: TransactionState.Pending }; let blockInfoInterval: NodeJS.Timeout; diff --git a/src/cosmosconnector.ts b/src/cosmosconnector.ts index e4b1371672..fd9c82e41e 100644 --- a/src/cosmosconnector.ts +++ b/src/cosmosconnector.ts @@ -1,8 +1,8 @@ import { ChainConnector, ChainId } from "@iov/bcp"; +import { CosmosBech32Prefix } from "./address"; import { cosmosCodec } from "./cosmoscodec"; import { CosmosConnection } from "./cosmosconnection"; -import { CosmosBech32Prefix } from "./address"; import { TokenInfos } from "./types"; /** diff --git a/src/decode.ts b/src/decode.ts index ad5a828480..4e2f57f305 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -12,7 +12,6 @@ import { SendTransaction, SignatureBytes, SignedTransaction, - TokenTicker, TransactionId, UnsignedTransaction, } from "@iov/bcp"; @@ -20,12 +19,10 @@ import { Encoding } from "@iov/encoding"; import amino from "@tendermint/amino-js"; import { TxsResponse } from "./restclient"; -import { isAminoStdTx, TokenInfos, coinToAmount } from "./types"; +import { coinToAmount, isAminoStdTx, TokenInfos } from "./types"; const { fromBase64 } = Encoding; -const atom = "ATOM" as TokenTicker; - export function decodePubkey(pubkey: amino.PubKey): PubkeyBundle { return { algo: Algorithm.Secp256k1, diff --git a/src/encode.ts b/src/encode.ts index 1c098084c2..42f2624f5b 100644 --- a/src/encode.ts +++ b/src/encode.ts @@ -13,7 +13,7 @@ import { Secp256k1 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import amino from "@tendermint/amino-js"; -import { AminoTx, TokenInfos, amountToCoin } from "./types"; +import { AminoTx, amountToCoin, TokenInfos } from "./types"; const { toBase64 } = Encoding; diff --git a/types/cosmosconnection.d.ts b/types/cosmosconnection.d.ts index 4c8e8f662a..de0cf74ad7 100644 --- a/types/cosmosconnection.d.ts +++ b/types/cosmosconnection.d.ts @@ -27,10 +27,10 @@ export declare class CosmosConnection implements BlockchainConnection { private static initialize; private readonly restClient; private readonly chainData; - private readonly primaryToken; - private readonly supportedTokens; private readonly _prefix; private readonly tokenInfo; + private readonly primaryToken; + private readonly supportedTokens; private get prefix(); private constructor(); disconnect(): void; diff --git a/types/cosmosconnector.d.ts b/types/cosmosconnector.d.ts index 509a806885..05a3d52830 100644 --- a/types/cosmosconnector.d.ts +++ b/types/cosmosconnector.d.ts @@ -1,6 +1,6 @@ import { ChainConnector, ChainId } from "@iov/bcp"; -import { CosmosConnection } from "./cosmosconnection"; import { CosmosBech32Prefix } from "./address"; +import { CosmosConnection } from "./cosmosconnection"; import { TokenInfos } from "./types"; /** * A helper to connect to a cosmos-based chain at a given url