Skip to content

Instantly share code, notes, and snippets.

@adilcpm
Created August 27, 2024 20:10
Show Gist options
  • Save adilcpm/fd5309d342a249f7fd854283f9d56827 to your computer and use it in GitHub Desktop.
Save adilcpm/fd5309d342a249f7fd854283f9d56827 to your computer and use it in GitHub Desktop.
on-chain trait syncer
const tradingEnabledCollections = await AllCollections.findAll({
where: {
symbol: 'vtopian',
},
})
for (const collection of tradingEnabledCollections) {
try {
const collectionSymbol = collection.symbol
if (collectionSymbol === undefined) {
console.error('Collection symbol is undefined')
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,
)
continue
}
const collectionKey = (
await findCollectionV2PDA(
VTOPIA_MARKETPLACE_KEY,
new PublicKey(verificationAddress),
)
)[0]
let traits: Array<{
traitType: string
traitValues: string[]
}>
try {
traits =
await getVtopiaMarketPlaceClient(
false,
).fetchCollectionTraitInfoArray(collectionKey)
} catch (e) {
console.log(
'Error fetching on-chain traits for collection : ' +
collectionSymbol,
)
continue
}
// 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
// Use Tensor Traits here
const alreadyUploadedTraitInfo =
await getCollectionTraitInfoScraped(collectionSymbol)
if (alreadyUploadedTraitInfo === null) {
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,
)
} else {
throw Error(
'Error in uploading to S3 for collection : ' + collectionSymbol,
)
}
} catch (e) {
console.error(e)
console.log(
'Error while uploading on-chain traits for collection : ' +
collection.symbol,
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment