Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit e91843d

Browse files
author
Jan Xie
committed
fix negative real number encoding in abi
1 parent f10bad9 commit e91843d

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

ethereum/abi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def encode_single(typ, arg):
200200
high, low = [int(x) for x in sub.split('x')]
201201
if not -2**(high - 1) <= arg < 2**(high - 1):
202202
raise ValueOutOfBounds(repr(arg))
203-
return zpad(encode_int(int((arg % 2**high) * 2**low)), 32)
203+
i = int(arg * 2**low)
204+
return zpad(encode_int(i % 2**(high+low)), 32)
204205
# Strings
205206
elif base == 'string' or base == 'bytes':
206207
if not is_string(arg):

ethereum/tests/test_abi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_abi_encode_single_int():
3030

3131
def test_abi_encode_single_real():
3232
assert abi.encode_single(['real', '128x128', []], 1.125) == (b'\x00'*15 + b'\x01' + b'\x20' + b'\x00'*15)
33+
assert abi.encode_single(['real', '128x128', []], -1.125) == (b'\xff'*15 + b'\xfe' + b'\xe0' + b'\x00'*15)
3334

3435
def test_abi_encode_single_hash():
3536
assert abi.encode_single(['hash', '8', []], b'\x00'*8) == b'\x00'*32

0 commit comments

Comments
 (0)