From 67a2889c6f9c749420d6db776949571ed9e1542a Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 22 May 2020 21:35:55 +0200 Subject: [PATCH] Export coin/coins like in cosmwasm-std --- packages/bcp/src/decode.spec.ts | 4 ++-- packages/bcp/src/decode.ts | 6 +++--- packages/bcp/src/encode.ts | 4 ++-- packages/bcp/types/decode.d.ts | 6 +++--- packages/bcp/types/encode.d.ts | 4 ++-- packages/sdk/src/coins.ts | 14 ++++++++++++++ packages/sdk/src/cosmwasmclient.searchtx.spec.ts | 10 ++-------- packages/sdk/src/cosmwasmclient.ts | 3 ++- packages/sdk/src/index.ts | 1 + packages/sdk/src/restclient.spec.ts | 2 +- packages/sdk/src/restclient.ts | 3 ++- packages/sdk/src/signingcosmwasmclient.spec.ts | 2 +- packages/sdk/src/signingcosmwasmclient.ts | 14 +++++--------- packages/sdk/src/types.ts | 7 ++----- packages/sdk/types/coins.d.ts | 8 ++++++++ packages/sdk/types/cosmwasmclient.d.ts | 3 ++- packages/sdk/types/index.d.ts | 1 + packages/sdk/types/restclient.d.ts | 3 ++- packages/sdk/types/signingcosmwasmclient.d.ts | 3 ++- packages/sdk/types/types.d.ts | 5 +---- 20 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 packages/sdk/src/coins.ts create mode 100644 packages/sdk/types/coins.d.ts diff --git a/packages/bcp/src/decode.spec.ts b/packages/bcp/src/decode.spec.ts index 5ff2d3ff..c90d9011 100644 --- a/packages/bcp/src/decode.spec.ts +++ b/packages/bcp/src/decode.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { IndexedTx, types } from "@cosmwasm/sdk"; +import { Coin, IndexedTx, types } from "@cosmwasm/sdk"; import { Address, Algorithm, isSendTransaction, SendTransaction, TokenTicker } from "@iov/bcp"; import { Encoding } from "@iov/encoding"; import { assert } from "@iov/utils"; @@ -135,7 +135,7 @@ describe("decode", () => { describe("decodeAmount", () => { it("works", () => { - const amount: types.Coin = { + const amount: Coin = { denom: "uatom", amount: "11657995", }; diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index f293eda6..0a617c65 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -1,4 +1,4 @@ -import { IndexedTx, types } from "@cosmwasm/sdk"; +import { Coin, IndexedTx, types } from "@cosmwasm/sdk"; import { Address, Algorithm, @@ -54,7 +54,7 @@ export function decodeFullSignature(signature: types.StdSignature, nonce: number }; } -export function coinToDecimal(tokens: BankTokens, coin: types.Coin): readonly [Decimal, string] { +export function coinToDecimal(tokens: BankTokens, coin: Coin): readonly [Decimal, string] { const match = tokens.find(({ denom }) => denom === coin.denom); if (!match) { throw Error(`unknown denom: ${coin.denom}`); @@ -63,7 +63,7 @@ export function coinToDecimal(tokens: BankTokens, coin: types.Coin): readonly [D return [value, match.ticker]; } -export function decodeAmount(tokens: BankTokens, coin: types.Coin): Amount { +export function decodeAmount(tokens: BankTokens, coin: Coin): Amount { const [value, ticker] = coinToDecimal(tokens, coin); return { quantity: value.atomics, diff --git a/packages/bcp/src/encode.ts b/packages/bcp/src/encode.ts index 8336463e..afabf588 100644 --- a/packages/bcp/src/encode.ts +++ b/packages/bcp/src/encode.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { encodeSecp256k1Pubkey, encodeSecp256k1Signature, types } from "@cosmwasm/sdk"; +import { Coin, encodeSecp256k1Pubkey, encodeSecp256k1Signature, types } from "@cosmwasm/sdk"; import { Algorithm, Amount, @@ -40,7 +40,7 @@ export function toErc20Amount(amount: Amount, erc20Token: Erc20Token): string { return amount.quantity; } -export function toBankCoin(amount: Amount, tokens: BankTokens): types.Coin { +export function toBankCoin(amount: Amount, tokens: BankTokens): Coin { const match = tokens.find((token) => token.ticker === amount.tokenTicker); if (!match) throw Error(`unknown ticker: ${amount.tokenTicker}`); if (match.fractionalDigits !== amount.fractionalDigits) { diff --git a/packages/bcp/types/decode.d.ts b/packages/bcp/types/decode.d.ts index 906d4862..f85f1367 100644 --- a/packages/bcp/types/decode.d.ts +++ b/packages/bcp/types/decode.d.ts @@ -1,4 +1,4 @@ -import { IndexedTx, types } from "@cosmwasm/sdk"; +import { Coin, IndexedTx, types } from "@cosmwasm/sdk"; import { Amount, ChainId, @@ -17,8 +17,8 @@ import { BankTokens, Erc20Token } from "./types"; export declare function decodePubkey(pubkey: types.PubKey): PubkeyBundle; export declare function decodeSignature(signature: string): SignatureBytes; export declare function decodeFullSignature(signature: types.StdSignature, nonce: number): FullSignature; -export declare function coinToDecimal(tokens: BankTokens, coin: types.Coin): readonly [Decimal, string]; -export declare function decodeAmount(tokens: BankTokens, coin: types.Coin): Amount; +export declare function coinToDecimal(tokens: BankTokens, coin: Coin): readonly [Decimal, string]; +export declare function decodeAmount(tokens: BankTokens, coin: Coin): Amount; export declare function parseMsg( msg: types.Msg, memo: string | undefined, diff --git a/packages/bcp/types/encode.d.ts b/packages/bcp/types/encode.d.ts index 3255e55f..dbe50247 100644 --- a/packages/bcp/types/encode.d.ts +++ b/packages/bcp/types/encode.d.ts @@ -1,9 +1,9 @@ -import { types } from "@cosmwasm/sdk"; +import { Coin, types } from "@cosmwasm/sdk"; import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp"; import { BankTokens, Erc20Token } from "./types"; export declare function encodePubkey(pubkey: PubkeyBundle): types.PubKey; export declare function toErc20Amount(amount: Amount, erc20Token: Erc20Token): string; -export declare function toBankCoin(amount: Amount, tokens: BankTokens): types.Coin; +export declare function toBankCoin(amount: Amount, tokens: BankTokens): Coin; export declare function encodeFee(fee: Fee, tokens: BankTokens): types.StdFee; export declare function encodeFullSignature(fullSignature: FullSignature): types.StdSignature; export declare function buildUnsignedTx( diff --git a/packages/sdk/src/coins.ts b/packages/sdk/src/coins.ts new file mode 100644 index 00000000..62f5f9ce --- /dev/null +++ b/packages/sdk/src/coins.ts @@ -0,0 +1,14 @@ +export interface Coin { + readonly denom: string; + readonly amount: string; +} + +/** Creates a coin */ +export function coin(amount: number, denom: string): Coin { + return { amount: amount.toString(), denom: denom }; +} + +/** Creates a list of coins with one element */ +export function coins(amount: number, denom: string): Coin[] { + return [coin(amount, denom)]; +} diff --git a/packages/sdk/src/cosmwasmclient.searchtx.spec.ts b/packages/sdk/src/cosmwasmclient.searchtx.spec.ts index 29de30ee..cfe91678 100644 --- a/packages/sdk/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/sdk/src/cosmwasmclient.searchtx.spec.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/camelcase */ import { assert, sleep } from "@iov/utils"; +import { Coin } from "./coins"; import { CosmWasmClient } from "./cosmwasmclient"; import { makeSignBytes } from "./encoding"; import { Secp256k1Pen } from "./pen"; @@ -15,14 +16,7 @@ import { wasmd, wasmdEnabled, } from "./testutils.spec"; -import { - Coin, - CosmosSdkTx, - isMsgExecuteContract, - isMsgInstantiateContract, - isMsgSend, - MsgSend, -} from "./types"; +import { CosmosSdkTx, isMsgExecuteContract, isMsgInstantiateContract, isMsgSend, MsgSend } from "./types"; describe("CosmWasmClient.searchTx", () => { let sendSuccessful: diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index 2802929b..73d81a45 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -1,10 +1,11 @@ import { Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; +import { Coin } from "./coins"; import { Log, parseLogs } from "./logs"; import { decodeBech32Pubkey } from "./pubkey"; import { BroadcastMode, RestClient } from "./restclient"; -import { Coin, CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types"; +import { CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types"; export interface GetNonceResult { readonly accountNumber: number; diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 9c3efaf3..5162bdab 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -3,6 +3,7 @@ import * as types from "./types"; export { logs, types }; export { pubkeyToAddress } from "./address"; +export { Coin, coin, coins } from "./coins"; export { unmarshalTx } from "./decoding"; export { makeSignBytes, marshalTx } from "./encoding"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient"; diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 5e968dc9..b65a9d90 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -5,6 +5,7 @@ import { assert, sleep } from "@iov/utils"; import { ReadonlyDate } from "readonly-date"; import { rawSecp256k1PubkeyToAddress } from "./address"; +import { Coin } from "./coins"; import { makeSignBytes } from "./encoding"; import { findAttribute, parseLogs } from "./logs"; import { makeCosmoshubPath, Pen, Secp256k1Pen } from "./pen"; @@ -31,7 +32,6 @@ import { wasmdEnabled, } from "./testutils.spec"; import { - Coin, isMsgInstantiateContract, isMsgStoreCode, Msg, diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 56775465..cb6316e9 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -1,7 +1,8 @@ import { Encoding, isNonNullObject } from "@iov/encoding"; import axios, { AxiosError, AxiosInstance } from "axios"; -import { Coin, CosmosSdkTx, JsonObject, Model, parseWasmData, StdTx, WasmData } from "./types"; +import { Coin } from "./coins"; +import { CosmosSdkTx, JsonObject, Model, parseWasmData, StdTx, WasmData } from "./types"; const { fromBase64, fromUtf8, toHex, toUtf8 } = Encoding; diff --git a/packages/sdk/src/signingcosmwasmclient.spec.ts b/packages/sdk/src/signingcosmwasmclient.spec.ts index 00bc77da..00fe65eb 100644 --- a/packages/sdk/src/signingcosmwasmclient.spec.ts +++ b/packages/sdk/src/signingcosmwasmclient.spec.ts @@ -2,12 +2,12 @@ import { Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { assert } from "@iov/utils"; +import { Coin } from "./coins"; import { PrivateCosmWasmClient } from "./cosmwasmclient"; import { Secp256k1Pen } from "./pen"; import { RestClient } from "./restclient"; import { SigningCosmWasmClient, UploadMeta } from "./signingcosmwasmclient"; import { getHackatom, makeRandomAddress, pendingWithoutWasmd } from "./testutils.spec"; -import { Coin } from "./types"; const { toHex } = Encoding; diff --git a/packages/sdk/src/signingcosmwasmclient.ts b/packages/sdk/src/signingcosmwasmclient.ts index 8b21422c..e0daf56c 100644 --- a/packages/sdk/src/signingcosmwasmclient.ts +++ b/packages/sdk/src/signingcosmwasmclient.ts @@ -3,12 +3,12 @@ import { Encoding } from "@iov/encoding"; import pako from "pako"; import { isValidBuilder } from "./builder"; +import { Coin, coins } from "./coins"; import { Account, CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient"; import { makeSignBytes } from "./encoding"; import { findAttribute, Log } from "./logs"; import { BroadcastMode } from "./restclient"; import { - Coin, MsgExecuteContract, MsgInstantiateContract, MsgSend, @@ -28,10 +28,6 @@ export interface FeeTable { readonly send: StdFee; } -function singleAmount(amount: number, denom: string): readonly Coin[] { - return [{ amount: amount.toString(), denom: denom }]; -} - function prepareBuilder(buider: string | undefined): string { if (buider === undefined) { return ""; // normalization needed by backend @@ -43,19 +39,19 @@ function prepareBuilder(buider: string | undefined): string { const defaultFees: FeeTable = { upload: { - amount: singleAmount(25000, "ucosm"), + amount: coins(25000, "ucosm"), gas: "1000000", // one million }, init: { - amount: singleAmount(12500, "ucosm"), + amount: coins(12500, "ucosm"), gas: "500000", // 500k }, exec: { - amount: singleAmount(5000, "ucosm"), + amount: coins(5000, "ucosm"), gas: "200000", // 200k }, send: { - amount: singleAmount(2000, "ucosm"), + amount: coins(2000, "ucosm"), gas: "80000", // 80k }, }; diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index c0b33c7d..0ab70b02 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,5 +1,7 @@ import { Encoding } from "@iov/encoding"; +import { Coin } from "./coins"; + const { fromBase64, fromHex } = Encoding; /** An Amino/Cosmos SDK StdTx */ @@ -119,11 +121,6 @@ export interface StdFee { readonly gas: string; } -export interface Coin { - readonly denom: string; - readonly amount: string; -} - export interface StdSignature { readonly pub_key: PubKey; readonly signature: string; diff --git a/packages/sdk/types/coins.d.ts b/packages/sdk/types/coins.d.ts new file mode 100644 index 00000000..9208f3e2 --- /dev/null +++ b/packages/sdk/types/coins.d.ts @@ -0,0 +1,8 @@ +export interface Coin { + readonly denom: string; + readonly amount: string; +} +/** Creates a coin */ +export declare function coin(amount: number, denom: string): Coin; +/** Creates a list of coins with one element */ +export declare function coins(amount: number, denom: string): Coin[]; diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index a5727e50..12ac73ec 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -1,6 +1,7 @@ +import { Coin } from "./coins"; import { Log } from "./logs"; import { BroadcastMode, RestClient } from "./restclient"; -import { Coin, CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types"; +import { CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types"; export interface GetNonceResult { readonly accountNumber: number; readonly sequence: number; diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 564817eb..474b2dd3 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -2,6 +2,7 @@ import * as logs from "./logs"; import * as types from "./types"; export { logs, types }; export { pubkeyToAddress } from "./address"; +export { Coin, coin, coins } from "./coins"; export { unmarshalTx } from "./decoding"; export { makeSignBytes, marshalTx } from "./encoding"; export { BroadcastMode, RestClient, TxsResponse } from "./restclient"; diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index 8c8cbf29..5d5deea4 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -1,4 +1,5 @@ -import { Coin, CosmosSdkTx, JsonObject, Model, StdTx } from "./types"; +import { Coin } from "./coins"; +import { CosmosSdkTx, JsonObject, Model, StdTx } from "./types"; export interface CosmosSdkAccount { /** Bech32 account address */ readonly address: string; diff --git a/packages/sdk/types/signingcosmwasmclient.d.ts b/packages/sdk/types/signingcosmwasmclient.d.ts index cb27ffdf..f6c34312 100644 --- a/packages/sdk/types/signingcosmwasmclient.d.ts +++ b/packages/sdk/types/signingcosmwasmclient.d.ts @@ -1,7 +1,8 @@ +import { Coin } from "./coins"; import { Account, CosmWasmClient, GetNonceResult, PostTxResult } from "./cosmwasmclient"; import { Log } from "./logs"; import { BroadcastMode } from "./restclient"; -import { Coin, StdFee, StdSignature } from "./types"; +import { StdFee, StdSignature } from "./types"; export interface SigningCallback { (signBytes: Uint8Array): Promise; } diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 63a777aa..aeaa640f 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -1,3 +1,4 @@ +import { Coin } from "./coins"; /** An Amino/Cosmos SDK StdTx */ export interface StdTx { readonly msg: ReadonlyArray; @@ -88,10 +89,6 @@ export interface StdFee { readonly amount: ReadonlyArray; readonly gas: string; } -export interface Coin { - readonly denom: string; - readonly amount: string; -} export interface StdSignature { readonly pub_key: PubKey; readonly signature: string;