mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Add strict toErc20Amount converter
This commit is contained in:
parent
dad4ca4a96
commit
e0db927bca
@ -75,7 +75,7 @@ describe("CosmWasmCodec", () => {
|
||||
recipient: "cosmos1dddd" as Address,
|
||||
memo: "My first BASH payment",
|
||||
amount: {
|
||||
fractionalDigits: 6,
|
||||
fractionalDigits: 0,
|
||||
quantity: "345",
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
},
|
||||
|
@ -477,7 +477,7 @@ describe("CosmWasmConnection", () => {
|
||||
memo: "My first payment",
|
||||
amount: {
|
||||
quantity: "75",
|
||||
fractionalDigits: 3, // todo: BROKEN!
|
||||
fractionalDigits: 0,
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
},
|
||||
});
|
||||
|
@ -20,6 +20,7 @@ import {
|
||||
encodeFee,
|
||||
encodeFullSignature,
|
||||
encodePubkey,
|
||||
toErc20Amount,
|
||||
} from "./encode";
|
||||
import { BankTokens, Erc20Token } from "./types";
|
||||
|
||||
@ -75,6 +76,37 @@ describe("encode", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("toErc20Amount", () => {
|
||||
const [ash, bash] = defaultErc20Tokens;
|
||||
|
||||
it("encodes an amount", () => {
|
||||
const amount: Amount = {
|
||||
quantity: "789",
|
||||
fractionalDigits: 0,
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
};
|
||||
expect(toErc20Amount(amount, bash)).toEqual("789");
|
||||
});
|
||||
|
||||
it("throws on ticker mismatch", () => {
|
||||
const amount: Amount = {
|
||||
quantity: "789",
|
||||
fractionalDigits: 0,
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
};
|
||||
expect(() => toErc20Amount(amount, ash)).toThrowError(/ticker mismatch/i);
|
||||
});
|
||||
|
||||
it("throws on ticker mismatch", () => {
|
||||
const amount: Amount = {
|
||||
quantity: "789",
|
||||
fractionalDigits: 2,
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
};
|
||||
expect(() => toErc20Amount(amount, bash)).toThrowError(/fractional digits mismatch/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("amountToBankCoin", () => {
|
||||
it("encodes an amount", () => {
|
||||
expect(amountToBankCoin(defaultAmount, defaultTokens)).toEqual({
|
||||
@ -288,7 +320,7 @@ describe("encode", () => {
|
||||
recipient: "cosmos1dddd" as Address,
|
||||
memo: defaultMemo,
|
||||
amount: {
|
||||
fractionalDigits: 6,
|
||||
fractionalDigits: 0,
|
||||
quantity: "345",
|
||||
tokenTicker: "BASH" as TokenTicker,
|
||||
},
|
||||
|
@ -33,6 +33,14 @@ export function encodePubkey(pubkey: PubkeyBundle): types.PubKey {
|
||||
}
|
||||
}
|
||||
|
||||
export function toErc20Amount(amount: Amount, erc20Token: Erc20Token): string {
|
||||
if (amount.tokenTicker !== erc20Token.ticker) throw new Error("Ticker mismatch between amount and token");
|
||||
if (amount.fractionalDigits !== erc20Token.fractionalDigits) {
|
||||
throw new Error("Fractional digits mismatch between amount and token");
|
||||
}
|
||||
return amount.quantity;
|
||||
}
|
||||
|
||||
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}`);
|
||||
@ -117,7 +125,7 @@ export function buildUnsignedTx(
|
||||
contract: matchingErc20Token.contractAddress,
|
||||
msg: {
|
||||
transfer: {
|
||||
amount: tx.amount.quantity,
|
||||
amount: toErc20Amount(tx.amount, matchingErc20Token),
|
||||
recipient: tx.recipient,
|
||||
},
|
||||
},
|
||||
|
1
packages/bcp/types/encode.d.ts
vendored
1
packages/bcp/types/encode.d.ts
vendored
@ -2,6 +2,7 @@ import { types } from "@cosmwasm/sdk";
|
||||
import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp";
|
||||
import { BankTokens, Erc20Token } from "./types";
|
||||
export declare function encodePubkey(pubkey: PubkeyBundle): types.PubKey;
|
||||
export declare function toErc20Amount(amount: Amount, erc20Token: Erc20Token): string;
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user