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

Commit 0541faa

Browse files
Update contract class to not mutate options object (#5394)
* Update contract class to not mutate options object * Add .send test for options mutation test * Update CHANGELOG * Remove errounous import
1 parent 0d38050 commit 0541faa

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,4 @@ Released with 1.0.0-beta.37 code base.
593593
### Fixed
594594
- Browser builds support polyfills (#5031) (#5053) (#4659) (#4767)
595595
- Start incrementing jsonrpc.id from random number (#5327)
596+
- `web3-eth-contract`'s `call` and `send` methods no longer mutate `options` argument (#5394)

packages/web3-eth-contract/src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,20 @@ Contract.prototype._checkListener = function(type, event){
379379
* @return {Object} the options with gaps filled by defaults
380380
*/
381381
Contract.prototype._getOrSetDefaultOptions = function getOrSetDefaultOptions(options) {
382-
var gasPrice = options.gasPrice ? String(options.gasPrice): null;
383-
var from = options.from ? utils.toChecksumAddress(formatters.inputAddressFormatter(options.from)) : null;
382+
var _options = { ...options };
383+
var gasPrice = _options.gasPrice ? String(_options.gasPrice): null;
384+
var from = _options.from ? utils.toChecksumAddress(formatters.inputAddressFormatter(_options.from)) : null;
384385

385-
options.data = options.data || this.options.data;
386+
_options.data = _options.data || this.options.data;
386387

387-
options.from = from || this.options.from;
388-
options.gasPrice = gasPrice || this.options.gasPrice;
389-
options.gas = options.gas || options.gasLimit || this.options.gas;
388+
_options.from = from || this.options.from;
389+
_options.gasPrice = gasPrice || this.options.gasPrice;
390+
_options.gas = _options.gas || _options.gasLimit || this.options.gas;
390391

391392
// TODO replace with only gasLimit?
392-
delete options.gasLimit;
393+
delete _options.gasLimit;
393394

394-
return options;
395+
return _options;
395396
};
396397

397398

test/contract.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,77 @@ var runTests = function(contractFactory) {
31523152
describe('typical usage', function() {
31533153
runTests(getEthContractInstance);
31543154

3155+
it('should not mutate options object - call', function (done) {
3156+
var provider = new FakeHttpProvider();
3157+
3158+
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
3159+
3160+
var eth = new Eth(provider);
3161+
var contract = new eth.Contract(abi, address);
3162+
var options = { from: address };
3163+
var expectedOptions = { ...options };
3164+
3165+
contract.methods.balance(address).call(options)
3166+
.then(function () {
3167+
assert.deepEqual(options, expectedOptions);
3168+
done();
3169+
});
3170+
});
3171+
3172+
it('should not mutate options object - send', function (done) {
3173+
var provider = new FakeHttpProvider();
3174+
3175+
provider.injectResult('0x1234000000000000000000000000000000000000000000000000000000056789');
3176+
provider.injectResult({
3177+
contractAddress: null,
3178+
cumulativeGasUsed: '0xa',
3179+
transactionIndex: '0x3',
3180+
transactionHash: '0x1234',
3181+
blockNumber: '0xa',
3182+
blockHash: '0x1234',
3183+
gasUsed: '0x0',
3184+
logs: [{
3185+
address: address,
3186+
topics: [
3187+
sha3('Unchanged(uint256,address,uint256)'),
3188+
'0x0000000000000000000000000000000000000000000000000000000000000002',
3189+
'0x000000000000000000000000'+ addressLowercase.replace('0x','')
3190+
],
3191+
blockNumber: '0xa',
3192+
transactionHash: '0x1234',
3193+
transactionIndex: '0x0',
3194+
blockHash: '0x1345',
3195+
logIndex: '0x4',
3196+
data: '0x0000000000000000000000000000000000000000000000000000000000000005'
3197+
},{
3198+
address: address,
3199+
topics: [
3200+
sha3('Changed(address,uint256,uint256,uint256)'),
3201+
'0x000000000000000000000000'+ addressLowercase.replace('0x',''),
3202+
'0x0000000000000000000000000000000000000000000000000000000000000001'
3203+
],
3204+
blockNumber: '0xa',
3205+
transactionHash: '0x1234',
3206+
transactionIndex: '0x0',
3207+
blockHash: '0x1345',
3208+
logIndex: '0x4',
3209+
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
3210+
'0000000000000000000000000000000000000000000000000000000000000008'
3211+
}]
3212+
});
3213+
3214+
var eth = new Eth(provider);
3215+
var contract = new eth.Contract(abi, address);
3216+
var options = { from: address, gasPrice: '21345678654321' };
3217+
var expectedOptions = { ...options };
3218+
3219+
contract.methods.mySend(address, 10).send(options)
3220+
.on('receipt', function () {
3221+
assert.deepEqual(options, expectedOptions);
3222+
done();
3223+
});
3224+
});
3225+
31553226
it('should update contract instance provider when assigned a provider to eth instance that contract instance came from', function () {
31563227
var provider1 = new FakeIpcProvider();
31573228
var provider2 = new FakeHttpProvider();

0 commit comments

Comments
 (0)