Ethereum教程

以太坊(Ethereum) – 部署智能合约到公链

现在,我们将智能合约部署到公链。步骤如下:
部署需要消耗Gas,获取测试以太币用于部署 部署智能合约 验证部署

获取测试以太币

部署需要消耗Gas,Gas需要支付以太币,我们部署到的是公链测试网Kovan,网络中的以太币没有市场价值。
可以从Kovan faucet Gitter聊天室获取测试用的伪以太币。只需把钱包地址发送出去,约5分钟内,有人会给你发测试用的伪以太币。
打开Ganache并复制列表中第一个帐户的地址(钱包地址),类似下面所示:
0x29920e756f41F8e691aE0b12D417C19204371E91
发送到聊天室内,稍等片刻,你的账号将收到一笔以太币。

部署智能合约

现在帐户里已经有了资金,可以进行部署了。
执行部署命令:
truffle migrate--network kovan
一旦部署完成,应该会看到部署成功的消息。
部署命令执行详情:
G:\qikegu\ethereum\mydapp>truffle migrate--network kovan
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Migrations dry-run (simulation)
===============================
> Network name:    'kovan-fork'
> Network id:      42
> Block gas limit: 0x7a1200
...
Starting migrations...
======================
> Network name:    'kovan'
> Network id:      42
> Block gas limit: 0x7a1200
1_initial_migration.js
======================
   Deploying 'Migrations'
  ----------------------
   > transaction hash:    0x7e30b5c716afed45888a9dd2d6af7e6f52a9fade0346e8ad7d0c268de508a26a
   > Blocks: 2            Seconds: 9
   > contract address:    0x168A7247B58786edd259502948f5Bf9449C863AD
   > block number:        13447029
   > block timestamp:     1568294312
   > account:             0x29920e756f41F8e691aE0b12D417C19204371E91
   > balance:             2.993465175
   > gas used:            261393
   > gas price:           25 gwei
   > value sent:          0 ETH
   > total cost:          0.006534825 ETH
   > Saving migration to chain.
   > Saving artifacts
  -------------------------------------
   > Total cost:         0.006534825 ETH
2_deploy_contracts.js
=====================
   Deploying 'MyContract'
  ----------------------
   > transaction hash:    0xc1f7ec8fee1a23e3d08d0c9e9d6e15fef24feb8ba163e0071dccb1bb90cc0eca
   > Blocks: 0            Seconds: 0
   > contract address:    0x4D3CFaF8457CEA76c0409f989f9870115B4d2d82
   > block number:        13447036
   > block timestamp:     1568294340
   > account:             0x29920e756f41F8e691aE0b12D417C19204371E91
   > balance:             2.9850534
   > gas used:            294448
   > gas price:           25 gwei
   > value sent:          0 ETH
   > total cost:          0.0073612 ETH
   > Saving migration to chain.
   > Saving artifacts
  -------------------------------------
   > Total cost:           0.0073612 ETH
Summary
=======
> Total deployments:   2
> final cost:          0.013896025 ETH
Summary
=======
> Total deployments:   2
> final cost:          0.013896025 ETH

验证部署

现在打开truffle控制台,与kovan测试网络上的智能合约进行交互:
$ truffle console--network kovan
在控制台中执行:
truffle(kovan)> MyContract.deployed().then((c) => { contract = c })
然后:
truffle(kovan)> contract.get()
'myValue'
truffle(kovan)> contract.set("hello world")
{ tx:
   '0x7bf63444f3a7bd70e981a7bd49228b1cf1a8c3754daf64c4c7765b8eee46bf37',
  receipt:
   { blockHash:
      '0xe03d0f43d85f4e41c18a90aa563ebda08899c6b9c38d0cd7779937046e2aed0c',
     blockNumber: 13447763,
     contractAddress: null,
     cumulativeGasUsed: 33629,
     from: '0x29920e756f41f8e691ae0b12d417c19204371e91',
     gasUsed: 33629,
     logs: [],
     logsBloom:
      '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
     root: null,
     status: true,
     to: '0x4d3cfaf8457cea76c0409f989f9870115b4d2d82',
     transactionHash:
      '0x7bf63444f3a7bd70e981a7bd49228b1cf1a8c3754daf64c4c7765b8eee46bf37',
     transactionIndex: 0,
     rawLogs: [] },
  logs: [] }
truffle(kovan)> contract.get()
'hello world'
可以看到智能合约已经成功部署。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4