Move decimalToCoin and coinToDecimal into SDK
This commit is contained in:
parent
e127058100
commit
9da9225345
@ -1,4 +1,4 @@
|
||||
import { isAminoStdTx, TxsResponse } from "@cosmwasm/sdk";
|
||||
import { coinToDecimal, isAminoStdTx, TxsResponse } from "@cosmwasm/sdk";
|
||||
import {
|
||||
Address,
|
||||
Algorithm,
|
||||
@ -20,7 +20,7 @@ import {
|
||||
import { Encoding } from "@iov/encoding";
|
||||
import amino from "@tendermint/amino-js";
|
||||
|
||||
import { coinToDecimal, TokenInfos } from "./types";
|
||||
import { TokenInfos } from "./types";
|
||||
|
||||
const { fromBase64 } = Encoding;
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
import { AminoTx } from "@cosmwasm/sdk";
|
||||
import { AminoTx, decimalToCoin } from "@cosmwasm/sdk";
|
||||
import {
|
||||
Algorithm,
|
||||
Amount,
|
||||
@ -14,7 +14,7 @@ import { Secp256k1 } from "@iov/crypto";
|
||||
import { Decimal, Encoding } from "@iov/encoding";
|
||||
import amino from "@tendermint/amino-js";
|
||||
|
||||
import { decimalToCoin, TokenInfos } from "./types";
|
||||
import { TokenInfos } from "./types";
|
||||
|
||||
const { toBase64 } = Encoding;
|
||||
|
||||
|
||||
@ -1,35 +1,8 @@
|
||||
import { TokenInfo } from "@cosmwasm/sdk";
|
||||
import { Nonce } from "@iov/bcp";
|
||||
import { Decimal } from "@iov/encoding";
|
||||
import amino from "@tendermint/amino-js";
|
||||
|
||||
export type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:no-bitwise
|
||||
const maxAcct = 1 << 23;
|
||||
// tslint:disable-next-line:no-bitwise
|
||||
|
||||
11
packages/bcp/types/types.d.ts
vendored
11
packages/bcp/types/types.d.ts
vendored
@ -1,17 +1,6 @@
|
||||
import { TokenInfo } from "@cosmwasm/sdk";
|
||||
import { Nonce } from "@iov/bcp";
|
||||
import { Decimal } from "@iov/encoding";
|
||||
import amino from "@tendermint/amino-js";
|
||||
export declare type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||
export declare function decimalToCoin(
|
||||
lookup: readonly TokenInfo[],
|
||||
value: Decimal,
|
||||
ticker: string,
|
||||
): amino.Coin;
|
||||
export declare function coinToDecimal(
|
||||
tokens: readonly TokenInfo[],
|
||||
coin: amino.Coin,
|
||||
): readonly [Decimal, string];
|
||||
export interface NonceInfo {
|
||||
readonly account_number: string;
|
||||
readonly sequence: string;
|
||||
|
||||
@ -38,6 +38,7 @@
|
||||
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@iov/encoding": "^2.0.0-alpha.7",
|
||||
"@tendermint/amino-js": "^0.7.0-alpha.1",
|
||||
"axios": "^0.19.0"
|
||||
},
|
||||
|
||||
13
packages/sdk/src/decoding.ts
Normal file
13
packages/sdk/src/decoding.ts
Normal file
@ -0,0 +1,13 @@
|
||||
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];
|
||||
}
|
||||
20
packages/sdk/src/encoding.ts
Normal file
20
packages/sdk/src/encoding.ts
Normal file
@ -0,0 +1,20 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
@ -1,2 +1,4 @@
|
||||
export { coinToDecimal } from "./decoding";
|
||||
export { decimalToCoin } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { AminoTx, isAminoStdTx, TokenInfo } from "./types";
|
||||
|
||||
7
packages/sdk/types/decoding.d.ts
vendored
Normal file
7
packages/sdk/types/decoding.d.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
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];
|
||||
8
packages/sdk/types/encoding.d.ts
vendored
Normal file
8
packages/sdk/types/encoding.d.ts
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
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;
|
||||
2
packages/sdk/types/index.d.ts
vendored
2
packages/sdk/types/index.d.ts
vendored
@ -1,2 +1,4 @@
|
||||
export { coinToDecimal } from "./decoding";
|
||||
export { decimalToCoin } from "./encoding";
|
||||
export { RestClient, TxsResponse } from "./restclient";
|
||||
export { AminoTx, isAminoStdTx, TokenInfo } from "./types";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user