Start out coin<->Amount<->Ticker mapping
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user