From 97101f9151a796e52bc9974df2542f4994930738 Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Thu, 29 Dec 2022 13:39:28 +0100 Subject: [PATCH] insert nft per sync --- app.js | 30 +++++++++++++++++------------- package-lock.json | 28 ++++++++++++++-------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app.js b/app.js index bdab80e..7651afa 100644 --- a/app.js +++ b/app.js @@ -21,7 +21,7 @@ const dNftContract = new Contract(dNFT_ABI["abi"], process.env.dNFT_ADDRESS); * @param {i} index from 0 to totalSupply * @param {newVersion} version of the sync */ -async function upsertNft(i, newVersion) { +async function insertNft(i, newVersion) { console.log(i); const tokenId = await dNftContract.methods.tokenByIndex(i).call(); @@ -29,19 +29,19 @@ async function upsertNft(i, newVersion) { const owner = await dNftContract.methods.ownerOf(tokenId).call(); const _nft = { - id: tokenId, xp: nft.xp, deposit: nft.deposit, withdrawn: nft.withdrawn, + tokenId: tokenId, isLiquidatable: nft.isLiquidatable, owner: owner, contractAddress: process.env.dNFT_ADDRESS, version: newVersion, }; - console.log(`upsert ${tokenId}`); - const { error } = await supabase.from("nfts").upsert(_nft); - console.log(error); + console.log(`insert ${tokenId}`); + const { error } = await supabase.from("nfts").insert(_nft); + console.log("insert", error); } /** @@ -73,23 +73,27 @@ async function insertLatestVersion(newVersion) { console.log(error); } +async function sleep(ms) { + await new Promise((resolve) => setTimeout(resolve, ms)); +} + /** * Upsert all NFTs from 0 to totalSupply */ -async function upsertNfts() { +async function insertNfts() { const lastVersion = await getLastVersion(); const newVersion = lastVersion + 1; console.log(`new version: ${newVersion}`); const totalSupply = await dNftContract.methods.totalSupply().call(); + insertLatestVersion(newVersion); + for (let i = 0; i < totalSupply; i++) { // without this supabase will throw an error, because of too many requests - await new Promise((resolve) => setTimeout(resolve, 10)); - upsertNft(i, newVersion); + await sleep(10); + insertNft(i, newVersion); } - - insertLatestVersion(newVersion); } /** @@ -118,7 +122,7 @@ async function upsertEnsNames() { } /** - * listen to sync events and upsert all NFTs + * listen to sync events and insert all NFTs */ function subscribeToSync() { console.log("subscribing to sync"); @@ -131,7 +135,7 @@ function subscribeToSync() { ], }, function (error, result) { - upsertNfts(); + insertNfts(); upsertEnsNames(); if (!error) console.log(result); @@ -139,5 +143,5 @@ function subscribeToSync() { ); } -upsertNfts(); +insertNfts(); subscribeToSync(); diff --git a/package-lock.json b/package-lock.json index 2d67d59..86cb37f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3319,6 +3319,15 @@ "node": ">=0.6" } }, + "node_modules/request/node_modules/uuid": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", + "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", + "deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.", + "bin": { + "uuid": "bin/uuid" + } + }, "node_modules/resolve-alpn": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/resolve-alpn/-/resolve-alpn-1.2.1.tgz", @@ -3929,15 +3938,6 @@ "node": ">= 0.4.0" } }, - "node_modules/uuid": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", - "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", - "deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.", - "bin": { - "uuid": "bin/uuid" - } - }, "node_modules/varint": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz", @@ -6917,6 +6917,11 @@ "version": "6.5.3", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.3.tgz", "integrity": "sha512-qxXIEh4pCGfHICj1mAJQ2/2XVZkjCDTcEgfoSQxc/fYivUZxTkk7L3bDBJSoNrEzXI17oUO5Dp07ktqE5KzczA==" + }, + "uuid": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", + "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" } } }, @@ -7398,11 +7403,6 @@ "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==" }, - "uuid": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", - "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" - }, "varint": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/varint/-/varint-5.0.2.tgz",