配置松露“发件人”地址的最佳方式

Best way to configure truffle `from` address

我正在设置我的 truffle 配置文件,我正在从这样的环境变量中设置 from 地址:

module.exports = {
  networks: {
    local: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
      from: process.env.OWNER,
    }
  }
};

那我运行OWNER=<address> truffle migrate --network local

有什么更好的方法可以让 truffle 使用 ganache 生成的第一个地址吗?

如果您在 truffle.cfg 中省略 from 参数,它将自动默认为 web3.eth.getAccounts 从您所连接的提供商返回的第一个帐户。

如果您想对所使用的帐户进行更多动态控制,可以使用 deployer 进行控制。

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

module.exports = function(deployer, network, accounts) {
  deployer.deploy(SimpleContract, { from: accounts[1] }); // Deploy contract from the 2nd account in the list
  deployer.deploy(SimpleContract, { from: accounts[2] }); // Deploy the same contract again (different address) from the 3rd account.
};

当然,您不必使用传入的帐户列表,您可以从任何其他数据源中提取列表。如果你想拥有特定于环境的逻辑,你也可以使用 network