ethereumjs-tx のバージョン2以上で、若干仕様が変わっているようなので、その部分をメモ程度に記述しておきます。
ethereumjs-tx 自体の説明は旧記事である下記ページをご覧下さい。
ethereumjs-txでトランザクションを生成する[Infula][sendSignedTransaction]
※本記事では、コントラクトをINFURA のROPSTEN Testnetにデプロイしているのを前提としています。ethereumjs-txの仕様変更と直接関係ありませんが、Truffle v5でのデプロイ方法等に関しては下記ページご覧下さい。
Truffle v5でINFURAのROPSTEN Testnetにコントラクトをデプロイする
下記コードが、INFURA のROPSTEN Testnetにデプロイしたコントラクトです。これからchangeWord関数の実行、すなわちコントラクトのwordステータスを変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
pragma solidity >=0.4.25 <0.6.0; contract HelloWorld { string word; constructor(string memory _word) public { word = _word; } function getWord() public view returns(string memory) { return word; } function changeWord(string memory _word) public { word = _word; } } |
ethereumjs–txをインストールします。
1 |
$ npm install --save ethereumjs-tx |
package.json でethereumjs–txのバージョンを確認します。
1 2 3 4 5 |
"dependencies": { "ethereumjs-tx": "^2.1.0", "express": "^4.17.1", "web3": "^1.2.1" } |
changeWord関数に引数を指定し、実行するコードです。ハイライト部分が ethereumjs–tx のバージョン2で変更となった部分です。INFURA KEYS(5行目)やアカウントおよびその秘密鍵(9、10行目)は各自設定して下さい。
sample.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
const Web3 = require('web3') const Tx = require('ethereumjs-tx').Transaction //取得したAPIキー(INFURA KEYS)を設定する const provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/◯◯◯') const web3 = new Web3(provider) //MetaMaskのアカウントとその秘密鍵 const account = '◯◯◯' const privateKey = Buffer.from('◯◯◯', 'hex') //デプロイしたコントラクトのアドレス const contractAddress = '0x8b351c5ED9818441283AC49d4C683F4ab626f299' //ABI情報 const HelloWorld = require('../build/contracts/HelloWorld.json') const abi = HelloWorld.abi const contract = new web3.eth.Contract(abi, contractAddress) //changeWord関数に引数を設定 const contractFunction = contract.methods.changeWord("こんばんわ") const functionAbi = contractFunction.encodeABI() web3.eth.getTransactionCount(account).then(_nonce => { const txParams = { nonce: web3.utils.toHex(_nonce), gasPrice: web3.utils.toHex(web3.utils.toWei('4', 'gwei')), gasLimit: web3.utils.toHex(210000), from: account, to: contractAddress, data: functionAbi }; const tx = new Tx(txParams, { 'chain': 'ropsten' }) tx.sign(privateKey) const serializedTx = tx.serialize() contract.methods.getWord().call().then(v => console.log("Value before increment: " + v)) web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')) .on('receipt', receipt => { console.log(receipt); contract.methods.getWord().call().then(v => console.log("Value after increment: " + v)) }) }) |
ハイライト部分をバージョン1のコードで実行しようすると、
TypeError: Tx is not a constructor
や
Error: Returned error: invalid sender
のようなエラーが表示されます。
実行し、下記のように表示されたら成功です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ node index3-new.js Value before increment: こんにちわ { blockHash: '0xab9284e5b685d4835981b6bffae1441a9458be6abbd0062cebf64f747e186937', blockNumber: 6195075, contractAddress: null, cumulativeGasUsed: 2968700, from: '0x3ffd57aefd2e379f5d4758525dbee2656dd32eb9', gasUsed: 33885, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: true, to: '0x8b351c5ed9818441283ac49d4c683f4ab626f299', transactionHash: '0xafdeace762ecab6679148a09a0cc54f91c943b260baaac4b0603f37305549590', transactionIndex: 15 } Value after increment: こんばんわ |
※本記事ではnpm install によってインストールしたweb3を利用しましたが、MetaMaskを直接利用すると、MetaMaskによってブラウザにインジェクトされたweb3を利用できます。その場合は、ethereumjs-txは必要ありません。詳細は下記ページをご覧下さい。
MetaMaskによってsendTransactionを実行する [getData]