From 6358b02f11147479ef6c177241932a692f25283d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 3 Feb 2020 18:33:14 +0100 Subject: [PATCH 1/2] Move TokenInfo, coinToDecimal, decimalToCoin from sdk to bcp --- packages/bcp/src/cosmwasmconnection.ts | 4 ++-- packages/bcp/src/decode.ts | 13 +++++++++++-- packages/bcp/src/encode.ts | 18 +++++++++++++++++- packages/bcp/src/types.ts | 16 +++++++++++++++- packages/bcp/types/cosmwasmconnection.d.ts | 2 +- packages/bcp/types/decode.d.ts | 2 ++ packages/bcp/types/encode.d.ts | 2 ++ packages/bcp/types/types.d.ts | 15 ++++++++++++++- packages/sdk/src/decoding.ts | 13 ------------- packages/sdk/src/encoding.ts | 20 -------------------- packages/sdk/src/index.ts | 6 +++--- packages/sdk/src/types.ts | 15 --------------- packages/sdk/types/decoding.d.ts | 7 ------- packages/sdk/types/encoding.d.ts | 8 -------- packages/sdk/types/index.d.ts | 4 +--- packages/sdk/types/types.d.ts | 14 -------------- 16 files changed, 68 insertions(+), 91 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index dd288a7d..53779038 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { RestClient, TokenInfo, TxsResponse } from "@cosmwasm/sdk"; +import { RestClient, TxsResponse } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -38,7 +38,7 @@ import { Stream } from "xstream"; import { CosmosBech32Prefix, decodeCosmosPubkey, pubkeyToAddress } from "./address"; import { Caip5 } from "./caip5"; import { decodeAmount, parseTxsResponse } from "./decode"; -import { accountToNonce } from "./types"; +import { accountToNonce, TokenInfo } from "./types"; interface ChainData { readonly chainId: ChainId; diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index f06b1e49..1343dc89 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -1,4 +1,4 @@ -import { coinToDecimal, isAminoStdTx, TxsResponse } from "@cosmwasm/sdk"; +import { isAminoStdTx, TxsResponse } from "@cosmwasm/sdk"; import { Address, Algorithm, @@ -17,7 +17,7 @@ import { TransactionId, UnsignedTransaction, } from "@iov/bcp"; -import { Encoding } from "@iov/encoding"; +import { Decimal, Encoding } from "@iov/encoding"; import amino from "@tendermint/amino-js"; import { TokenInfos } from "./types"; @@ -55,6 +55,15 @@ export function decodeFullSignature(signature: amino.StdSignature, nonce: number }; } +export function coinToDecimal(tokens: TokenInfos, coin: amino.Coin): readonly [Decimal, string] { + const match = tokens.find(({ denom }) => denom === coin.denom); + if (!match) { + throw Error(`unknown denom: ${coin.denom}`); + } + const value = Decimal.fromAtomics(coin.amount, match.fractionalDigits); + return [value, match.ticker]; +} + export function decodeAmount(tokens: TokenInfos, coin: amino.Coin): Amount { const [value, ticker] = coinToDecimal(tokens, coin); return { diff --git a/packages/bcp/src/encode.ts b/packages/bcp/src/encode.ts index 5fb4b842..5c6bc7e1 100644 --- a/packages/bcp/src/encode.ts +++ b/packages/bcp/src/encode.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { AminoTx, decimalToCoin } from "@cosmwasm/sdk"; +import { AminoTx } from "@cosmwasm/sdk"; import { Algorithm, Amount, @@ -35,6 +35,22 @@ export function encodePubkey(pubkey: PubkeyBundle): amino.PubKey { } } +export function decimalToCoin(lookup: TokenInfos, value: Decimal, ticker: string): amino.Coin { + const match = lookup.find(token => token.ticker === ticker); + if (!match) { + throw Error(`unknown ticker: ${ticker}`); + } + if (match.fractionalDigits !== value.fractionalDigits) { + throw new Error( + "Mismatch in fractional digits between token and value. If you really want, implement a conversion here. However, this indicates a bug in the caller code.", + ); + } + return { + denom: match.denom, + amount: value.atomics, + }; +} + export function encodeAmount(amount: Amount, tokens: TokenInfos): amino.Coin { return decimalToCoin( tokens, diff --git a/packages/bcp/src/types.ts b/packages/bcp/src/types.ts index 07003e90..32133828 100644 --- a/packages/bcp/src/types.ts +++ b/packages/bcp/src/types.ts @@ -1,6 +1,20 @@ -import { TokenInfo } from "@cosmwasm/sdk"; import { Nonce } from "@iov/bcp"; +export interface TokenInfo { + readonly denom: string; + readonly ticker: string; + /** + * The number of fractional digits the token supports. + * + * A quantity is expressed as atomic units. 10^fractionalDigits of those + * atomic units make up 1 token. + * + * E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000 + * the last 18 digits are the fractional part and the rest the wole part. + */ + readonly fractionalDigits: number; +} + export type TokenInfos = ReadonlyArray; // tslint:disable-next-line:no-bitwise diff --git a/packages/bcp/types/cosmwasmconnection.d.ts b/packages/bcp/types/cosmwasmconnection.d.ts index 4a8a3d83..764e9512 100644 --- a/packages/bcp/types/cosmwasmconnection.d.ts +++ b/packages/bcp/types/cosmwasmconnection.d.ts @@ -1,4 +1,3 @@ -import { TokenInfo } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -22,6 +21,7 @@ import { } from "@iov/bcp"; import { Stream } from "xstream"; import { CosmosBech32Prefix } from "./address"; +import { TokenInfo } from "./types"; export declare type TokenConfiguration = readonly (TokenInfo & { readonly name: string; })[]; diff --git a/packages/bcp/types/decode.d.ts b/packages/bcp/types/decode.d.ts index 68122fd9..8a30ebbb 100644 --- a/packages/bcp/types/decode.d.ts +++ b/packages/bcp/types/decode.d.ts @@ -12,11 +12,13 @@ import { SignedTransaction, UnsignedTransaction, } from "@iov/bcp"; +import { Decimal } from "@iov/encoding"; import amino from "@tendermint/amino-js"; import { TokenInfos } from "./types"; export declare function decodePubkey(pubkey: amino.PubKey): PubkeyBundle; export declare function decodeSignature(signature: string): SignatureBytes; export declare function decodeFullSignature(signature: amino.StdSignature, nonce: number): FullSignature; +export declare function coinToDecimal(tokens: TokenInfos, coin: amino.Coin): readonly [Decimal, string]; export declare function decodeAmount(tokens: TokenInfos, coin: amino.Coin): Amount; export declare function parseMsg(msg: amino.Msg, chainId: ChainId, tokens: TokenInfos): SendTransaction; export declare function parseFee(fee: amino.StdFee, tokens: TokenInfos): Fee; diff --git a/packages/bcp/types/encode.d.ts b/packages/bcp/types/encode.d.ts index 1068443a..dddaf1d2 100644 --- a/packages/bcp/types/encode.d.ts +++ b/packages/bcp/types/encode.d.ts @@ -1,8 +1,10 @@ import { AminoTx } from "@cosmwasm/sdk"; import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp"; +import { Decimal } from "@iov/encoding"; import amino from "@tendermint/amino-js"; import { TokenInfos } from "./types"; export declare function encodePubkey(pubkey: PubkeyBundle): amino.PubKey; +export declare function decimalToCoin(lookup: TokenInfos, value: Decimal, ticker: string): amino.Coin; export declare function encodeAmount(amount: Amount, tokens: TokenInfos): amino.Coin; export declare function encodeFee(fee: Fee, tokens: TokenInfos): amino.StdFee; export declare function encodeFullSignature(fullSignature: FullSignature): amino.StdSignature; diff --git a/packages/bcp/types/types.d.ts b/packages/bcp/types/types.d.ts index 8bdcbf3b..e8ac6ce2 100644 --- a/packages/bcp/types/types.d.ts +++ b/packages/bcp/types/types.d.ts @@ -1,5 +1,18 @@ -import { TokenInfo } from "@cosmwasm/sdk"; import { Nonce } from "@iov/bcp"; +export interface TokenInfo { + readonly denom: string; + readonly ticker: string; + /** + * The number of fractional digits the token supports. + * + * A quantity is expressed as atomic units. 10^fractionalDigits of those + * atomic units make up 1 token. + * + * E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000 + * the last 18 digits are the fractional part and the rest the wole part. + */ + readonly fractionalDigits: number; +} export declare type TokenInfos = ReadonlyArray; export interface NonceInfo { readonly account_number: string; diff --git a/packages/sdk/src/decoding.ts b/packages/sdk/src/decoding.ts index 701e1e69..e69de29b 100644 --- a/packages/sdk/src/decoding.ts +++ b/packages/sdk/src/decoding.ts @@ -1,13 +0,0 @@ -import { Decimal } from "@iov/encoding"; -import amino from "@tendermint/amino-js"; - -import { TokenInfo } from "./types"; - -export function coinToDecimal(tokens: readonly TokenInfo[], coin: amino.Coin): readonly [Decimal, string] { - const match = tokens.find(({ denom }) => denom === coin.denom); - if (!match) { - throw Error(`unknown denom: ${coin.denom}`); - } - const value = Decimal.fromAtomics(coin.amount, match.fractionalDigits); - return [value, match.ticker]; -} diff --git a/packages/sdk/src/encoding.ts b/packages/sdk/src/encoding.ts index c4bed170..e69de29b 100644 --- a/packages/sdk/src/encoding.ts +++ b/packages/sdk/src/encoding.ts @@ -1,20 +0,0 @@ -import { Decimal } from "@iov/encoding"; -import amino from "@tendermint/amino-js"; - -import { TokenInfo } from "./types"; - -export function decimalToCoin(lookup: readonly TokenInfo[], value: Decimal, ticker: string): amino.Coin { - const match = lookup.find(token => token.ticker === ticker); - if (!match) { - throw Error(`unknown ticker: ${ticker}`); - } - if (match.fractionalDigits !== value.fractionalDigits) { - throw new Error( - "Mismatch in fractional digits between token and value. If you really want, implement a conversion here. However, this indicates a bug in the caller code.", - ); - } - return { - denom: match.denom, - amount: value.atomics, - }; -} diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 888fb6c3..6cf13652 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,4 +1,4 @@ -export { coinToDecimal } from "./decoding"; -export { decimalToCoin } from "./encoding"; +// export { coinToDecimal } from "./decoding"; +// export { decimalToCoin } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; -export { AminoTx, isAminoStdTx, TokenInfo } from "./types"; +export { AminoTx, isAminoStdTx } from "./types"; diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 83be077d..c8a0ef72 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -8,18 +8,3 @@ export function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx { typeof memo === "string" && Array.isArray(msg) && typeof fee === "object" && Array.isArray(signatures) ); } - -export interface TokenInfo { - readonly denom: string; - readonly ticker: string; - /** - * The number of fractional digits the token supports. - * - * A quantity is expressed as atomic units. 10^fractionalDigits of those - * atomic units make up 1 token. - * - * E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000 - * the last 18 digits are the fractional part and the rest the wole part. - */ - readonly fractionalDigits: number; -} diff --git a/packages/sdk/types/decoding.d.ts b/packages/sdk/types/decoding.d.ts index 6c1552eb..e69de29b 100644 --- a/packages/sdk/types/decoding.d.ts +++ b/packages/sdk/types/decoding.d.ts @@ -1,7 +0,0 @@ -import { Decimal } from "@iov/encoding"; -import amino from "@tendermint/amino-js"; -import { TokenInfo } from "./types"; -export declare function coinToDecimal( - tokens: readonly TokenInfo[], - coin: amino.Coin, -): readonly [Decimal, string]; diff --git a/packages/sdk/types/encoding.d.ts b/packages/sdk/types/encoding.d.ts index c8e3334e..e69de29b 100644 --- a/packages/sdk/types/encoding.d.ts +++ b/packages/sdk/types/encoding.d.ts @@ -1,8 +0,0 @@ -import { Decimal } from "@iov/encoding"; -import amino from "@tendermint/amino-js"; -import { TokenInfo } from "./types"; -export declare function decimalToCoin( - lookup: readonly TokenInfo[], - value: Decimal, - ticker: string, -): amino.Coin; diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 888fb6c3..f8dbdc95 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -1,4 +1,2 @@ -export { coinToDecimal } from "./decoding"; -export { decimalToCoin } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; -export { AminoTx, isAminoStdTx, TokenInfo } from "./types"; +export { AminoTx, isAminoStdTx } from "./types"; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index e41597e4..cb5a44a9 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -3,17 +3,3 @@ export declare type AminoTx = amino.Tx & { readonly value: amino.StdTx; }; export declare function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx; -export interface TokenInfo { - readonly denom: string; - readonly ticker: string; - /** - * The number of fractional digits the token supports. - * - * A quantity is expressed as atomic units. 10^fractionalDigits of those - * atomic units make up 1 token. - * - * E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000 - * the last 18 digits are the fractional part and the rest the wole part. - */ - readonly fractionalDigits: number; -} From 6444be5aa4a8636201d692d427a574e1b8749957 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 3 Feb 2020 18:42:36 +0100 Subject: [PATCH 2/2] Remove obsolete comments --- packages/sdk/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 6cf13652..f8dbdc95 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,4 +1,2 @@ -// export { coinToDecimal } from "./decoding"; -// export { decimalToCoin } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; export { AminoTx, isAminoStdTx } from "./types";