Class: AppChain::Contract

Inherits:
Object
  • Object
show all
Includes:
Web3::Eth::Abi::AbiCoder
Defined in:
lib/appchain/contract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abi, url, address = nil, rpc = nil) ⇒ void

Parameters:

  • abi (String | Hash)

    json string or hash

  • url (String)

    chain url

  • address (String) (defaults to: nil)

    contract address

  • rpc (AppChain::RPC) (defaults to: nil)


17
18
19
20
21
22
23
# File 'lib/appchain/contract.rb', line 17

def initialize(abi, url, address = nil, rpc = nil)
  @url = url
  @abi = abi
  @address = address
  @rpc = rpc
  parse_url
end

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



9
10
11
# File 'lib/appchain/contract.rb', line 9

def abi
  @abi
end

#addressObject (readonly)

Returns the value of attribute address.



9
10
11
# File 'lib/appchain/contract.rb', line 9

def address
  @address
end

#rpcObject (readonly)

Returns the value of attribute rpc.



9
10
11
# File 'lib/appchain/contract.rb', line 9

def rpc
  @rpc
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/appchain/contract.rb', line 9

def url
  @url
end

Instance Method Details

#call_func(method:, params: [], tx: {}) ⇒ any

call contract functions by rpc ‘call` method

Parameters:

  • method (Symbol | String)

    the method name you call

  • params (Array) (defaults to: [])

    the method params you call

  • tx (Hash) (defaults to: {})

    see rpc ‘call` doc for more info

Returns:

  • (any)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/appchain/contract.rb', line 43

def call_func(method:, params: [], tx: {}) # rubocop:disable Naming/UncommunicativeMethodParamName
  data, output_types = function_data_with_ot(method, *params)
  resp = @rpc.call_rpc(:call, params: [tx.merge(data: data, to: address), "latest"])
  result = resp["result"]

  data = [Utils.remove_hex_prefix(result)].pack("H*")
  return if data.nil?

  re = decode_abi output_types, data
  re.length == 1 ? re.first : re
end

#function_data(method_name, *params) ⇒ String

wrapper Web3::Eth abi encoder for encoded data

Parameters:

  • method_name (Symbol | String)

    method name you call

  • *params (Array)

    method params you call

Returns:

  • (String)

    hex data



31
32
33
34
# File 'lib/appchain/contract.rb', line 31

def function_data(method_name, *params)
  data, _output_types = function_data_with_ot(method_name, *params)
  data
end

#send_func(tx:, private_key:, method:, params: []) ⇒ nil | Hash

call contract functions by sendRawTransaction

Parameters:

  • tx (Hash | AppChain::Transaction)
  • private_key (String)

    hex string

  • method (Symbol | String)

    method name you call

  • *params (Array)

    your params

Returns:

  • (nil | Hash)

    “”, status: “”, sendRawTransactionResult



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/appchain/contract.rb', line 63

def send_func(tx:, private_key:, method:, params: []) # rubocop:disable Naming/UncommunicativeMethodParamName
  data, _output_types = function_data_with_ot(method, *params)
  transaction = if tx.is_a?(Hash)
                  Transaction.from_hash(tx)
                else
                  tx
                end
  transaction.data = data
  resp = @rpc.send_transaction(transaction, private_key)

  resp&.dig("result")
end