A simple method of deploying smart contract to Ethernet network (truss + infra) with truss

jamesdodo 2022-04-05 16:05:08 阅读数:722

simple method deploying smart contract

1. Sign in infura Register on the official website , obtain API-KEY
Infura It is a managed cluster of Ethereum nodes , You can deploy the Ethereum smart contract you wrote to infura On the node provided , You don't need to build your own Ethereum node .

https://infura.io/

2. install HDWalletProvider
Infura Of HDWalletProvider It's an independent npm software package , Switch to truffle The project directory uses the following command to install

npm install truffle-hdwallet-provider

And then modify truffle.js or truffle-config.js The contents of the document are as follows :

// Definition HDWalletProvider object
var HDWalletProvider = require("truffle-hdwallet-provider");
// Provide mnemonics (mnemonic) Or private key To import your account
var mnemonic = "topic foster find apple famous have bonus month remain middle funny smart";
// add to Ropsten Test network definition
module.exports = {
  networks: {
    ropsten: {
      provider: function() {
      // Define Ethereum nodes https://ropsten.infura.io/your-api-key
       return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/503fc5af65d64cdaa090d2873201bc5a")
      },
      network_id: 3
    }
  }
};

3.  Add profile .env
Dotenv Is a zero dependency module , For from ".env" Import environment variables into the file process.env, Yes DAPP Come on , So you don't have to upload wallet mnemonics and other core secrets to GITHUB, Facilitate asset security .

.env file

mnemonic=topic foster find apple famous have bonus month remain middle funny smart
infurakey=xxxxxxxxxxxxxxxxxxxxxxxxxx
prakey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

So the above code is as follows :

// Definition HDWalletProvider object
var HDWalletProvider = require("truffle-hdwallet-provider");
const reselt=require('dotenv').config(); // By default, read the... Under the root directory of the project .env file , use process.env. call
if (result.error) {
  throw result.error;
}
console.log(result.parsed);
module.exports = {
  networks: {
    ropsten: {
      provider: function() {
      // Define Ethereum nodes https://ropsten.infura.io/your-api-key
       return new HDWalletProvider(process.env.mnemonic, "https://ropsten.infura.io/"+ process.env.infurakey);
      },
      network_id: 3
    }
  }
};

4. Deploy to Ethereum network
Execute the following command , Deploy to Ethereum test network ropsten

truffle migrate --network ropsten

If... Occurs during deployment network up to date The reason is that contract migration is lazy compilation , If you find that it has been published , And if the version number of the release has not changed, it will not be released again , So use --reset. Use truffle migrate --reset To force recompilation and release of all contracts .

If something similar happens during deployment  node_modules\node-sss\ventor not found And other error messages First installation npm install node-sss --save , If it still appears use npm rebuild node-sass --save Rebuild and generate .

版权声明:本文为[jamesdodo]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2022/04/202204051506396592.html