本文介绍了多边形事务在孟买上运行良好,但在主网上运行不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试使用Polygon创建一个NFT,它在孟买运行得很好,但一旦我切换到主网,交易就不会进行,而是在孟买在5秒内完成。尽管我使用的是完全相同的合同,只是部署在主机上,而不是孟买,代码也是相同的。我所做的只是切换合同地址和RPC URL,但由于某种原因,它在Polygon Mainnet上不起作用。下面是我使用的代码。

// 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 Explorer for Transaction,耗时永远不变,几乎是瞬间完成。永远:即时:主网上的一个使用了30吉瓦的天然气:有人知道为什么会发生这种事吗?此外,是的,我知道快速的有两个额外的GWI的气体,但即使手动设置,它仍然需要永远,根据https://polygonscan.com/gastracker,即使有一个GWI,它应该在30秒内处理。即使使用50Gwei,似乎也要花几个小时才能处理,或者它可能正在被丢弃?交易似乎甚至没有达成合同,它们只是卡在了链条上的某个地方。联系地址: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;
    }
}

推荐答案

您可以使用简单的代码进行测试,只需生成一个NFT。将gas Price和gas Limit参数直接添加到minting函数可以帮助

 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);
});

这篇关于多边形事务在孟买上运行良好,但在主网上运行不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:36