Class: CITA::RPC

Inherits:
Object
  • Object
show all
Defined in:
lib/cita/rpc.rb

Constant Summary collapse

METHOD_NAMES =

CITA v0.23 RPC list

%w(
  peerCount
  blockNumber
  sendRawTransaction
  getBlockByHash
  getBlockByNumber
  getTransaction
  getTransactionReceipt
  getLogs
  call
  getTransactionCount
  getCode
  getAbi
  getBalance
  newFilter
  newBlockFilter
  uninstallFilter
  getFilterChanges
  getFilterLogs
  getTransactionProof
  getMetaData
  getBlockHeader
  getStateProof
  getVersion
  peersInfo
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RPC

Returns a new instance of RPC.



36
37
38
39
# File 'lib/cita/rpc.rb', line 36

def initialize(url)
  @url = url
  @http = Http.new(@url)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



6
7
8
# File 'lib/cita/rpc.rb', line 6

def http
  @http
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/cita/rpc.rb', line 6

def url
  @url
end

Instance Method Details

#call_rpc(method, jsonrpc: Http::DEFAULT_JSONRPC, params: Http::DEFAULT_PARAMS, id: Http::DEFAULT_ID) ⇒ Hash

Returns response body.

Returns:

  • (Hash)

    response body



53
54
55
56
# File 'lib/cita/rpc.rb', line 53

def call_rpc(method, jsonrpc: Http::DEFAULT_JSONRPC, params: Http::DEFAULT_PARAMS, id: Http::DEFAULT_ID)
  resp = http.call_rpc(method, params: params, jsonrpc: jsonrpc, id: id)
  JSON.parse(resp.body)
end

#send_transaction(transaction, private_key) ⇒ Hash

Parameters:

Returns:

  • (Hash)


60
61
62
63
# File 'lib/cita/rpc.rb', line 60

def send_transaction(transaction, private_key)
  content = TransactionSigner.encode(transaction, private_key)
  send_raw_transaction(content)
end

#transfer(to:, private_key:, value:, quota: 30_000) ⇒ Hash

easy to transfer tokens

Parameters:

  • to (String)

    to address

  • private_key (String)
  • value (String | Integer)

    hex string or decimal integer

  • quota (Integer) (defaults to: 30_000)

    default to 30_000

Returns:

  • (Hash)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cita/rpc.rb', line 73

def transfer(to:, private_key:, value:, quota: 30_000)
  valid_until_block = block_number["result"].hex + 88
   = ("latest")["result"]
  version = ["version"]

  chain_id = case version
             when 0
               ["chainId"]
             when 1, 2
               ["chainIdV1"]
             end

  transaction = Transaction.new(nonce: Utils.nonce, valid_until_block: valid_until_block, chain_id: chain_id, to: to, value: value, quota: quota, version: version)
  send_transaction(transaction, private_key)
end