aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/test/abi_decoder_test.ts
blob: 60143461437a11dcda8fef7e5e83d9c2020c7dcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as chai from 'chai';
import { MethodAbi } from 'ethereum-types';
import 'mocha';

import { AbiDecoder, AbiEncoder } from '../src';

import { chaiSetup } from './utils/chai_setup';

chaiSetup.configure();
const expect = chai.expect;

describe('AbiDecoder', () => {
    it('should successfully add a new ABI and decode calldata for it', async () => {
        // Add new ABI
        const abi: MethodAbi = {
            name: 'foobar',
            type: 'function',
            inputs: [
                {
                    name: 'testAddress',
                    type: 'address',
                },
            ],
            outputs: [
                {
                    name: 'butter',
                    type: 'string',
                },
            ],
            constant: false,
            payable: false,
            stateMutability: 'pure',
        };
        const contractName = 'newContract';
        const testAddress = '0x0001020304050607080900010203040506070809';
        const abiDecoder = new AbiDecoder([]);
        abiDecoder.addABI([abi], contractName);
        // Create some tx data
        const foobarEncoder = new AbiEncoder.Method(abi);
        const foobarSignature = foobarEncoder.getSignature();
        const foobarTxData = foobarEncoder.encode([testAddress]);
        // Decode tx data using contract name
        const decodedTxData = abiDecoder.tryDecodeCalldata(foobarTxData, contractName);
        const expectedFunctionName = abi.name;
        const expectedFunctionArguments = { testAddress };
        expect(decodedTxData.functionName).to.be.equal(expectedFunctionName);
        expect(decodedTxData.functionSignature).to.be.equal(foobarSignature);
        expect(decodedTxData.functionArguments).to.be.deep.equal(expectedFunctionArguments);
    });
});