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

Commit a8ce1c2

Browse files
committed
Fix issue with encoding large negative numbers
1 parent 2a8fe13 commit a8ce1c2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,13 @@ ABICoder.prototype.formatParam = function (type, param) {
268268
if (match) {
269269
let size = parseInt(match[2] || "256");
270270
if (size / 8 < param.length) {
271-
// pad to correct bit width
272-
param = utils.leftPad(param, size);
271+
if (param.startsWith('-')) {
272+
// pad to correct bit width with - at the beginning
273+
param = `-${utils.leftPad(param.substring(1), size)}`;
274+
} else {
275+
// pad to correct bit width
276+
param = utils.leftPad(param, size);
277+
}
273278
}
274279
}
275280

test/eth.abi.encodeParameter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ var tests = [{
1111
},{
1212
params: ['bytes32[]', ['0xdf3234', '0xfdfd']],
1313
result: '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000'
14+
},{
15+
params: ['int128', '-170141183460469231731687303715884105727'],
16+
result: '0xffffffffffffffffffffffffffffffff80000000000000000000000000000001'
1417
}];
1518

1619
describe('encodeParameter', function () {

0 commit comments

Comments
 (0)