[英]How to check if the token on opensea is ERC721 or ERC1155 using node.js
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):
ERC721: https ://opensea.io/assets/0x5e8bbe6b1a1e9aa353218c5b6693f5f7c5648327/1080
在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。 因此,我们可以使用supportsInterface
的EIP165检查合同是否是ERC721或ERC1155。
ERC1155 的接口 id 是0xd9b67a26
,而0xd9b67a26
的接口是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);
});
上述解决方案需要连接到Ethereum node
例如 infura 才能工作。
如果有更好的解决方案,请赐教。
谢谢。
编辑:我发现 OpenSea 提供 API 供您检查。 这是链接https://docs.opensea.io/reference/retrieving-a-single-contract
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.