Skip to content

Commit

Permalink
Fix Eth::Abi::DecodingError in call method (#105)
Browse files Browse the repository at this point in the history
* Fix Eth::Abi::DecodingError in call method

* Fix argument address
  • Loading branch information
kurotaky authored May 21, 2022
1 parent 5eee050 commit 33089f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def call(contract, function_name, *args, **kwargs)
func = contract.functions.select { |func| func.name == function_name }[0]
raise ArgumentError, "function_name does not exist!" if func.nil?
output = call_raw(contract, func, *args, **kwargs)
if output.length == 1
if output&.length == 1
return output[0]
else
return output
Expand Down Expand Up @@ -422,6 +422,7 @@ def call_raw(contract, func, *args, **kwargs)
end
raw_result = eth_call(params)["result"]
types = func.outputs.map { |i| i.type }
return nil if raw_result == "0x"
Eth::Abi.decode(types, raw_result)
end

Expand Down
8 changes: 8 additions & 0 deletions spec/eth/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@
subject(:test_key) { Key.new }
subject(:contract) { Eth::Contract.from_file(file: "spec/fixtures/contracts/dummy.sol") }
subject(:test_contract) { Eth::Contract.from_file(file: "spec/fixtures/contracts/simple_registry.sol") }
let(:erc20_abi_file) { File.read "spec/fixtures/abi/ERC20.json" }
let(:address) { Eth::Address.new("0xd496b23d61f88a8c7758fca7560dcfac7b3b01f9").address }
subject(:erc20_abi) { JSON.parse erc20_abi_file }
subject(:erc20_contract) { Eth::Contract.from_abi(abi: erc20_abi, name: "ERC20", address: address) }

it "call function name" do
geth_dev_http.deploy_and_wait(contract)
result = geth_dev_http.call(contract, "get")
expect(result).to eq(0)
end

it "return nil if raw result is 0x" do
expect(geth_dev_http.call(erc20_contract, "balanceOf", address)).to be_nil
end

it "called function name not defined" do
expect { geth_dev_http.call(contract, "ge") }.to raise_error ArgumentError
end
Expand Down

0 comments on commit 33089f7

Please sign in to comment.