Skip to content

Commit

Permalink
eth/abi: allow parsing numerics from string inputs (#112)
Browse files Browse the repository at this point in the history
* eth/abi: allow parsing numerics from string inputs

* sepc: run rufo
  • Loading branch information
q9f authored Jun 10, 2022
1 parent c0bbae9 commit bb640c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/eth/abi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def signature(interface)

# Properly encodes unsigned integers.
def encode_uint(arg, type)
arg = arg.to_i if arg.is_a? String
raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::UINT_MAX or arg < Constant::UINT_MIN
real_size = type.sub_type.to_i
i = arg.to_i
Expand All @@ -315,6 +316,7 @@ def encode_uint(arg, type)

# Properly encodes signed integers.
def encode_int(arg, type)
arg = arg.to_i if arg.is_a? String
raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::INT_MAX or arg < Constant::INT_MIN
real_size = type.sub_type.to_i
i = arg.to_i
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 @@ -61,6 +61,13 @@
end
end

it "can encode numbers from string inputs" do
expect(Abi.encode(["uint256"], ["10000000000000000000000"])).to eq Abi.encode(["uint256"], [10000000000000000000000])
expect(Abi.decode(["uint256"], "00000000000000000000000000000000000000000000021e19e0c9bab2400000")).to eq [10000000000000000000000]
expect(Abi.encode(["int256"], ["20000000000000000000000"])).to eq Abi.encode(["int256"], [20000000000000000000000])
expect(Abi.decode(["int256"], "00000000000000000000000000000000000000000000043c33c1937564800000")).to eq [20000000000000000000000]
end

it "can do encode and decode complex types" do
types = [
"bool",
Expand Down

0 comments on commit bb640c7

Please sign in to comment.