Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Opcode when calling getName #287

Open
jabramov1 opened this issue Jul 29, 2024 · 1 comment
Open

Invalid Opcode when calling getName #287

jabramov1 opened this issue Jul 29, 2024 · 1 comment
Labels
question Further information is requested

Comments

@jabramov1
Copy link

I'm encountering an "invalid opcode" error with my Ethereum smart contract. While deployment and getNumber calls work, setName results in an error. Here are the details:

Solidity Contract:

pragma solidity ^0.8.19;

contract Construct {
    uint256 private number;
    string private name;

    constructor(uint256 _number) {
        number = _number;
    }

    function getNumber() public view returns (uint256) {
        return number;
    }

    function setNumber(uint256 _number) public {
        number = _number;
    }

    function getName() public view returns (string memory) {
        return name;
    }

    function setName(string memory _name) public {
        name = _name;
    }
}

Ruby Script:

require 'eth'
require 'forwardable'

client = Eth::Client.create('http://127.0.0.1:8545')
contract_address = "0x459E0a953e6fAe85b248017b7c4484500b08F57C"
contract = Eth::Contract.from_file(file: '../contracts/construct.sol')
tester_contract = Eth::Contract.from_abi(name: "Construct", address: contract_address, abi: contract.abi)

deployer_account = Eth::Key.new(priv: '0xc1f3f4d9fd9a7fe90b966825a780697a400c675e56f9153ed21bc045c0e7207e')
puts "Deployer account balance: #{client.eth_get_balance(deployer_account.address)['result'].to_i(16) / 1e18} ETH"

client.max_fee_per_gas = 41_000_000_000
client.max_priority_fee_per_gas = 4_000_000_000

# Call the getNumber function
begin
  response = client.call(tester_contract, "getNumber")
  puts "Current number: #{response}"
rescue StandardError => e
  puts "Error while getting number: #{e.message}"
  puts e.backtrace
end

# Call the setNumber function
begin
  response = client.transact_and_wait(tester_contract, "setNumber", 12345, sender_key: deployer_account, gas_limit: 10_000_000)
  puts "Transaction hash for setNumber: #{response}"
rescue StandardError => e
  puts "Error while calling setNumber: #{e.message}"
  puts e.backtrace
end

# Call the setName function
begin
  response = client.transact_and_wait(tester_contract, "setName", "huy", sender_key: deployer_account, gas_limit: 10_000_000)
  puts "Transaction hash for setName: #{response}"
rescue StandardError => e
  puts "Error while calling setName: #{e.message}"
  puts e.backtrace
end

# Call the getName function
begin
  response = client.call(tester_contract, "getName")
  puts "Current name: #{response}"
rescue StandardError => e
  puts "Error while getting name: #{e.message}"
  puts e.backtrace
end

Additional Context:

  • The number works fine, indicating the contract is deployed correctly.
  • The "invalid opcode" error suggests the issue might be with the setName or getName functions or how strings are being handled.

Any help or guidance would be greatly appreciated!

@jabramov1 jabramov1 reopened this Jul 29, 2024
@q9f
Copy link
Owner

q9f commented Aug 28, 2024

Hey, could you paste the actual error output?

"Invalid Opcode" is usually nothing that our libary handles, this comes from the node you are connecting to:

client = Eth::Client.create('http://127.0.0.1:8545')

What node and version and what chain config do you use?

@q9f q9f added the question Further information is requested label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants
@q9f @jabramov1 and others