Ethereum教程

以太坊(Ethereum) – truffle脚本

Truffle包含一个脚本运行器,可对以太坊网络执行自定义脚本。
让我们创建一个脚本并执行。
在项目根目录下,创建 script.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: [] }
下面的代码智能合约 MyContract中,读取value值,将 script.js脚本文件中的代码替换为:
const MyContract = artifacts.require("./MyContract.sol");
module.exports = async function(callback) {
  const contract = await MyContract.deployed()
  const value = await contract.get()
  console.log("Value:", value)
}
执行脚本:
truffle exec script.js--network kovan
输出:
Value: hello world
脚本运行器是一个非常有用的功能。
昵称: 邮箱:
wdsc 2021-07-13

写的很好写的很好写的很好写的很好写的很好写的很好写的很好写的很好

Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4