Hardhat-Deploy

對於hardhat-deploy,你在哪裡給建構子添加額外的參數?

  • February 20, 2022

使用https://hardhat.org/plugins/hardhat-deploy.html,如何在部署程式碼中獲取額外的值?例如,給定範常式式碼:

// deploy/00_deploy_my_contract.js
module.exports = async ({getNamedAccounts, deployments}) => {
 const {deploy} = deployments;
 const {deployer} = await getNamedAccounts();
 await deploy('MyContract', {
   from: deployer,
   args: ['Hello'],
   log: true,
 });
};
module.exports.tags = ['MyContract'];

是否有一個標準的安全帽部署系統可以用動態的東西替換靜態的“Hello”?例如 getNamedAccounts 之類的東西,但不是專門針對帳戶的。或者這只是您使用標準 Javascript 工具將值輸入程式碼的那種事情?

或者這只是您使用標準 Javascript 工具將值輸入程式碼的那種事情?

是的,正是這個。這是JS。如果您需要程式/動態值而不是靜態值,請編寫 JS,例如:

   args: [Math.random()+""],

如果您出於某種原因不想在 JS 中編寫它,則將其作為字元串並使用您選擇的工具動態輸出 JS。

您可以使用部署腳本來指定部署邏輯

export interface DeployOptions = {
 from: string; // address (or private key) that will perform the transaction. you can use `getNamedAccounts` to retrived the address you want by name.
 contract?: // this is an optional field. If not specified it defaults to the contract with the same name as the first parameter
   | string // this field can be either a string for the name of the contract
   | { // or abi and bytecode
       abi: ABI;
       bytecode: string;
       deployedBytecode?: string;
     };
 args?: any[]; // the list of argument for the constructor (or the upgrade function in case of proxy)
 skipIfAlreadyDeployed?: boolean; // if set it to true, will not attempt to deploy even if the contract deployed under the same name is different
 log?: boolean; // if true, it will log the result of the deployment (tx hash, address and gas used)
 linkedData?: any; // This allow to associate any JSON data to the deployment. Useful for merkle tree data for example
 libraries?: { [libraryName: string]: Address }; // This let you associate libraries to the deployed contract
 proxy?: boolean | string | ProxyOptions; // This options allow to consider your contract as a proxy (see below for more details)

 // here some common tx options :
 gasLimit?: string | number | BigNumber;
 gasPrice?: string | BigNumber;
 value?: string | BigNumber;
 nonce?: string | number | BigNumber;

 estimatedGasLimit?: string | number | BigNumber; // to speed up the estimation, it is possible to provide an upper gasLimit
 estimateGasExtra?: string | number | BigNumber; // this option allow you to add a gas buffer on top of the estimation

 autoMine?: boolean; // this force a evm_mine to be executed. this is useful to speed deployment on test network that allow to specify a block delay (ganache for example). This option basically skip the delay by force mining.
 deterministicDeployment? boolean | string; // if true, it will deploy the contract at a deterministic address based on bytecode and constuctor arguments. The address will be the same across all network. It use create2 opcode for that, if it is a string, the string will be used as the salt.
};

args上述程式碼段中的欄位用於建構子的參數列表(或代理情況下的升級函式)

引用自:https://ethereum.stackexchange.com/questions/102709