add optional default block parameter to contract call fixes #159

This commit is contained in:
Fabian Vogelsteller 2015-06-05 11:34:50 +02:00
parent 1d3d727f31
commit f242489f81
8 changed files with 95 additions and 20 deletions

17
dist/web3-light.js vendored
View File

@ -2479,7 +2479,7 @@ Filter.prototype.watch = function (callback) {
Filter.prototype.stopWatching = function () {
RequestManager.getInstance().stopPolling(this.filterId);
// remove async
// remove filter async
this.implementation.uninstallFilter(this.filterId, function(){});
this.callbacks = [];
};
@ -2753,6 +2753,7 @@ module.exports = {
var web3 = require('../web3');
var coder = require('../solidity/coder');
var utils = require('../utils/utils');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
/**
@ -2776,6 +2777,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
}
};
SolidityFunction.prototype.extractDefaultBlock = function (args) {
if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
}
};
/**
* Should be used to create payload from arguments
*
@ -2827,15 +2834,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var defaultBlock = this.extractDefaultBlock(args);
var payload = this.toPayload(args);
if (!callback) {
var output = web3.eth.call(payload);
var output = web3.eth.call(payload, defaultBlock);
return this.unpackOutput(output);
}
var self = this;
web3.eth.call(payload, function (error, output) {
web3.eth.call(payload, defaultBlock, function (error, output) {
callback(error, self.unpackOutput(output));
});
};
@ -2954,7 +2963,7 @@ SolidityFunction.prototype.attachToContract = function (contract) {
module.exports = SolidityFunction;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9}],19:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],19:[function(require,module,exports){
/*
This file is part of ethereum.js.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

17
dist/web3.js vendored
View File

@ -2479,7 +2479,7 @@ Filter.prototype.watch = function (callback) {
Filter.prototype.stopWatching = function () {
RequestManager.getInstance().stopPolling(this.filterId);
// remove async
// remove filter async
this.implementation.uninstallFilter(this.filterId, function(){});
this.callbacks = [];
};
@ -2753,6 +2753,7 @@ module.exports = {
var web3 = require('../web3');
var coder = require('../solidity/coder');
var utils = require('../utils/utils');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
/**
@ -2776,6 +2777,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
}
};
SolidityFunction.prototype.extractDefaultBlock = function (args) {
if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
}
};
/**
* Should be used to create payload from arguments
*
@ -2827,15 +2834,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var defaultBlock = this.extractDefaultBlock(args);
var payload = this.toPayload(args);
if (!callback) {
var output = web3.eth.call(payload);
var output = web3.eth.call(payload, defaultBlock);
return this.unpackOutput(output);
}
var self = this;
web3.eth.call(payload, function (error, output) {
web3.eth.call(payload, defaultBlock, function (error, output) {
callback(error, self.unpackOutput(output));
});
};
@ -2954,7 +2963,7 @@ SolidityFunction.prototype.attachToContract = function (contract) {
module.exports = SolidityFunction;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9}],19:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],19:[function(require,module,exports){
/*
This file is part of ethereum.js.

6
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

@ -23,6 +23,7 @@
var web3 = require('../web3');
var coder = require('../solidity/coder');
var utils = require('../utils/utils');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
/**
@ -46,6 +47,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
}
};
SolidityFunction.prototype.extractDefaultBlock = function (args) {
if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
}
};
/**
* Should be used to create payload from arguments
*
@ -97,15 +104,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var defaultBlock = this.extractDefaultBlock(args);
var payload = this.toPayload(args);
if (!callback) {
var output = web3.eth.call(payload);
var output = web3.eth.call(payload, defaultBlock);
return this.unpackOutput(output);
}
var self = this;
web3.eth.call(payload, function (error, output) {
web3.eth.call(payload, defaultBlock, function (error, output) {
callback(error, self.unpackOutput(output));
});
};

View File

@ -133,6 +133,7 @@ describe('web3.eth.contract', function () {
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)'
var address = '0x1234567890123456789012345678901234567890';
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
@ -147,6 +148,28 @@ describe('web3.eth.contract', function () {
assert.deepEqual(new BigNumber(0x32), r);
});
it('should call constant function with default block', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)'
var address = '0x1234567890123456789012345678901234567890';
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
to: address
}, '0xb']);
});
var contract = web3.eth.contract(desc).at(address);
var r = contract.balance(address, 11);
assert.deepEqual(new BigNumber(0x32), r);
});
it('should sendTransaction to contract function', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
@ -218,6 +241,31 @@ describe('web3.eth.contract', function () {
});
it('should explicitly make a call with optional params and defaultBlock', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)';
var address = '0x1234567890123456789012345678901234567890';
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
to: address,
from: address,
gas: '0xc350'
}, '0xb']);
});
var contract = web3.eth.contract(desc).at(address);
var r = contract.balance.call(address, {from: address, gas: 50000}, 11);
assert.deepEqual(new BigNumber(0x32), r);
});
it('should sendTransaction with optional params', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);