Skip to content

Function name is not constant: name, requires to sign transaction #2

@arenzel

Description

@arenzel

What I do

Calling a simple "name" function on my smartcontract, which is a pure function that returns a string.
The abi of this function:

# config/web3.yml
contract_abi = "[{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]"

More readable version:

[{"inputs"=>[],
  "name"=>"name",
  "outputs"=>[{"internalType"=>"string", "name"=>"", "type"=>"string"}],
  "stateMutability"=>"view",
  "type"=>"function"}]

The error

When calling the function, I got the following error:

RuntimeError:
       Function name is not constant: name, requires to sign transaction

I'm surprised that the lib asks for a signature, whereas - AFAIK - pure functions are not supposed to require signature, right? 🤔


My code

class ContractService
  def initialize
    contract_address = "0x..."
    provider = Web3::Eth::Rpc.new(
      host: infura_url,
      port: 443,
      connect_options: {
        open_timeout: 200,
        read_timeout: 1400,
        use_ssl: true,
        rpc_path: "/v3/#{Rails.configuration.web3['infura_project_id']}"
      }
    )
    contract_abi = Rails.configuration.web3["contract_abi"]
    contract_factory = provider.eth.contract(contract_abi)

    @contract_instance = contract_factory.at(contract_address)
  end

  def name
    result = @contract_instance.name
    puts result
  end
end
# Function call
ContractService.new.name

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions