From 35f0a0af7a7fc6c8524b7885387c37666bd03b10 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 22 Jan 2020 17:44:50 +0100 Subject: [PATCH] Start out coin<->Amount<->Ticker mapping --- src/types.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/types.ts b/src/types.ts index c8a0ef7242..c74573d99f 100644 --- a/src/types.ts +++ b/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, 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, 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, + }; +}