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

Commit f67a025

Browse files
author
Jan Xie
committed
fix real type decoding in abi
1 parent 6ff6662 commit f67a025

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

ethereum/abi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ def decode_single(typ, data):
389389
return big_endian_to_int(data) * 1.0 / 2**low
390390
elif base == 'real':
391391
high, low = [int(x) for x in sub.split('x')]
392-
return (big_endian_to_int(data) * 1.0 / 2**low) % 2**high
392+
o = big_endian_to_int(data)
393+
i = (o - 2**(high+low)) if o >= 2**(high+low-1) else o
394+
return (i * 1.0 / 2**low)
393395
elif base == 'bool':
394396
return bool(int(data.encode('hex'), 16))
395397

ethereum/tests/test_abi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_abi_encode_single_prefixed_address():
4545
prefixed_address = '0x' + '0'*40
4646
assert abi.encode_single(['address', '', []], prefixed_address) == b'\x00' * 32
4747

48+
def test_abi_decode_single_real():
49+
real_data = abi.encode_single(['real', '128x128', []], 1)
50+
assert abi.decode_single(['real', '128x128', []], real_data) == 1
51+
52+
real_data = abi.encode_single(['real', '128x128', []], 2**127-1)
53+
assert abi.decode_single(['real', '128x128', []], real_data) == (2**127-1)*1.0
54+
55+
real_data = abi.encode_single(['real', '128x128', []], -1)
56+
assert abi.decode_single(['real', '128x128', []], real_data) == -1
57+
58+
real_data = abi.encode_single(['real', '128x128', []], -2**127)
59+
assert abi.decode_single(['real', '128x128', []], real_data) == -2**127
60+
4861
# SETUP TESTS IN GLOBAL NAME SPACE
4962
def gen_func(filename, testname, testdata):
5063
return lambda: do_test_state(filename, testname, testdata)

0 commit comments

Comments
 (0)