-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_opensea_assets.js
More file actions
40 lines (36 loc) · 1.23 KB
/
fetch_opensea_assets.js
File metadata and controls
40 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Fetch all assets in OpenSea belonging to the wallet address
*/
const fetchAssets = function(offset, previousResponse, address) {
if (address) {
return fetch(`https://api.opensea.io/api/v1/assets?owner=${address}&order_direction=desc&offset=${offset}&limit=20`)
.then(response => response.json())
.then(data => {
const newResponse = [...previousResponse, ...data['assets']]
if (data['assets'] && data['assets'].length > 0) {
offset = offset + 50;
return get_assets(offset, newResponse, address);
}
return newResponse;
})
}
}
const contractAddress = '<enter_contract_address_here';
const allAccountAssets = await fetchAssets(0, [], accounts[0])
const tokenIds = [];
/*
* Loops through assets of address and checks which are equal
* to the contractAddress specified. Stores all token IDs of
* the assets belonging to that contractAddress in an array
* named tokenIds.
*/
if (allAccountAssets) {
for (const asset of allAccountAssets) {
if (asset.assetContract && asset.assetContract.address) {
if (asset.assetContract.address.toLowerCase() === contractAddress.toLowerCase()) {
tokenIds.push(asset.token_id)
}
}
}
setTokenIds(tokenIds);
}