class CITA::Contract
Attributes
Public Class Methods
@param abi [String | Hash] json string or hash @param url [String] chain url @param address [String] contract address @param rpc [CITA::RPC]
@return [void]
# File lib/cita/contract.rb, line 17 def initialize(abi, url, address = nil, rpc = nil) @url = url @abi = abi @address = address @rpc = rpc parse_url end
Public Instance Methods
call contract functions by rpc `call` method
@param method [Symbol | String] the method name you call @param params [Array] the method params you call @param tx [Hash] see rpc `call` doc for more info
@return [any]
# File lib/cita/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
wrapper Web3::Eth
abi encoder for encoded data
@param method_name [Symbol | String] method name you call @param *params [Array] method params you call
@return [String] hex data
# File lib/cita/contract.rb, line 31 def function_data(method_name, *params) data, _output_types = function_data_with_ot(method_name, *params) data end
call contract functions by sendRawTransaction
@param tx [Hash | CITA::Transaction] @param private_key [String] hex string @param method [Symbol | String] method name you call @param *params [Array] your params
@return [nil | Hash] {hash: “”, status: “”}, sendRawTransactionResult
# File lib/cita/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
Private Instance Methods
wrapper Web3::Eth
abi encoder for encoded data
# File lib/cita/contract.rb, line 92 def function_data_with_ot(method_name, *params) web3 = Web3::Eth::Rpc.new host: @host, port: @port, connect_options: { use_ssl: https? } contract = web3.eth.contract(abi).at(address) contract.function_data(method_name, *params) end
is this url in https?
# File lib/cita/contract.rb, line 87 def https? @scheme == "https" end
parse url to host, port and scheme
# File lib/cita/contract.rb, line 79 def parse_url uri = URI.parse(@url) @host = uri.host @port = uri.port @scheme = uri.scheme end