Skip to content

Commit db4b096

Browse files
committed
Add support for tuple array
1 parent a9ed492 commit db4b096

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function _getABIs() {
1111
}
1212

1313
function _typeToString(input) {
14+
if (input.type === "tuple[]") {
15+
return "(" + input.components.map(_typeToString).join(",") + ")[]";
16+
}
1417
if (input.type === "tuple") {
1518
return "(" + input.components.map(_typeToString).join(",") + ")";
1619
}

test/test.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const abiDecoder = require("../index.js");
55
const testABI = [{"inputs": [{"type": "address", "name": ""}], "constant": true, "name": "isInstantiation", "payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"}, {"inputs": [{"type": "address[]", "name": "_owners"}, {"type": "uint256", "name": "_required"}, {"type": "uint256", "name": "_dailyLimit"}], "constant": false, "name": "create", "payable": false, "outputs": [{"type": "address", "name": "wallet"}], "type": "function"}, {"inputs": [{"type": "address", "name": ""}, {"type": "uint256", "name": ""}], "constant": true, "name": "instantiations", "payable": false, "outputs": [{"type": "address", "name": ""}], "type": "function"}, {"inputs": [{"type": "address", "name": "creator"}], "constant": true, "name": "getInstantiationCount", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"indexed": false, "type": "address", "name": "sender"}, {"indexed": false, "type": "address", "name": "instantiation"}], "type": "event", "name": "ContractInstantiation", "anonymous": false}];
66
const testArrNumbersABI = [{"constant":false,"inputs":[{"name":"n","type":"uint256[]"}],"name":"numbers","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}];
77
const abiV2 = [{"constant":false,"inputs":[{"components":[{"components":[{"internalType":"address","name":"target","type":"address"},{"internalType":"uint256","name":"gasLimit","type":"uint256"},{"internalType":"uint256","name":"gasPrice","type":"uint256"},{"internalType":"bytes","name":"encodedFunction","type":"bytes"}],"internalType":"struct EIP712Sig.CallData","name":"callData","type":"tuple"},{"components":[{"internalType":"address","name":"senderAccount","type":"address"},{"internalType":"uint256","name":"senderNonce","type":"uint256"},{"internalType":"address","name":"relayAddress","type":"address"},{"internalType":"uint256","name":"pctRelayFee","type":"uint256"}],"internalType":"struct EIP712Sig.RelayData","name":"relayData","type":"tuple"}],"internalType":"struct EIP712Sig.RelayRequest","name":"relayRequest","type":"tuple"},{"internalType":"bytes","name":"signature","type":"bytes"},{"internalType":"bytes","name":"approvalData","type":"bytes"}],"name":"relayCall","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}];
8+
const testArrTuplesABI = [{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"components":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bool","name":"stable","type":"bool"}],"internalType":"struct RouterV2.route[]","name":"routes","type":"tuple[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"}];
89

910
describe("abi decoder", function () {
1011
it("get abis", () => {
@@ -43,6 +44,47 @@ describe("abi decoder", function () {
4344
expect(decodedData.params[0].value).to.deep.equal([["0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B", "1000000", "24000000000", "0x2ac0df260000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000"], ["0x50A5cf333FC36A18c8F96B1D1e7a2B013C6267aC", "0", "0x46DCcF96Fe3f3bEEf51c72c68A1F3Ad9183a6561", "12"]]);
4445
});
4546

47+
it("add abis which contain tuple array", () => {
48+
abiDecoder.addABI(testArrTuplesABI);
49+
const methodIDs = abiDecoder.getMethodIDs();
50+
const abis = abiDecoder.getABIs();
51+
expect(abis).to.have.length(8);
52+
expect(Object.keys(methodIDs)[6]).to.be.equal("f41766d8");
53+
});
54+
55+
it("decode tuple array data", () => {
56+
abiDecoder.addABI(testArrTuplesABI);
57+
const testData = "0xf41766d80000000000000000000000000000000000000000000000460b9c534b23acd15500000000000000000000000000000000000000000000002cbc7e1db5899a524000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000045a1f7fabfd9c69384f75740a8c1cc4aed33d7a90000000000000000000000000000000000000000000000000000000063edf4b60000000000000000000000000000000000000000000000000000000000000002000000000000000000000000f4c8e32eadec4bfe97e0f595add0f4450a863a11000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d560000000000000000000000000000000000000000000000000000000000000000";
58+
const decodedData = abiDecoder.decodeMethod(testData);
59+
expect(decodedData).to.be.an("object");
60+
expect(decodedData).to.have.all.keys("name", "params");
61+
expect(decodedData.name).to.be.a("string");
62+
expect(decodedData.params).to.be.a("array");
63+
expect(decodedData.params).to.have.length(5);
64+
expect(decodedData.params[0].value).to.deep.equal("1292108720372638863701");
65+
expect(decodedData.params[0].name).to.equal("amountIn");
66+
expect(decodedData.params[0].type).to.equal("uint256");
67+
expect(decodedData.params[1].value).to.equal("825239065434951144000");
68+
expect(decodedData.params[1].name).to.equal("amountOutMin");
69+
expect(decodedData.params[1].type).to.equal("uint256");
70+
expect(decodedData.params[2].value).to.be.a("array");
71+
expect(decodedData.params[2].value).to.have.length(2);
72+
expect(decodedData.params[2].name).to.equal("routes");
73+
expect(decodedData.params[2].type).to.equal("tuple[]");
74+
expect(decodedData.params[2].value[0].from).to.equal("0xF4C8E32EaDEC4BFe97E0F595AdD0f4450a863a11");
75+
expect(decodedData.params[2].value[0].to).to.equal("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c");
76+
expect(decodedData.params[2].value[0].stable).to.equal(false);
77+
expect(decodedData.params[2].value[1].from).to.equal("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c");
78+
expect(decodedData.params[2].value[1].to).to.equal("0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56");
79+
expect(decodedData.params[2].value[1].stable).to.equal(false);
80+
expect(decodedData.params[3].value).to.equal("0x45a1f7fabfd9c69384f75740a8c1cc4aed33d7a9");
81+
expect(decodedData.params[3].name).to.equal("to");
82+
expect(decodedData.params[3].type).to.equal("address");
83+
expect(decodedData.params[4].value).to.equal("1676539062");
84+
expect(decodedData.params[4].name).to.equal("deadline");
85+
expect(decodedData.params[4].type).to.equal("uint256");
86+
});
87+
4688
it("decode data", () => {
4789
abiDecoder.addABI(testABI);
4890
const testData = "0x53d9d9100000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000a6d9c5f7d4de3cef51ad3b7235d79ccc95114de5000000000000000000000000a6d9c5f7d4de3cef51ad3b7235d79ccc95114daa";
@@ -161,13 +203,13 @@ describe("abi decoder", function () {
161203
it("remove ABI", () => {
162204
let methods = abiDecoder.getMethodIDs();
163205
expect(methods).to.be.an("object");
164-
expect(Object.keys(methods)).to.have.length(44);
206+
expect(Object.keys(methods)).to.have.length(45);
165207

166208
abiDecoder.removeABI(testABI);
167209

168210
methods = abiDecoder.getMethodIDs();
169211
expect(methods).to.be.an("object");
170-
expect(Object.keys(methods)).to.have.length(39);
212+
expect(Object.keys(methods)).to.have.length(40);
171213
});
172214

173215
});

0 commit comments

Comments
 (0)