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


2.3验证部署
现在打开控制台,与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'
可以看到智能合约已经成功部署 。
3.脚本
包含一个脚本运行器,可对以太坊网络执行自定义脚本 。
让我们创建一个脚本并执行 。
在项目根目录下,创建.js文件,内容如下:
module.exports = function(callback) {web3.eth.getBlock('latest').then(console.log)}
该脚本将从Kovan测试网络获取最新区块的信息 。
执行脚本:
truffle exec script.js --network kovan
输出:
{ author: '0x596e8221a30bfe6e7eff67fee664a01c73ba3c56',difficulty: '340282366920938463463374607431768211454',extraData:'0xde830205058f5061726974792d457468657265756d86312e33362e30826c69',gasLimit: '0x7a1200',gasUsed: '0x5e61',hash:'0x225a1e0b13fd20396af60d049ce9bb94c2f3f7df06c7db260880b62c91997004',logsBloom:'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',miner: '0x596e8221A30bFe6e7eFF67Fee664A01C73BA3C56',number: 13448162,parentHash:'0x28d00fd7b66771130ed98de5073c7797ee293e7bee4b546793a4b79171555066',receiptsRoot:'0x44617b5733ee59bde159af08ffd6edae36e0964f1724c333f3d1bef0808dee15',sealFields:[ '0x84175e95d7','0xb8412ed900e67f4a72925fb3b495efb3f547411f40d26e972cc0e8b2cf26e40cf84a545e0328199d4880b79c62670129a7db12ac58234bee0866c6376b46ab99e8a200' ],sha3Uncles:'0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',signature:'2ed900e67f4a72925fb3b495efb3f547411f40d26e972cc0e8b2cf26e40cf84a545e0328199d4880b79c62670129a7db12ac58234bee0866c6376b46ab99e8a200',size: 797,stateRoot:'0xe1bbaacfb950361bec70f4ad53a2605e1ac1d2ff0bfd913fe063dc6c5f3252a0',step: '392074711',timestamp: 1568298844,totalDifficulty: '4525729278306228651801195598997744985609807728',transactions:[ '0xf1ae41eac6b32419bc62a6cde9cab4b4ca244899a3d49b4a2461bcf94f504176' ],transactionsRoot:'0xf08c8097ea946f84ce9594ce73648fc0f9f683adef105a5db00c5f1f15e61c2c',uncles: [] }