Class: Platon::Client

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

Direct Known Subclasses

HttpClient, IpcClient

Constant Summary collapse

DEFAULT_GAS_LIMIT =
400_000
DEFAULT_GAS_PRICE =

1 GVON

1_000_000_000
RPC_COMMANDS =
%w(web3_clientVersion web3_sha3 net_version net_peerCount net_listening platon_protocolVersion platon_syncing platon_gasPrice platon_accounts platon_blockNumber platon_getBalance platon_getStorageAt platon_getTransactionCount platon_getBlockTransactionCountByHash platon_getBlockTransactionCountByNumber platon_getCode platon_sign platon_sendTransaction platon_sendRawTransaction platon_call platon_estimateGas platon_getBlockByHash platon_getBlockByNumber platon_getTransactionByHash platon_getTransactionByBlockHashAndIndex platon_getTransactionByBlockNumberAndIndex platon_getTransactionReceipt platon_getUncleByBlockHashAndIndex platon_newFilter platon_newBlockFilter platon_newPendingTransactionFilter platon_uninstallFilter platon_getFilterChanges platon_getFilterLogs platon_getLogs platon_getWork platon_submitWork platon_submitHashrate db_putString db_getString db_putHex db_getHex shh_post shh_version shh_newIdentity shh_hasIdentity shh_newGroup shh_addToGroup shh_newFilter shh_uninstallFilter shh_getFilterChanges shh_getMessages)
PLATON_RPC_COMMANDS =

PLATON_UNSUPPORT_COMMANDS = %w(platon_coinbase ,plton_mining,platon_hashrate,platon_getUncleCountByBlockHash,platon_getUncleCountByBlockNumber,platon_getUncleByBlockNumberAndIndex platon_getCompilers platon_compileLLL platon_compileSolidity platon_compileSerpent)

%w(platon_evidences admin_getProgramVersion admin_getSchnorrNIZKProve)
RPC_MANAGEMENT_COMMANDS =
%w(admin_addPeer admin_datadir admin_nodeInfo admin_peers admin_setSolc admin_startRPC admin_startWS admin_stopRPC admin_stopWS debug_backtraceAt debug_blockProfile debug_cpuProfile debug_dumpBlock debug_gcStats debug_getBlockRlp debug_goTrace debug_memStats debug_seedHash debug_setHead debug_setBlockProfileRate debug_stacks debug_startCPUProfile debug_startGoTrace debug_stopCPUProfile debug_stopGoTrace debug_traceBlock debug_traceBlockByNumber debug_traceBlockByHash debug_traceBlockFromFile debug_traceTransaction debug_verbosity debug_vmodule debug_writeBlockProfile debug_writeMemProfile miner_hashrate miner_makeDAG miner_setExtra miner_setGasPrice miner_start miner_startAutoDAG miner_stop miner_stopAutoDAG personal_importRawKey personal_listAccounts personal_lockAccount personal_newAccount personal_unlockAccount personal_sendTransaction txpool_content txpool_inspect txpool_status)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain_name = nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/platon/client.rb', line 15

def initialize(chain_name=nil)
  @id = 0
  @batch = nil
  # @formatter = Platon::Formatter.new
  @gas_price = DEFAULT_GAS_PRICE 
  @gas_limit = DEFAULT_GAS_LIMIT


  @ppos = Platon::Ppos.new self

  set_network(chain_name.downcase.to_sym)

end

Instance Attribute Details

#chain_idObject

Returns the value of attribute chain_id.



13
14
15
# File 'lib/platon/client.rb', line 13

def chain_id
  @chain_id
end

#commandObject

Returns the value of attribute command.



13
14
15
# File 'lib/platon/client.rb', line 13

def command
  @command
end

#default_accountObject

Returns the value of attribute default_account.



13
14
15
# File 'lib/platon/client.rb', line 13

def 
  @default_account
end

#gas_limitObject

Returns the value of attribute gas_limit.



13
14
15
# File 'lib/platon/client.rb', line 13

def gas_limit
  @gas_limit
end

#gas_priceObject

Returns the value of attribute gas_price.



13
14
15
# File 'lib/platon/client.rb', line 13

def gas_price
  @gas_price
end

#hrpObject

Returns the value of attribute hrp.



13
14
15
# File 'lib/platon/client.rb', line 13

def hrp
  @hrp
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/platon/client.rb', line 13

def id
  @id
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/platon/client.rb', line 13

def logger
  @logger
end

#pposObject

Returns the value of attribute ppos.



13
14
15
# File 'lib/platon/client.rb', line 13

def ppos
  @ppos
end

Class Method Details

.create(host_or_ipcpath) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
# File 'lib/platon/client.rb', line 29

