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
200 stars 86 forks source link

eth/contract greeter.sol - "Hash (NoMethodError) Did you mean? min" error #113

Closed unixneo closed 2 years ago

unixneo commented 2 years ago

Hi All. Thank you very much for this eth gem.

Having trouble with my first ethgreeter test contract. Here is the module, ruby console and run-time error.

What am I missing? Must be something simple but I have read the docs over-and-over; banging head :)

module EthGem 
    require "eth"

    CHAINSTACK_ENDPOINT_ROPSTEN=ENV['CHAINSTACK_ENDPOINT_ROPSTEN'] unless const_defined?(:CHAINSTACK_ENDPOINT_ROPSTEN)
    METAMASK_ETH_ADDRESS=ENV['METAMASK_ETH_ADDRESS'] unless const_defined?(:METAMASK_ETH_ADDRESS)
    DEBUG = false unless const_defined?(:DEBUG)

    class Core
       def initialize
       end

       def self.greeter 
        chain = Eth::Client.create CHAINSTACK_ENDPOINT_ROPSTEN
        puts "METAMASK_ETH_ADDRESS: #{METAMASK_ETH_ADDRESS}"
        deposit_contract = Eth::Address.new METAMASK_ETH_ADDRESS
        balance = (chain.get_balance deposit_contract).to_f
        puts "Balance is #{balance.round(8)}"
        solc = Eth::Solidity.new
        contract = solc.compile "#{Rails.root}/greeter.sol"
        chain.deploy_and_wait(contract)
       end
    end
end

and the error:

irb(main):002:0> EthGem::Core.greeter
METAMASK_ETH_ADDRESS: "OMITTED: AVAILABLE ON REQUEST IF NEEDED TO DEBUG"
Balance is 4.0e+18              
/Users/tim/rails/eth/vendor/bundle/ruby/3.0.0/gems/eth-0.5.5/lib/eth/client.rb:182:in `deploy': undefined method `bin' for {"Greeter"=>{"abi"=>[{"inputs"=>[{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}], "stateMutability"=>"nonpayable", "type"=>"constructor"}, {"inputs"=>[], "name"=>"greet", "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}], "stateMutability"=>"view", "type"=>"function"}, {"inputs"=>[{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}], "name"=>"setGreeting", "outputs"=>[], "stateMutability"=>"nonpayable", "type"=>"function"}], "bin"=>"608060405234801561001057600080fd5b5060405161054c38038061054c83398101604081905261002f916100f8565b8051610042906000906020840190610049565b5050610201565b828054610055906101c7565b90600052602060002090601f01602090048101928261007757600085556100bd565b82601f1061009057805160ff19168380011785556100bd565b828001600101855582156100bd579182015b828111156100bd5782518255916020019190600101906100a2565b506100c99291506100cd565b5090565b5b808211156100c957600081556001016100ce565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561010b57600080fd5b82516001600160401b038082111561012257600080fd5b818501915085601f83011261013657600080fd5b815181811115610148576101486100e2565b604051601f8201601f19908116603f01168101908382118183101715610170576101706100e2565b81604052828152888684870101111561018857600080fd5b600093505b828410156101aa578484018601518185018701529285019261018d565b828411156101bb5760008684830101525b98975050505050505050565b600181811c908216806101db57607f821691505b6020821081036101fb57634e487b7160e01b600052602260045260246000fd5b50919050565b61033c806102106000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae321714610050575b600080fd5b61004e6100493660046101c6565b61006e565b005b610058610085565b6040516100659190610277565b60405180910390f35b8051610081906000906020840190610117565b5050565b606060008054610094906102cc565b80601f01602080910402602001604051908101604052809291908181526020018280546100c0906102cc565b801561010d5780601f106100e25761010080835404028352916020019161010d565b820191906000526020600020905b8154815290600101906020018083116100f057829003601f168201915b5050505050905090565b828054610123906102cc565b90600052602060002090601f016020900481019282610145576000855561018b565b82601f1061015e57805160ff191683800117855561018b565b8280016001018555821561018b579182015b8281111561018b578251825591602001919060010190610170565b5061019792915061019b565b5090565b5b80821115610197576000815560010161019c565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156101d857600080fd5b813567ffffffffffffffff808211156101f057600080fd5b818401915084601f83011261020457600080fd5b813581811115610216576102166101b0565b604051601f8201601f19908116603f0116810190838211818310171561023e5761023e6101b0565b8160405282815287602084870101111561025757600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b818110156102a457858101830151858201604001528201610288565b818111156102b6576000604083870101525b50601f01601f1916929092016040019392505050565b600181811c908216806102e057607f821691505b60208210810361030057634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122096013f8b33bbb3794d36acf00a07a399d713334cdb3716058e145765786b5c0664736f6c634300080e0033"}}:Hash (NoMethodError)
Did you mean?  min
irb(main):003:0> 

