solidity[59]-infura-deploy

infura

在之前,我们使用了本地的ganache-cli测试网络+mocha测试部署交互合约。现在,我们需要使用到infura将合约部署到真实的区块链的网络当中。
我们都知道,如果我们本身不是一个以太坊的节点,那么我们就需要将我们的交易发送给其他的节点来挖矿确认。infura就是默认的维护了许多的节点,我们通过它就能够与以太坊进行连接。

安装模块

1
npm install --save truffle-hdwallet-provider

利用infura部署合约

deploy.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 const HDWalletProvider = require('truffle-hdwallet-provider');

const Web3 = require('web3');
const {interface,bytecode} = require('./compile');

//借助于infura提供provider,ropsten网络
const provider = new HDWalletProvider(
  'type give repair twenty split notable humor sweet obey pizza click absurd',
  'https://ropsten.infura.io/v3/de22b468cb7846788b4d1ae36bcc26c2'
);


const web3 = new Web3(provider);

const deploy =  async ()=>{
  console.log(interface);
  const accounts = await  web3.eth.getAccounts();
  //console.log('Attemp to deploy contract',accounts[0]);
    const result =   await new web3.eth.Contract(JSON.parse(interface)).deploy({data:'0x'+bytecode})
            .send({from:accounts[0],gas:'1000000'});
   //打印出部署到合约中的地址。
    console.log('contract deployed to ',result.options.address);

}
deploy();

部署

1
> node deploy.js

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2329698
59
59A