refactor: calculate the trait rarity based on the collection supply #679
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
https://app.clickup.com/t/86drbrvby
After a lot of experimentation on what works and what doesn't, we settled on calculating the trait rarity based on the data from our own database. So, we'll calculate the trait rarity by taking the number of NFTs in a collection that have this exact same trait and dividing it by the total supply of the collection.
Of course, we are unable to do this for collections that do not have the supply or have a supply > 50k, as we don't index NFTs for those collections.
When do we calculate these traits?
Depending on the number of traits, it can take a while to recalculate the rarity. For example, if collection has 10k of traits it can take up to 30 seconds to recalculate the rarity. Because of that, we don't want to run this on some schedule. Not only that, but it doesn't make sense to keep running it as rarity only changes once there are new traits and NFTs in the database. So, this job will only run for a collection once we store all of the collection's NFTs. We schedule a job to fetch NFTs (
php artisan collections:fetch-nfts
), and when there are no more NFTs to fetch, we recalculate the rarities.Backfilling missing data
To retroactively update the data, you can run
php artisan collections:calculate-trait-rarities
. It will dispatch a series of jobs for every collection and calculate the traits.Comparisons (us vs Rarible)
So far it seems to be pretty reliable, aside from some minor differences (10.1% compared to 10% or 10.3%, etc)
https://rarible.com/token/0x7f4ac7c9f7015178248ad6729523f0d5f628b933:3?tab=properties
https://rarible.com/token/0x6fecaa110f74c8181e4a90285b1065be32596c9d:9?tab=properties
https://rarible.com/token/0xc5a6afbe82eb9a119ac0d15392ca43b683ae8136:6?tab=properties
https://rarible.com/token/0xc5a6afbe82eb9a119ac0d15392ca43b683ae8136:1?tab=properties
Testing
For this to work properly, you need to index all of NFTs for a collection. You can do this with
php artisan collections:fetch-nfts --collection-id=
. When this job finishes, rarities will be recalculated automatically.Checklist