aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/test/utils_test.ts
blob: 0608b72a251ce2262714d598d074bbc52e7e7645 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';

import { formatABIDataItem } from '../src/utils';

const { expect } = chai;

describe('Utils tests', () => {
    describe('#formatABIDataItem', () => {
        it('correctly handles arrays', () => {
            const calls: Array<{ type: string; value: any }> = [];
            const abi = {
                name: 'values',
                type: 'uint256[]',
            };
            const val = [1, 2, 3];
            const formatted = formatABIDataItem(abi, val, (type: string, value: any) => {
                calls.push({ type, value });
                return value; // no-op
            });
            expect(formatted).to.be.deep.equal(val);
            expect(calls).to.be.deep.equal([
                { type: 'uint256', value: 1 },
                { type: 'uint256', value: 2 },
                { type: 'uint256', value: 3 },
            ]);
        });
        it('correctly handles tuples', () => {
            const calls: Array<{ type: string; value: any }> = [];
            const abi = {
                components: [
                    {
                        name: 'to',
                        type: 'address',
                    },
                    {
                        name: 'amount',
                        type: 'uint256',
                    },
                ],
                name: 'data',
                type: 'tuple',
            };
            const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
            const val = { to: ZERO_ADDRESS, amount: new BigNumber(1) };
            const formatted = formatABIDataItem(abi, val, (type: string, value: any) => {
                calls.push({ type, value });
                return value; // no-op
            });
            expect(formatted).to.be.deep.equal(val);
            expect(calls).to.be.deep.equal([
                {
                    type: 'address',
                    value: val.to,
                },
                {
                    type: 'uint256',
                    value: val.amount,
                },
            ]);
        });
        it('correctly handles nested arrays of tuples', () => {
            const calls: Array<{ type: string; value: any }> = [];
            const abi = {
                components: [
                    {
                        name: 'to',
                        type: 'address',
                    },
                    {
                        name: 'amount',
                        type: 'uint256',
                    },
                ],
                name: 'data',
                type: 'tuple[2][]',
            };
            const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
            const val = [
                [{ to: ZERO_ADDRESS, amount: new BigNumber(1) }, { to: ZERO_ADDRESS, amount: new BigNumber(2) }],
            ];
            const formatted = formatABIDataItem(abi, val, (type: string, value: any) => {
                calls.push({ type, value });
                return value; // no-op
            });
            expect(formatted).to.be.deep.equal(val);
            expect(calls).to.be.deep.equal([
                {
                    type: 'address',
                    value: val[0][0].to,
                },
                {
                    type: 'uint256',
                    value: val[0][0].amount,
                },
                {
                    type: 'address',
                    value: val[0][1].to,
                },
                {
                    type: 'uint256',
                    value: val[0][1].amount,
                },
            ]);
        });
    });
});