Merge pull request #176 from CosmWasm/export-coin

Export coin/coins like in cosmwasm-std
This commit is contained in:
Simon Warta 2020-05-25 09:35:54 +02:00 committed by GitHub
commit 091e60556d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 58 additions and 45 deletions

View File

@ -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",
};

View File

@ -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,

View File

@ -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) {

View File

@ -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,

View File

@ -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(

14
packages/sdk/src/coins.ts Normal file
View File

@ -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)];
}

View File

@ -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:

View File

@ -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;

View File

@ -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";

View File

@ -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,

View File

@ -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;

View File

@ -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;

View File

@ -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
},
};

View File

@ -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;

8
packages/sdk/types/coins.d.ts vendored Normal file
View File

@ -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[];

View File

@ -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;

View File

@ -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";

View File

@ -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;

View File

@ -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<StdSignature>;
}

View File

@ -1,3 +1,4 @@
import { Coin } from "./coins";
/** An Amino/Cosmos SDK StdTx */
export interface StdTx {
readonly msg: ReadonlyArray<Msg>;
@ -88,10 +89,6 @@ export interface StdFee {
readonly amount: ReadonlyArray<Coin>;
readonly gas: string;
}
export interface Coin {
readonly denom: string;
readonly amount: string;
}
export interface StdSignature {
readonly pub_key: PubKey;
readonly signature: string;