LoginSignup
3
1

More than 5 years have passed since last update.

JSON-RPC使って、gethを動かしてみた

Last updated at Posted at 2018-09-28

公式サイトをみながらgethを動かして行こうと思います。
https://github.com/ethereum/wiki/wiki/JSON-RPC

必須

こちらを参考にgethのインストールやmyGenesis.jsonファイルを準備してください。
Ethereum [Solidity 開発環境準備]
Ethereum [プライベートネットワークに接続する]
Ethereum [etherの採掘]

Json RPC

こちらを実行してください。

$ geth --rpc --networkid 15 --nodiscover --datadir "ethereum-rpc" console
// Result
<略>
INFO [09-28|16:49:25.542] HTTP endpoint opened         url=http://127.0.0.1:8545       cors= vhosts=localhost
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.14-stable/darwin-amd64/go1.10.3
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

となっていればおkです。

--rpcの説明

gethのオプションで--rpcをつければrpc接続できます。

$ geth --rpc --rpcaddr "0.0.0.0" --rpcport "8545" --rpcapi "web3,eth,net,personal" 

ブラウザからRPCにアクセスする場合は、適切なドメインを設定してCORSを有効にする必要があります。
また、--rpcapi "web3,eth,net" をつけることによって、利用する機能を個別に指定するオプションを追加することができます。

  • --rpc: HTTP-RPCサーバを有効にする。
  • --rpcaddr "0.0.0.0": エンドポイントのホストアドレスの指定。無指定の場合、"localhost"。
  • --rpcport "8545": エンドポイントのポート番号の指定。無指定の場合、8545。
  • --rpcapi "web3,eth,net,personal": APIで受け付けるHTTP-RPCインターフェースの種類の指定。インターフェース名をコンマ区切りのリストで記述する。無指定の場合、"eth,net,web3"。
  • –rpccorsdomain:クロスドメインを許可する設定。

JSON-RPC Errors

Code Message Meaning
-32700 Parse error Invalid JSON was received by the server.An error occurred on the server while parsing the JSON text.
-32600 Invalid Request The JSON sent is not a valid Request object.
-32601 Method not found The method does not exist / is not available.
-32602 Invalid params Invalid method parameter(s).
-32603 Internal error Internal JSON-RPC error.
-32000 to -32099 Server error Reserved for implementation-defined server-errors.

Versionの取得

web3_clientVersionで現在のクライアントのバージョンを返してもらいます。
新しいタブを開いて以下を実行してください。

$ curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://localhost:8545
// Result
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.8.14-stable/darwin-amd64/go1.10.3"}

このように返ってきたと思います。

直前のブロックの数を返す

eth_blockNumberを使って直前のブロック数を表示します。

$ curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://localhost:8545
// Result
{"jsonrpc":"2.0","id":83,"result":"0x0"}

採掘をしていないので、0が16進数で返されます。
16進数を10進数にする場合は、このようにします。

$ echo $((0x0))
// Result
0

このようにして、JSON-RPCを使っていきます。
もっといろんなメソッドを使いたい方は、公式サイトを参考にしてください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1