LoginSignup
0
0

More than 5 years have passed since last update.

Ethereum の function call データフォーマット

Last updated at Posted at 2018-05-08
  • マルチシグウォレットを使用して他のスマートコントラクトのメソッドを実行したりするのに必要なので調べたよ
  • Solidity で書かれたスマートコントラクトに次のようなメソッドがある場合で説明します
  • 以下の countUp() とか countDown() を実行するためにはどういったトランザクションを作って送ればいいか、という話
pragma solidity ^0.4.19;

contract Count {
    uint256 public count;

    function countUp() public {
        count = count + 1;
    }

    function countDown() public {
        count = count - 1;
    }

}
  • function call のデータについて、最初の4バイトはシグネチャと呼ばれる次のフォーマットにした関数のプロトタイプのようなものを Keccak256 でハッシュ化したものを使うとのこと。
  • 引数は型のみを使用します。
  • 引数の型はカンマで区切ります。
funtionName(type1,type2,...)

上記の countUp() の場合、Node.js で書くとつぎのような感じで、Keccak256 のハッシュが得られるので、最初の 4bytes 0x0bd8599e がトランザクションに含むべきデータになります。

const Web3 = require('web3');

console.log(Web3.version);
// => 1.0.0-beta.34

const signature = 'countUp()';
const hash = Web3.utils.keccak256(signature);
console.log(hash);
// => 0x0bd8599e08d359240ca8194d54975e0eda037fe2109af0514fc9505f2cfc9407
  • 例えば、上記のコントラクトのアドレスが、0xe126016d061369cd23de92475d04fb762b663a97 だとしたら、countUp() を実行したい場合、つぎのようなトランザクションを送る

    • to: 0xe126016d061369cd23de92475d04fb762b663a97
    • value: 0
    • data: 0x0bd8599e
  • 実際にためしてみたtx: https://ropsten.etherscan.io/tx/0x32b415366043be03300d2fcd1acb8f3dce1f8234fa86b204f6d68e9b7c20b642

  • 引数をとる場合については時間がなくなってしまったのでまた今度書きます。

  • 今回はいつにもまして乱文になってしまった..

Note

0
0
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
0
0