q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
204 stars 88 forks source link

Unexpected argument mutation #174

Closed d4mk0 closed 1 year ago

d4mk0 commented 1 year ago

Hi! I found unexpected problem with mutating object which i send as param. Code example

require 'eth'
params = { to: "0x0000000000000000000000000000000000000000", value: 101 }
Eth::Client.create("https://cloudflare-eth.com").eth_estimate_gas(params)
pp params # {:to=>"0x0000000000000000000000000000000000000000", :value=>"0x65"}

May be we solve this unexpected behavior by adding something like params = params.dup after https://github.com/q9f/eth.rb/blob/main/lib/eth/client.rb#L464 ?

I can work with it, but some people can get problem like me now.

q9f commented 1 year ago

Yes, we marshall the parameters.

par = { to: "0x0000000000000000000000000000000000000000", value: 101 }
# => {:to=>"0x0000000000000000000000000000000000000000", :value=>101}
inf = Client.create "https://mainnet.infura.io/v3/#{TOKEN}"
# => #<Eth::Client::Http:0x0000563d103d1f50
# @gas_limit=21000,
# @host="mainnet.infura.io",
# @id=0,
# @max_fee_per_gas=0.4269e11,
# @max_priority_fee_per_gas=0.101e10,
# @port=443,
# @ssl=true,
# @uri=#<URI::HTTPS https://mainnet.infura.io/v3/#{TOKEN}>>
inf.eth_estimate_gas(par)
# => {"jsonrpc"=>"2.0", "id"=>1, "result"=>"0x5208"}
par
# => {:to=>"0x0000000000000000000000000000000000000000", :value=>"0x65"}

I have not considered this a problem, but I understand that this might cause an inconvenience. I'd be happy to accept a .dup here.