Thanks for helping me out.

unixneo commented 2 years ago

DEBUGGING INFO

My "guess" is there is a bug related to this line? Looks like the contract hash is formatted differently than the line below expects?

contract['Greeter']['bin'] versus. contract.bin ?
/Users/tim/rails/eth/vendor/bundle/ruby/3.0.0/gems/eth-0.5.5/lib/eth/client.rb:182
def deploy(contract, *args, **kwargs)
      raise ArgumentError, "Cannot deploy contract without source or binary!" if contract.bin.nil?
      # ...
      # ...
end
irb(main):002:0> solc = Eth::Solidity.new
=> #<Eth::Solidity:0x0000000112a17530 @compiler="/opt/homebrew/bin/solc">
MacStudio:eth tim$ /opt/homebrew/bin/solc --version
solc, the solidity compiler commandline interface
Version: 0.8.14+commit.80d49f37.Darwin.appleclang
irb(main):001:0> ENV["PATHEXT"]
=> nil
irb(main):002:0> ENV["PATH"]
=> "/Users/tim/rails/eth/vendor/bundle/ruby/3.0.0/bin:/Users/tim/.rbenv/versions/3.0.3/bin:/opt/homebrew/Cellar/rbenv/1.2.0/libexec:/Users/tim/.local/share/solana/install/active_release/bin:/Users/tim/.rbenv/shims:/Users/tim/.rbenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

Or it is some other problem?

{"Greeter"=>                    
  {"abi"=>                      
    [{"inputs"=>                
       [{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}],
      "stateMutability"=>"nonpayable",
      "type"=>"constructor"},
     {"inputs"=>[],
      "name"=>"greet",
      "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}],
      "stateMutability"=>"view",
      "type"=>"function"},
     {"inputs"=>
       [{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}],
      "name"=>"setGreeting",
      "outputs"=>[],
      "stateMutability"=>"nonpayable",
      "type"=>"function"}],
   "bin"=>
    "608060405234801561001057600080fd5b5060405161054c38038061054c83398101604081905261002f916100f8565b8051610042906000906020840190610049565b5050610201565b828054610055906101c7565b90600052602060002090601f01602090048101928261007757600085556100bd565b82601f1061009057805160ff19168380011785556100bd565b828001600101855582156100bd579182015b828111156100bd5782518255916020019190600101906100a2565b506100c99291506100cd565b5090565b5b808211156100c957600081556001016100ce565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561010b57600080fd5b82516001600160401b038082111561012257600080fd5b818501915085601f83011261013657600080fd5b815181811115610148576101486100e2565b604051601f8201601f19908116603f01168101908382118183101715610170576101706100e2565b81604052828152888684870101111561018857600080fd5b600093505b828410156101aa578484018601518185018701529285019261018d565b828411156101bb5760008684830101525b98975050505050505050565b600181811c908216806101db57607f821691505b6020821081036101fb57634e487b7160e01b600052602260045260246000fd5b50919050565b61033c806102106000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae321714610050575b600080fd5b61004e6100493660046101c6565b61006e565b005b610058610085565b6040516100659190610277565b60405180910390f35b8051610081906000906020840190610117565b5050565b606060008054610094906102cc565b80601f01602080910402602001604051908101604052809291908181526020018280546100c0906102cc565b801561010d5780601f106100e25761010080835404028352916020019161010d565b820191906000526020600020905b8154815290600101906020018083116100f057829003601f168201915b5050505050905090565b828054610123906102cc565b90600052602060002090601f016020900481019282610145576000855561018b565b82601f1061015e57805160ff191683800117855561018b565b8280016001018555821561018b579182015b8281111561018b578251825591602001919060010190610170565b5061019792915061019b565b5090565b5b80821115610197576000815560010161019c565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156101d857600080fd5b813567ffffffffffffffff808211156101f057600080fd5b818401915084601f83011261020457600080fd5b813581811115610216576102166101b0565b604051601f8201601f19908116603f0116810190838211818310171561023e5761023e6101b0565b8160405282815287602084870101111561025757600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b818110156102a457858101830151858201604001528201610288565b818111156102b6576000604083870101525b50601f01601f1916929092016040019392505050565b600181811c908216806102e057607f821691505b60208210810361030057634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122096013f8b33bbb3794d36acf00a07a399d713334cdb3716058e145765786b5c0664736f6c634300080e0033"}}
