发布于 2026-01-06 2 阅读
0

如何在 Opensea 上铸造 10000 个 NFT?就是这样!!

如何在 Opensea 上铸造 10000 个 NFT

就是这样!!

铸币至 Opensea

创建完所有 NFT 之后,最困难的一步是在没有合约的情况下,将大量的 NFT 铸造到 Opensea 平台。

过程 GIF

为了实现这一点,我会分享我的代码库,它使用 Puppeteer 来自动化 NFT 的铸造过程。你只需要一个 images 文件夹文件和要铸造的 NFT 名称即可。下面我会教你如何使用这个代码库。

如果您想在铸币时间上添加其他属性,请给我发电子邮件,我们可以讨论一下oi@manel.dev

安装软件包

首先,我们需要安装运行脚本所需的软件包,在您的项目上运行:

yarn add @chainsafe/dappeteer puppeteer esbuild esbuild-register
Enter fullscreen mode Exit fullscreen mode

@chainsafe/dappeteer作用是自动连接 Metamask 来铸造 NFT。

我们将使用它puppeteer来上传图片和填写信息。

我们将使用esbuildandesbuild-register来运行我们的脚本。

创建脚本

让我们开始创建一个名为 file 的文件,script.ts进行首次导入并添加你的第一个变量。

脚本.ts

import puppeteer, { Page } from 'puppeteer';
import dappeteer from '@chainsafe/dappeteer';
import fs from 'fs';

const collectionName = "Your Collection Name"
const collectionURL = `https://opensea.io/collection/${collectionName}/assets/create`
const openseaDescription = `Your description here`
const lockedContent = `Locked content text here`
Enter fullscreen mode Exit fullscreen mode

连接钱包功能

因此,为了在脚本打开浏览器时选择 Metamask 钱包,让我们创建一个函数来点击 Metamask 按钮并连接到钱包。

脚本.ts

const connectWallet = async (page: Page, metamask) => {
  const button = await page.$('button.dBFmez:first-child');
  await button.click();

  await metamask.approve();

  return;
}
Enter fullscreen mode Exit fullscreen mode

创建上传图像功能

现在,为了在创建 NFT 页面上上传图像,让我们创建一个函数,该函数接收页面和文件,然后获取 HTML 中的输入元素并将图像上传到该元素。

脚本.ts

const uploadImage = async (page: Page, file: string) => {
  const elementHandle = await page.$("#media");
  await elementHandle.uploadFile(`images/${file}`);

  return;
}
Enter fullscreen mode Exit fullscreen mode

创建页面超时函数

此功能旨在给我们的脚本留出时间,以便在应用程序中执行填写或点击操作。

脚本.ts

const pageTimeout = async (time: number, page: Page) => {
  await page.waitForTimeout(time)
  return;
}
Enter fullscreen mode Exit fullscreen mode

创建填充字段函数

这个函数会从创建 NFT 页面获取每个字段并填充它们。

此功能的操作步骤如下:
1. 填写字段名称;
2. 填写字段输入内容;
3. 启用可解锁内容并填写可解锁内容文本;
4. 选择多边形链。

脚本.ts


const fillFields = async (page: Page, fileName: string) => {

// Get and fill in the input name
  await page.focus('#name')
  await page.keyboard.type(fileName)

  await pageTimeout(1000, page)

//Get and fill in the input name
  await page.$eval('#description', (el, value) => el.value = value, openseaDescription);
  await page.focus('#description')
  await page.keyboard.type(' ')

  await pageTimeout(1000, page)

// Click on the unlockable content checkbox
  await page.evaluate(() => {
    document.querySelector("#unlockable-content-toggle").parentElement.click();
  });

  await pageTimeout(1000, page)

// Fill in the unlockable content text
  await page.$eval('textarea[placeholder="Enter content (access key, code to redeem, link to a file, etc.)"]', (el, value) => el.value = value, lockedContent);
  await page.focus('textarea[placeholder="Enter content (access key, code to redeem, link to a file, etc.)"]')
  await page.keyboard.type(' ')

// Open the select chain input
  const input = await page.$("#chain")
  input.click()

  await pageTimeout(1000, page)

// Select the polygon chain
  await page.click('img[src="/static/images/logos/polygon.svg"]')

  return;
}
Enter fullscreen mode Exit fullscreen mode

