多边形交易在孟买运行良好,但在主网上却不行
Posted
技术标签:
【中文标题】多边形交易在孟买运行良好,但在主网上却不行【英文标题】:Polygon transaction working just fine on Mumbai but not on Mainnet 【发布时间】:2021-11-13 06:22:12 【问题描述】:您好,我正在尝试使用 Polygon 铸造一个 NFT,它在孟买运行得很好,但是一旦我切换到主网,交易就不会通过,而是在 5 秒内通过孟买。即使我使用的是刚刚部署在主网上而不是 Mumbai 上的完全相同的合约,并且代码也是相同的。我所做的只是切换合约地址和 rpc url,但由于某种原因,它在 Polygon 主网上不起作用,下面是我使用的代码。
// Init contract
const contractABI = require('../../contract-abi.json');
const contractAddress = config.mintingContractAddress;
const contract = await new this.web3.eth.Contract(contractABI, contractAddress);
// Mint NFT
const nft = contract.methods.mintNFT(user.walletAddress, metadataUploadURL, user.paymentAddress).encodeABI();
// Get gas pricing
const priorityFees = await axios.get('https://gasstation-mainnet.matic.network');
const estBaseGas = await this.web3.eth.estimateGas(
data: nft,
to: contractAddress,
);
console.log('USING GAS: ' + estBaseGas);
// Sign NFT minting transaction
const totalGas = estBaseGas + priorityFees.data.standard;
console.log('TOTALGAS: ', Math.round(totalGas).toString());
const transaction = await this.web3.eth.accounts.signTransaction(
from: user.walletAddress,
to: contractAddress,
nonce: await this.web3.eth.getTransactionCount(user.walletAddress, 'pending'), // Get count of all transactions sent to the contract from this address including pending ones
data: nft,
// maxPriorityFee: priorityFees.data.average, Not supported on Polygon MATIC yet
gas: Math.round(totalGas).toString(),
gasPrice: await this.web3.eth.getGasPrice(),
,
wallet.privateKey,
);
this.logger.silly('Finished signing NFT transaction');
// Send the transaction that we signed
const mintT = await this.web3.eth.sendSignedTransaction(transaction.rawTransaction);
this.logger.silly('Sent transaction');
console.log(mintT);
也试过这个来签名
// Get gas pricing
const priorityFees = await axios.get('https://gasstation-mainnet.matic.network');
const estBaseGas = await this.web3.eth.estimateGas(
data: nft,
to: contractAddress,
);
console.log('USING GAS: ' + estBaseGas);
// Sign NFT minting transaction
const totalGas = estBaseGas + priorityFees.data.standard;
console.log('TOTALGAS: ', Math.round(totalGas).toString());
console.log('P', priorityFees.data.standard);
const gp = this.web3.utils.toWei(priorityFees.data.standard.toString(), 'Gwei').toString();
console.log('GP', gp);
const transaction = await this.web3.eth.accounts.signTransaction(
from: user.walletAddress,
to: contractAddress,
nonce: await this.web3.eth.getTransactionCount(user.walletAddress, 'pending'), // Get count of all transactions sent to the contract from this address including pending ones
data: nft,
// maxPriorityFee: priorityFees.data.average, Not supported on Polygon MATIC yet
gas: '1000000',
gasPrice: gp,
,
wallet.privateKey,
);
Mempool 浏览器,用于需要永久且几乎即时的交易。 永远: 立即的: 主网上的一个使用 30 gwei 的 gas: 有谁知道为什么会这样? 另外,是的,我知道快速的气体确实有 2 个额外的 gwei,但即使手动设置它仍然需要很长时间,根据https://polygonscan.com/gastracker,即使有 1 个 gwei,它也应该在 30 秒内处理。即使使用 50 Gwei,它似乎也需要几个小时来处理,或者它可能被丢弃了?交易似乎甚至没有到达合同,它们只是卡在链中的某个地方。 合约地址:0xa915E82285e6F82eD10b0579511F48fD716a2043
合约源码:
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721URIStorage
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
event MintedNFT(address recipent,string tokenURI,address artist, uint256 tokenID);
mapping(uint256 => address) private artists; // Used to store token ids => artist addresses
// mapping(uint256 => uint256) private royalties; // tokenId => royaltyPercentage
// mapping(uint256 => address) private nftMintInitators; // Used to store token ids => sender addresses
// mapping(uint256 => bool) private royaltiesSet;
constructor(string memory name_, string memory symbol_)
ERC721(name_, symbol_)
// // Support for https://eips.ethereum.org/EIPS/eip-2981
// /// @notice Called with the sale price to determine how much royalty
// // is owed and to whom.
// /// @param _tokenId - the NFT asset queried for royalty information
// /// @param _salePrice - the sale price of the NFT asset specified by _tokenId
// /// @return receiver - address of who should be sent the royalty payment
// /// @return royaltyAmount - the royalty payment amount for _salePrice
// function royaltyInfo(
// uint256 _tokenId,
// uint256 _salePrice
// ) external view returns (
// address receiver,
// uint256 royaltyAmount
// )
// return (
// artists[_tokenId],
// _salePrice * royalties[_tokenId] // Take percentage
// );
//
// function updateRoyaltyPercentage(
// uint256 royaltyPercentage, // In decimal like 0.5 or 0.25 (Send 0.0 for no royalties)
// uint256 tokenID
// ) public
// if (msg.sender == nftMintInitators[tokenID] && royaltiesSet[tokenID] == false)
// royalties[tokenID] = royaltyPercentage;
// royaltiesSet[tokenID] = true;
//
//
function mintNFT(address recipient,
string memory tokenURI,
address artist // Address for the artist not using _msgSender() because this transaction is sent by the users NFT holding account
)
public
returns (uint256)
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
artists[newItemId] = artist;
// nftMintInitators[newItemId] = msg.sender;
// royaltiesSet[newItemId] = false;
emit MintedNFT(recipient,tokenURI,artist,newItemId);
return newItemId;
【问题讨论】:
请编辑您的问题并分享合约源代码(包括构造函数参数值,如果有的话)或在blockchain explorer 上进行验证。mintNFT()
可能由于某些依赖调用而在主网上失败(这可能在测试网上通过,但在主网上失败)。
@PetrHejda 确定添加
【参考方案1】:
您可以使用简单的代码进行测试,以创建一个 NFT。将 gasPrice 和 gasLimit 参数直接添加到铸币函数中会有所帮助
const hre = require("hardhat");
async function main()
const NFT = await hre.ethers.getContractFactory("NFTNAME");
const WALLET_ADDRESS = "0xxxxxxxxxxxxxx"
const CONTRACT_ADDRESS = "0xa915E82285e6F82eD10b0579511F48fD716a2043"
const contract = NFT.attach(CONTRACT_ADDRESS);
mintedNFT = await contract.mintNFT(WALLET_ADDRESS, gasLimit: 285000, gasPrice: ethers.utils.parseUnits('30', 'gwei'));
console.log("NFT minted:", mintedNFT);
main().then(() => process.exit(0)).catch(error =>
console.error(error);
process.exit(1);
);
【讨论】:
以上是关于多边形交易在孟买运行良好,但在主网上却不行的主要内容,如果未能解决你的问题,请参考以下文章
合约未在 opensea 主网上收听,但在 metamask 中显示 NFT 并在测试网上工作
为啥我的文件/目录操作在 Windows 上运行良好,但在 Linux 上却不行?
UIStackView 在 iOS 12 中运行良好,但在 iOS 11 中却不行
应用程序在 iOS 8 中运行良好,但在 iOS 7 中却不行
Nuxt / Vue JS 页面在桌面和连接 WiFi 的移动设备上渲染良好,但在 4G/5G 设备上却不行。知道如何解决这个问题吗?