From 3d2babd059551fc0e1a571d3cf1cb544ec85f52c Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Wed, 6 Feb 2019 17:21:19 -0800 Subject: Tests for transaction decoder --- packages/utils/test/transaction_decoder_test.ts | 54 ++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/utils/test/transaction_decoder_test.ts b/packages/utils/test/transaction_decoder_test.ts index bc40f4840..3845bb134 100644 --- a/packages/utils/test/transaction_decoder_test.ts +++ b/packages/utils/test/transaction_decoder_test.ts @@ -1,9 +1,61 @@ import * as chai from 'chai'; +import { MethodAbi } from 'ethereum-types'; import 'mocha'; import { chaiSetup } from './utils/chai_setup'; +import { AbiEncoder, TransactionDecoder } from '../src'; chaiSetup.configure(); const expect = chai.expect; -describe.only('TransactionDecoder', () => {}); +describe('TransactionDecoder', () => { + it('should successfully add a new ABI and decode tx data for it', async () => { + // Add new ABI + const abi: MethodAbi = { + name: 'foobar', + type: 'function', + inputs: [ + { + name: 'addr', + type: 'address', + }, + ], + outputs: [ + { + name: 'butter', + type: 'string', + }, + ], + constant: false, + payable: false, + stateMutability: 'pure', + }; + const contractName = 'newContract'; + const contractAddress = '0x0001020304050607080900010203040506070809'; + const networkId = 1; + const contractInfo = [ + { + contractAddress, + networkId, + }, + ]; + const transactionDecoder = new TransactionDecoder(); + transactionDecoder.addABI([abi], contractName, contractInfo); + // Create some tx data + const foobarEncoder = new AbiEncoder.Method(abi); + const foobarSignature = foobarEncoder.getSignature(); + const foobarTxData = foobarEncoder.encode([contractAddress]); + // Decode tx data using contract name + const decodedTxData = transactionDecoder.decode(foobarTxData, { contractName }); + const expectedFunctionName = abi.name; + const expectedFunctionArguments = { + addr: contractAddress, + }; + expect(decodedTxData.functionName).to.be.equal(expectedFunctionName); + expect(decodedTxData.functionSignature).to.be.equal(foobarSignature); + expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments); + // Decode tx data using contract address + const decodedTxDataDecodedWithAddress = transactionDecoder.decode(foobarTxData, { contractAddress }); + expect(decodedTxDataDecodedWithAddress).to.be.deep.equal(decodedTxData); + }); +}); -- cgit