1

您好,我正在尝试使用 Polygon 铸造一个 NFT,它在孟买运行得很好,但是一旦我切换到主网,交易就不会通过,而是在 5 秒内通过孟买。即使我使用的是刚刚部署在主网上而不是孟买的完全相同的合约,并且代码也是相同的。我所做的只是切换合约地址和 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,
        );

用于永久且几乎即时的交易的内存池浏览器。永远: 永远服用 即时: 快速地 主网上使用 30 gwei 的气体: 30 格威 有人知道为什么会这样吗?另外,是的,我确实知道快速的气体确实有 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;
    }
}
4

1 回答 1

0

您可以使用简单的代码进行测试,以创建一个 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);
});
于 2021-10-21T12:58:14.133 回答