diff --git a/db/migrations/1711496391339-Data.js b/db/migrations/1711496391339-Data.js new file mode 100644 index 000000000..711867fe0 --- /dev/null +++ b/db/migrations/1711496391339-Data.js @@ -0,0 +1,15 @@ +module.exports = class Data1711496391339 { + name = 'Data1711496391339' + + async up(db) { + await db.query(`ALTER TABLE "token" ADD "last_sale_id" character varying`) + await db.query(`CREATE INDEX "IDX_cd3552dc7426905ed6aa234add" ON "token" ("last_sale_id") `) + await db.query(`ALTER TABLE "token" ADD CONSTRAINT "FK_cd3552dc7426905ed6aa234add1" FOREIGN KEY ("last_sale_id") REFERENCES "listing_sale"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) + } + + async down(db) { + await db.query(`ALTER TABLE "token" DROP COLUMN "last_sale_id"`) + await db.query(`DROP INDEX "public"."IDX_cd3552dc7426905ed6aa234add"`) + await db.query(`ALTER TABLE "token" DROP CONSTRAINT "FK_cd3552dc7426905ed6aa234add1"`) + } +} diff --git a/schema.graphql b/schema.graphql index d89399983..99c2454b4 100644 --- a/schema.graphql +++ b/schema.graphql @@ -582,6 +582,7 @@ type Token @entity { traits: [TraitToken] @derivedFrom(field: "token") bestListing: Listing recentListing: Listing + lastSale: ListingSale # Extras nonFungible: Boolean! diff --git a/src/job-handlers/fetch-collection-extra.ts b/src/job-handlers/fetch-collection-extra.ts index f4c66d74f..b10d1ed24 100644 --- a/src/job-handlers/fetch-collection-extra.ts +++ b/src/job-handlers/fetch-collection-extra.ts @@ -41,8 +41,8 @@ export default async (job: Queue.Job, done: Queue.DoneCallback) => { website: _c.website, }) - syncCollectionStats(collection.id) - computeTraits(collection.id) + syncCollectionStats(_c.collectionId) + computeTraits(_c.collectionId) await em.update(Collection, { id: _c.collectionId }, collection as any).catch((err) => { // eslint-disable-next-line no-console diff --git a/src/mappings/marketplace/events/auction_finalized.ts b/src/mappings/marketplace/events/auction_finalized.ts index be6aaf55a..d9f9b08a8 100644 --- a/src/mappings/marketplace/events/auction_finalized.ts +++ b/src/mappings/marketplace/events/auction_finalized.ts @@ -92,7 +92,8 @@ export async function auctionFinalized( listing, createdAt: new Date(block.timestamp), }) - ctx.store.save(sale) + listing.makeAssetId.lastSale = sale + await ctx.store.save(sale) } listing.isActive = false @@ -114,8 +115,8 @@ export async function auctionFinalized( if (bestListing) { listing.makeAssetId.bestListing = bestListing } - await ctx.store.save(listing.makeAssetId) } + await ctx.store.save(listing.makeAssetId) syncCollectionStats(listing.makeAssetId.collection.id) diff --git a/src/mappings/marketplace/events/listing_filled.ts b/src/mappings/marketplace/events/listing_filled.ts index 95aa9ac9a..9132a0caa 100644 --- a/src/mappings/marketplace/events/listing_filled.ts +++ b/src/mappings/marketplace/events/listing_filled.ts @@ -112,6 +112,8 @@ export async function listingFilled( createdAt: new Date(block.timestamp), }) + listing.makeAssetId.lastSale = sale + await Promise.all([ctx.store.save(listing), ctx.store.save(sale)]) if (listing.makeAssetId.bestListing?.id === listing.id && data.amountRemaining === 0n) { @@ -120,9 +122,10 @@ export async function listingFilled( if (bestListing) { listing.makeAssetId.bestListing = bestListing } - await ctx.store.save(listing.makeAssetId) } + await ctx.store.save(listing.makeAssetId) + syncCollectionStats(listing.makeAssetId.collection.id) return getEvent(item, data, listing) diff --git a/src/mappings/multiTokens/events/token_destroyed.ts b/src/mappings/multiTokens/events/token_destroyed.ts index 3d790839c..d0f576bf3 100644 --- a/src/mappings/multiTokens/events/token_destroyed.ts +++ b/src/mappings/multiTokens/events/token_destroyed.ts @@ -73,11 +73,10 @@ export async function tokenDestroyed( throwError(`[TokenDestroyed] We have not found token ${data.collectionId}-${data.tokenId}.`, 'fatal') return getEvent(item, data) } - // TODO: We are removing all events that are related to this token. - // We should only update the events that have relationship so it is null. - // await ctx.store.delete(Event, { tokenId: token.id }) + token.bestListing = null token.recentListing = null + token.lastSale = null await ctx.store.save(token) diff --git a/src/model/generated/token.model.ts b/src/model/generated/token.model.ts index 9d4050fd3..7605ea3c0 100644 --- a/src/model/generated/token.model.ts +++ b/src/model/generated/token.model.ts @@ -8,6 +8,7 @@ import {TokenAccount} from "./tokenAccount.model" import {Attribute} from "./attribute.model" import {Listing} from "./listing.model" import {TraitToken} from "./traitToken.model" +import {ListingSale} from "./listingSale.model" import {Metadata} from "./_metadata" @Entity_() @@ -80,6 +81,10 @@ export class Token { @ManyToOne_(() => Listing, {nullable: true}) recentListing!: Listing | undefined | null + @Index_() + @ManyToOne_(() => ListingSale, {nullable: true}) + lastSale!: ListingSale | undefined | null + @Column_("bool", {nullable: false}) nonFungible!: boolean