UNPKG

12.2 kBJavaScriptView Raw
1const expect = require('chai').expect;
2const abiDecoder = require('../index.js');
3
4// Test Params
5const 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}];
6
7describe('abi decoder', function () {
8 it('get abis', () => {
9 const abis = abiDecoder.getABIs();
10 expect(abis).to.be.an('array');
11 expect(abis).to.have.length.of(0);
12 });
13
14 it('add abis', () => {
15 abiDecoder.addABI(testABI);
16 const abis = abiDecoder.getABIs();
17 expect(abis).to.be.an('array');
18 expect(abis).to.have.length.of(5);
19 const methodIDs = abiDecoder.getMethodIDs();
20 expect(methodIDs).to.be.an('object');
21 expect(Object.keys(methodIDs)).to.have.length.of(5);
22 });
23
24 it('decode data', () => {
25 abiDecoder.addABI(testABI);
26 const testData = "0x53d9d9100000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000a6d9c5f7d4de3cef51ad3b7235d79ccc95114de5000000000000000000000000a6d9c5f7d4de3cef51ad3b7235d79ccc95114daa";
27 const decodedData = abiDecoder.decodeMethod(testData);
28 expect(decodedData).to.be.an('object');
29 expect(decodedData).to.have.all.keys('name', 'params');
30 expect(decodedData.name).to.be.a('string');
31 expect(decodedData.params).to.be.a('array');
32 expect(decodedData.params).to.have.length.of(3);
33 expect(decodedData.params[0].value).to.deep.equal(['0xa6d9c5f7d4de3cef51ad3b7235d79ccc95114de5', '0xa6d9c5f7d4de3cef51ad3b7235d79ccc95114daa']);
34 expect(decodedData.params[0].name).to.equal('_owners');
35 expect(decodedData.params[0].type).to.equal('address[]');
36 expect(decodedData.params[1].value).to.equal('1');
37 expect(decodedData.params[1].name).to.equal('_required');
38 expect(decodedData.params[1].type).to.equal('uint256');
39 expect(decodedData.params[2].value).to.equal('0');
40 expect(decodedData.params[2].name).to.equal('_dailyLimit');
41 expect(decodedData.params[2].type).to.equal('uint256');
42 });
43
44 it('decode logs without indexed', () => {
45 const testLogs = [
46 {
47 data: "0x00000000000000000000000065039084cc6f4773291a6ed7dcf5bc3a2e894ff3000000000000000000000000435a4167bc34107bd03e267f9d6b869255151a27",
48 topics: ["0x4fb057ad4a26ed17a57957fa69c306f11987596069b89521c511fc9a894e6161"],
49 address: "0x0457874Bb0a346962128a0C01310d00Fc5bb6a83"
50 }
51 ];
52
53 const decodedLogs = abiDecoder.decodeLogs(testLogs);
54 expect(decodedLogs).to.be.an('array');
55 expect(decodedLogs).to.have.length(1);
56 expect(decodedLogs[0].name).to.equal('ContractInstantiation');
57 expect(decodedLogs[0].events).to.have.length(2);
58 expect(decodedLogs[0].address).to.equal('0x0457874Bb0a346962128a0C01310d00Fc5bb6a83');
59 expect(decodedLogs[0].events[0].name).to.equal('sender');
60 expect(decodedLogs[0].events[0].value).to.equal('0x65039084cc6f4773291a6ed7dcf5bc3a2e894ff3');
61 expect(decodedLogs[0].events[0].type).to.equal('address');
62 expect(decodedLogs[0].events[1].name).to.equal('instantiation');
63 expect(decodedLogs[0].events[1].value).to.equal('0x435a4167bc34107bd03e267f9d6b869255151a27');
64 expect(decodedLogs[0].events[1].type).to.equal('address');
65 });
66
67 it('decode logs with indexed param', () => {
68 const walletABI = [{"inputs": [{"type": "uint256", "name": ""}], "constant": true, "name": "owners", "payable": false, "outputs": [{"type": "address", "name": ""}], "type": "function"}, {"inputs": [{"type": "address", "name": "owner"}], "constant": false, "name": "removeOwner", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": false, "name": "revokeConfirmation", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "address", "name": ""}], "constant": true, "name": "isOwner", "payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"}, {"inputs": [{"type": "uint256", "name": ""}, {"type": "address", "name": ""}], "constant": true, "name": "confirmations", "payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"}, {"inputs": [], "constant": true, "name": "calcMaxWithdraw", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"type": "bool", "name": "pending"}, {"type": "bool", "name": "executed"}], "constant": true, "name": "getTransactionCount", "payable": false, "outputs": [{"type": "uint256", "name": "count"}], "type": "function"}, {"inputs": [], "constant": true, "name": "dailyLimit", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [], "constant": true, "name": "lastDay", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"type": "address", "name": "owner"}], "constant": false, "name": "addOwner", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": true, "name": "isConfirmed", "payable": false, "outputs": [{"type": "bool", "name": ""}], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": true, "name": "getConfirmationCount", "payable": false, "outputs": [{"type": "uint256", "name": "count"}], "type": "function"}, {"inputs": [{"type": "uint256", "name": ""}], "constant": true, "name": "transactions", "payable": false, "outputs": [{"type": "address", "name": "destination"}, {"type": "uint256", "name": "value"}, {"type": "bytes", "name": "data"}, {"type": "bool", "name": "executed"}], "type": "function"}, {"inputs": [], "constant": true, "name": "getOwners", "payable": false, "outputs": [{"type": "address[]", "name": ""}], "type": "function"}, {"inputs": [{"type": "uint256", "name": "from"}, {"type": "uint256", "name": "to"}, {"type": "bool", "name": "pending"}, {"type": "bool", "name": "executed"}], "constant": true, "name": "getTransactionIds", "payable": false, "outputs": [{"type": "uint256[]", "name": "_transactionIds"}], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": true, "name": "getConfirmations", "payable": false, "outputs": [{"type": "address[]", "name": "_confirmations"}], "type": "function"}, {"inputs": [], "constant": true, "name": "transactionCount", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"type": "uint256", "name": "_required"}], "constant": false, "name": "changeRequirement", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": false, "name": "confirmTransaction", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "address", "name": "destination"}, {"type": "uint256", "name": "value"}, {"type": "bytes", "name": "data"}], "constant": false, "name": "submitTransaction", "payable": false, "outputs": [{"type": "uint256", "name": "transactionId"}], "type": "function"}, {"inputs": [{"type": "uint256", "name": "_dailyLimit"}], "constant": false, "name": "changeDailyLimit", "payable": false, "outputs": [], "type": "function"}, {"inputs": [], "constant": true, "name": "MAX_OWNER_COUNT", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [], "constant": true, "name": "required", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"type": "address", "name": "owner"}, {"type": "address", "name": "newOwner"}], "constant": false, "name": "replaceOwner", "payable": false, "outputs": [], "type": "function"}, {"inputs": [{"type": "uint256", "name": "transactionId"}], "constant": false, "name": "executeTransaction", "payable": false, "outputs": [], "type": "function"}, {"inputs": [], "constant": true, "name": "spentToday", "payable": false, "outputs": [{"type": "uint256", "name": ""}], "type": "function"}, {"inputs": [{"type": "address[]", "name": "_owners"}, {"type": "uint256", "name": "_required"}, {"type": "uint256", "name": "_dailyLimit"}], "type": "constructor"}, {"payable": true, "type": "fallback"}, {"inputs": [{"indexed": false, "type": "uint256", "name": "dailyLimit"}], "type": "event", "name": "DailyLimitChange", "anonymous": false}, {"inputs": [{"indexed": true, "type": "address", "name": "sender"}, {"indexed": true, "type": "uint256", "name": "transactionId"}], "type": "event", "name": "Confirmation", "anonymous": false}, {"inputs": [{"indexed": true, "type": "address", "name": "sender"}, {"indexed": true, "type": "uint256", "name": "transactionId"}], "type": "event", "name": "Revocation", "anonymous": false}, {"inputs": [{"indexed": true, "type": "uint256", "name": "transactionId"}], "type": "event", "name": "Submission", "anonymous": false}, {"inputs": [{"indexed": true, "type": "uint256", "name": "transactionId"}], "type": "event", "name": "Execution", "anonymous": false}, {"inputs": [{"indexed": true, "type": "uint256", "name": "transactionId"}], "type": "event", "name": "ExecutionFailure", "anonymous": false}, {"inputs": [{"indexed": true, "type": "address", "name": "sender"}, {"indexed": false, "type": "uint256", "name": "value"}], "type": "event", "name": "Deposit", "anonymous": false}, {"inputs": [{"indexed": true, "type": "address", "name": "owner"}], "type": "event", "name": "OwnerAddition", "anonymous": false}, {"inputs": [{"indexed": true, "type": "address", "name": "owner"}], "type": "event", "name": "OwnerRemoval", "anonymous": false}, {"inputs": [{"indexed": false, "type": "uint256", "name": "required"}], "type": "event", "name": "RequirementChange", "anonymous": false}];
69 abiDecoder.addABI(walletABI);
70 const testLogs = [
71 {
72 data: "0x00000000000000000000000000000000000000000000000000038d7ea4c68000",
73 topics: ["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c", "0x00000000000000000000000065039084cc6f4773291a6ed7dcf5bc3a2e894ff3"],
74 address: '0x0457874Bb0a346962128a0C01310d00Fc5bb6a81'
75 }
76 ];
77 const decodedLogs = abiDecoder.decodeLogs(testLogs);
78 expect(decodedLogs).to.be.an('array');
79 expect(decodedLogs).to.have.length(1);
80 expect(decodedLogs[0].name).to.equal('Deposit');
81 expect(decodedLogs[0].events).to.have.length(2);
82 expect(decodedLogs[0].address).to.equal('0x0457874Bb0a346962128a0C01310d00Fc5bb6a81');
83 expect(decodedLogs[0].events[0].name).to.equal('sender');
84 expect(decodedLogs[0].events[0].type).to.equal('address');
85 expect(decodedLogs[0].events[0].value).to.equal('0x65039084cc6f4773291a6ed7dcf5bc3a2e894ff3');
86 expect(decodedLogs[0].events[1].name).to.equal('value');
87 expect(decodedLogs[0].events[1].value).to.equal('1000000000000000');
88 expect(decodedLogs[0].events[1].type).to.equal('uint256');
89 });
90
91 it('remove ABI', () => {
92 let methods = abiDecoder.getMethodIDs();
93 expect(methods).to.be.an('object');
94 expect(Object.keys(methods)).to.have.length.of(41);
95
96 abiDecoder.removeABI(testABI);
97
98 methods = abiDecoder.getMethodIDs();
99 expect(methods).to.be.an('object');
100 expect(Object.keys(methods)).to.have.length.of(36);
101 });
102
103});