Truffle v5を利用して簡単なコントラクトをデプロイしてみます。
下記リンクの記事では Truffle v4.1.8 で説明していますが、v5 だとそのまま実行できないので本記事において更新しています。
Truffleをインストールします。
1 |
$ npm install -g truffle |
バージョンを確認。
1 2 3 4 5 |
$ truffle version Truffle v5.0.31 (core: 5.0.31) Solidity - 0.5.10 (solc-js) Node v12.6.0 Web3.js v1.2.1 |
適当なプロジェクト(ディレクトリ)を作成します。
1 2 |
$ mkdir sample-truffle $ cd sample-truffle/ |
初期化します。
1 |
$ truffle init |
1 2 3 4 5 6 7 8 9 10 11 12 |
✔ Preparing to download ✔ Downloading ✔ Cleaning up temporary files ✔ Setting up box Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test |
下記のようなディレクトリ構造が生成されます。
1 2 3 4 5 6 7 8 |
$ tree . ├── contracts │ └── Migrations.sol ├── migrations │ └── 1_initial_migration.js ├── test └── truffle-config.js |
今回作成するコントラクトである HelloWorld.sol を contractsディレクトリ内に作成します。下記コマンドで作成できます。
1 |
$ truffle create contract HelloWorld |
下記のように編集しました。
HelloWorld.sol
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; } } |
migrationsディレクトリ内に下記のマイグレーションファイルを追加します。
2_deploy_contracts.js
1 2 3 4 5 6 |
const HelloWorld = artifacts.require("HelloWorld"); module.exports = (deployer) => { const word = "Hello"; deployer.deploy(HelloWorld, word); }; |
本記事ではEthereumクライアントとしてローカルで ganache-cli を利用します。
1 |
$ npm install -g ganache-cli |
起動させます。
1 |
$ ganache-cli |
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 |
Ganache CLI v6.5.1 (ganache-core: 2.6.1-beta.0) Available Accounts ================== (0) 0x33940127aA6C8c59775FAe566584E790dF48c274 (100 ETH) (1) 0x51120Ce320e63AC6B2128C1a35593318A26e116E (100 ETH) (2) 0xd7E9a4DeE895bD2Ec59F77302cB9764C0cD548E6 (100 ETH) (3) 0xaE89ff5E21C9C4D95aeB15233e832d7a83bb939A (100 ETH) (4) 0x7ea61D1b5C8368936489C9a326a3a0BCa1290C62 (100 ETH) (5) 0xE43782974E4b0DD998D52e047953209C64A23Ff2 (100 ETH) (6) 0x26e4110d2650Efbbb20443c85A294DEa52800F23 (100 ETH) (7) 0xDF3BB057d32f8a7FfB22aE554597A279A39da742 (100 ETH) (8) 0x20816368c6F21DEE65F6d14Fe44638376c655B9C (100 ETH) (9) 0xa74160d7482F9Ea841a1dfa78De6A94EE7cA6CC8 (100 ETH) Private Keys ================== (0) 0x4bf0553382270bfb2e1c73aee8cbdc60bb003ffe81fa8f41ff5820124f0904b8 (1) 0x774cd822d1f9659a5ff81d6aef0c3560f8cacd08f884e5ea6760b45bc35e7706 (2) 0x8e713ea17b070953125ae2d85dd681c84538ad85ce4f3304981eed86b84e0733 (3) 0x108aba293565565eae11f993bbe52e86748257addd85001f9029f92b26ac0ed5 (4) 0x040c0d713c3dd2120c13c28f0cc03973d3baf5d6e4c48d1fd1f95cc7e7c09f7d (5) 0xb4176e8d5882016917ec68fdfb445924899ca9b72fe786e1d8873ccb21b54aee (6) 0x221d8afc88861c6f97989a2f3b5958101bae91ec0de4886fc5ca5a5ba3bc5ec3 (7) 0xca59d9fe38405d5b1b517ffa60524ecb4b7845338567b3da0a24a0b690118fe9 (8) 0xe54e2e622c522e0014f4a0871d5fd4f6ace6f90b7efa1129d7d2baafaf2908c1 (9) 0x546b5d6883ca64fa3d321239d5821d56aa0363f2cfc171d9b441706f9f3bdea6 HD Wallet ================== Mnemonic: private evolve notable mask edge dolphin manual tomorrow small panel gasp pony Base HD Path: m/44'/60'/0'/0/{account_index} Gas Price ================== 20000000000 Gas Limit ================== 6721975 Listening on 127.0.0.1:8545 |
次に truffle-config.js ファイルの networks の下記部分のコメントを外します。hostとportが一致していることを確認して下さい(上記のListening on 〜)。
1 2 3 4 5 |
development: { host: "127.0.0.1", // Localhost (default: none) port: 8545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) }, |
コンパイルします。
1 |
$ truffle compile |
コントラクトをデプロイします。
1 |
$ truffle migrate |
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 |
〜省略 2_deploy_contracts.js ===================== Deploying 'HelloWorld' ---------------------- > transaction hash: 0x71e4807d8723993dc8f809a59f7156eaee354ee62176ad3b0d618097fb4e1a14 > Blocks: 0 Seconds: 0 > contract address: 0xC2bb1d7d0Cbde27F89F7FF88B9660a15627377a2 > block number: 3 > block timestamp: 1565343388 > account: 0x33940127aA6C8c59775FAe566584E790dF48c274 > balance: 99.98791318 > gas used: 300925 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.0060185 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.0060185 ETH Summary ======= > Total deployments: 2 > Final cost: 0.01124636 ETH |
Truffle のコンソールを起動させます。
1 2 |
$ truffle console truffle(development)> |
TruffleはWeb3.jsによってganache–cliと接続します。Promiseが返ってくるので、awaitを付加しコントラクトのインスタンスを取得しています。
1 2 |
truffle(development)> let instance = await HelloWorld.deployed() undefined |
関連ページ
実際にコントラクトで実装したgetWord()を呼び出してみます。
1 2 |
truffle(development)> instance.getWord() 'Hello' |
次に changeWord() に適当な引数を指定して実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
truffle(development)> instance.changeWord("Hello Tarou") { tx: '0x77964e914ee634ddb98c9c6552aeea5362f1503461556dc31d4a28438504f7f3', receipt: { transactionHash: '0x77964e914ee634ddb98c9c6552aeea5362f1503461556dc31d4a28438504f7f3', transactionIndex: 0, blockHash: '0xd22fb2322667f1118d7d9cca967074aee4e7c0e242494ff578b7aa82ec547ec3', blockNumber: 5, from: '0x33940127aa6c8c59775fae566584e790df48c274', to: '0xc2bb1d7d0cbde27f89f7ff88b9660a15627377a2', gasUsed: 33629, cumulativeGasUsed: 33629, contractAddress: null, logs: [], status: true, logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', v: '0x1b', r: '0x1ecfd8bec5a87b28eca1ebc149db319277efcfc11729c9a41ce0d3460b8d8546', s: '0x27c32566d3fed68e199c17d5dc6e7ad6e616462f5425ede687254778cd4c14e1', rawLogs: [] }, logs: [] } |
word が変更されていることを確認します。
1 2 |
truffle(development)> instance.getWord() 'Hello Tarou' |
参照ページ
INTERACTING WITH YOUR CONTRACTS