Fix memo handling in parseMsg

This commit is contained in:
Simon Warta 2020-02-12 14:48:54 +01:00
parent 4e97af33e6
commit 8ad5524b09
3 changed files with 21 additions and 10 deletions

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
import { types } from "@cosmwasm/sdk";
import { Address, Algorithm, TokenTicker } from "@iov/bcp";
import { Address, Algorithm, SendTransaction, TokenTicker } from "@iov/bcp";
import { Encoding } from "@iov/encoding";
import {
@ -37,12 +37,14 @@ describe("decode", () => {
quantity: "11657995",
tokenTicker: "ATOM" as TokenTicker,
};
const defaultSendTransaction = {
kind: "bcp/send" as const,
const defaultMemo = "Best greetings";
const defaultSendTransaction: SendTransaction = {
kind: "bcp/send",
chainId: testdata.chainId,
sender: "cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r" as Address,
recipient: "cosmos1z7g5w84ynmjyg0kqpahdjqpj7yq34v3suckp0e" as Address,
amount: defaultAmount,
memo: defaultMemo,
};
const defaultFee = {
tokens: {
@ -136,7 +138,7 @@ describe("decode", () => {
],
},
};
expect(parseMsg(msg, testdata.chainId, defaultTokens)).toEqual(defaultSendTransaction);
expect(parseMsg(msg, defaultMemo, testdata.chainId, defaultTokens)).toEqual(defaultSendTransaction);
});
});
@ -155,7 +157,6 @@ describe("decode", () => {
});
});
describe("parseSignedTx", () => {
it("works", () => {
expect(parseSignedTx(cosmoshub.tx.value, testdata.chainId, testdata.nonce, defaultTokens)).toEqual(

View File

@ -70,7 +70,12 @@ export function decodeAmount(tokens: BankTokens, coin: types.Coin): Amount {
};
}
export function parseMsg(msg: types.Msg, chainId: ChainId, tokens: BankTokens): UnsignedTransaction {
export function parseMsg(
msg: types.Msg,
memo: string | undefined,
chainId: ChainId,
tokens: BankTokens,
): UnsignedTransaction {
if (types.isMsgSend(msg)) {
if (msg.value.amount.length !== 1) {
throw new Error("Only MsgSend with one amount is supported");
@ -81,6 +86,7 @@ export function parseMsg(msg: types.Msg, chainId: ChainId, tokens: BankTokens):
sender: msg.value.from_address as Address,
recipient: msg.value.to_address as Address,
amount: decodeAmount(tokens, msg.value.amount[0]),
memo: memo,
};
return send;
} else {
@ -117,13 +123,12 @@ export function parseSignedTx(
}
const [primarySignature] = txValue.signatures.map(signature => decodeFullSignature(signature, nonce));
const msg = parseMsg(txValue.msg[0], chainId, tokens);
const msg = parseMsg(txValue.msg[0], txValue.memo, chainId, tokens);
const fee = parseFee(txValue.fee, tokens);
const transaction = {
const transaction: UnsignedTransaction = {
...msg,
chainId: chainId,
memo: txValue.memo,
fee: fee,
};

View File

@ -18,7 +18,12 @@ export declare function decodeSignature(signature: string): SignatureBytes;
export declare function decodeFullSignature(signature: types.StdSignature, nonce: number): FullSignature;
export declare function coinToDecimal(tokens: BankTokens, coin: types.Coin): readonly [Decimal, string];
export declare function decodeAmount(tokens: BankTokens, coin: types.Coin): Amount;
export declare function parseMsg(msg: types.Msg, chainId: ChainId, tokens: BankTokens): UnsignedTransaction;
export declare function parseMsg(
msg: types.Msg,
memo: string | undefined,
chainId: ChainId,
tokens: BankTokens,
): UnsignedTransaction;
export declare function parseFee(fee: types.StdFee, tokens: BankTokens): Fee;
export declare function parseSignedTx(
txValue: types.StdTx,