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


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)
});
我将得到schemaName(如果ERC721或ERC1155)从opensea空令牌:
  • ERC721:https://opensea.io/assets/0x5e8bbe6b1a1e9aa353218c5b6693f5f7c5648327/1080

  • ERC1155:https://opensea.io/assets/0x495f947276749ce646f68ac8c248420045cb7b5e/77145546951944958763741319904361551411909238433895885342936554025825874214913

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

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

根据EIP721和EIP1155,两者都必须实现EIP165。总而言之,EIP165所做的是允许我们检查契约是否实现了接口。https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified详细信息

根据eip, ERC721和ERC1155将实现EIP165。因此,我们可以使用EIP165的supportsInterface来检查合同是ERC721还是ERC1155。

ERC1155的接口id为0xd9b67a26, ERC721的接口id为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);
});

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

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

最新更新