Class: Klaytn::Contract
Constant Summary collapse
- BASE_URL =
'https://wallet-api.klaytnapi.com/v2/tx/contract'.freeze
Constants inherited from Base
Base::FUNCTION_NOT_FOUND, Base::INVALID_CLIENT, Base::MISSING_ABI, Base::MISSING_ACCOUNT_POOL_KRN, Base::MISSING_ACCOUNT_WALLET, Base::MISSING_CONTRACT, Base::MISSING_JSONRPC_METHOD, Base::MISSING_KAS_CREDS
Instance Attribute Summary collapse
-
#abi ⇒ Object
readonly
Returns the value of attribute abi.
-
#encoder ⇒ Object
readonly
Returns the value of attribute encoder.
-
#kas_account_pool_krn ⇒ Object
readonly
Returns the value of attribute kas_account_pool_krn.
-
#kas_account_wallet_address ⇒ Object
readonly
Returns the value of attribute kas_account_wallet_address.
Attributes inherited from Client
#basic_auth, #chain_id, #contract_address, #headers
Instance Method Summary collapse
- #deploy(bytecode, submit: true, gas: 900000) ⇒ Object
- #function_body_builder(definition, inputs, params, gas) ⇒ Object
-
#initialize(opts) ⇒ Contract
constructor
A new instance of Contract.
-
#invoke_function(definition, params, gas: 0) ⇒ Object
definition ex: ‘addAddressToWhitelist’ - string literal Solidity function definition, without params/parens inputs ex: [OpenStruct.new({ “internalType”: “address”, “name”: “addr”, “type”: “address” })] - array of dot-notation object from ABI params ex: [‘0x0xxxx’] - array of arguments accepted by function.
- #raw_transaction(input_data, gas: 0) ⇒ Object
Methods inherited from Client
#setup_basic_auth, #setup_chain_id
Constructor Details
#initialize(opts) ⇒ Contract
Returns a new instance of Contract.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/klaytn/contract.rb', line 7 def initialize(opts) raise MISSING_ACCOUNT_WALLET if opts[:kas_account_wallet_address].blank? raise MISSING_ACCOUNT_POOL_KRN if opts[:kas_account_pool_krn].blank? @kas_account_wallet_address = opts[:kas_account_wallet_address] @kas_account_pool_krn = opts[:kas_account_pool_krn] @abi = opts[:abi] @encoder = Encoder.new super end |
Instance Attribute Details
#abi ⇒ Object (readonly)
Returns the value of attribute abi.
5 6 7 |
# File 'lib/klaytn/contract.rb', line 5 def abi @abi end |
#encoder ⇒ Object (readonly)
Returns the value of attribute encoder.
5 6 7 |
# File 'lib/klaytn/contract.rb', line 5 def encoder @encoder end |
#kas_account_pool_krn ⇒ Object (readonly)
Returns the value of attribute kas_account_pool_krn.
5 6 7 |
# File 'lib/klaytn/contract.rb', line 5 def kas_account_pool_krn @kas_account_pool_krn end |
#kas_account_wallet_address ⇒ Object (readonly)
Returns the value of attribute kas_account_wallet_address.
5 6 7 |
# File 'lib/klaytn/contract.rb', line 5 def kas_account_wallet_address @kas_account_wallet_address end |
Instance Method Details
#deploy(bytecode, submit: true, gas: 900000) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/klaytn/contract.rb', line 20 def deploy(bytecode, submit: true, gas: 900000) body = { from: kas_account_wallet_address, # must be created inside KAS console > Account Pool, and MUST be capital version from Klaytn Scope! input: bytecode, gas: gas, submit: submit } resp = HTTParty.post(BASE_URL + '/deploy', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth) JSON.parse(resp.body) end |
#function_body_builder(definition, inputs, params, gas) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/klaytn/contract.rb', line 64 def function_body_builder(definition, inputs, params, gas) { from: kas_account_wallet_address, to: contract_address, input: encoder.encode_function(definition, inputs, params), gas: gas, submit: true } end |
#invoke_function(definition, params, gas: 0) ⇒ Object
definition ex: ‘addAddressToWhitelist’ - string literal Solidity function definition, without params/parens inputs ex: [OpenStruct.new({ “internalType”: “address”, “name”: “addr”, “type”: “address” })] - array of dot-notation object from ABI params ex: [‘0x0xxxx’] - array of arguments accepted by function
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/klaytn/contract.rb', line 36 def invoke_function(definition, params, gas: 0) raise MISSING_CONTRACT if contract_address.blank? raise MISSING_ABI if abi.blank? found_function = abi.filter {|input| input[:name] == definition }.first raise FUNCTION_NOT_FOUND.gsub('XXX', definition) if found_function.nil? built_defintion = "#{definition}(#{found_function[:inputs].map {|i| i[:type]}.join(',')})" built_inputs = found_function[:inputs].map { |i| OpenStruct.new(i) } body = function_body_builder(built_defintion, built_inputs, params, gas) resp = HTTParty.post(BASE_URL + '/execute', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth) JSON.parse(resp.body) end |
#raw_transaction(input_data, gas: 0) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/klaytn/contract.rb', line 51 def raw_transaction(input_data, gas: 0) raise MISSING_CONTRACT if contract_address.blank? body = { from: kas_account_wallet_address, to: contract_address, input: input_data, gas: gas, submit: true } resp = HTTParty.post(BASE_URL + '/execute', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth) JSON.parse(resp.body) end |