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

接下来,我们将编译好的智能合约部署到本地的Ganache区块链网络。步骤如下:

  • 更新项目的配置文件,修改网络配置连接到本地区块链网络(Ganache)。
  • 创建迁移脚本,告诉Truffle如何部署智能合约。
  • 运行新创建的迁移脚本,部署智能合约。

1. 更新配置文件

更新项目的配置文件,修改网络配置连接到本地区块链网络(Ganache)。

打开位于项目根目录下的truffle-config.js文件,修改内容如下:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    }
  },
  solc: {
    optimizer: {
      enabled: true,
      runs: 200
    }
  }
}

这些网络配置,包括ip地址、端口等,应该与Ganache的网络配置匹配:

图

2. 创建迁移脚本

接下来,我们将在migrations目录中创建迁移脚本,告诉Truffle如何部署智能合约,在该目录中创建文件2_deploy_contracts.js

注意,在migrations目录中所有文件都有编号,作用是让Truffle知道执行它们的顺序。

2_deploy_contracts.js文件内容如下:

var MyContract = artifacts.require("./MyContract.sol");

module.exports = function(deployer) {
  deployer.deploy(MyContract);
};

上面的代码中:

  • 首先,require了创建的合约,并将其分配给一个名为“MyContract”的变量。
  • 接着,将合约加入部署清单,运行迁移命令时合约将被部署。

3. 执行迁移命令

现在让我们从命令行执行迁移命令, 部署智能合约。

$ truffle migrate

执行详情如下:

G:\qikegu\ethereum\mydapp>truffle migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      5777
> Block gas limit: 0x6691b7


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xe62fb8a27c9ccc894562fbd7a7797526ad9323ab67a44516ae342642bf4ffcc6
   > Blocks: 0            Seconds: 0
   > contract address:    0x168A7247B58786edd259502948f5Bf9449C863AD
   > block number:        1
   > block timestamp:     1568189958
   > account:             0x29920e756f41F8e691aE0b12D417C19204371E91
   > balance:             99.99477214
   > gas used:            261393
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00522786 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00522786 ETH


2_deploy_contracts.js
=====================

   Deploying 'MyContract'
   ----------------------
   > transaction hash:    0xe9dcef6f70332e476684e8f93ab96969af53920555161054f1f4bcc6277116fb
   > Blocks: 0            Seconds: 0
   > contract address:    0x4D3CFaF8457CEA76c0409f989f9870115B4d2d82
   > block number:        3
   > block timestamp:     1568189959
   > account:             0x29920e756f41F8e691aE0b12D417C19204371E91
   > balance:             99.98804272
   > gas used:            294448
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00588896 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00588896 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.01111682 ETH











  receipt:
   { transactionHash:
      '0x83be6ef86fe542b3c94ae1dd5f2e04570c199d6b2e7997af60f3d91cda9259ec',
     transactionIndex: 0,
     blockHash:
      '0x6e58c2c77b5998004b8a8c66760ca923814865307c69f1c779673cc2cbca06bc',
     blockNumber: 5,
     from: '0x29920e756f41f8e691ae0b12d417c19204371e91',
     to: '0x4d3cfaf8457cea76c0409f989f9870115b4d2d82',
     gasUsed: 33501,
     cumulativeGasUsed: 33501,
     contractAddress: null,
     logs: [],
     status: true,
     logsBloom:
      '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
     v: '0x1c',
     r:
      '0xdaf1578a7987ec5d4e7d25c4b66f570d97f880b783d3403b54fa7eb30b1ab836',
     s:
      '0x4024f2b26bab6277cc86da9727a9bccc1ba7832773b9c2781b265f8dd87df46f',
     rawLogs: [] },
  logs: [] }

可以看到,我们已经将智能合约成功部署到本地的Ganache区块链网络。



浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载