Refactor amountToBankCoin
This commit is contained in:
parent
f38e07632a
commit
dad4ca4a96
@ -14,9 +14,9 @@ import {
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import {
|
||||
amountToBankCoin,
|
||||
buildSignedTx,
|
||||
buildUnsignedTx,
|
||||
encodeAmount,
|
||||
encodeFee,
|
||||
encodeFullSignature,
|
||||
encodePubkey,
|
||||
@ -75,9 +75,9 @@ describe("encode", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("encodeAmount", () => {
|
||||
describe("amountToBankCoin", () => {
|
||||
it("encodes an amount", () => {
|
||||
expect(encodeAmount(defaultAmount, defaultTokens)).toEqual({
|
||||
expect(amountToBankCoin(defaultAmount, defaultTokens)).toEqual({
|
||||
denom: "uatom",
|
||||
amount: "11657995",
|
||||
});
|
||||
|
||||
@ -10,7 +10,7 @@ import {
|
||||
SignedTransaction,
|
||||
UnsignedTransaction,
|
||||
} from "@iov/bcp";
|
||||
import { Decimal, Encoding } from "@iov/encoding";
|
||||
import { Encoding } from "@iov/encoding";
|
||||
|
||||
import { BankTokens, Erc20Token } from "./types";
|
||||
|
||||
@ -33,30 +33,20 @@ export function encodePubkey(pubkey: PubkeyBundle): types.PubKey {
|
||||
}
|
||||
}
|
||||
|
||||
export function decimalToCoin(lookup: BankTokens, value: Decimal, ticker: string): types.Coin {
|
||||
const match = lookup.find(token => token.ticker === ticker);
|
||||
if (!match) {
|
||||
throw Error(`unknown ticker: ${ticker}`);
|
||||
}
|
||||
if (match.fractionalDigits !== value.fractionalDigits) {
|
||||
export function amountToBankCoin(amount: Amount, tokens: BankTokens): types.Coin {
|
||||
const match = tokens.find(token => token.ticker === amount.tokenTicker);
|
||||
if (!match) throw Error(`unknown ticker: ${amount.tokenTicker}`);
|
||||
if (match.fractionalDigits !== amount.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,
|
||||
amount: amount.quantity,
|
||||
};
|
||||
}
|
||||
|
||||
export function encodeAmount(amount: Amount, tokens: BankTokens): types.Coin {
|
||||
return decimalToCoin(
|
||||
tokens,
|
||||
Decimal.fromAtomics(amount.quantity, amount.fractionalDigits),
|
||||
amount.tokenTicker,
|
||||
);
|
||||
}
|
||||
|
||||
export function encodeFee(fee: Fee, tokens: BankTokens): types.StdFee {
|
||||
if (fee.tokens === undefined) {
|
||||
throw new Error("Cannot encode fee without tokens");
|
||||
@ -65,7 +55,7 @@ export function encodeFee(fee: Fee, tokens: BankTokens): types.StdFee {
|
||||
throw new Error("Cannot encode fee without gas limit");
|
||||
}
|
||||
return {
|
||||
amount: [encodeAmount(fee.tokens, tokens)],
|
||||
amount: [amountToBankCoin(fee.tokens, tokens)],
|
||||
gas: fee.gasLimit,
|
||||
};
|
||||
}
|
||||
@ -101,7 +91,7 @@ export function buildUnsignedTx(
|
||||
value: {
|
||||
from_address: tx.sender,
|
||||
to_address: tx.recipient,
|
||||
amount: [encodeAmount(tx.amount, bankTokens)],
|
||||
amount: [amountToBankCoin(tx.amount, bankTokens)],
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
4
packages/bcp/types/encode.d.ts
vendored
4
packages/bcp/types/encode.d.ts
vendored
@ -1,10 +1,8 @@
|
||||
import { types } from "@cosmwasm/sdk";
|
||||
import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp";
|
||||
import { Decimal } from "@iov/encoding";
|
||||
import { BankTokens, Erc20Token } from "./types";
|
||||
export declare function encodePubkey(pubkey: PubkeyBundle): types.PubKey;
|
||||
export declare function decimalToCoin(lookup: BankTokens, value: Decimal, ticker: string): types.Coin;
|
||||
export declare function encodeAmount(amount: Amount, tokens: BankTokens): types.Coin;
|
||||
export declare function amountToBankCoin(amount: Amount, tokens: BankTokens): types.Coin;
|
||||
export declare function encodeFee(fee: Fee, tokens: BankTokens): types.StdFee;
|
||||
export declare function encodeFullSignature(fullSignature: FullSignature): types.StdSignature;
|
||||
export declare function buildUnsignedTx(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user