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

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

在 opensea 的 Details 面板中,我可以看到合约模式名称如下:Token Standard: ERC-1155

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

4

1 回答 1

5

我一直在关注这个问题以获得一个好的答案。然而,似乎没有人回答。因此,我给出了自己的解决方案。

根据 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 的接口是0x80ac58cd。您可以查看 EIP165 关于如何计算接口 id 的提案。

下面是代码示例。

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

上面的解决方案需要连接到Ethereum node诸如 infura 才能工作。

如果有更好的解决方案,请赐教。

谢谢。

编辑:我发现 OpenSea 提供 API 供您检查。这是链接https://docs.opensea.io/reference/retrieving-a-single-contract

于 2021-10-26T05:31:34.670 回答