fixed calling contract only with array param

This commit is contained in:
Marek Kotewicz 2015-04-26 10:08:58 +02:00
parent 4912ec6383
commit a2f561f263
10 changed files with 88 additions and 11 deletions

2
dist/web3-light.js vendored
View File

@ -2644,7 +2644,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments);
var options = {};
if (utils.isObject(args[args.length -1])) {
if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/web3.js vendored
View File

@ -2644,7 +2644,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments);
var options = {};
if (utils.isObject(args[args.length -1])) {
if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;

2
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

4
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -49,7 +49,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments);
var options = {};
if (utils.isObject(args[args.length -1])) {
if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop();
}
options.to = this._address;

View File

@ -2,6 +2,7 @@ var chai = require('chai');
var assert = chai.assert;
var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2');
var utils = require('../lib/utils/utils');
var desc = [{
@ -27,6 +28,18 @@ var desc = [{
"type": "uint256"
}],
"outputs": []
}, {
"name": "testArr(int[])",
"type": "function",
"inputs": [{
"name": "value",
"type": "int[]"
}],
"constant": true,
"outputs": [{
"name": "d",
"type": "int"
}]
}, {
"name":"Changed",
"type":"event",
@ -313,5 +326,38 @@ describe('web3.eth.contract', function () {
contract.send.sendTransaction(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000});
});
it('should call testArr method and properly parse result', function () {
var provider = new FakeHttpProvider2();
web3.setProvider(provider);
web3.reset();
var sha3 = '0x5131231231231231231231';
var address = '0x1234567890123456789012345678901234567890';
provider.injectResultList([{
result: sha3
}, {
result: '0x0000000000000000000000000000000000000000000000000000000000000005'
}]);
var step = 0;
provider.injectValidation(function (payload) {
if (step === 1) { // getting sha3 is first
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: sha3.slice(0, 10) +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003',
to: address
},
'latest'
]);
}
step++;
});
var Contract = web3.eth.contract(desc);
var contract = new Contract(address);
contract.testArr([3]);
});
});
});

View File

@ -26,7 +26,7 @@ FakeHttpProvider.prototype.send = function (payload) {
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)));
}
return this.response;
return this.getResponse();
};
FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
@ -36,7 +36,7 @@ FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)), callback);
}
callback(this.error, this.response);
callback(this.error, this.getResponse());
};
FakeHttpProvider.prototype.injectResponse = function (response) {
@ -56,6 +56,10 @@ FakeHttpProvider.prototype.injectBatchResults = function (results) {
});
};
FakeHttpProvider.prototype.getResponse = function () {
return this.response;
};
FakeHttpProvider.prototype.injectError = function (error) {
this.error = error;
};

View File

@ -0,0 +1,27 @@
var FakeHttpProvider = require('./FakeHttpProvider');
var FakeHttpProvider2 = function () {
this.counter = 0;
this.resultList = [];
};
FakeHttpProvider2.prototype = new FakeHttpProvider();
FakeHttpProvider2.prototype.constructor = FakeHttpProvider2;
FakeHttpProvider2.prototype.injectResultList = function (list) {
this.resultList = list;
};
FakeHttpProvider2.prototype.getResponse = function () {
var result = this.resultList[this.counter];
this.counter++;
if (result.type === 'batch') {
this.injectBatchResults(result.result);
} else {
this.injectResult(result.result);
}
return this.response;
};
module.exports = FakeHttpProvider2;