创建主函数

在这个函数中,我们将创建运行脚本所需的所有主要函数,步骤如下:

1. 运行 dappeteer 以在 Opensea 上设置 Metamask。2
. 从 images 文件夹中获取文件。3
. 删除第一个文件 (.DS_Store) - 仅适用于 macOS。4
. 打开收藏集的创建资产页面。5
. 运行连接钱包功能
。6. 对 images 文件夹中的每张图片运行循环(逐个创建资产)。

请看下面代码的逐步执行步骤:

脚本.ts

(async () => { // Async function because we need promises to do it
  const browser = await dappeteer.launch(puppeteer, { metamaskVersion: 'v10.1.1' }); // Launch the browser with metamask
  const metamask = await dappeteer.setupMetamask(browser, { seed: "Secret phase here"}); // Add your secret phase here to metamask connect with your account
  const files = await fs.promises.readdir("images/"); // Get an List with all images on images folder
  files.shift() // WARN: just on macOS: remove the first file .DS_Store 

// Open the create assets url of the  collection
  const page = await browser.newPage();
  await page.goto(collectionURL);

// Get the tabs and close the first tab openned by the dappeteer
  const firstTabs = await browser.pages()
  await firstTabs[0].close()

  await pageTimeout(2000, page)

// Run our function to click on the Metamask button in the Opensea
  await connectWallet(page, metamask)

// Start the loop on each image of images folder
  for (let i = 0; i <= files.length ; i++) {
    const tabs = await browser.pages() // Get the tabs on each loop
    const data = {
      name: `Your Asset name here #${1 + i}`, // Add your NFT name (the count start on 1 and stop on the quantity of the files)
    }

// At the first time on loop you need to do an sign to create the assets 
    if(i === 0) {
      await tabs[1].bringToFront() // Move one tab
      await tabs[1].goto(collectionURL) // Change the page to collection url again

      await pageTimeout(2000, page)

      await metamask.sign() // Use the metamask to do the transaction
      await metamask.page.waitForTimeout(2000) // wait for the transaction
    }

// Now if not the first time, after creating the first NFT just open the create assets page again to create the second NFT and so sequentially.
    if(i === 0) {
      await tabs[1].bringToFront()
      await tabs[1].goto(collectionURL)
    } else {
      await tabs[1].bringToFront()
      await tabs[1].goto(collectionURL)
    }

    await pageTimeout(2000, page)

// Upload the current image file
    await uploadImage(page, files[i]);

// Fill the fields using the asset name with the count
    await fillFields(page, data.name);

// Click on create asset button
    const button = await page.$('.AssetForm--action button');
    await button.click()

    await pageTimeout(4000, page)

// Rename the image name to know if already is completed
    fs.renameSync(`images/${files[i]}`, `images/completed-${files[i]}`)

    console.log({ message: `Mint the NFT: ${data.name}`, fileName: files[i]})
    console.log(`Mint the NFT: ${data.name}`)
  }

  console.log('Minted all NFTs with success')
})();
Enter fullscreen mode Exit fullscreen mode

准备就绪!现在让我们配置一下,package.json用一行代码运行脚本。

package.json

{
  "name": "node",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@chainsafe/dappeteer": "^2.2.0",
    "dappeteer": "^1.0.0",
    "esbuild": "^0.13.10",
    "esbuild-register": "^3.0.0",
    "puppeteer": "^10.4.0"
  },
  "scripts": {
    "es": "node -r esbuild-register"
  }
}
Enter fullscreen mode Exit fullscreen mode

现在,只需在命令行界面 (CLI) 中运行脚本即可:

yarn es ./src/script.ts
Enter fullscreen mode Exit fullscreen mode

下载代码库

首先,你需要从这里下载我的代码库,其中包含铸造 NFT 的脚本。

在这里为我的仓库点赞

完成了,现在操纵者会一个接一个地快速创建你所有的 NFT。

就是这样!!

在Twitter上关注我
我的GitHub

文章来源:https://dev.to/emanuelferreira/how-to-mint-10000-nfts-on-opensea-487c