Method: Eth::Client#transact

Defined in:
lib/eth/client.rb

#transact(contract, function) ⇒ Object #transact(contract, function, *args) ⇒ Object #transact(contract, function, *args, **kwargs) ⇒ Object

Executes a contract function with a transaction (transactional contract read/write).

Note, that many remote providers (e.g., Infura) do not provide any accounts. Provide a sender_key: if you experience issues.

Overloads:

  • #transact(contract, function) ⇒ Object

    Parameters:

    • contract (Eth::Contract)

      the subject contract to write to.

    • function (String)

      method name to be executed.

  • #transact(contract, function, *args) ⇒ Object

    Parameters:

    • contract (Eth::Contract)

      the subject contract to write to.

    • function (String)

      method name to be executed.

    • *args

      optional function arguments.

  • #transact(contract, function, *args, **kwargs) ⇒ Object

    Parameters:

    • contract (Eth::Contract)

      the subject contract to write to.

    • function_name (String)

      method name to be executed.

    • *args

      optional function arguments.

    • **sender_key (Eth::Key)

      the sender private key.

    • **legacy (Boolean)

      enables legacy transactions (pre-EIP-1559).

    • **address (Eth::Address)

      contract address.

    • **gas_limit (Integer)

      optional gas limit override for transacting with the contract.

    • **nonce (Integer)

      optional specific nonce for transaction.

    • **tx_value (Integer)

      optional transaction value field filling.

Returns:

  • (Object)

    returns the result of the transaction.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/eth/client.rb', line 319

def transact(contract, function, *args, **kwargs)
  gas_limit = if kwargs[:gas_limit]
      kwargs[:gas_limit]
    else
      Tx.estimate_intrinsic_gas(contract.bin)
    end
  params = {
    value: kwargs[:tx_value] || 0,
    gas_limit: gas_limit,
    chain_id: chain_id,
    to: kwargs[:address] || contract.address,
    data: contract.function(function, args: args.size).encode_call(*args),
  }
  send_transaction(params, kwargs[:legacy], kwargs[:sender_key], kwargs[:nonce])
end