From 7d95c37d3096d54a8533259d198555814d3fc9db Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Sun, 1 Jan 2023 22:45:37 +0100 Subject: [PATCH] comments --- .DS_Store | Bin 10244 -> 10244 bytes app.js | 84 ++++++++++++++----------------------------------- utils/sleep.js | 3 ++ 3 files changed, 26 insertions(+), 61 deletions(-) create mode 100644 utils/sleep.js diff --git a/.DS_Store b/.DS_Store index 76ab250849d004172e82585a45fb63349e0172e9..e44b518f656d3fbacc2030416681ca81c8f28f28 100644 GIT binary patch delta 241 zcmZn(XbIS$DiFuBMVEnrfrUYjA)O(Up(Hoo#U&{xKM5$t5qlxHuW|hmM^yO~yz&JZ zhQZ1CxdlKy3=E9T!wh8Z;eb>yN93U setTimeout(resolve, ms)); -} - /** - * Upsert all NFTs from 0 to totalSupply + * Get all dNFTs for this sync version and insert them into the nfts table. */ async function insertNfts() { const lastVersion = await getLastVersion(); - const newVersion = lastVersion + 1; - console.log(`new version: ${newVersion}`); + const nextVersion = lastVersion + 1; + console.log(`next version: ${nextVersion}`); const totalSupply = await dNftContract.methods.totalSupply().call(); - - insertLatestVersion(newVersion); + insertNextVersion(nextVersion); for (let i = 0; i < totalSupply; i++) { // without this supabase will throw an error, because of too many requests - await sleep(10); - insertNft(i, newVersion); + await sleep(TIME_BETWEEN_NFT_INSERTIONS); + insertNft(i, nextVersion); } } /** - * Get the ens names of all NFT owners and store them in the nfts table - */ -async function upsertEnsNames() { - console.log("upserting ens names"); - let nfts = await supabase - .from("nfts") - .select("id, owner") - .eq("contractAddress", process.env.dNFT_ADDRESS) - .eq("version_id", lastSyncVersion); - - let owners = []; - nfts.data.map((nft) => { - owners.push(nft.owner); - }); - let ensNames = await getEnsName(owners); - - let ids2EnsName = []; - ensNames.map((ensName, i) => { - ids2EnsName.push({ - id: nfts.data[i].id, - ensName: ensName, - }); - }); - - const { error } = await supabase.from("nfts").upsert(ids2EnsName); - console.log(error); -} - -/** - * listen to sync events and insert all NFTs + * Listen to sync events. If a sync event is emitted, get all dNFTs and insert + * them into the nfts table. */ function subscribeToSync() { console.log("subscribing to sync"); @@ -134,16 +103,9 @@ function subscribeToSync() { "logs", { address: process.env.dNFT_ADDRESS, - topics: [ - "0xff14d8520387b9b85d2a017dc41ae15db04a22d4c67deac04eb45048631ffa86", - ], + topics: [SYNC_LOG_SIGNATURE], }, - function (error, result) { - insertNfts(); - // upsertEnsNames(); - - if (!error) console.log(result); - } + () => insertNfts() ); } diff --git a/utils/sleep.js b/utils/sleep.js new file mode 100644 index 0000000..6dcc6ac --- /dev/null +++ b/utils/sleep.js @@ -0,0 +1,3 @@ +export default async function sleep(ms) { + await new Promise((resolve) => setTimeout(resolve, ms)); +}