Skip to content

Commit

Permalink
feat: IDXR-19 Add lastSale to token (#981)
Browse files Browse the repository at this point in the history
* add lastSale to token

* add migration

* temporary change to update existing tokens

* fix

* fix

* test floor query

* remove jobId

* .

* remove script

* fix
  • Loading branch information
justraman authored Mar 29, 2024
1 parent 5948cb5 commit 9d17558
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
15 changes: 15 additions & 0 deletions db/migrations/1711496391339-Data.js
Original file line number Diff line number Diff line change
@@ -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"`)
}
}
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ type Token @entity {
traits: [TraitToken] @derivedFrom(field: "token")
bestListing: Listing
recentListing: Listing
lastSale: ListingSale

# Extras
nonFungible: Boolean!
Expand Down
4 changes: 2 additions & 2 deletions src/job-handlers/fetch-collection-extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default async (job: Queue.Job<JobData>, 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
Expand Down
5 changes: 3 additions & 2 deletions src/mappings/marketplace/events/auction_finalized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion src/mappings/marketplace/events/listing_filled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/mappings/multiTokens/events/token_destroyed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions src/model/generated/token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_()
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9d17558

Please sign in to comment.