Class: AppChain::Contract
- Inherits:
-
Object
- Object
- AppChain::Contract
- Includes:
- Web3::Eth::Abi::AbiCoder
- Defined in:
- lib/appchain/contract.rb
Instance Attribute Summary collapse
-
#abi ⇒ Object
readonly
Returns the value of attribute abi.
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#rpc ⇒ Object
readonly
Returns the value of attribute rpc.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#call_func(method:, params: [], tx: {}) ⇒ any
call contract functions by rpc ‘call` method.
-
#function_data(method_name, *params) ⇒ String
wrapper Web3::Eth abi encoder for encoded data.
- #initialize(abi, url, address = nil, rpc = nil) ⇒ void constructor
-
#send_func(tx:, private_key:, method:, params: []) ⇒ nil | Hash
call contract functions by sendRawTransaction.
Constructor Details
#initialize(abi, url, address = nil, rpc = nil) ⇒ void
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
#abi ⇒ Object (readonly)
Returns the value of attribute abi.
9 10 11 |
# File 'lib/appchain/contract.rb', line 9 def abi @abi end |
#address ⇒ Object (readonly)
Returns the value of attribute address.
9 10 11 |
# File 'lib/appchain/contract.rb', line 9 def address @address end |
#rpc ⇒ Object (readonly)
Returns the value of attribute rpc.
9 10 11 |
# File 'lib/appchain/contract.rb', line 9 def rpc @rpc end |
#url ⇒ Object (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
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
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
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 |