可能会迟到回答这个问题,但我遇到了类似的问题。如果使用 API,您只能访问有限数量 (50) 的资产。
使用您链接到的页面上引用的 API,您可以执行 for 循环来获取范围内集合的资产。例如,使用 Python:
import requests
def get_asset(collection_address:str, asset_id:str) ->str:
url = "https://api.opensea.io/api/v1/assets?token_ids="+asset_id+"&asset_contract_address="+collection_address+"&order_direction=desc&offset=0&limit=20"
response = requests.request("GET", url)
asset_details = response.text
return asset_details
#using the Dogepound collection with address 0x73883743dd9894bd2d43e975465b50df8d3af3b2
collection_address = '0x73883743dd9894bd2d43e975465b50df8d3af3b2'
asset_ids = [i for i in range(10)]
assets = [get_asset(collection_address, str(i)) for i in asset_ids]
print(assets)
对我来说,我实际上使用了 Typescript,因为这就是 opensea 用于他们的 SDK ( https://github.com/ProjectOpenSea/opensea-js ) 的内容。它更加通用,允许您自动对资产进行报价、购买和销售。无论如何,这是您如何在 Typescript 中获取所有这些资产的方法(您可能需要比下面引用的更多的依赖项):
import * as Web3 from 'web3'
import { OpenSeaPort, Network } from 'opensea-js'
// This example provider won't let you make transactions, only read-only calls:
const provider = new Web3.providers.HttpProvider('https://mainnet.infura.io')
const seaport = new OpenSeaPort(provider, {
networkName: Network.Main
})
async function getAssets(seaport: OpenSeaPort, collectionAddress: string, tokenIDRange:number) {
let assets:Array<any> = []
for (let i=0; i<tokenIDRange; i++) {
try {
let results = await client.api.getAsset({'collectionAddress':collectionAddress, 'tokenId': i,})
assets = [...assets, results ]
} catch (err) {
console.log(err)
}
}
return Promise.all(assets)
}
(async () => {
const seaport = connectToOpenSea();
const assets = await getAssets(seaport, collectionAddress, 10);
//Do something with assets
})();
最后要注意的是,正如您所说,他们的 API 是速率受限的。因此,您只能在一个时间范围内对他们的 API 进行一定数量的调用,然后才会出现讨厌的 429 错误。所以要么找到绕过速率限制的方法,要么在你的请求上设置一个计时器。