Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shafu0x committed Jan 1, 2023
1 parent 005f5c0 commit 7d95c37
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
Binary file modified .DS_Store
Binary file not shown.
84 changes: 23 additions & 61 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { createClient } from "@supabase/supabase-js";
import Web3 from "web3";
import Contract from "web3-eth-contract";
import dNFT_ABI from "./abi/dNFT.json" assert { type: "json" };
import { getEnsName } from "./utils/ensName.js";
import * as dotenv from "dotenv";
import sleep from "./utils/sleep";
dotenv.config();

const SYNC_LOG_SIGNATURE =
"0xff14d8520387b9b85d2a017dc41ae15db04a22d4c67deac04eb45048631ffa86";

const TIME_BETWEEN_NFT_INSERTIONS = 10; // ms

const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY
Expand All @@ -17,11 +22,11 @@ Contract.setProvider(process.env.INFURA_RPC);
const dNftContract = new Contract(dNFT_ABI["abi"], process.env.dNFT_ADDRESS);

/**
* Upsert (update if exists or insert if not) a single NFT
* Get the dNFT with the given index and insert it into the nfts table.
* @param {i} index from 0 to totalSupply
* @param {newVersion} version of the sync
* @param {nextVersion} version of the sync
*/
async function insertNft(i, newVersion) {
async function insertNft(i, nextVersion) {
console.log(i);

const tokenId = await dNftContract.methods.tokenByIndex(i).call();
Expand All @@ -36,17 +41,14 @@ async function insertNft(i, newVersion) {
isLiquidatable: nft.isLiquidatable,
owner: owner,
contractAddress: process.env.dNFT_ADDRESS,
version_id: newVersion,
version_id: nextVersion,
};

console.log(`insert ${tokenId}`);
const { error } = await supabase.from("nfts").insert(_nft);
console.log("insert", error);
}

/**
*
*/
async function getLastVersion() {
let lastVersion = await supabase
.from("versions")
Expand All @@ -63,87 +65,47 @@ async function getLastVersion() {
}
}

async function insertLatestVersion(newVersion) {
console.log("inserting latest version");
async function insertNextVersion(nextVersion) {
console.log("inserting next version");
const { error } = await supabase.from("versions").insert({
version: newVersion,
version: nextVersion,
contractAddress: process.env.dNFT_ADDRESS,
mode: process.env.MODE,
});
console.log(error);
}

async function sleep(ms) {
await new Promise((resolve) => 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");
web3.eth.subscribe(
"logs",
{
address: process.env.dNFT_ADDRESS,
topics: [
"0xff14d8520387b9b85d2a017dc41ae15db04a22d4c67deac04eb45048631ffa86",
],
topics: [SYNC_LOG_SIGNATURE],
},
function (error, result) {
insertNfts();
// upsertEnsNames();

if (!error) console.log(result);
}
() => insertNfts()
);
}

Expand Down
3 changes: 3 additions & 0 deletions utils/sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function sleep(ms) {
await new Promise((resolve) => setTimeout(resolve, ms));
}

0 comments on commit 7d95c37

Please sign in to comment.