Skip to content

Commit

Permalink
✨ Handle ISCN ownership change for /sell
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Oct 11, 2022
1 parent 7a62072 commit fff0a99
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/routes/likernft/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Router } from 'express';
import { db, likeNFTCollection } from '../../util/firebase';
import { isValidLikeAddress } from '../../util/cosmos';
import { filterOwnedClassIds } from '../../util/api/likernft/user';
import { ValidationError } from '../../util/ValidationError';
import { ONE_DAY_IN_S } from '../../constant';

const UPDATE_COOLDOWN = 60 * 1000;
const updateCooldownMap = {};

const router = Router();

Expand All @@ -12,20 +16,27 @@ router.get(
try {
const { wallet } = req.params;
if (!isValidLikeAddress(wallet)) throw new ValidationError('INVALID_WALLET');
const [sellingNftsQuery, createdClassesQuery] = await Promise.all([
const [sellingNftsQuery, ownedClassesQuery] = await Promise.all([
db.collectionGroup('nft')
.where('sellerWallet', '==', wallet)
.where('soldCount', '>', 0).get(),
// TODO: what if iscn owner changed?
likeNFTCollection.where('creatorWallet', '==', wallet).get(),
likeNFTCollection.where('ownerWallet', '==', wallet).get(),
]);
const classIdSet = new Set();
const now = Date.now();
if (!updateCooldownMap[wallet] || now - updateCooldownMap[wallet] > UPDATE_COOLDOWN) {
updateCooldownMap[wallet] = now;
const ownedClassIds = await filterOwnedClassIds(ownedClassesQuery.docs, wallet);
ownedClassIds.forEach(classId => classIdSet.add(classId));
} else {
ownedClassesQuery.docs.forEach((doc) => {
classIdSet.add(doc.data().classId);
});
}
sellingNftsQuery.docs.forEach((doc) => {
classIdSet.add(doc.data().classId);
});
createdClassesQuery.docs.forEach((doc) => {
classIdSet.add(doc.data().classId);
});
res.set('Cache-Control', `public, max-age=60 s-maxage=60 stale-if-error=${ONE_DAY_IN_S}`);
res.json({
list: Array.from(classIdSet),
});
Expand Down
37 changes: 37 additions & 0 deletions src/util/api/likernft/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { db } from '../../firebase';
import { getNFTISCNOwner } from '../../cosmos/nft';

const BATCH_SIZE = 200;

export async function filterOwnedClassIds(iscnDocs, wallet) {
const classIdSet = new Set();
iscnDocs.forEach((doc) => {
classIdSet.add(doc.data().classId);
});
const docsToUpdate = [];
const checkOwnerPromises = iscnDocs.map(async (doc) => {
const iscnPrefix = decodeURIComponent(doc.id);
const owner = await getNFTISCNOwner(iscnPrefix);
if (owner && owner !== wallet) {
docsToUpdate.push(doc);
classIdSet.delete(doc.data().classId);
}
});
await Promise.all(checkOwnerPromises);

if (docsToUpdate.length) {
const batches = [];
for (let i = 0; i < docsToUpdate.length; i += BATCH_SIZE) {
batches.push(docsToUpdate.slice(i, i + BATCH_SIZE));
}
const updatePromises = batches.map((docs) => {
const batch = db.batch();
docs.forEach(doc => batch.update(doc.ref, { ownerWallet: wallet }));
return batch.commit();
});
await Promise.all(updatePromises);
}
return Array.from(classIdSet);
}

export default filterOwnedClassIds;

0 comments on commit fff0a99

Please sign in to comment.