Last active
April 12, 2022 20:37
-
-
Save nalinbhardwaj/f4db24e2bdd22f3ad6ce345bdc9a2388 to your computer and use it in GitHub Desktop.
exgrasia Inventory Plugin
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
import BigNumber from "https://cdn.skypack.dev/bignumber.js"; | |
class Plugin { | |
name = "Inventory"; | |
description = | |
"Displays owned ERC20 and ERC721 that are enumerable + in tiles right now"; | |
FETCH_OWNED_TOKEN_BASE_URL = | |
"https://api-kovan-optimistic.etherscan.io/api?module=account&action=tokentx&startblock=0&endblock=999999999&sort=asc&apikey=YourApiKeyToken&address="; | |
FETCH_TOKEN_BALANCE_BASE_URL = | |
"https://api-kovan-optimistic.etherscan.io/api?module=account&action=tokenbalance&tag=latest&apikey=YourApiKeyToken&contractaddress="; | |
FETCH_OWNED_NFT_BASE_URL = "https://api-kovan-optimistic.etherscan.io/api?module=account&action=tokennfttx&startblock=0&endblock=999999999&sort=asc&apikey=YourApiKeyToken&address="; | |
constructor() { | |
console.log("Inventory initted"); | |
} | |
async fetchOwnedTokens(address) { | |
const explorer_url = this.FETCH_OWNED_TOKEN_BASE_URL + address; | |
const res = await fetch(explorer_url) | |
.then(async (response) => { | |
return await response.json(); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
return res; | |
} | |
async fetchOwnedNFTs(address) { | |
const explorer_url = this.FETCH_OWNED_NFT_BASE_URL + address; | |
const res = await fetch(explorer_url) | |
.then(async (response) => { | |
return await response.json(); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
return res; | |
} | |
sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async fetchTokenBalance(address, contractAddress) { | |
const fetch_url = | |
this.FETCH_TOKEN_BALANCE_BASE_URL + | |
contractAddress + | |
"&address=" + | |
address; | |
console.log("fetch_url", fetch_url); | |
let res = await fetch(fetch_url) | |
.then(async (response) => { | |
return await response.json(); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
if (res['result'].includes("rate")) { | |
await this.sleep(1500); | |
res = await fetch(fetch_url) | |
.then(async (response) => { | |
return await response.json(); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
} | |
return res; | |
} | |
address(str) { | |
let ret = str.toLowerCase(); | |
if (ret.slice(0, 2) === "0x") { | |
ret = ret.slice(2); | |
} | |
for (const c of ret) { | |
if ("0123456789abcdef".indexOf(c) === -1) | |
throw new Error("not a valid address"); | |
} | |
if (ret.length !== 40) throw new Error("not a valid address"); | |
return `0x${ret}`; | |
} | |
async render(coords) { | |
if (gm.getTiles()[coords.x][coords.y].smartContractMetaData.emoji !== "") { | |
return { __html: "" }; | |
} | |
const res = await gm.getPlayerInfos().then(async (playerInfos) => { | |
for (const playerAddr of playerInfos.keys()) { | |
const playerInfo = playerInfos.get(playerAddr); | |
if ( | |
playerInfo.coords.x === coords.x && | |
playerInfo.coords.y === coords.y | |
) { | |
const ownedTokens = await this.fetchOwnedTokens( | |
playerInfo.proxyAddress | |
); | |
console.log("ownedTokens", ownedTokens); | |
var res = ""; | |
const seenTokens = []; | |
for (const ownedToken of ownedTokens['result']) { | |
if(!ownedToken['contractAddress']) continue; | |
if (seenTokens.includes(ownedToken['contractAddress'])) { | |
continue; | |
} | |
const balanceReturn = await this.fetchTokenBalance( | |
playerInfo.proxyAddress, | |
ownedToken['contractAddress'] | |
); | |
console.log('balanceReturn', balanceReturn['result']); | |
console.log('tokenDecimal', ownedToken['tokenDecimal']); | |
seenTokens.push(ownedToken['contractAddress']); | |
const cleanBalance = BigNumber(balanceReturn['result']).div( | |
BigNumber(10).pow(BigNumber(ownedToken['tokenDecimal'])) | |
); | |
res += `<div style='margin-bottom:1em;'><i>${ | |
ownedToken['tokenName'] | |
} (${ | |
ownedToken['tokenSymbol'] | |
})</i>: ${cleanBalance.toString()}</div>`; | |
} | |
const ownedNFTs = await this.fetchOwnedNFTs(playerInfo.proxyAddress); | |
const addrToToken = {}; | |
const ownedNFTAddrs = []; | |
for (const ownedNFT of ownedNFTs['result']) { | |
if (!ownedNFT['contractAddress']) continue; | |
ownedNFTAddrs.push(this.address(ownedNFT['contractAddress'])); | |
addrToToken[ownedNFT['contractAddress']] = ownedNFT; | |
} | |
const fetchedNFTs = await gm.getTileNFTs(ownedNFTAddrs, this.address(playerInfo.proxyAddress)); | |
fetchedNFTs.forEach((value, key) => { | |
console.log('value', value); | |
console.log('key', key); | |
res += `<div style='margin-bottom:1em;'><i>${addrToToken[key]['tokenName']} (${addrToToken[key]['tokenSymbol']})</i>:</div>`; | |
for (const nft of value) { | |
const json = atob(nft.substring(29)); | |
const result = JSON.parse(json); | |
res += `<div style='margin-bottom:1em;'><img src="${result.image}" alt="NFT" /></div>` | |
} | |
}); | |
if (res === "") return { __html: "" }; | |
return { __html: `<div>${res}</div>` }; | |
} | |
} | |
return { __html: "" }; | |
}); | |
return res; | |
} | |
destroy() { | |
console.log("destroyed"); | |
} | |
} | |
export default Plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment