簡體   English   中英

Ethers.js 合約工廠

[英]Ethers.js Contract Factories

我正在嘗試在 ethers.js 上創建一個 ContractFactory,並通過 Alchemy 在 Polygon 的 tes.net Mumbai 上部署智能合約。

我在部署 function 時遇到問題,因為在文檔中不清楚如何格式化 arguments。

作為 Alchemy 文檔,我必須指定gasLimitgasPrice ,但我還想為我的合約構造函數指定自定義 arguments 。

我得到的錯誤:

  • 如果我在部署 function 中只輸入構造函數的參數,它表示我沒有指定gasLimitgasPrice參數
  • 如果我在部署 function 中放入構造函數和gasLimitgasPrice的參數,由於參數的順序,智能合約的構造函數出現問題。
  • 如果我只gasLimitgasPrice ,它會正確部署,但我沒有我想要的自定義智能合約,因為缺少構造函數的參數;

這是部署智能合約的一段代碼:

const price_unit = "gwei";
const contractFile = await fs.readFileSync('artifacts/contracts/Midly.sol/NFTCollectible.json');
const contract = JSON.parse(contractFile.toString());

const provider = new ethers.providers.AlchemyProvider("maticmum", process.env.ALCHEMY_API_KEY);
const wallet = new ethers.Wallet(process.env.WALLET_PRIVATE_KEY, provider);
const price = ethers.utils.formatUnits(await provider.getGasPrice(), price_unit);

const factory = new ethers.ContractFactory(contract.abi, contract.bytecode, wallet);

const deployedContract = await factory.deploy({
  gasLimit: 2,
  gasPrice: ethers.utils.parseUnits(price, price_unit),
}, tokenURI, maxQuantity, cost)
.then(data => console.log(data))
.catch(error => console.log(error));

謝謝你的時間:)

您需要首先傳遞構造函數 arguments,最后傳遞overrides object。

const deployedContract = await factory.deploy(tokenURI, maxQuantity, cost, {
  gasLimit: 2,
  gasPrice: ethers.utils.parseUnits(price, price_unit),
})

文檔: https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory-deploy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM