Class: Klaytn::Transaction

Inherits:
Client show all
Defined in:
lib/klaytn/transaction.rb

Constant Summary collapse

BASE_URL =
'https://wallet-api.klaytnapi.com/v2/tx'.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

Attributes inherited from Client

#basic_auth, #chain_id, #contract_address, #headers

Instance Method Summary collapse

Methods inherited from Client

#setup_basic_auth, #setup_chain_id

Constructor Details

#initialize(opts) ⇒ Transaction

Returns a new instance of Transaction.



7
8
9
10
11
12
# File 'lib/klaytn/transaction.rb', line 7

def initialize(opts)
  @kas_account_wallet_address = opts[:kas_account_wallet_address] # created via KAS > Service > Wallet > Account Pool > Create Account Pool > Create Account
  @kas_account_pool_krn = opts[:kas_account_pool_krn]
  @encoder = Encoder.new
  super
end

Instance Attribute Details

#encoderObject (readonly)

Returns the value of attribute encoder.



5
6
7
# File 'lib/klaytn/transaction.rb', line 5

def encoder
  @encoder
end

#kas_account_pool_krnObject (readonly)

Returns the value of attribute kas_account_pool_krn.



5
6
7
# File 'lib/klaytn/transaction.rb', line 5

def 
  @kas_account_pool_krn
end

#kas_account_wallet_addressObject (readonly)

Returns the value of attribute kas_account_wallet_address.



5
6
7
# File 'lib/klaytn/transaction.rb', line 5

def 
  @kas_account_wallet_address
end

Instance Method Details

#get(hash) ⇒ Object



14
15
16
17
# File 'lib/klaytn/transaction.rb', line 14

def get(hash)
  resp = HTTParty.get(BASE_URL + "/#{hash}", headers: headers, basic_auth: basic_auth)
  JSON.parse(resp.body)
end

#send(address, amount_in_peb, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/klaytn/transaction.rb', line 19

def send(address, amount_in_peb, opts = {})
  raise MISSING_ACCOUNT_WALLET if .blank?
  raise MISSING_ACCOUNT_POOL_KRN if .blank?

  body = {
    from: ,
    to: address,
    value: encoder.encode_integer(amount_in_peb), # '0x0' = 0 KLAY, '0x4563918244f40000' = 5 KLAY
    memo: opts[:memo], # can be a text note; viewable on Klaytn Scope!
    gas: opts[:gas] || 200000, # not required, default is 1,000,000 if excluded
    submit: should_submit?(opts) # create real transaction by default
  }

  resp = HTTParty.post(BASE_URL + '/value', body: body.to_json, headers: headers.merge('x-krn' => ), basic_auth: basic_auth)
  JSON.parse(resp.body)
end

#should_submit?(opts) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/klaytn/transaction.rb', line 36

def should_submit?(opts)
  opts[:submit] == false ? false : true
end