如何使用node.js检查opensea上的代币是ERC721还是ERC1155

azv*_*ast 7 python node.js solidity web3py opensea

const { OpenSeaPort, Network } = require("opensea-js");

const offer = await seaport.createBuyOrder({
      asset: {
        tokenId,
        tokenAddress,
        schemaName
      },
      accountAddress: WALLET_ADDRESS,
      startAmount: newOffer / (10 ** 18),
      expirationTime: Math.round(Date.now() / 1000 + 60 * 60 * 24)
    });
Run Code Online (Sandbox Code Playgroud)

我将从opensea 空令牌中获取schemaName (如果是 ERC721 或 ERC1155):

在 opensea 的“详细信息”面板中,我可以看到合约模式名称,如下所示:Token Standard: ERC-1155

如何使用 node.js 或 python 从 opensea 令牌 url 获取模式名称?

小智 17

根据EIP721和EIP1155,它们都必须实现EIP165。综上所述,EIP165的作用就是让我们检查合约是否实现了该接口。有关https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified的详细信息

根据EIP,ERC721和ERC1155将实现EIP165。因此,我们可以使用supportsInterfaceEIP165的信息来检查合约是ERC721还是ERC1155。

ERC1155的接口id是0xd9b67a26,而ERC721的接口id是0x80ac58cd。接口id的计算方式可以查看EIP165提案。

下面是代码示例。

import Web3 from "web3";
import dotenv from "dotenv";
dotenv.config();
var web3 = new Web3(
  new Web3.providers.HttpProvider(process.env.RINKEBY_URL || "")
);

const ERC165Abi: any = [
  {
    inputs: [
      {
        internalType: "bytes4",
        name: "interfaceId",
        type: "bytes4",
      },
    ],
    name: "supportsInterface",
    outputs: [
      {
        internalType: "bool",
        name: "",
        type: "bool",
      },
    ],
    stateMutability: "view",
    type: "function",
  },
];

const ERC1155InterfaceId: string = "0xd9b67a26";
const ERC721InterfaceId: string = "0x80ac58cd";
const openSeaErc1155Contract: string =
  "0x88b48f654c30e99bc2e4a1559b4dcf1ad93fa656";
const myErc721Contract: string = "0xb43d4526b7133464abb970029f94f0c3f313b505";

const openSeaContract = new web3.eth.Contract(
  ERC165Abi,
  openSeaErc1155Contract
);
openSeaContract.methods
  .supportsInterface(ERC1155InterfaceId)
  .call()
  .then((res: any) => {
    console.log("Is Opensea", openSeaErc1155Contract, " ERC1155 - ", res);
  });

openSeaContract.methods
  .supportsInterface(ERC721InterfaceId)
  .call()
  .then((res: any) => {
    console.log("Is Opensea", openSeaErc1155Contract, " ERC721 - ", res);
  });

const myContract = new web3.eth.Contract(ERC165Abi, myErc721Contract);
myContract.methods
  .supportsInterface(ERC1155InterfaceId)
  .call()
  .then((res: any) => {
    console.log("Is MyContract", myErc721Contract, " ERC1155 - ", res);
  });

myContract.methods
  .supportsInterface(ERC721InterfaceId)
  .call()
  .then((res: any) => {
    console.log("Is MyContract", myErc721Contract, " ERC721 - ", res);
  });

Run Code Online (Sandbox Code Playgroud)

上述解决方案需要连接到以太坊节点(例如 infura)才能工作。

我发现OpenSea提供了API供您查看。这是链接https://docs.opensea.io/reference/retriving-a-single-contract