Add ERC20 support to parseUnsignedTx

This commit is contained in:
Simon Warta 2020-02-17 14:38:17 +01:00
parent c94a49a1fb
commit f3d24e1acd
3 changed files with 55 additions and 7 deletions

View File

@ -209,10 +209,56 @@ describe("decode", () => {
});
describe("parseUnsignedTx", () => {
it("works", () => {
expect(parseUnsignedTx(cosmoshub.tx.value, testdata.chainId, defaultTokens)).toEqual(
testdata.sendTxJson,
);
it("works for bank send transaction", () => {
expect(
parseUnsignedTx(cosmoshub.tx.value, testdata.chainId, defaultTokens, defaultErc20Tokens),
).toEqual(testdata.sendTxJson);
});
it("works for ERC20 send transaction", () => {
const msg: types.MsgExecuteContract = {
type: "wasm/execute",
value: {
sender: "cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r",
contract: defaultErc20Tokens[0].contractAddress,
msg: {
transfer: {
amount: "887878484",
recipient: "cosmos1z7g5w84ynmjyg0kqpahdjqpj7yq34v3suckp0e",
},
},
sent_funds: [],
},
};
const tx: types.StdTx = {
msg: [msg],
memo: defaultMemo,
fee: {
amount: [
{
denom: "uatom",
amount: "5000",
},
],
gas: "200000",
},
signatures: [],
};
const unsigned = parseUnsignedTx(tx, testdata.chainId, defaultTokens, defaultErc20Tokens);
assert(isSendTransaction(unsigned));
expect(unsigned).toEqual({
kind: "bcp/send",
chainId: testdata.chainId,
sender: "cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r" as Address,
recipient: "cosmos1z7g5w84ynmjyg0kqpahdjqpj7yq34v3suckp0e" as Address,
amount: {
quantity: "887878484",
tokenTicker: "ASH" as TokenTicker,
fractionalDigits: 5,
},
memo: defaultMemo,
fee: defaultFee,
});
});
});

View File

@ -144,6 +144,7 @@ export function parseUnsignedTx(
txValue: types.StdTx,
chainId: ChainId,
tokens: BankTokens,
erc20Tokens: readonly Erc20Token[],
): UnsignedTransaction {
if (!types.isStdTx(txValue)) {
throw new Error("Only StdTx is supported");
@ -152,7 +153,7 @@ export function parseUnsignedTx(
throw new Error("Only single-message transactions currently supported");
}
const msg = parseMsg(txValue.msg[0], txValue.memo, chainId, tokens);
const msg = parseMsg(txValue.msg[0], txValue.memo, chainId, tokens, erc20Tokens);
const fee = parseFee(txValue.fee, tokens);
return {
@ -170,7 +171,7 @@ export function parseSignedTx(
): SignedTransaction {
const [primarySignature] = txValue.signatures.map(signature => decodeFullSignature(signature, nonce));
return {
transaction: parseUnsignedTx(txValue, chainId, tokens),
transaction: parseUnsignedTx(txValue, chainId, tokens, []),
signatures: [primarySignature],
};
}
@ -183,7 +184,7 @@ export function parseTxsResponseUnsigned(
): ConfirmedTransaction<UnsignedTransaction> {
const height = parseInt(response.height, 10);
return {
transaction: parseUnsignedTx(response.tx.value, chainId, tokens),
transaction: parseUnsignedTx(response.tx.value, chainId, tokens, []),
height: height,
confirmations: currentHeight - height + 1,
transactionId: response.txhash as TransactionId,

View File

@ -31,6 +31,7 @@ export declare function parseUnsignedTx(
txValue: types.StdTx,
chainId: ChainId,
tokens: BankTokens,
erc20Tokens: readonly Erc20Token[],
): UnsignedTransaction;
export declare function parseSignedTx(
txValue: types.StdTx,