Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

big-number (BN & BigNumber) mapping added to abi.encodeParameters #3238

Merged
merged 3 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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