def self.create(host_or_ipcpath)
  return IpcClient.new(host_or_ipcpath) if host_or_ipcpath.end_with? '.ipc'
  return HttpClient.new(host_or_ipcpath) if host_or_ipcpath.start_with? 'http'
  raise ArgumentError.new('Unable to detect client type')
end

Instance Method Details

#batchObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/platon/client.rb', line 57

def batch
  @batch = []

  yield
  result = send_batch(@batch)

  @batch = nil
  reset_id

  return result
end

#encode_params(params) ⇒ Object



86
87
88
# File 'lib/platon/client.rb', line 86

def encode_params(params)
  params.map(&method(:int_to_hex))
end

#get_idObject



69
70
71
72
# File 'lib/platon/client.rb', line 69

def get_id
  @id += 1
  return @id
end

#get_nonce(address) ⇒ Object



90
91
92
# File 'lib/platon/client.rb', line 90

def get_nonce(address)
  platon_get_transaction_count(address, "pending")
end

#int_to_hex(p) ⇒ Object



82
83
84
# File 'lib/platon/client.rb', line 82

def int_to_hex(p)
  p.is_a?(Integer) ? "0x#{p.to_s(16)}" : p 
end

#reset_idObject



74
75
76
# File 'lib/platon/client.rb', line 74

def reset_id
  @id = 0
end

#send_command(command, args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/platon/client.rb', line 131

def send_command(command,args)

  # if ["platon_call"].include?(command)
  #   args << "latest"
  # end
  
  payload = {jsonrpc: "2.0", method: command, params: encode_params(args), id: get_id}
  puts payload
  # @logger.info("Sending #{payload.to_json}") if @log
  if @batch
    @batch << payload
    return true
  else
    p output = JSON.parse(send_single(payload.to_json))
    # @logger.info("Received #{output.to_json}") if @log
    reset_id
    raise IOError, output["error"]["message"] if output["error"]
    
    if %W(net_peerCount platon_protocolVersion platon_gasPrice platon_blockNumber platon_getBalance platon_getTransactionCount platon_getBlockTransactionCountByHash platon_getBlockTransactionCountByNumber platon_estimateGas).include?(command)
      return output["result"].to_i(16)
    end

    return output["result"]
  end
end

#set_network(network) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/platon/client.rb', line 35

def set_network(network) 
  config = {
    platondev: {hrp: "lat", chain_id: 210309},
    alaya:{hrp:"atp",chain_id:201018} ,
    alayadev:{hrp:"atp",chain_id: 201030}
  }
  if config[network]
    @hrp = config[network][:hrp]  
    @chain_id = config[network][:chain_id]

    puts "Using Network: #{network}: hrp->#{hrp} , chain_id->#{chain_id}"
  else
    puts "Warning: Network:#{network} not found. You can use 'update_setting' to set hrp & chain_id"
  end

end

#transfer(key, bech32_address, amount, other = nil) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/platon/client.rb', line 103

def transfer(key, bech32_address, amount,other=nil)
  raise ArgumentError.new("#{bech32_address} not match current network hrp :#{hrp}") unless Bech32.decode(bech32_address)[0] == hrp
  args = { 
    from: key.address,
    to: Utils.decode_bech32_address(bech32_address),
    value: amount.to_i,
    data: "",
    nonce: get_nonce(key.bech32_address(hrp:hrp)),
    gas_limit: (other && other[:gas_limit])|| gas_limit,
    gas_price: (other && other[:gas_price]) || gas_price,
    chain_id: chain_id
  }

  tx = Platon::Tx.new(args)
  tx.sign key
  platon_send_raw_transaction(tx.hex)
end

#transfer_and_wait(key, address, amount, other = nil) ⇒ Object



121
122
123
# File 'lib/platon/client.rb', line 121

def transfer_and_wait(key, address, amount,other=nil)
  return wait_for(transfer(key, address, amount,other))
end

#transfer_to(address, amount) ⇒ Object



94
95
96
# File 'lib/platon/client.rb', line 94

def transfer_to(address, amount)
  platon_send_transaction({to: address, value: int_to_hex(amount)})
end

#transfer_to_and_wait(address, amount) ⇒ Object



98
99
100
# File 'lib/platon/client.rb', line 98

def transfer_to_and_wait(address, amount)
  wait_for(transfer_to(address, amount))
end

#update_setting(params) ⇒ Object



52
53
54
55
# File 'lib/platon/client.rb', line 52

def update_setting(params)
  @hrp = params[:hrp]
  @chain_id = params[:chain_id]
end

#wait_for(tx) ⇒ Object



125
126
127
128
129
# File 'lib/platon/client.rb', line 125

def wait_for(tx)
  transaction = Platon::Transaction.new(tx, self, "", [])
  transaction.wait_for_miner
  return transaction
end