Skip to content

Commit

Permalink
Start out coin<->Amount<->Ticker mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jan 22, 2020
1 parent 6495167 commit 35f0a0a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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 };

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

0 comments on commit 35f0a0a

Please sign in to comment.