Merge pull request #35 from confio/extract-token-info

Move TokenInfo, coinToDecimal, decimalToCoin from sdk to bcp
This commit is contained in:
Ethan Frey 2020-02-03 18:48:27 +01:00 committed by GitHub
commit eeca7d1a8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 66 additions and 91 deletions

View File

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

View File

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

View File

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

View File

@ -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<TokenInfo>;
// tslint:disable-next-line:no-bitwise

View File

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

View File

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

View File

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

View File

@ -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<TokenInfo>;
export interface NonceInfo {
readonly account_number: string;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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