Skip to content

Instantly share code, notes, and snippets.

@adilcpm
Created August 3, 2024 20:20
Show Gist options
  • Save adilcpm/dc02eaa643809ef0af166c0089f4e193 to your computer and use it in GitHub Desktop.
Save adilcpm/dc02eaa643809ef0af166c0089f4e193 to your computer and use it in GitHub Desktop.
let successCount = 0
let errorCount = 0
const tradingEnabledCollections = await AllCollections.findAll({
where: {
isTradingEnabled: true,
},
})
for (const collection of tradingEnabledCollections) {
try {
const collectionSymbol = collection.symbol
if (collectionSymbol === undefined) {
console.error('Collection symbol is undefined')
errorCount++
continue
}
const s3Key = PATH_PREFIX + `traits/${collectionSymbol}_traits.json`
const formattedTraitInfo: FormattedTraitInfo = {}
const verificationAddress = collection.verificationAddress
if (verificationAddress === undefined) {
console.error(
'Verification address is undefined for collection : ' +
collectionSymbol,
)
errorCount++
continue
}
const collectionKey = (
await findCollectionV2PDA(
VTOPIA_MARKETPLACE_KEY,
new PublicKey(verificationAddress),
)
)[0]
const traits =
await getVtopiaMarketPlaceClient(false).fetchCollectionTraitInfoArray(
collectionKey,
)
// Fetching traits from the scraper, this is a temporary solution to get the floor price and listed count, since on-chain traits do not have this info
const alreadyUploadedTraitInfo =
await getCollectionTraitInfoScraped(collectionSymbol)
if (alreadyUploadedTraitInfo === null) {
errorCount++
console.error(
'Error fetching uploaded traits for collection : ' +
collectionSymbol,
)
continue
}
for (const trait of traits) {
const traitInfoFromScraper =
alreadyUploadedTraitInfo.traitsInfo[trait.traitType]
const traitType = trait.traitType
const traitValues = trait.traitValues
if (formattedTraitInfo[traitType] === undefined) {
formattedTraitInfo[traitType] = []
}
for (const traitValue of traitValues) {
// finding the trait value from the scraper, for metadata
const traitValuesFromScraper = traitInfoFromScraper?.find(
t => t.value === traitValue,
)
formattedTraitInfo[traitType].push({
value: traitValue,
floorPrice: traitValuesFromScraper?.floorPrice ?? 0,
listedCount: traitValuesFromScraper?.listedCount ?? 0,
totalCount: traitValuesFromScraper?.totalCount ?? 0,
listedPercentage: traitValuesFromScraper?.listedPercentage ?? '0',
})
}
}
const formattedTraitInfoWithMetadata = {
source: 'Vtopia',
traitsInfo: formattedTraitInfo,
updatedAt: new Date().toUTCString(),
}
const s3UploadResult = await s3.send(
new PutObjectCommand({
Bucket: S3_BUCKET,
Key: s3Key,
Body: JSON.stringify(formattedTraitInfoWithMetadata),
}),
)
if (s3UploadResult.$metadata.httpStatusCode === 200) {
console.log(
'Uploaded on-chain Trait Output to S3 for collection : ',
collectionSymbol,
)
successCount++
} else {
throw Error(
'Error in uploading to S3 for collection : ' + collectionSymbol,
)
}
} catch (e) {
console.error(e)
errorCount++
console.log(
'Error while uploading on-chain traits for collection : ' +
collection.symbol,
)
}
}
console.log(
'Successfully uploaded traits for : ',
successCount,
' collections',
)
console.log('Failed to upload traits for : ', errorCount, ' collections')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment