Skip to content

Commit

Permalink
fix: IDXR-18 fixes floor price of collection and best listing of a to…
Browse files Browse the repository at this point in the history
…ken (#979)

* fixes floor price and best listing of collection

* fix best listing

* sync

* remove quick fix

* remove import

* tmp change

* undo
  • Loading branch information
justraman authored Mar 26, 2024
1 parent 89c96e5 commit 2de8ddc
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/job-handlers/collection-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JobData>, done: Queue.DoneCallback) => {
if (!job.data.collectionId) {
Expand Down
9 changes: 4 additions & 5 deletions src/mappings/marketplace/events/auction_finalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
9 changes: 4 additions & 5 deletions src/mappings/marketplace/events/bid_placed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
9 changes: 4 additions & 5 deletions src/mappings/marketplace/events/listing_cancelled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
8 changes: 3 additions & 5 deletions src/mappings/marketplace/events/listing_created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
9 changes: 4 additions & 5 deletions src/mappings/marketplace/events/listing_filled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
11 changes: 6 additions & 5 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -294,6 +294,7 @@ function getParticipants(args: any, signer: string): string[] {

return [signer]
}

processor.run(
new FullTypeormDatabase({
isolationLevel: 'READ COMMITTED',
Expand Down

0 comments on commit 2de8ddc

Please sign in to comment.