diff --git a/src/job-handlers/collection-stats.ts b/src/job-handlers/collection-stats.ts index a99048cd..adeb71bd 100644 --- a/src/job-handlers/collection-stats.ts +++ b/src/job-handlers/collection-stats.ts @@ -3,8 +3,7 @@ import { Collection, CollectionStats, Listing, ListingSale, ListingStatus, Token import { JobData } from '../jobs/collection-stats' import connection from '../connection' -const 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")` +const 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 "listing"."is_active" = TRUE` export default async (job: Queue.Job, done: Queue.DoneCallback) => { if (!job.data.collectionId) { diff --git a/src/mappings/marketplace/events/auction_finalized.ts b/src/mappings/marketplace/events/auction_finalized.ts index 71952874..be6aaf55 100644 --- a/src/mappings/marketplace/events/auction_finalized.ts +++ b/src/mappings/marketplace/events/auction_finalized.ts @@ -62,8 +62,7 @@ function getEvent( export async function auctionFinalized( ctx: CommonContext, block: SubstrateBlock, - item: EventItem<'Marketplace.AuctionFinalized', { event: { args: true; extrinsic: true } }>, - skipSave: boolean + item: EventItem<'Marketplace.AuctionFinalized', { event: { args: true; extrinsic: true } }> ): Promise<[EventModel, AccountTokenEvent] | undefined> { const data = getEventData(ctx, item.event) if (!data) return undefined @@ -107,6 +106,8 @@ export async function auctionFinalized( createdAt: new Date(block.timestamp), }) + await Promise.all([ctx.store.insert(ListingStatus, listingStatus as any), ctx.store.save(listing)]) + if (listing.makeAssetId.bestListing?.id === listing.id) { const bestListing = await getBestListing(ctx, listing.makeAssetId.id) listing.makeAssetId.bestListing = null @@ -116,9 +117,7 @@ export async function auctionFinalized( await ctx.store.save(listing.makeAssetId) } - await Promise.all([ctx.store.insert(ListingStatus, listingStatus as any), ctx.store.save(listing)]) - - if (!skipSave) syncCollectionStats(listing.makeAssetId.collection.id) + syncCollectionStats(listing.makeAssetId.collection.id) return getEvent(item, data, listing) } diff --git a/src/mappings/marketplace/events/bid_placed.ts b/src/mappings/marketplace/events/bid_placed.ts index c629fb18..08dfbf2b 100644 --- a/src/mappings/marketplace/events/bid_placed.ts +++ b/src/mappings/marketplace/events/bid_placed.ts @@ -60,8 +60,7 @@ function getEvent( export async function bidPlaced( ctx: CommonContext, block: SubstrateBlock, - item: EventItem<'Marketplace.BidPlaced', { event: { args: true; extrinsic: true } }>, - skipSave: boolean + item: EventItem<'Marketplace.BidPlaced', { event: { args: true; extrinsic: true } }> ): Promise<[EventModel, AccountTokenEvent] | undefined> { const data = getEventData(ctx, item.event) if (!data) return undefined @@ -99,6 +98,8 @@ export async function bidPlaced( }) listing.updatedAt = new Date(block.timestamp) + await Promise.all([ctx.store.save(bid), ctx.store.save(listing)]) + if (listing.makeAssetId.bestListing?.id === listing.id) { const bestListing = await getBestListing(ctx, listing.makeAssetId.id) if (bestListing?.id !== listing.id) { @@ -107,9 +108,7 @@ export async function bidPlaced( } } - await Promise.all([ctx.store.save(bid), ctx.store.save(listing)]) - - if (!skipSave) syncCollectionStats(listing.makeAssetId.collection.id) + syncCollectionStats(listing.makeAssetId.collection.id) return getEvent(item, data, listing, account) } diff --git a/src/mappings/marketplace/events/listing_cancelled.ts b/src/mappings/marketplace/events/listing_cancelled.ts index 773c57c9..dc1143bd 100644 --- a/src/mappings/marketplace/events/listing_cancelled.ts +++ b/src/mappings/marketplace/events/listing_cancelled.ts @@ -53,8 +53,7 @@ function getEvent( export async function listingCancelled( ctx: CommonContext, block: SubstrateBlock, - item: EventItem<'Marketplace.ListingCancelled', { event: { args: true; extrinsic: true } }>, - skipSave: boolean + item: EventItem<'Marketplace.ListingCancelled', { event: { args: true; extrinsic: true } }> ): Promise<[EventModel, AccountTokenEvent] | undefined> { const data = getEventData(ctx, item.event) if (!data) return undefined @@ -84,6 +83,8 @@ export async function listingCancelled( createdAt: new Date(block.timestamp), }) + await Promise.all([ctx.store.insert(ListingStatus, listingStatus as any), ctx.store.save(listing)]) + if (listing.makeAssetId.bestListing?.id === listing.id) { const bestListing = await getBestListing(ctx, listing.makeAssetId.id) listing.makeAssetId.bestListing = null @@ -93,9 +94,7 @@ export async function listingCancelled( await ctx.store.save(listing.makeAssetId) } - await Promise.all([ctx.store.insert(ListingStatus, listingStatus as any), ctx.store.save(listing)]) - - if (!skipSave) syncCollectionStats(listing.makeAssetId.collection.id) + syncCollectionStats(listing.makeAssetId.collection.id) return getEvent(item, listing) } diff --git a/src/mappings/marketplace/events/listing_created.ts b/src/mappings/marketplace/events/listing_created.ts index 76027eca..1482c47c 100644 --- a/src/mappings/marketplace/events/listing_created.ts +++ b/src/mappings/marketplace/events/listing_created.ts @@ -80,8 +80,7 @@ function getEvent( export async function listingCreated( ctx: CommonContext, block: SubstrateBlock, - item: EventItem<'Marketplace.ListingCreated', { event: { args: true; extrinsic: true } }>, - skipSave = false + item: EventItem<'Marketplace.ListingCreated', { event: { args: true; extrinsic: true } }> ): Promise<[EventModel, AccountTokenEvent] | undefined> { const data = getEventData(ctx, item.event) if (!data) return undefined @@ -143,6 +142,7 @@ export async function listingCreated( height: block.height, createdAt: new Date(block.timestamp), }) + // update best listing if ((makeAssetId.bestListing && makeAssetId.bestListing?.highestPrice >= listing.price) || !makeAssetId.bestListing) { makeAssetId.bestListing = listing @@ -155,9 +155,7 @@ export async function listingCreated( ctx.store.save(makeAssetId), ]) - if (!skipSave) { - await syncCollectionStats(data.listing.makeAssetId.collectionId.toString()) - } + syncCollectionStats(data.listing.makeAssetId.collectionId.toString()) return getEvent(item, data) } diff --git a/src/mappings/marketplace/events/listing_filled.ts b/src/mappings/marketplace/events/listing_filled.ts index 4cb244ad..95aa9ac9 100644 --- a/src/mappings/marketplace/events/listing_filled.ts +++ b/src/mappings/marketplace/events/listing_filled.ts @@ -65,8 +65,7 @@ function getEvent( export async function listingFilled( ctx: CommonContext, block: SubstrateBlock, - item: EventItem<'Marketplace.ListingFilled', { event: { args: true; extrinsic: true } }>, - skipSave: boolean + item: EventItem<'Marketplace.ListingFilled', { event: { args: true; extrinsic: true } }> ): Promise<[EventModel, AccountTokenEvent] | undefined> { const data = getEventData(ctx, item.event) if (!data) return undefined @@ -113,6 +112,8 @@ export async function listingFilled( createdAt: new Date(block.timestamp), }) + await Promise.all([ctx.store.save(listing), ctx.store.save(sale)]) + if (listing.makeAssetId.bestListing?.id === listing.id && data.amountRemaining === 0n) { const bestListing = await getBestListing(ctx, listing.makeAssetId.id) listing.makeAssetId.bestListing = null @@ -122,9 +123,7 @@ export async function listingFilled( await ctx.store.save(listing.makeAssetId) } - await Promise.all([ctx.store.save(listing), ctx.store.save(sale)]) - - if (!skipSave) syncCollectionStats(listing.makeAssetId.collection.id) + syncCollectionStats(listing.makeAssetId.collection.id) return getEvent(item, data, listing) } diff --git a/src/processor.ts b/src/processor.ts index 29fb7bd5..43fca3e6 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -217,15 +217,15 @@ async function handleEvents( case 'Claims.ExchangeRateSet': return map.claims.events.exchangeRateSet(ctx, block, item) case 'Marketplace.ListingCreated': - return map.marketplace.events.listingCreated(ctx, block, item, skipSave) + return map.marketplace.events.listingCreated(ctx, block, item) case 'Marketplace.ListingCancelled': - return map.marketplace.events.listingCancelled(ctx, block, item, skipSave) + return map.marketplace.events.listingCancelled(ctx, block, item) case 'Marketplace.ListingFilled': - return map.marketplace.events.listingFilled(ctx, block, item, skipSave) + return map.marketplace.events.listingFilled(ctx, block, item) case 'Marketplace.BidPlaced': - return map.marketplace.events.bidPlaced(ctx, block, item, skipSave) + return map.marketplace.events.bidPlaced(ctx, block, item) case 'Marketplace.AuctionFinalized': - return map.marketplace.events.auctionFinalized(ctx, block, item, skipSave) + return map.marketplace.events.auctionFinalized(ctx, block, item) case 'PolkadotXcm.Attempted': return map.xcm.events.attempted(ctx, block, item) case 'FuelTanks.AccountAdded': @@ -294,6 +294,7 @@ function getParticipants(args: any, signer: string): string[] { return [signer] } + processor.run( new FullTypeormDatabase({ isolationLevel: 'READ COMMITTED',