truffle.js configuration file

Let’s introduce the truffle.js configuration file in detail below.

truffle.js is the configuration file of the Ethereum development framework truffle. This manual introduces the location of the truffle.js configuration file, naming conflict resolution under Windows, and various configuration options.

truffle configuration file location

The configuration file of the truffle project is located in the root directory of the project and is named truffle.js. This file is a Javascript script that can execute any necessary code to create a configuration suitable for you. truffle.js must export an object that represents your project configuration, for example:

module.exports = {
    
    
  networks: {
    
    
    development: {
    
    
      host: "127.0.0.1",
      port: 8545,
      network_id: "*" // 可匹配任意网络
    }
  }
};

In the default configuration file generated when creating the truffle project, an Ethereum node named development is declared, which is listening at the address 127.0.0.1:8545.
Naming conflict resolution under WINDOWS

When using truffle on the Windows command line, the default configuration file name conflicts with truffle - when you enter truffle on the command line, Windows will actually call the configuration script truffle.js in the project directory.

There are several ways to resolve this conflict:

输入truffle.cmd全称,例如:

D:\ez-dapp> truffle.cmd compile

修改PATHEXT环境变量,将.js后缀从可执行后缀中删除。
将truffle.js更名为truffle-config.js。
使用不会产生冲突的Windows的power Shell或Git BASH。

Network node options

Specify which network node to use when deploying contracts and sending transactions. When a contract is compiled or deployed on a specific network node, the contract will be cached for subsequent use. When truffle's contract abstraction layer detects that you are connected to a certain network node, it will use the original cache contract on this network node to simplify the deployment process.

The following networks object uses a network name as the configuration key, and the value defines its network parameters. The corresponding option for networks is not required, but if specified, each network must define a corresponding network_id. If you want to declare a default network, you can do this by marking the value of netword_id as default. When no other network is matched, the default network will be used. It should be noted that there should be only one default network in the entire configuration. Generally speaking, the default network is mainly used when development, configuration, contract and other data do not need to be saved for a long time, and the network ID will also change frequently due to the restart of TestRPC.

The network node name is used when called through the user interface. For example, use the –network option when deploying a contract to specify the network node to use:

$ truffle migrate --network live

live is a network node defined in truffle.js:

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.
  }
}

For each configured network node, when the following transaction parameters are not explicitly set, their default values ​​are used:

gas:部署合约的油耗上限,默认值:4712388
gasPrice:部署合约时的油价,默认值:100000000000 wei,即100 shannon
from:执行迁移脚本时使用的账户,默认使用节点旳第一个账户
provider:默认的provider使用host和port选项构造:new Web3.providers.HttpProvider("http://host:port")

For each network node configured, you can set provider or host/port, but not both. If you need an HTTP provider, it is recommended to use the host/port option. If you need a custom provider, such as HDWalletProvider, you must use the provider option.
Contract compilation output directory option: CONTRACTS_BUILD_DIRECTORY

The default output directory for contract compilation is ./build/contracts in the project root directory, but this can be modified using the contracts_build_directory configuration item in the configuration file.

For example, you can store the contract compilation results in the ./output/contracts directory in the project root directory:

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

The contract components generated by compilation do not need to be placed in the project directory, for example:

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

Of course, you can also use absolute paths, but we do not recommend this because if you build your project on other machines, the absolute path you set may not be found. If you use an absolute path under Windows, remember to escape the backslash, for example: C:\Users\Username\output.
Testing framework options: MOCHA

Use mocha options to pass in the configuration options required by the MochaJS test framework, for example:

mocha: {
    
    
  useColors: true
}

For the configuration content of mochajs, please refer to its official documentation.
solidity compile option: SOLC

Use the solc option to pass in the options required by the solidity compiler, for example:

solc: {
    
    
  optimizer: {
    
    
    enabled: true,
    runs: 200
  }
}

Guess you like

Origin blog.csdn.net/leaning_java/article/details/125441383