Skip to content

Commit

Permalink
Support negative number from JSON RPC (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
wafcio authored Jun 23, 2024
1 parent fbd70c1 commit a89936e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/eth/abi/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def primitive_type(type, data)
Util.deserialize_big_endian_to_int data
when "int"
u = Util.deserialize_big_endian_to_int data
i = u >= 2 ** (type.sub_type.to_i - 1) ? (u - 2 ** type.sub_type.to_i) : u
i = u >= 2 ** (type.sub_type.to_i - 1) ? (u - 2 ** 256) : u

# decoded integer
i
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/abi/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def int(arg, type)
real_size = type.sub_type.to_i
i = arg.to_i
raise ValueOutOfBounds, arg unless i >= -2 ** (real_size - 1) and i < 2 ** (real_size - 1)
Util.zpad_int(i % 2 ** type.sub_type.to_i)
Util.zpad_int(i < 0 ? (i + 2 ** 256) : i % 2 ** type.sub_type.to_i)
end

# Properly encodes booleans.
Expand Down
7 changes: 7 additions & 0 deletions spec/eth/abi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,12 @@ def assert(data, types, args)
pending("https://github.com/q9f/eth.rb/issues/102")
assert(data, types, args)
end

it "test negative number" do
types = ["int24"]
args = [-887220]
data = Util.hex_to_bin "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2764c"
assert(data, types, args)
end
end
end

0 comments on commit a89936e

Please sign in to comment.