Last active
July 17, 2024 08:49
-
-
Save earthchie/b19446821be63272b742cde57d8f4fb2 to your computer and use it in GitHub Desktop.
Retrieve all Tokens and NFTs of a specific wallet address on JBC network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getAssetsOfAddress(addr){ | |
const parseNFTMetadata = async function (metadata, ipfs_gateway = 'https://ipfs.8api.sh/ipfs/'){ | |
const applyIPFSGateway = function(uri){ | |
if(uri instanceof Array){ | |
return uri.map(i=>applyIPFSGateway(i)); | |
}else if(typeof uri === 'object'){ | |
Object.keys(uri).forEach(k=>{ | |
uri[k] = applyIPFSGateway(uri[k]); | |
}); | |
return uri; | |
}else if(typeof uri === 'string'){ | |
return uri.replace('ipfs://', ipfs_gateway); | |
}else{ | |
return uri; | |
} | |
}; | |
metadata = applyIPFSGateway(metadata); | |
try{ | |
const URI = new URL(metadata); | |
return applyIPFSGateway(await (await fetch(metadata)).json()); | |
}catch(e){ | |
return metadata; | |
} | |
}; | |
const res = await (await fetch("https://graph.jibchain.net/subgraphs/name/jbc/all", { | |
method: "POST", | |
headers: { | |
"content-type": "application/json" | |
}, | |
body: JSON.stringify({ | |
query: `{ | |
account(id: "${addr}") { | |
id | |
ERC20balances { | |
contract { | |
id | |
name | |
symbol | |
} | |
value | |
} | |
ERC721tokens { | |
contract { | |
id | |
name | |
symbol | |
} | |
uri | |
identifier | |
} | |
} | |
}` | |
}) | |
})).json(); | |
if(res.data){ | |
let data = res.data.account; | |
data.address = data.id; | |
for(let i = 0; i < data.ERC20balances.length; i++){ | |
Object.keys(data.ERC20balances[i].contract).forEach(k=>{ | |
data.ERC20balances[i][k] = data.ERC20balances[i].contract[k]; | |
}); | |
data.ERC20balances[i].contract = data.ERC20balances[i].id; | |
data.ERC20balances[i].balance = data.ERC20balances[i].value; | |
delete data.ERC20balances[i].id; | |
delete data.ERC20balances[i].value; | |
} | |
for(let i = 0; i < data.ERC721tokens.length; i++){ | |
data.ERC721tokens[i].metadata = await parseNFTMetadata(data.ERC721tokens[i].uri); | |
data.ERC721tokens[i].tokenId = data.ERC721tokens[i].identifier; | |
Object.keys(data.ERC721tokens[i].contract).forEach(k=>{ | |
data.ERC721tokens[i][k] = data.ERC721tokens[i].contract[k]; | |
}) | |
data.ERC721tokens[i].contract = data.ERC721tokens[i].id; | |
delete data.ERC721tokens[i].id; | |
delete data.ERC721tokens[i].identifier; | |
delete data.ERC721tokens[i].uri; | |
} | |
data.ERC20 = data.ERC20balances; | |
data.ERC721 = data.ERC721tokens; | |
delete data.ERC20balances; | |
delete data.ERC721tokens; | |
delete data.id; | |
return data; | |
}else{ | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example