ゼロから始めるスマートコントラクト(その7)
スマートコントラクトのデプロイ
さっき作ったtest.solをdeployしてきましょう!Migrationは以下のファイルを作成します。
// migrations/2_deploy.js
const Test = artifacts.require("Test");
module.exports = async function (deployer) {
await deployer.deploy(Test);
};
デプロイするために、ローカル環境のGanacheへつなげます。ローカルホストと7545ポートを下記のように追加してください。
// truffle-config.js
module.exports = {
...
networks: {
...
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
...
migrateコマンドを使用して、コントラクトをGanacheにデプロイできます。こんな感じ
$ npx truffle migrate --network development
Compiling your contracts...
===========================
> Compiling .\contracts\test.sol
> Artifacts written to C:\Users\xxx\xxxx\001test\build\contracts
> Compiled successfully using:
- solc: 0.6.12+commit.27d51765.Emscripten.clang
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xe3b88f9a73aaf390238d7250c9f20acb85a391f4239020f2522d9f4e91fcb888
> Blocks: 0 Seconds: 0
> contract address: 0x25231099f47d31c1a2F479C5632e0Dff26BC0789
> block number: 1
> block timestamp: 1607431741
> account: 0x6d26Bd603B45eFdCB57D188bb60A9e301eAbeDf6
> balance: 99.99626074
> gas used: 186963 (0x2da53)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00373926 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00373926 ETH
2_deploy.js
===========
Deploying 'Test'
---------------
> transaction hash: 0xc83352520652e41201d0982fd5b8cd2a1fa47dcb212dddfe2c9f17b2ecf7ac6d
> Blocks: 0 Seconds: 0
> contract address: 0x94E07A06B94b08e087Bad7BEF899C735A92962DE
> block number: 3
> block timestamp: 1607431741
> account: 0x6d26Bd603B45eFdCB57D188bb60A9e301eAbeDf6
> balance: 99.98683946
> gas used: 428729 (0x68ab9)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00857458 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00857458 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.01231384 ETH
無事完了するとGanacheの一番目(INDEX0)のBALANCEが減っていることがわかります。
