0

I want to programmatically mint a NFT on Sea Drop on OpenSea.

Here is a sample contract that I deployed to test out mint function Sepolia Etherscan

Here is the mint page OpenSea

If I switch to Write contract tab on Etherscan to directly interact with the smart contract, I can see a mint function listed in ABI, which is called mintSeaDrop, and it takes two arguments (minter and quantity)

With that function, I wrote a simple script to mint that NFT, but the result transaction always gets reverted!

const ethers = require("ethers");

const abi = [
  {
    inputs: [
      { internalType: "address", name: "minter", type: "address" },
      { internalType: "uint256", name: "quantity", type: "uint256" },
    ],
    name: "mintSeaDrop",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function",
  },
];

const ca = "0x3be7fe2ebb70b38d2df12e83e31d8b859a6607a0";
const provider = new ethers.JsonRpcProvider("https://rpc-sepolia.rockx.com");
const wallet = new ethers.Wallet(privateKey, provider);

const contract = new ethers.Contract(ca, abi, wallet);
const minter = wallet.address;
const quantity = 1;

async function mintToken() {
  try {
    const tx = await contract.mintPublic(ca, minter, minter, quantity);
    await tx.wait();
    console.log("Minted token");
  } catch (error) {
    console.error("Error minting token", error);
  }
}

mintToken();

After a while of researching, I found out that when I click mint on OpenSea, MetaMask asks me to confirm to mint the NFT, I switch to Hexdecimal tab to check out, the function that actually gets called is Mint Public, takes 4 parameters (it is not listed in the ABI).

MetaMask Confirmation

The function Mint Public is in the file ISeaDrop.sol according to Etherscan, but I don't know how to call it from the ABI since it is not listed here.

Is there any advice?

1 Answer 1

0

You can get a hint regarding the 4 parameters being passed by decoding a previous transaction, like this:

enter image description here

enter image description here

Here's the revised code with the ABI of the mintPublic function, and passing the parameters while calling the function accordingly:

const ethers = require("ethers");
const privateKey = "PRIVATE_KEY"
const abi = [
    { "inputs": [{ "internalType": "address", "name": "nftContract", "type": "address" }, { "internalType": "address", "name": "feeRecipient", "type": "address" }, { "internalType": "address", "name": "minterIfNotPayer", "type": "address" }, { "internalType": "uint256", "name": "quantity", "type": "uint256" }], "name": "mintPublic", "outputs": [], "stateMutability": "payable", "type": "function" }
];

const seaDropContract = "0x00005EA00Ac477B1030CE78506496e8C2dE24bf5"
const nftContract = "0x3be7fe2ebb70b38d2df12e83e31d8b859a6607a0";
const provider = new ethers.JsonRpcProvider("https://rpc-sepolia.rockx.com");
const wallet = new ethers.Wallet(privateKey, provider);

const contract = new ethers.Contract(seaDropContract, abi, wallet);
const minter = wallet.address;
const feeRecipient = "0x0000a26b00c1F0DF003000390027140000fAa719";
const minterIfNotPayer = ethers.ZeroAddress;
const quantity = 1;

async function mintToken() {
    try {
        const tx = await contract.mintPublic(nftContract, feeRecipient, minterIfNotPayer, quantity);
        const receipt = await tx.wait();
        console.log(`Minted token at ${receipt.hash}`);
    } catch (error) {
        console.error("Error minting token", error);
    }
}

mintToken();

P.S. mintPublic function is inside the SeaDrop contract, not the NFT contract.


To push the gas for faster minting of the NFT, you can specify a higher gas price when sending the transaction. This can be done by adding a buffer to the existing gas price, and pass the total value as the gasPrice parameter to the transaction. Here's how you can modify your code to do that:

async function mintToken() {
    try {
        const gasPriceBuffer = ethers.toBigInt(100000000000); // Adjust the buffer as needed
        const gasData = await provider.getFeeData();
        const gasPrice = gasData.gasPrice + gasPriceBuffer;
        const tx = await contract.mintPublic(nftContract, feeRecipient, minterIfNotPayer, quantity, { gasPrice: gasPrice });
        const receipt = await tx.wait();
        console.log(`Minted token at ${receipt.hash}`);
    } catch (error) {
        console.error("Error minting token", error);
    }
}
5
  • Thank you, it works like a charm. By the way, what if I want to push the gas to fast mint the NFT, what should I do ? (Example: On Base, what are the gas price that I should set to fast mint the NFT?) Thanks in advance!
    – duyanhhz
    Commented Apr 14 at 8:46
  • basescan.org/tx/… You can take this as an example...
    – duyanhhz
    Commented Apr 14 at 8:48
  • @duyanhhz, I've added that to my answer as well. Please check. Commented Apr 14 at 9:39
  • Thanks a lot!!!! You are so kind sir 🙏🙏
    – duyanhhz
    Commented Apr 14 at 12:20
  • You're welcome, @duyanhhz. Happy to help. Commented Apr 14 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.