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

✨ collection totalItems & updatedAt #153

Merged
merged 4 commits into from
Oct 18, 2022
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
15 changes: 15 additions & 0 deletions db/migrations/1666001811868-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1666001811868 {
name = 'Data1666001811868'

async up(db) {
await db.query(`ALTER TABLE "collection_entity" ADD "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL`)
await db.query(`ALTER TABLE "collection_entity" ADD "nft_count" integer NOT NULL`)
await db.query(`ALTER TABLE "collection_entity" ADD "supply" integer NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "collection_entity" DROP COLUMN "updated_at"`)
await db.query(`ALTER TABLE "collection_entity" DROP COLUMN "nft_count"`)
await db.query(`ALTER TABLE "collection_entity" DROP COLUMN "supply"`)
}
}
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type CollectionEntity @entity {
blockNumber: BigInt
meta: MetadataEntity
createdAt: DateTime!
updatedAt: DateTime!
nftCount: Int!
supply: Int!
}

type NFTEntity @entity {
Expand Down
20 changes: 20 additions & 0 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ async function mint(context: Context): Promise<void> {
final.blockNumber = BigInt(blockNumber)
final.metadata = collection.metadata
final.createdAt = timestamp
final.updatedAt = timestamp
final.nftCount = 0
final.supply = 0

if (final.metadata) {
const metadata = await handleMetadata(final.metadata, final.name, context.store)
Expand Down Expand Up @@ -160,13 +163,18 @@ async function mintNFT(
final.updatedAt = timestamp
final.emoteCount = 0

collection.updatedAt = timestamp
collection.nftCount += 1
collection.supply += 1

if (final.metadata) {
const metadata = await handleMetadata(final.metadata, final.name, context.store)
final.meta = metadata
}

logger.success(`[MINT] ${final.id}`)
await context.store.save(final)
await context.store.save(collection)
await createEvent(final, RmrkEvent.MINTNFT, { blockNumber, caller, timestamp }, '', context.store)

} catch (e) {
Expand Down Expand Up @@ -222,8 +230,14 @@ async function buy(context: Context) {
nft.price = BigInt(0)
nft.updatedAt = timestamp

plsBe(real, nft.collection)
const collection = ensure<CollectionEntity>(await get<CollectionEntity>(context.store, CollectionEntity, nft.collection.toString()))
plsBe(real, collection)
collection.updatedAt = timestamp

logger.success(`[BUY] ${nft.id} from ${caller}`)
await context.store.save(nft)
await context.store.save(collection)
await createEvent(nft, RmrkEvent.BUY, { blockNumber, caller, timestamp }, String(originalPrice), context.store, originalOwner)
} catch (e) {
logError(e, (e) =>
Expand All @@ -247,9 +261,15 @@ async function consume(context: Context) {
nft.price = BigInt(0)
nft.burned = true
nft.updatedAt = timestamp
plsBe(real, nft.collection)
const collection = ensure<CollectionEntity>(await get<CollectionEntity>(context.store, CollectionEntity, nft.collection.toString()))
plsBe(real, collection)
collection.updatedAt = timestamp
collection.supply -= 1

logger.success(`[CONSUME] ${nft.id} from ${caller}`)
await context.store.save(nft)
await context.store.save(collection)
await createEvent(nft, RmrkEvent.CONSUME, { blockNumber, caller, timestamp }, '', context.store)
} catch (e) {
logError(e, (e) =>
Expand Down
9 changes: 9 additions & 0 deletions src/model/generated/collectionEntity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ export class CollectionEntity {

@Column_("timestamp with time zone", {nullable: false})
createdAt!: Date

@Column_("timestamp with time zone", {nullable: false})
updatedAt!: Date

@Column_("int4", {nullable: false})
nftCount!: number

@Column_("int4", {nullable: false})
supply!: number
}