truffle common configuration

deploy command

Initial deployment: truffle migrate

Redeploy:truffle migrate --reset

deployment file

Modify first./migrations/2_initial_migration.js

Note that filenames are prefixed with numbers and suffixed with descriptions. In order to record whether the migration ran successfully, a numbered prefix is ​​required. The suffix is ​​purely for human readability and comprehension.

1. Basic configuration

artifacts.require()

At the start of the migration, we tell Truffle which contracts we want to interact with via the artifacts.require() method. This method is similar to what Node requires, but in our case it specifically returns an abstract contract that we can use in the rest of the deployment script. The name specified should match the name defined by the contract in this source file. The name of the source file is not passed, as a file can contain multiple contracts. Example:

// fileName: ./contracts/Contracts.sol
contract ContractOne { // ... }

contract ContractTwo { // ... }

ContractTwoIf you just write it like this in your contract artifacts.require():

var ContractTwo = artifacts.require("ContractTwo");

If you want to use both:

var ContractOne = artifacts.require("ContractOne");
var ContractTwo = artifacts.require("ContractTwo");

module.exports

All migrations must export functions via the module.exports syntax. The function exported by each migration should accept a deployer object as its first argument . This object aids deployment by providing a clear syntax for deploying smart contracts as well as performing some of the deployment's more mundane responsibilities, such as saving deployed artifacts for later use. The Deployer object is the main interface for staging deployment tasks, and its API is described at the bottom of this page.

Initial migration

These two files must be preserved contracts/Migrations.sol in  the Truffle  framework  ,migrations/1_initial_migration.js

When deploying, the contract will be deployed first.

2. Deployer

Your deployment file will use the deployer to complete the deployment task. So you can write deployment tasks synchronously and they will be executed in the correct order:

// 先部署A再部署B,两者直接无直接联系
deployer.deploy(A);
deployer.deploy(B);
// 部署A后把A的地址作为参数再部署B
deployer.deploy(A).then(function() {
  return deployer.deploy(B, A.address);
});

There are detailed API documentation instructions later.

3. Network configuration

Deployment steps can be run conditionally based on the network to which they are deployed. This is an advanced feature, so please review the "Networks" section before proceeding.

To conditionally stage deployment steps, write your migrations so that they accept a second parameter, called Network. example:

module.exports = function(deployer, network) {
  if (network == "live") {
    // Do something specific to the network named "live".
  } else {
    // Perform a different step otherwise.
  }
}

4. Select an account

The migration also passes the list of accounts provided to you by the Ethereum client and web3 provider for you to use during deployment. This is web3.eth.getAccounts()the same account list returned from.

module.exports = function(deployer, network, accounts) {
  // Use the accounts within your migrations.
}

5.Deployer.API

The deployer includes many features that can be used to simplify migration.

deployer.deploy

deployer.deploy(contract, args..., options)

Deploys a specific contract specified by a contract object with optional constructor arguments. This is useful for singleton contracts, so only one instance of this contract exists for the dapp. This will set the contract's address after deployment (ie, Contract.address will be equal to the newly deployed address), and it will overwrite any previous addresses stored.

You can choose to pass an array of contracts or an array to speed up the deployment of multiple contracts. Also, the last parameter is an optional object that can contain a key named overwrite as well as other transaction parameters such as gas and from. If overwrite is set to false, the deployer will not deploy this contract (if it has already been deployed). This is useful in some cases where the contract address is provided by an external dependency.

Note that you need to first deploy and link any libraries your contract depends on before calling deploy. See the link function below for details.

See the truffle contract documentation for more information .

// 在没有构造函数参数的情况下部署单个合约
deployer.deploy(A);

// 使用构造函数参数部署单个合同
deployer.deploy(A, arg1, arg2, ...);

// 如果已经部署了此合同,请不要部署它
deployer.deploy(A, {overwrite: false});

//为部署设置最大Gas 和 “from” 地址
deployer.deploy(A, {gas: 4612388, from: "0x...."});

// Deploy multiple contracts, some with arguments and some without.
// This is quicker than writing three `deployer.deploy()` statements as the deployer
// can perform the deployment as a single batched request.
deployer.deploy([
  [A, arg1, arg2, ...],
  B,
  [C, arg1]
]);

// External dependency example:
//
// For this example, our dependency provides an address when we're deploying to the
// live network, but not for any other networks like testing and development.
// When we're deploying to the live network we want it to use that address, but in
// testing and development we need to deploy a version of our own. Instead of writing
// a bunch of conditionals, we can simply use the `overwrite` key.
deployer.deploy(SomeDependency, {overwrite: false});

deployer.link

deployer.link(library, destinations)

Link a deployed library to a contract or contracts. The destination can be a single contract or an array of multiple contracts. If any contract within the destination does not depend on the linked library, the contract will be ignored.

// Deploy library LibA, then link LibA to contract B, then deploy B.
deployer.deploy(LibA);
deployer.link(LibA, B);
deployer.deploy(B);

// Link LibA to many contracts
deployer.link(LibA, [B, C, D]);

deployer.then

deployer.then(function() {...})

Just promiselike , run arbitrary deployment steps. Use this option to call specific contract functions during migration to add, edit, and reorganize contract data.

var a, b;
deployer.then(function() {
  // Create a new version of A
  return A.new();
}).then(function(instance) {
  a = instance;
  // Get the deployed instance of B
  return B.deployed();
}).then(function(instance) {
  b = instance;
  // Set the new instance of A's address on B via B's setA() function.
  return b.setA(a.address);
});

Deploy other configurations

furrow

Compiler configuration:

compilers: {
	solc: {
		version: "0.5.1",
		settings: {
       		optimizer: {
         		enabled: true,
         		runs: 200,
       		}}}}

wallet

In the project configuration file truffle.js in the project root directory , you can use seeds to deploy contracts on the mainnet or testnet. The following provides a configuration to deploy to the testnetrinkeby

const HDWalletProvider = require('truffle-hdwallet-provider');
const fs = require('fs');
// 读取种子,12个单词组成的种子
const mnemonic = fs.readFileSync("./path/to/mnemonic.secret").toString().trim();

module.exports ={
    networks:{
        rinkebyTest:{
        	provider: () => new HDWalletProvider(
        		mnemonic, 
        		`https://rinkeby.infura.io/v3/aa86f***60803c`,// your infura API key
        		0, // 地址的起始索引
        		10 // 生成的地址数量
      		),
      		network_id: 4,
      		// gas: 6500000,
      		confirmations: 2,
      		gasPrice: 5000000000, // 5 Gwei
      		skipDryRun: true // 跳过预执行,直接部署
        }
    }
}

Truffle Study Notes (1) Basic Commands and Configuration - JustinQP's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/u013288190/article/details/123853814