Class: Klay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/klay/client.rb

Overview

Provides the Client super-class to connect to Klaytn network's RPC-API endpoints (IPC or HTTP).

Direct Known Subclasses

Http, Ipc

Defined Under Namespace

Classes: Http, Ipc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_) ⇒ Client

Constructor for the Klay::Client super-class. Should not be used; use create intead.



55
56
57
58
59
60
# File 'lib/klay/client.rb', line 55

def initialize(_)
  @id = 0
  @max_priority_fee_per_gas = 0
  @max_fee_per_gas = Tx::DEFAULT_GAS_PRICE
  @gas_limit = Tx::DEFAULT_GAS_LIMIT
end

Instance Attribute Details

#chain_idInteger (readonly)

Gets the chain ID of the connected network.

Returns:

  • (Integer)

    the chain ID.



26
27
28
# File 'lib/klay/client.rb', line 26

def chain_id
  @chain_id
end

#default_accountKlay::Address

Gets the default account (coinbase) of the connected client.

Returns:



29
30
31
# File 'lib/klay/client.rb', line 29

def 
  @default_account
end

#gas_limitObject

The default gas limit for the transaction.



38
39
40
# File 'lib/klay/client.rb', line 38

def gas_limit
  @gas_limit
end

#idObject (readonly)

The client's RPC-request ID starting at 0.



23
24
25
# File 'lib/klay/client.rb', line 23

def id
  @id
end

#max_fee_per_gasObject

The default transaction max fee per gas in Wei.



35
36
37
# File 'lib/klay/client.rb', line 35

def max_fee_per_gas
  @max_fee_per_gas
end

#max_priority_fee_per_gasObject

The default transaction max priority fee per gas in Wei.



32
33
34
# File 'lib/klay/client.rb', line 32

def max_priority_fee_per_gas
  @max_priority_fee_per_gas
end

Class Method Details

.create(host) ⇒ Klay::Client::Ipc, Klay::Client::Http

Creates a new RPC-Client, either by providing an HTTP/S host or an IPC path.

Parameters:

  • host (String)

    either an HTTP/S host or an IPC path.

Returns:

Raises:

  • (ArgumentError)

    in case it cannot determine the client type.



47
48
49
50
51
# File 'lib/klay/client.rb', line 47

def self.create(host)
  return Client::Ipc.new host if host.end_with? ".ipc"
  return Client::Http.new host if host.start_with? "http"
  raise ArgumentError, "Unable to detect client type!"
end

Instance Method Details

#get_balance(address) ⇒ Integer

Gets the balance for an address.

Parameters:

  • address (Klay::Address)

    the address to get the balance for.

Returns:

  • (Integer)

    the balance in Wei.



80
81
82
# File 'lib/klay/client.rb', line 80

def get_balance(address)
  eth_get_balance(address)["result"].to_i 16
end

#get_nonce(address) ⇒ Integer

Gets the next nonce for an address used to draft new transactions.

Parameters:

Returns:

  • (Integer)

    the next nonce to be used.



88
89
90
# File 'lib/klay/client.rb', line 88

def get_nonce(address)
  eth_get_transaction_count(address, "pending")["result"].to_i 16
end

#is_mined_tx?(hash) ⇒ Boolean

Checkes wether a transaction is mined or not.

Parameters:

  • hash (String)

    the transaction hash.

Returns:

  • (Boolean)

    true if included in a block.



164
165
166
167
# File 'lib/klay/client.rb', line 164

def is_mined_tx?(hash)
  mined_tx = eth_get_transaction_by_hash hash
  !mined_tx.nil? && !mined_tx["result"].nil? && !mined_tx["result"]["blockNumber"].nil?
end

#reset_idInteger

Gives control over resetting the RPC request ID back to zero. Usually not needed.

Returns:

  • (Integer)

    0



156
157
158
# File 'lib/klay/client.rb', line 156

def reset_id
  @id = 0
end

#transfer(destination, amount, sender_key = nil, legacy = false) ⇒ String

Simply transfer Klay to an account without any call data or access lists attached. Uses eth_coinbase and external signer if no sender key is provided.

Parameters:

  • destination (Klay::Address)

    the destination address.

  • amount (Integer)

    the transfer amount in Wei.

  • sender_key (Klay::Key) (defaults to: nil)

    the sender private key.

  • legacy (Boolean) (defaults to: false)

    enables legacy transactions (pre-EIP-1559).

Returns:

  • (String)

    the transaction hash.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/klay/client.rb', line 114

def transfer(destination, amount, sender_key = nil, legacy = false)
  params = {
    value: amount,
    to: destination,
    gas_limit: gas_limit,
    chain_id: chain_id,
  }
  if legacy
    params.merge!({
      gas_price: max_fee_per_gas,
    })
  else
    params.merge!({
      priority_fee: max_priority_fee_per_gas,
      max_gas_fee: max_fee_per_gas,
    })
  end
  unless sender_key.nil?

    # use the provided key as sender and signer
    params.merge!({
      from: sender_key.address,
      nonce: get_nonce(sender_key.address),
    })
    tx = Klay::Tx.new(params)
    tx.sign sender_key
    return eth_send_raw_transaction(tx.hex)["result"]
  else

    # use the default account as sender and external signer
    params.merge!({
      from: ,
      nonce: get_nonce(),
    })
    return eth_send_transaction(params)["result"]
  end
end

#transfer_and_wait(destination, amount, sender_key = nil, legacy = false) ⇒ String

Simply transfer Klay to an account and waits for it to be mined. Uses eth_coinbase and external signer if no sender key is provided.

Parameters:

  • destination (Klay::Address)

    the destination address.

  • amount (Integer)

    the transfer amount in Wei.

  • sender_key (Klay::Key) (defaults to: nil)

    the sender private key.

  • legacy (Boolean) (defaults to: false)

    enables legacy transactions (pre-EIP-1559).

Returns:

  • (String)

    the transaction hash.



101
102
103
# File 'lib/klay/client.rb', line 101

def transfer_and_wait(destination, amount, sender_key = nil, legacy = false)
  wait_for_tx(transfer(destination, amount, sender_key, legacy))
end

#wait_for_tx(hash) ⇒ String

Waits for an transaction to be mined by the connected chain.

Parameters:

  • hash (String)

    the transaction hash.

Returns:

  • (String)

    the transactin hash once the transaction is mined.

Raises:

  • (Timeout::Error)

    if it's not mined within 5 minutes.



174
175
176
177
178
179
180
181
182
183
# File 'lib/klay/client.rb', line 174

def wait_for_tx(hash)
  start_time = Time.now
  timeout = 300
  retry_rate = 0.1
  loop do
    raise Timeout::Error if ((Time.now - start_time) > timeout)
    return hash if is_mined_tx? hash
    sleep retry_rate
  end
end