6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ethereum のトランザクション作成時に必要な nonce を得る

Last updated at Posted at 2018-06-19
  • ローカルで Ethereum のトランザクションに署名する際には、nonce を指定する必要があります。
  • このときに必要な nonce を取得する方法をメモしておきます。

nonce とは

  • Ethereum のトランザクションにおける nonce は、ある Ethereum のアドレス(アカウント)から実行されたトランザクションの総数です。
  • nonce は各トランザクションがそれぞれ一度のみ実行されることを保証するために必要な値です。
  • 0 から 1 つずつインクリメントされていきます。

Ethereum アドレスの現在の nonce を取得する

  • Ethereum のトランザクションを作成するためには、トランザクションを作成したいアカウントが過去に何回トランザクションを実行していたか(トランザクションの総数==現在の nonce)を知る必要があります。
  • Ethereum アドレスの現在の nonce は、web3.js を使用する場合は web3.eth.getTransactionCount で取得することができます。
  • web3.eth.getTransactionCount では、過去のトランザクション数が0回であれば0を、3回であれば3を返します。

サンプルコード

code:getTransaction.js
 web3.eth.getTransactionCount(address, (error, count) => {
   console.log(count);
 });

デモ

参考

関連

Note

6
8
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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?