mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 13:47:12 +00:00
Start out coin<->Amount<->Ticker mapping
This commit is contained in:
parent
6495167a0e
commit
35f0a0af7a
30
src/types.ts
30
src/types.ts
@ -1,4 +1,6 @@
|
||||
import amino from "@tendermint/amino-js";
|
||||
import {Amount, Token} from "@iov/bcp";
|
||||
|
||||
|
||||
export type AminoTx = amino.Tx & { readonly value: amino.StdTx };
|
||||
|
||||
@ -8,3 +10,31 @@ 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 extends Token {
|
||||
readonly denom: string;
|
||||
}
|
||||
|
||||
// TODO: alias amino types
|
||||
export function amountToCoin(lookup: ReadonlyArray<TokenInfo>, amount: Amount): amino.Coin {
|
||||
const match = lookup.find(({tokenTicker}) => tokenTicker === amount.tokenTicker);
|
||||
if (!match) {
|
||||
throw Error(`unknown ticker: ${amount.tokenTicker}`);
|
||||
}
|
||||
return {
|
||||
denom: match.denom,
|
||||
amount: amount.quantity,
|
||||
};
|
||||
}
|
||||
|
||||
export function coinToAmount(lookup: ReadonlyArray<TokenInfo>, coin: amino.Coin): Amount {
|
||||
const match = lookup.find(({denom}) => denom === coin.denom);
|
||||
if (!match) {
|
||||
throw Error(`unknown denom: ${coin.denom}`);
|
||||
}
|
||||
return {
|
||||
tokenTicker: match.tokenTicker,
|
||||
fractionalDigits: match.fractionalDigits,
|
||||
quantity: coin.amount,
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user