-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Handle ISCN ownership change for
/sell
- Loading branch information
1 parent
7a62072
commit fff0a99
Showing
2 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |