部署智能合约到公链( 二 )


$ truffle console --network kovan
要验证连接,可以从区块链中读取一些数据,获取一些关于最新区块的信息,在控制台上执行:
web3.eth.getBlock('latest').then(console.log)
输出:
{ author: '0x03801efb0efe2a25ede5dd3a003ae880c0292e4d',difficulty: '340282366920938463463374607431768211454',extraData:'0xde830206028f5061726974792d457468657265756d86312e33362e30826c69',gasLimit: '0x7a1200',gasUsed: '0x17d23',hash:'0xc7390c4f492c8c1da60608135fc9e05930123b645b39f221cba33d8b3c577b2a',logsBloom:'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000080000000000000000000100000008000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400800000000000010000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000000008000000',receiptsRoot:'0x3d05bb2ed4fcc90234eea6d840e7d0e3ce7f598a15e5314536b17bcd11c78b5b',sealFields:[ '0x84175e8801','0xb84155a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101' ],sha3Uncles:'0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',signature:'55a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101',size: 877,stateRoot:'0x03af5adce52a81ce5d332cddb9955e344214bff00859b78868116e1e839efdf7',step: '392071169',timestamp: 1568284676,totalDifficulty: '4524524338444961608702071789512829094373049115',transactions:[ '0xded7fed0842fd65ec808bc3652ec4175bc190acc11345c49c44b1fb5d954610f','0x7e9112a46fa3c07aad813ea86355b15eebb44023c040d198ee7d15d379bbc2be' ],transactionsRoot:'0x0dd10d90686dda2684bd0ba70d1c9e1d9a5302c30ca75eb2c5b07a7b6e4498b9',uncles: [] }
可以看到,已经成功连接到了公链 。
2.部署智能合约到公链
现在,我们将智能合约部署到公链 。步骤如下:
部署需要消耗Gas,获取测试以太币用于部署部署智能合约验证部署 2.1获取测试以太币
部署需要消耗Gas,Gas需要支付以太币,我们部署到的是公链测试网Kovan,网络中的以太币没有市场价值 。
可以从Kovan聊天室获取测试用的伪以太币 。只需把钱包地址发送出去,约5分钟内,有人会给你发测试用的伪以太币 。
打开并复制列表中第一个帐户的地址(钱包地址),类似下面所示:
0x29920e756f41F8e691aE0b12D417C19204371E91

部署智能合约到公链

文章插图
发送到聊天室内,稍等片刻,你的账号将收到一笔以太币 。
2.2 部署智能合约
现在帐户里已经有了资金,可以进行部署了 。
执行部署命令:
truffle migrate --network kovan
一旦部署完成,应该会看到部署成功的消息 。
部署命令执行详情:
G:\qikegu\ethereum\mydapp>truffle migrate --network kovanCompiling 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: 0x7a12001_initial_migration.js======================Deploying 'Migrations'----------------------> transaction hash:0x7e30b5c716afed45888a9dd2d6af7e6f52a9fade0346e8ad7d0c268de508a26a> Blocks: 2Seconds: 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 ETH2_deploy_contracts.js=====================Deploying 'MyContract'----------------------> transaction hash:0xc1f7ec8fee1a23e3d08d0c9e9d6e15fef24feb8ba163e0071dccb1bb90cc0eca> Blocks: 0Seconds: 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 ETHSummary=======> Total deployments:2> Final cost:0.013896025 ETHSummary=======> Total deployments:2> Final cost:0.013896025 ETH