Ethereum(5) - 送金(解決編)

前回は送金前にアカウントのロックを解除しようとしてエラーが発生しました。

今回はその解決編です。

geth起動オプションの追加

エラーメッセージの”account unlock with HTTP access is forbidden at web3.js”を検索したところ、gethの起動時に–allow-insecure-unlockオプションを追加すればよいとの情報を見つけました。

[コマンド]

1
2
geth --allow-insecure-unlock --networkid "10" --nodiscover --datadir "~/eth_net" --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" console 2>> ~/eth_net/geth_err.log

[結果]

1
2
3
4
5
6
7
8
9
10
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.4-stable-aa637fd3/linux-amd64/go1.16.4
coinbase: 0xf4572ec53ab834afa1bd93a2cb32f070fda1c06c
at block: 485 (Sat Jun 26 2021 06:50:42 GMT+0900 (JST))
datadir: /home/aki/eth_net
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

To exit, press ctrl-d
>

問題なく起動できました。

アカウントロックの解除

前回失敗したアカウントロックの解除コマンドを再度実行します。

[コマンド]

1
personal.unlockAccount(eth.accounts[0])

[結果]

1
2
3
Unlock account 0xf4572ec53ab834afa1bd93a2cb32f070fda1c06c
Passphrase:
true

今度は問題なくロックを解除することができました。

送金

マイニングを開始し、送金処理を実行してみます。

それぞれのパラメータの意味は次の通りです。

  • from
    送金元のアドレス。
  • to
    送信先のアドレス。
  • value
    送金額。wei表示での送金額。web3.toWei関数を使ってetherをweiに変換しています。

[コマンド]

1
2
miner.start(2)
eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:web3.toWei(5, "ether")})

[結果]

1
"0x489deb0f0e2b7366733e48f29481ace37d7906fb5f3b052879e87dda29cd275c"

結果には16進のトランザクションハッシュが出力されます。

トランザクション確認

トランザクションの中身を確認します。

引数には送金した際に出力されたトランザクションハッシュを指定します。

[コマンド]

1
eth.getTransaction('0x489deb0f0e2b7366733e48f29481ace37d7906fb5f3b052879e87dda29cd275c')

[結果]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
blockHash: "0xe747b7c38b25a01a50cb5702414411fc947f1b5e2d9b8926c362ead467537120",
blockNumber: 506,
from: "0xf4572ec53ab834afa1bd93a2cb32f070fda1c06c",
gas: 21000,
gasPrice: 1000000000,
hash: "0x489deb0f0e2b7366733e48f29481ace37d7906fb5f3b052879e87dda29cd275c",
input: "0x",
nonce: 0,
r: "0xc2e868fe0ad7eb061ef298665d7b2066c162e14b5c1cf256861ab17c7bd91ef8",
s: "0x45a8876c1ff252b80ce9d3abfedbd5924bbf59b8c380b25dfeffa4ab862115a0",
to: "0xcdf900d55700e14e1f1f92d20da5aa70611010d7",
transactionIndex: 0,
type: "0x0",
v: "0x42",
value: 5000000000000000000
}

fromには送信元のアドレスが表示され、toには送信先のアドレスが表示されます。

valueにはwei表示での送金額が表示されます。etherに換算すると5etherになります。

トランザクションのレシート確認

次にトランザクションのレシートを確認します。

[コマンド]

1
eth.getTransactionReceipt('0x489deb0f0e2b7366733e48f29481ace37d7906fb5f3b052879e87dda29cd275c')

[結果]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
blockHash: "0xe747b7c38b25a01a50cb5702414411fc947f1b5e2d9b8926c362ead467537120",
blockNumber: 506,
contractAddress: null,
cumulativeGasUsed: 21000,
effectiveGasPrice: 1000000000,
from: "0xf4572ec53ab834afa1bd93a2cb32f070fda1c06c",
gasUsed: 21000,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
status: "0x1",
to: "0xcdf900d55700e14e1f1f92d20da5aa70611010d7",
transactionHash: "0x489deb0f0e2b7366733e48f29481ace37d7906fb5f3b052879e87dda29cd275c",
transactionIndex: 0,
type: "0x0"
}

トランザクションがブロックに取り込みが完了されていれば上記のような情報が表示されます。

「null」が返ってきた場合は、まだブロックに取り込まれていないので少し待つ必要があります。(Ethereumでは約15秒間隔でマイニングされます。)

blockNumberを見ると、506ブロックに取り込まれたことが確認できます。

送金先の残高確認

送金先アドレスの残高を確認します

[コマンド]

1
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")

[結果]

1
5

5etherがきちんと送金されていることが分かります。