Skip to content

Commit

Permalink
insert nft per sync
Browse files Browse the repository at this point in the history
  • Loading branch information
shafu0x committed Dec 29, 2022
1 parent 413e47b commit 97101f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
30 changes: 17 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ 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();
const nft = await dNftContract.methods.idToNft(tokenId).call();
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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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");
Expand All @@ -131,13 +135,13 @@ function subscribeToSync() {
],
},
function (error, result) {
upsertNfts();
insertNfts();
upsertEnsNames();

if (!error) console.log(result);
}
);
}

upsertNfts();
insertNfts();
subscribeToSync();
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 97101f9

Please sign in to comment.