aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/test/utils_test.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-28 17:05:36 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-04-02 17:34:29 +0800
commit4d9029bb0e3b215efdf165704c80d3bacef0e85a (patch)
tree8f699dcd0a7b5fba3c31c065a62d640407bba8c3 /packages/base-contract/test/utils_test.ts
parentbc49dde4d5d24b9a8b01459bde061f9b23fb5898 (diff)
downloaddexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.gz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.bz2
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.lz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.xz
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.zst
dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.zip
Add metacoin example project
Diffstat (limited to 'packages/base-contract/test/utils_test.ts')
-rw-r--r--packages/base-contract/test/utils_test.ts108
1 files changed, 108 insertions, 0 deletions
diff --git a/packages/base-contract/test/utils_test.ts b/packages/base-contract/test/utils_test.ts
new file mode 100644
index 000000000..c083704f4
--- /dev/null
+++ b/packages/base-contract/test/utils_test.ts
@@ -0,0 +1,108 @@
+import { BigNumber } from '@0xproject/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,
+ },
+ ]);
+ });
+ });
+});