python - 如何使用 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)
});
我将从 opensea 空令牌中获取schemaName(如果是 ERC721 或 ERC1155):
在 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
,而 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
推荐阅读
- xml - 如何在 B2C 声明中传递有关引荐页面的信息
- java - 从带有 Maven 的 Visual Studio Code for Java 开始
- c++ - 在 main 之前调试对全局变量的赋值
- android - firebase 推送通知将数据传递给启动器
- ruby-on-rails - 使用 Iex-Ruby-Client Gem 配置 Ruby On Rails 应用程序
- imagemagick - 我可以使用 ImageMagick 获得单个字符的边界框吗?
- sql - 检查数据表中的出生日期是否即将到来
- continuous-integration - 用于持续集成的 Team City 构建配置
- python - 如何在 Python 中获取 GstBuffer 标志
- microservices - Api gateway、Service Regisry 和 Service Mesh,它们如何协同工作?