Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFTIO-1276 Remove rank and improve query performance #375

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ type CollectionStats {
supply: BigInt
tokenCount: Int!
salesCount: Int!
rank: Int!
marketCap: BigInt!
volume: BigInt!
}
Expand Down
1 change: 0 additions & 1 deletion src/createEfiToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export async function createEfiToken(ctx: CommonContext, block: SubstrateBlock)
highestSale: null,
tokenCount: 1,
salesCount: 0,
rank: 0,
supply: 2_000_000_000n,
marketCap: 0n,
volume: 0n,
Expand Down
1 change: 0 additions & 1 deletion src/mappings/multiTokens/events/attribute_set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export async function attributeSet(
highestSale: null,
tokenCount: 0,
salesCount: 0,
rank: 0,
supply: 0n,
marketCap: 0n,
volume: 0n,
Expand Down
1 change: 0 additions & 1 deletion src/mappings/multiTokens/events/collection_created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export async function collectionCreated(
highestSale: null,
tokenCount: 0,
salesCount: 0,
rank: 0,
supply: 0n,
marketCap: 0n,
volume: 0n,
Expand Down
12 changes: 0 additions & 12 deletions src/model/generated/_collectionStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class CollectionStats {
private _supply!: bigint | undefined | null
private _tokenCount!: number
private _salesCount!: number
private _rank!: number
private _marketCap!: bigint
private _volume!: bigint

Expand All @@ -21,7 +20,6 @@ export class CollectionStats {
this._supply = json.supply == null ? undefined : marshal.bigint.fromJSON(json.supply)
this._tokenCount = marshal.int.fromJSON(json.tokenCount)
this._salesCount = marshal.int.fromJSON(json.salesCount)
this._rank = marshal.int.fromJSON(json.rank)
this._marketCap = marshal.bigint.fromJSON(json.marketCap)
this._volume = marshal.bigint.fromJSON(json.volume)
}
Expand Down Expand Up @@ -77,15 +75,6 @@ export class CollectionStats {
this._salesCount = value
}

get rank(): number {
assert(this._rank != null, 'uninitialized access')
return this._rank
}

set rank(value: number) {
this._rank = value
}

get marketCap(): bigint {
assert(this._marketCap != null, 'uninitialized access')
return this._marketCap
Expand All @@ -112,7 +101,6 @@ export class CollectionStats {
supply: this.supply == null ? undefined : marshal.bigint.toJSON(this.supply),
tokenCount: this.tokenCount,
salesCount: this.salesCount,
rank: this.rank,
marketCap: marshal.bigint.toJSON(this.marketCap),
volume: marshal.bigint.toJSON(this.volume),
}
Expand Down
61 changes: 27 additions & 34 deletions src/services/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,39 @@ import { Collection, CollectionStats, Listing, ListingSale, ListingStatus, Token
export class CollectionService {
constructor(public em: EntityManager) {}

private get salesQuery() {
return this.em
.createQueryBuilder()
.select('l.collection_id AS collection_id')
.addSelect('RANK() OVER (ORDER BY SUM(l.highest_price) DESC NULLS LAST)::int AS rank')
.addSelect('MAX(l.highest_price) AS highest_sale')
.addSelect('MAX(l.last_sale) AS last_sale')
.addSelect('SUM(l.highest_price * l.amount) AS total_volume')
.addSelect('SUM(l.count)::int AS sales')
.from((qb) => {
return qb
.select('listing.id AS id')
.addSelect('COUNT(sale.id) AS count')
.addSelect('listing.highest_price AS highest_price')
.addSelect('collection.id AS collection_id')
.addSelect('listing.amount AS amount')
.addSelect(
'CASE WHEN lead(listing.id) OVER(PARTITION BY collection.id ORDER BY listing.created_at) IS NULL THEN listing.highest_price END AS last_sale'
)
.from(Listing, 'listing')
.innerJoin(Token, 'token', 'listing.make_asset_id_id = token.id')
.innerJoin(Collection, 'collection', 'token.collection = collection.id')
.innerJoin(ListingStatus, 'status', `listing.id = status.listing AND status.type = 'Finalized'`)
.leftJoin(ListingSale, 'sale', 'listing.id = sale.listing')
.addGroupBy('listing.id')
.addGroupBy('collection.id')
}, 'l')
.groupBy('l.collection_id')
.getQuery()
}

private floorQuery = `SELECT MIN("listing"."highest_price") AS floor_price FROM "listing" AS "listing" INNER JOIN "token" "token" ON "token"."id" = "listing"."make_asset_id_id" INNER JOIN "collection" "collection" ON "collection"."id" = "token"."collection_id" WHERE "collection"."id" = $1 AND
(SELECT count(*) FROM "listing_status" AS "listing_status" WHERE "listing_status"."type" = 'Active' AND "listing_status"."listing_id" = "listing"."id") = (SELECT count(*) FROM "listing_status" AS "listing_status_1" WHERE "listing_status_1"."listing_id" = "listing"."id")`

async sync(collectionId: string) {
if (!collectionId) throw new Error('null collectionId not allowed')

const promises = [
this.em.query(`;With cte AS (${this.salesQuery}) SELECT * FROM cte WHERE collection_id = $1;`, [collectionId]),
this.em
.createQueryBuilder()
.addSelect('MAX(l.highest_price) AS highest_sale')
.addSelect('MAX(l.last_sale) AS last_sale')
.addSelect('SUM(l.volume) AS total_volume')
.addSelect('SUM(l.count)::int AS sales')
.from((qb) => {
return qb
.select('listing.id AS id')
.addSelect('COUNT(sale.id) AS count')
.addSelect('listing.highest_price AS highest_price')
.addSelect('SUM(sale.amount * sale.price) AS volume')
.addSelect('listing.amount AS amount')
.addSelect(
'CASE WHEN lead(listing.id) OVER(ORDER BY listing.created_at) IS NULL THEN listing.highest_price END AS last_sale'
)
.from(ListingSale, 'sale')
.innerJoin(Listing, 'listing', 'listing.id = sale.listing')
.innerJoin(Token, 'token', 'listing.make_asset_id_id = token.id')
.innerJoin(Collection, 'collection', 'token.collection = collection.id')
.leftJoin(ListingStatus, 'status', `listing.id = status.listing AND status.type = 'Finalized'`)
.where('collection.id = :collectionId', { collectionId })
.groupBy('listing.id')
}, 'l')
.getRawOne(),

this.em.query(this.floorQuery, [collectionId]),
this.em
.getRepository(Token)
Expand All @@ -54,13 +48,12 @@ export class CollectionService {
]

// eslint-disable-next-line @typescript-eslint/naming-convention
const [[sales], [{ floor_price }], { tokenCount, supply }] = await Promise.all(promises)
const [sales, [{ floor_price }], { tokenCount, supply }] = await Promise.all(promises)

const stats = new CollectionStats({
tokenCount: Number(tokenCount),
supply: BigInt(supply ?? 0n),
salesCount: sales?.sales ?? 0,
rank: sales?.rank ?? 0,
volume: sales?.total_volume ?? 0n,
marketCap: BigInt(sales?.last_sale ?? 0n) * BigInt(tokenCount),
floorPrice: floor_price,
Expand Down