Skip to content

Commit

Permalink
Handle overload functions (#1185)
Browse files Browse the repository at this point in the history
* Handle overload functions

* next -> nextMethod
  • Loading branch information
jbaylina authored and frozeman committed Nov 23, 2017
1 parent c4c92c3 commit d19f889
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
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,
nextMethod: _this.methods[method.name]
});
_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.nextMethod) {
return this.nextMethod.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

0 comments on commit d19f889

Please sign in to comment.