Polygon ERC20合约部署交易Pending问题排查求助
Polygon上Hardhat部署ERC20合约交易Pending问题排查
问题描述
我计划部署一个继承自OpenZeppelin ERC20的标准ERC20合约,导入方式如下:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"
使用Hardhat部署的代码如下:
const fees = await getGasStationFees(); // Convert to wei let maxPriorityFeePerGas = ethers.utils.parseUnits(fees.priority.toString(), "gwei"); let maxFeePerGas = ethers.utils.parseUnits(fees.fee.toString(), "gwei"); // OPTIONAL: enforce a tip minimum (important!) const tipFloor = ethers.utils.parseUnits("100", "gwei"); if (maxPriorityFeePerGas.lt(tipFloor)) { maxPriorityFeePerGas = tipFloor; // IMPORTANT: Adjust maxFeePerGas accordingly maxFeePerGas = maxFeePerGas.add(tipFloor.sub(ethers.utils.parseUnits(fees.priority.toString(), "gwei"))); } console.log(`[worker] current gas fees: maxPriorityFeePerGas=${ethers.utils.formatUnits(maxPriorityFeePerGas, 'gwei')} gwei, maxFeePerGas=${ethers.utils.formatUnits(maxFeePerGas, 'gwei')} gwei`); // Deploy const contract = await factory.deploy(name, symbol, scaledSupply,Number(decimals), { maxPriorityFeePerGas, maxFeePerGas } ); async function getGasStationFees() { const res = await fetch("https://gasstation.polygon.technology/v2"); const json = await res.json(); // e.g. choose "standard" tier return { maxPriorityFeePerGas: ethers.utils.parseUnits(json.standard.maxPriorityFee.toString(), "gwei"), maxFeePerGas: ethers.utils.parseUnits(json.standard.maxFee.toString(), "gwei") }; }
交易一直处于Pending状态,试过硬编码Gas费参数也无效,请问遗漏了什么配置?
排查与解决方案
Gas费参数匹配错误:你的
getGasStationFees函数返回的字段是maxPriorityFeePerGas和maxFeePerGas,但后续代码错误使用了不存在的fees.priority和fees.fee字段,且重复执行了parseUnits转换,导致Gas费计算完全错误。修正代码:// 替换原有Gas费转换代码 let maxPriorityFeePerGas = fees.maxPriorityFeePerGas; let maxFeePerGas = fees.maxFeePerGas;未等待交易上链确认:
factory.deploy仅发起交易,需等待交易确认才能确认部署状态,添加以下代码:await contract.deployed(); console.log("合约部署地址:", contract.address);Hardhat网络配置校验:检查
hardhat.config.js中Polygon网络配置是否正确,确保RPC URL可用、私钥有效、链ID匹配(主网137/测试网80001):module.exports = { networks: { polygon: { url: "https://polygon-rpc.com/", accounts: ["你的部署账户私钥"], chainId: 137 } } };RPC节点可靠性:若使用第三方RPC节点,切换至Alchemy、Infura等稳定节点;若使用本地节点,确认节点已完成同步。
账户余额检查:确认部署账户拥有足够MATIC支付Gas费,ERC20合约部署需消耗一定量的MATIC。
内容的提问来源于stack exchange,提问作者Alex Smith
相关产品推荐
相关产品推荐