unixneo commented 2 years ago

DEBUGGING

def deploy(contract, *args, **kwargs)
      raise ArgumentError, "Cannot deploy contract without source or binary!" if contract.bin.nil?
      # ...
      # ...
end
irb(main):025:0> contract = solc.compile("#{Rails.root}/greeter.sol")
=> 
{"Greeter"=>                                              
...                                                       
irb(main):026:0> contract['Greeter']['bin']
=> "608060405234801561001057600080fd5b5060405161054c38038061054c83398101604081905261002f916100f8565b8051610042906000906020840190610049565b5050610201565b828054610055906101c7565b90600052602060002090601f01602090048101928261007757600085556100bd565b82601f1061009057805160ff19168380011785556100bd565b828001600101855582156100bd579182015b828111156100bd5782518255916020019190600101906100a2565b506100c99291506100cd565b5090565b5b808211156100c957600081556001016100ce565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561010b57600080fd5b82516001600160401b038082111561012257600080fd5b818501915085601f83011261013657600080fd5b815181811115610148576101486100e2565b604051601f8201601f19908116603f01168101908382118183101715610170576101706100e2565b81604052828152888684870101111561018857600080fd5b600093505b828410156101aa578484018601518185018701529285019261018d565b828411156101bb5760008684830101525b98975050505050505050565b600181811c908216806101db57607f821691505b6020821081036101fb57634e487b7160e01b600052602260045260246000fd5b50919050565b61033c806102106000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae321714610050575b600080fd5b61004e6100493660046101c6565b61006e565b005b610058610085565b6040516100659190610277565b60405180910390f35b8051610081906000906020840190610117565b5050565b606060008054610094906102cc565b80601f01602080910402602001604051908101604052809291908181526020018280546100c0906102cc565b801561010d5780601f106100e25761010080835404028352916020019161010d565b820191906000526020600020905b8154815290600101906020018083116100f057829003601f168201915b5050505050905090565b828054610123906102cc565b90600052602060002090601f016020900481019282610145576000855561018b565b82601f1061015e57805160ff191683800117855561018b565b8280016001018555821561018b579182015b8281111561018b578251825591602001919060010190610170565b5061019792915061019b565b5090565b5b80821115610197576000815560010161019c565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156101d857600080fd5b813567ffffffffffffffff808211156101f057600080fd5b818401915084601f83011261020457600080fd5b813581811115610216576102166101b0565b604051601f8201601f19908116603f0116810190838211818310171561023e5761023e6101b0565b8160405282815287602084870101111561025757600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b818110156102a457858101830151858201604001528201610288565b818111156102b6576000604083870101525b50601f01601f1916929092016040019392505050565b600181811c908216806102e057607f821691505b60208210810361030057634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122096013f8b33bbb3794d36acf00a07a399d713334cdb3716058e145765786b5c0664736f6c634300080e0033"
irb(main):027:0> contract.Greeter
(irb):27:in `<main>': undefined method `Greeter' for {"Greeter"=>{"abi"=>[{"inputs"=>[{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}], "stateMutability"=>"nonpayable", "type"=>"constructor"}, {"inputs"=>[], "name"=>"greet", "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}], "stateMutability"=>"view", "type"=>"function"}, {"inputs"=>[{"internalType"=>"string", "name"=>"_greeting", "type"=>"string"}], "name"=>"setGreeting", "outputs"=>[], "stateMutability"=>"nonpayable", "type"=>"function"}], "bin"=>"608060405234801561001057600080fd5b5060405161054c38038061054c83398101604081905261002f916100f8565b8051610042906000906020840190610049565b5050610201565b828054610055906101c7565b90600052602060002090601f01602090048101928261007757600085556100bd565b82601f1061009057805160ff19168380011785556100bd565b828001600101855582156100bd579182015b828111156100bd5782518255916020019190600101906100a2565b506100c99291506100cd565b5090565b5b808211156100c957600081556001016100ce565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561010b57600080fd5b82516001600160401b038082111561012257600080fd5b818501915085601f83011261013657600080fd5b815181811115610148576101486100e2565b604051601f8201601f19908116603f01168101908382118183101715610170576101706100e2565b81604052828152888684870101111561018857600080fd5b600093505b828410156101aa578484018601518185018701529285019261018d565b828411156101bb5760008684830101525b98975050505050505050565b600181811c908216806101db57607f821691505b6020821081036101fb57634e487b7160e01b600052602260045260246000fd5b50919050565b61033c806102106000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063cfae321714610050575b600080fd5b61004e6100493660046101c6565b61006e565b005b610058610085565b6040516100659190610277565b60405180910390f35b8051610081906000906020840190610117565b5050565b606060008054610094906102cc565b80601f01602080910402602001604051908101604052809291908181526020018280546100c0906102cc565b801561010d5780601f106100e25761010080835404028352916020019161010d565b820191906000526020600020905b8154815290600101906020018083116100f057829003601f168201915b5050505050905090565b828054610123906102cc565b90600052602060002090601f016020900481019282610145576000855561018b565b82601f1061015e57805160ff191683800117855561018b565b8280016001018555821561018b579182015b8281111561018b578251825591602001919060010190610170565b5061019792915061019b565b5090565b5b80821115610197576000815560010161019c565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156101d857600080fd5b813567ffffffffffffffff808211156101f057600080fd5b818401915084601f83011261020457600080fd5b813581811115610216576102166101b0565b604051601f8201601f19908116603f0116810190838211818310171561023e5761023e6101b0565b8160405282815287602084870101111561025757600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b818110156102a457858101830151858201604001528201610288565b818111156102b6576000604083870101525b50601f01601f1916929092016040019392505050565b600181811c908216806102e057607f821691505b60208210810361030057634e487b7160e01b600052602260045260246000fd5b5091905056fea264697066735822122096013f8b33bbb3794d36acf00a07a399d713334cdb3716058e145765786b5c0664736f6c634300080e0033"}}:Hash (NoMethodError)
unixneo commented 2 years ago

FWIW (More debugging)

same error with helloworld.sol

irb(main):001:0> require "#{Rails.root}/lib/assets/eth.rb"
=> true
irb(main):002:0> EthGem::Core.greeter
METAMASK_ETH_ADDRESS: 0xE179C056024150d56A4e94af9C5A36BCC0B4e502
Balance is 4.0e+18    
{"HelloWorld"=>
  {"abi"=>
    [{"inputs"=>[],
      "name"=>"helloWorld",
      "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}],
      "stateMutability"=>"pure",
      "type"=>"function"}],
   "bin"=>
    "608060405234801561001057600080fd5b5060eb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c605f76c14602d575b600080fd5b604080518082018252600d81526c48656c6c6f2c20576f726c642160981b60208201529051605a91906063565b60405180910390f35b600060208083528351808285015260005b81811015608e578581018301518582016040015282016074565b81811115609f576000604083870101525b50601f01601f191692909201604001939250505056fea2646970667358221220ad97979748f2b07a3ac87aea935c673825456b92a5f5ea06c09a2d9213b431dd64736f6c634300080e0033"}}
/Users/tim/rails/eth/vendor/bundle/ruby/3.0.0/gems/eth-0.5.5/lib/eth/client.rb:182:in `deploy': undefined method `bin' for {"HelloWorld"=>{"abi"=>[{"inputs"=>[], "name"=>"helloWorld", "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}], "stateMutability"=>"pure", "type"=>"function"}], "bin"=>"608060405234801561001057600080fd5b5060eb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c605f76c14602d575b600080fd5b604080518082018252600d81526c48656c6c6f2c20576f726c642160981b60208201529051605a91906063565b60405180910390f35b600060208083528351808285015260005b81811015608e578581018301518582016040015282016074565b81811115609f576000604083870101525b50601f01601f191692909201604001939250505056fea2646970667358221220ad97979748f2b07a3ac87aea935c673825456b92a5f5ea06c09a2d9213b431dd64736f6c634300080e0033"}}:Hash (NoMethodError)
Did you mean?  min
q9f commented 2 years ago

I'm sorry, there is some documentation missing. What you need is an Eth::Contract:

chain = Eth::Client.create CHAINSTACK_ENDPOINT_ROPSTEN
puts "METAMASK_ETH_ADDRESS: #{METAMASK_ETH_ADDRESS}"
deposit_contract = Eth::Address.new METAMASK_ETH_ADDRESS
balance = (chain.get_balance deposit_contract).to_f
puts "Balance is #{balance.round(8)}"

contract = Eth::Contract.from_file(file: "#{Rails.root}/greeter.sol")
address = chain.deploy_and_wait(contract)
q9f commented 2 years ago

Updated the wiki: https://github.com/q9f/eth.rb/wiki/Smart-Contracts

Let me know if you have any further questions!

unixneo commented 2 years ago

Continued here:

https://github.com/q9f/eth.rb/issues/115