Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit f242489

Browse files
committed
add optional default block parameter to contract call fixes #159
1 parent 1d3d727 commit f242489

File tree

8 files changed

+95
-20
lines changed

8 files changed

+95
-20
lines changed

dist/web3-light.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3-light.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3-light.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/web3/function.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var web3 = require('../web3');
2424
var coder = require('../solidity/coder');
2525
var utils = require('../utils/utils');
26+
var formatters = require('./formatters');
2627
var sha3 = require('../utils/sha3');
2728

2829
/**
@@ -46,6 +47,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
4647
}
4748
};
4849

50+
SolidityFunction.prototype.extractDefaultBlock = function (args) {
51+
if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
52+
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
53+
}
54+
};
55+
4956
/**
5057
* Should be used to create payload from arguments
5158
*
@@ -97,15 +104,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
97104
SolidityFunction.prototype.call = function () {
98105
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
99106
var callback = this.extractCallback(args);
107+
var defaultBlock = this.extractDefaultBlock(args);
100108
var payload = this.toPayload(args);
101109

110+
102111
if (!callback) {
103-
var output = web3.eth.call(payload);
112+
var output = web3.eth.call(payload, defaultBlock);
104113
return this.unpackOutput(output);
105114
}
106115

107116
var self = this;
108-
web3.eth.call(payload, function (error, output) {
117+
web3.eth.call(payload, defaultBlock, function (error, output) {
109118
callback(error, self.unpackOutput(output));
110119
});
111120
};

test/contract.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ describe('web3.eth.contract', function () {
133133
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
134134
var signature = 'balance(address)'
135135
var address = '0x1234567890123456789012345678901234567890';
136+
136137
provider.injectValidation(function (payload) {
137138
assert.equal(payload.method, 'eth_call');
138139
assert.deepEqual(payload.params, [{
@@ -147,6 +148,28 @@ describe('web3.eth.contract', function () {
147148
assert.deepEqual(new BigNumber(0x32), r);
148149
});
149150

151+
it('should call constant function with default block', function () {
152+
var provider = new FakeHttpProvider();
153+
web3.setProvider(provider);
154+
web3.reset();
155+
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
156+
var signature = 'balance(address)'
157+
var address = '0x1234567890123456789012345678901234567890';
158+
159+
provider.injectValidation(function (payload) {
160+
assert.equal(payload.method, 'eth_call');
161+
assert.deepEqual(payload.params, [{
162+
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
163+
to: address
164+
}, '0xb']);
165+
});
166+
167+
var contract = web3.eth.contract(desc).at(address);
168+
169+
var r = contract.balance(address, 11);
170+
assert.deepEqual(new BigNumber(0x32), r);
171+
});
172+
150173
it('should sendTransaction to contract function', function () {
151174
var provider = new FakeHttpProvider();
152175
web3.setProvider(provider);
@@ -218,6 +241,31 @@ describe('web3.eth.contract', function () {
218241

219242
});
220243

244+
it('should explicitly make a call with optional params and defaultBlock', function () {
245+
246+
var provider = new FakeHttpProvider();
247+
web3.setProvider(provider);
248+
web3.reset();
249+
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
250+
var signature = 'balance(address)';
251+
var address = '0x1234567890123456789012345678901234567890';
252+
provider.injectValidation(function (payload) {
253+
assert.equal(payload.method, 'eth_call');
254+
assert.deepEqual(payload.params, [{
255+
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
256+
to: address,
257+
from: address,
258+
gas: '0xc350'
259+
}, '0xb']);
260+
});
261+
262+
var contract = web3.eth.contract(desc).at(address);
263+
264+
var r = contract.balance.call(address, {from: address, gas: 50000}, 11);
265+
assert.deepEqual(new BigNumber(0x32), r);
266+
267+
});
268+
221269
it('should sendTransaction with optional params', function () {
222270
var provider = new FakeHttpProvider();
223271
web3.setProvider(provider);

0 commit comments

Comments
 (0)