From 89a75f48eb0d90d9b6d8dc443647d3bc04f27f9c Mon Sep 17 00:00:00 2001 From: cgewecke Date: Sat, 26 Oct 2019 18:30:59 -0700 Subject: [PATCH 1/2] Improve eth-abi decodeParameters error message --- CHANGELOG.md | 1 + packages/web3-eth-abi/src/index.js | 6 ++- test/e2e.method.call.js | 68 ++++++++++++++++++++++++++++++ test/sources/Misc.json | 15 +++++++ test/sources/Misc.sol | 9 ++++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 test/e2e.method.call.js create mode 100644 test/sources/Misc.json create mode 100644 test/sources/Misc.sol diff --git a/CHANGELOG.md b/CHANGELOG.md index e4cccf77ca5..23365ae7665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,3 +90,4 @@ Released with 1.0.0-beta.37 code base. ### Fixed - Fix TS types for eth.subscribe syncing, newBlockHeaders, pendingTransactions (#3159) +- Improve web3-eth-abi decodeParameters error message (#3134) diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index a9c1a8596f2..08c9a787d3b 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -223,7 +223,11 @@ ABICoder.prototype.decodeParameter = function (type, bytes) { */ ABICoder.prototype.decodeParameters = function (outputs, bytes) { if (outputs.length > 0 && (!bytes || bytes === '0x' || bytes === '0X')) { - throw new Error('Returned values aren\'t valid, did it run Out of Gas?'); + throw new Error( + 'Returned values aren\'t valid, did it run Out of Gas? ' + + 'Are you using the correct ABI for the contract you are ' + + 'retrieving data from?' + ); } var res = ethersAbiCoder.decode(this.mapTypes(outputs), '0x' + bytes.replace(/0x/i, '')); diff --git a/test/e2e.method.call.js b/test/e2e.method.call.js new file mode 100644 index 00000000000..db673aa08c9 --- /dev/null +++ b/test/e2e.method.call.js @@ -0,0 +1,68 @@ +var assert = require('assert'); +var Basic = require('./sources/Basic'); +var Misc = require('./sources/Misc'); +var utils = require('./helpers/test.utils'); +var Web3 = utils.getWeb3(); + +describe('method.call [ @E2E ]', function() { + var web3; + var accounts; + var basic; + var instance; + var options; + + var basicOptions = { + data: Basic.bytecode, + gasPrice: '1', + gas: 4000000 + }; + + var miscOptions = { + data: Misc.bytecode, + gasPrice: '1', + gas: 4000000 + }; + + before(async function(){ + web3 = new Web3('http://localhost:8545'); + accounts = await web3.eth.getAccounts(); + + basic = new web3.eth.Contract(Basic.abi, basicOptions); + instance = await basic.deploy().send({from: accounts[0]}); + }) + + it('retrieves a uint value', async function(){ + var expected = '1'; + + await instance + .methods + .setValue(expected) + .send({from: accounts[0]}); + + var value = await instance + .methods + .getValue() + .call({from: accounts[0]}); + + assert.equal(value, expected); + }); + + it('errors correctly when abi and bytecode do not match', async function(){ + // Misc n.eq Basic + var wrong = new web3.eth.Contract(Basic.abi, miscOptions); + var wrongInstance = await wrong.deploy().send({from: accounts[0]}); + + try { + await wrongInstance + .methods + .getValue() + .call(); + + assert.fail(); + + } catch(err){ + assert(err.message.includes("Returned values aren't valid")); + assert(err.message.includes('the correct ABI')); + } + }) +}); diff --git a/test/sources/Misc.json b/test/sources/Misc.json new file mode 100644 index 00000000000..b56e47bce80 --- /dev/null +++ b/test/sources/Misc.json @@ -0,0 +1,15 @@ +{ + "contractName": "Misc", + "abi": [ + { + "inputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "constructor" + } + ], + "bytecode": "0x6080604052348015600f57600080fd5b50603580601d6000396000f3fe6080604052600080fdfea165627a7a723058205b81906cade92d8eeda5d0627076490bd44b563a504fe790edbcd6afadbe359d0029", + "deployedBytecode": "0x6080604052600080fdfea165627a7a723058205b81906cade92d8eeda5d0627076490bd44b563a504fe790edbcd6afadbe359d0029", + "linkReferences": {}, + "deployedLinkReferences": {} +} diff --git a/test/sources/Misc.sol b/test/sources/Misc.sol new file mode 100644 index 00000000000..398114cee23 --- /dev/null +++ b/test/sources/Misc.sol @@ -0,0 +1,9 @@ +pragma solidity ^0.5.1; + +contract Misc { + + string misc; + + constructor() public { + } +} From 93feb45de022a1fe44fb9fdd212180cfc7c588c5 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 30 Oct 2019 20:58:00 -0700 Subject: [PATCH 2/2] Add possible causes to error message --- packages/web3-eth-abi/src/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index 08c9a787d3b..d21d80a846c 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -225,8 +225,10 @@ ABICoder.prototype.decodeParameters = function (outputs, bytes) { if (outputs.length > 0 && (!bytes || bytes === '0x' || bytes === '0X')) { throw new Error( 'Returned values aren\'t valid, did it run Out of Gas? ' + - 'Are you using the correct ABI for the contract you are ' + - 'retrieving data from?' + 'You might also see this error if you are not using the ' + + 'correct ABI for the contract you are retrieving data from, ' + + 'requesting data from a block number that does not exist, ' + + 'or querying a node which is not fully synced.' ); }