Skip to content

Commit

Permalink
Merge pull request #3238 from ethereum/issue/3221
Browse files Browse the repository at this point in the history
big-number (BN & BigNumber) mapping added to abi.encodeParameters
  • Loading branch information
nivida committed Nov 26, 2019
2 parents 3404e20 + 20103ba commit e914ec3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Released with 1.0.0-beta.37 code base.
- ``eth_requestAccounts`` as ``requestAccounts`` added to web3-eth package (#3219)
- ``sha3Raw`` and ``soliditySha3Raw`` added to web3-utils package (#3226)
- ``eth_getProof`` as ``getProof`` added to web3-eth package (#3220)
- ``BN`` and ``BigNumber`` objects are now supported by the ``abi.encodeParameter(s)`` method (#3238)
- ``getPendingTransactions`` added to web3-eth package (#3239)

### Fixed
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions packages/web3-eth-abi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ ABICoder.prototype.encodeEventSignature = function (functionName) {
* Should be used to encode plain param
*
* @method encodeParameter
* @param {String} type
* @param {Object} param
*
* @param {String|Object} type
* @param {any} param
*
* @return {String} encoded plain param
*/
ABICoder.prototype.encodeParameter = function (type, param) {
Expand All @@ -88,12 +90,23 @@ ABICoder.prototype.encodeParameter = function (type, param) {
* Should be used to encode list of params
*
* @method encodeParameters
* @param {Array} types
* @param {Array} params
*
* @param {Array<String|Object>} types
* @param {Array<any>} params
*
* @return {String} encoded list of params
*/
ABICoder.prototype.encodeParameters = function (types, params) {
return ethersAbiCoder.encode(this.mapTypes(types), params);
return ethersAbiCoder.encode(
this.mapTypes(types),
params.map(function (param) {
if (utils.isBN(param) || utils.isBigNumber(param)) {
return param.toString(10);
}

return param;
})
);
};

/**
Expand Down
24 changes: 23 additions & 1 deletion test/abi.encodeParameter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var chai = require('chai');
var assert = chai.assert;
var BN = require('bn.js');
var BigNumber = require('bignumber.js');
var coder = require('../packages/web3-eth-abi');


Expand Down Expand Up @@ -305,7 +307,17 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000120' +
'000000000000000000000000000000000000000000000000000000000000000f' +
'74657374696e672074657374696e670000000000000000000000000000000000' })
'74657374696e672074657374696e670000000000000000000000000000000000' });
test({
type: 'uint256',
value: new BN(42),
expected: '000000000000000000000000000000000000000000000000000000000000002a'
});
test({
type: 'uint256',
value: new BigNumber(42),
expected: '000000000000000000000000000000000000000000000000000000000000002a'
});
});
});

Expand Down Expand Up @@ -656,6 +668,16 @@ describe('lib/solidity/coder', function () {
'bc00000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'de00000000000000000000000000000000000000000000000000000000000000' })
test({
types: ['uint256', 'uint256'],
values: [new BN(42), new BN(42)],
expected: '000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002a'
});
test({
types: ['uint256', 'uint256'],
values: [new BigNumber(42), new BigNumber(42)],
expected: '000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002a'
});
});
});

Expand Down

0 comments on commit e914ec3

Please sign in to comment.