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

Handle overload functions #1185

Merged
merged 4 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ var Contract = function Contract(jsonInterface, address, options) {


// add method only if not one already exists
if(!_this.methods[method.name])
if(!_this.methods[method.name]) {
_this.methods[method.name] = func;
} else {
var cascadeFunc = _this._createTxObject.bind({
method: method,
parent: _this,
next: _this.methods[method.name]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we please rename this to: next -> nextMethod

btw we have the same issue for events :0

});
_this.methods[method.name] = cascadeFunc;
}

// definitely add the method based on its signature
_this.methods[method.signature] = func;
Expand Down Expand Up @@ -678,6 +686,9 @@ Contract.prototype._createTxObject = function _createTxObject(){
txObject.estimateGas = this.parent._executeMethod.bind(txObject, 'estimate');

if (args && this.method.inputs && args.length !== this.method.inputs.length) {
if (this.next) {
return this.next.apply(null, args);
}
throw errors.InvalidNumberOfParams(args.length, this.method.inputs.length, this.method.name);
}

Expand Down
63 changes: 63 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ var abi = [{
{"name":"addressFrom","type":"address","indexed":true},
{"name":"t1","type":"uint256","indexed":false}
]
}, {
"name":"overloadedFunction",
"type":"function",
"inputs":[
{"name":"a","type":"uint256"}
],
"constant":true,
"outputs":[
{"name":"", "type":"uint256"}
],
"payable":false,
"stateMutability":"view"
}, {
"name":"overloadedFunction",
"type":"function",
"inputs":[],
"constant":true,
"outputs":[
{"name":"","type":"uint256"}
],
"payable":false,
"stateMutability":"view"
}];

var address = '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe';
Expand Down Expand Up @@ -1414,6 +1436,47 @@ describe('contract', function () {
});
});

it('should send overload functions with zero parameters', function(done) {
var provider = new FakeIpcProvider();
var eth = new Eth(provider);

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0xbb853481',
to: addressLowercase
}, 'latest']);
});
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000005');

var contract = new eth.Contract(abi, address);
contract.methods.overloadedFunction().call(function (err, res) {
assert.equal(res, 5);
done();
});
});

it('should send overload functions with one parameters', function(done) {
var provider = new FakeIpcProvider();
var eth = new Eth(provider);

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0x533678270000000000000000000000000000000000000000000000000000000000000006',
to: addressLowercase
}, 'latest']);
});
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000006');

var contract = new eth.Contract(abi, address);

contract.methods.overloadedFunction(6).call(function (err, res) {
assert.equal(res, 6);
done();
});
});

it('should call constant function', function (done) {
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
Expand Down