Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve eth-abi decodeParameters error message #3161

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 7 additions & 1 deletion packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ 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? ' +
'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.'
);
}

var res = ethersAbiCoder.decode(this.mapTypes(outputs), '0x' + bytes.replace(/0x/i, ''));
Expand Down
68 changes: 68 additions & 0 deletions test/e2e.method.call.js
Original file line number Diff line number Diff line change
@@ -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'));
}
})
});
15 changes: 15 additions & 0 deletions test/sources/Misc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"contractName": "Misc",
"abi": [
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
],
"bytecode": "0x6080604052348015600f57600080fd5b50603580601d6000396000f3fe6080604052600080fdfea165627a7a723058205b81906cade92d8eeda5d0627076490bd44b563a504fe790edbcd6afadbe359d0029",
"deployedBytecode": "0x6080604052600080fdfea165627a7a723058205b81906cade92d8eeda5d0627076490bd44b563a504fe790edbcd6afadbe359d0029",
"linkReferences": {},
"deployedLinkReferences": {}
}
9 changes: 9 additions & 0 deletions test/sources/Misc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.5.1;

contract Misc {

string misc;

constructor() public {
}
}