truffle + infura + web.js 编写编译,以及在测试网络上部署智能合约

infura 是什么?一开始我接触的是Remix 一个网页的IDE并内置编译器(什么编译器,是solc吗?),然后注册了metamask钱包,注册两个账户并获取以太币。

火狐浏览器插件的Metamask上也可以通过新建账户buy,可以得到两个ether。

https://ethtools.com/ropsten/tools/faucet/ 填入地址和申请的数量(不超过5个)后,metamask账户没有受到ether?

http://faucet.ropsten.be:3001/ 从这里可以每次获取一个ether

https://faucet.metamask.io/ 这里可以获取ether

https://infura.io/dashboard 这里创建新项目后就有API key

编写合约,部署脚本,truffle.js 后编译合约

truffle + ganache,develop是truffle 内置的区块链环境;truffle migrate 命令默认部署将合约部署到truffle.js声明网络上。

ganache 原来称为testRPC,一个以太坊客户端,允许连接到特定网络,并监听特定网络上部署的区块链,petshop实例中ganache就是连接到truffle.js 声明的特定网络。ganache本地模拟一个节点,并本地模拟私有链,ganache将合约部署在私有链上,当前私有链上也就只有本地一个节点,此时ganache的用途只能检测到该链上部署智能合约的行为,因为只有本地一个节点,不能和其他节点交易。当智能合约部署到链上时,ganache会自动挖矿。

pet-shop应用中用浏览器插件metamask 导入本地ganache账户,点击Adopt后将会调用智能合约,ganache检测到这笔交易后挖矿并消耗gas,消耗gas的账户余额可以在metamask和ganache上看到。

truffle 也可以连接到geth 客户端

Truffle 使用的工作量证明是什么?PoA

infura 提供一个infura网络上仿真节点,不像develop 本地模拟一个节点并部署私有链到本地会占用本地的资源。

能在infura提供的网络上挖矿吗?

智能合约部署好了,如何调用智能合约呢?智能合约编译后产生的build文件夹下有合约对应json文件,会有abi接口,调用abi接口间接调用合约。

Remix IDE内置EVM虚拟机,可以支持部署到测试网络,以UI形式显示了abi接口可以供用户直接使用。

truffle.js中配置多个网络,网络id(network_id)可以一样吗?

在特定网络上运行时,truffle migrate --network live,需要在truffle.js中配置live

networks: {
  development: {
    host: "127.0.0.1",
    port: 8545,
    network_id: "*" // match any network
  },
  live: {
    host: "178.25.19.88", // Random IP for example purposes (do not use)
    port: 80,
    network_id: 1,        // Ethereum public network
    // optional config values:
    // gas
    // gasPrice
    // from - default address to use for any transaction Truffle makes during migrations
    // provider - web3 provider instance Truffle should use to talk to the Ethereum network.
    //          - function that returns a web3 provider instance (see below.)
    //          - if specified, host and port are ignored.
  }
}

 from 表示合约部署期间,truffle 做出的任何交易使用的地址,默认是第一个账户

 provider  是truffle和以太坊网络通信的的web3实例 ;如果指明provider,则可以忽略host和port,host和port也就是使用默认的  web3实例,等价于 provider: 的new Web3.providers.HttpProvider("http://<host>:<port>")

对于每个网络可以指定host/port或provider,但不能同时指定两者。如果需要HTTP提供程序,建议使用host和port,如果需要自定义提供程序HDWalletProvider,则必须使用provider。

以下网络列表由本地测试网络和infura托管的Ropsten网络组成,两者均由HDWalletProvider提供。确保truffle-hdwallet提供程序包装在函数中。

networks: {
  ropsten: {
    provider: function() {
      return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/");
    },
    network_id: '3',
  },
  test: {
    provider: function() {
      return new HDWalletProvider(mnemonic, "http://127.0.0.1:8545/");
    },
    network_id: '*',
  },
}

 只能合约编译后默认保存在./build/contracts 目录下,通过contracts_build_directory更改

module.exports = {
  contracts_build_directory: "./output",
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
    }
  }
};

   window上更改合约编译路径也可以使用绝对路径,但是一定要使用双反斜杠:C:\\Users\\Username\\output.

当合约编译后使用truffle migrate 部署合约时,truffle 将会更新json文件,使其包含于该网络相关的信息。"

networks": {

  "5777":
  {   
     "events": {},
     "links": {},
     "address": "0x6ec3e02ad549fae9e7e830a3530b8db4c58e8b20",
     "transactionHash":"0x8d05875aa8d1b98e5530ab8b8cf5d09f9226295308c835ab38223bdd085b2d6a"
  }
},
"schemaVersion": "2.0.1",
"updatedAt": "2018-07-18T23:03:10.100Z"

ETHPM 配置,以太坊包管理,适用于truffle.js

如果要发布一个包,包的名字必须是ETHPM中唯一的,格式package_name:“adder”;

版本,软件版的版本,格式 version:"0.0.3"

描述,可读的文字描述,格式description:"Simple contract to add two numbers"

作者,格式 authors:[

        "Tim Counter <[email protected]>"

]

关键字,格式keywords :[

      "ethereum",

       "addition"

]

依赖,格式dependencies:{

       "owned":"^0.0.1",

       "erc20-token":"1.0.0"

}

执照,格式license: "MIT"

truffle_config.js 文件配置

 如果想让truffle 在触发构建时运行外部命令或执行其他功能,只需要将该命令作为字符创包含在truffle_config.js中

module.exports = {
  // This will run the `webpack` command on each build.
  //
  // The following environment variables will be set when running the command:
  // WORKING_DIRECTORY: root location of the project
  // BUILD_DESTINATION_DIRECTORY: expected destination of built assets (important for `truffle serve`)
  // BUILD_CONTRACTS_DIRECTORY: root location of your build contract files (.sol.js)
  //
  build: "webpack"
}
module.exports = {
  build: function(options, callback) {
     // Do something when a build is required. `options` contains these values:
     //
     // working_directory: root location of the project
     // contracts_directory: root directory of .sol files
     // destination_directory: directory where truffle expects the built assets (important for `truffle serve`)
  }
}

 可以创建自定义模块,例如可以创建一个包含上述build功能的对象,

var DefaultBuilder = require("truffle-default-builder");
module.exports = {
  build: new DefaultBuilder(...) // specify the default builder configuration here.
}

 在应用中使用智能合约

// Step 1: Get a contract into my application
var json = require("./build/contracts/MyContract.json");

// Step 2: Turn that contract into an abstraction I can use
var contract = require("truffle-contract");
var MyContract = contract(json);

// Step 3: Provision the contract with a web3 provider
MyContract.setProvider(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));

// Step 4: Use the contract!
MyContract.deployed().then(function(deployed) {
  return deployed.someFunction();
});

猜你喜欢

转载自blog.csdn.net/liudaoqiang_tj/article/details/81088873
今日推荐