Skip to content

Commit

Permalink
Merge pull request #252 from daiagi/tokenEntity-cop-out
Browse files Browse the repository at this point in the history
avoid deletion
  • Loading branch information
vikiival authored Mar 21, 2024
2 parents 81d3f7b + 25f70eb commit ff1f755
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
11 changes: 11 additions & 0 deletions db/migrations/1711017751137-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = class Data1711017751137 {
name = 'Data1711017751137'

async up(db) {
await db.query(`ALTER TABLE "token_entity" ADD "deleted" boolean NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "token_entity" DROP COLUMN "deleted"`)
}
}
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type TokenEntity @entity {
createdAt: DateTime!
supply: Int!
count: Int!
deleted: Boolean!
}

# Entity to represent a collection
Expand Down
24 changes: 18 additions & 6 deletions src/mappings/shared/token/tokenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class TokenAPI {
collection,
name: tokenName(nft.name, collection.id),
count: 1,
supply: 1,
supply: nft.burned ? 0 : 1,
hash: md5(tokenId),
image: nft.image,
media: nft.media,
Expand All @@ -29,6 +29,7 @@ export class TokenAPI {
blockNumber: nft.blockNumber,
updatedAt: nft.updatedAt,
id: tokenId,
deleted: false,
})

await this.store.save(token)
Expand All @@ -46,19 +47,26 @@ export class TokenAPI {
await emOf(this.store).update(NE, nft.id, { token: null })
const updatedCount = await emOf(this.store).countBy(NE, {
token: {
id: token.id,
id: token.id,
},
})
})

const updatedSupply = await emOf(this.store).countBy(NE, {
token: {
id: token.id,
},
burned: false,
})
await emOf(this.store).update(TE, token.id, {
supply: token.supply - 1,
supply: updatedSupply,
count: updatedCount,
updatedAt: nft.updatedAt,
})

if (updatedCount === 0) {
debug(OPERATION, { deleteEmptyToken: `delete empty token ${token.id}` })
try {
await emOf(this.store).delete(TE, token.id)
await emOf(this.store).update(TE, token.id, { deleted: true })
} catch (error) {
debug(OPERATION, {
deleteEmptyToken: `Failed to delete token ${token.id}`,
Expand All @@ -69,10 +77,14 @@ export class TokenAPI {
}

async addNftToToken(nft: NE, token: TE): Promise<TE> {
if (nft.token?.id === token.id) {
return token
}
debug(OPERATION, { updateToken: `Add NFT ${nft.id} to TOKEN ${token.id} for ` })
token.count += 1
token.supply += 1
token.supply += nft.burned ? 0 : 1
token.updatedAt = nft.updatedAt
token.deleted = false
nft.token = token
await this.store.save(token)
await this.store.save(nft)
Expand Down
3 changes: 3 additions & 0 deletions src/model/generated/tokenEntity.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ export class TokenEntity {

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

@Column_("bool", {nullable: false})
deleted!: boolean
}
1 change: 1 addition & 0 deletions src/server-extension/query/tokenEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ FROM
LEFT JOIN cheapest ON t.id = cheapest.token_id
WHERE
nc.supply > 0 AND
t.deleted = false AND
($10::text IS NULL OR LOWER(t.name) LIKE LOWER('%' || $10 || '%'))
`
24 changes: 13 additions & 11 deletions src/server-extension/query/totalTokenEntities.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export const totalTokenEntities = `
SELECT COUNT(DISTINCT token_id) as total_count
FROM nft_entity
SELECT COUNT(DISTINCT ne.token_id) as total_count
FROM nft_entity as ne
JOIN token_entity ON ne.token_id = token_entity.id AND
token_entity.deleted = false
WHERE
($1::text IS NULL OR current_owner = $1) AND
($6::text IS NULL OR issuer = $6) AND
($5::text[] IS NULL OR issuer NOT IN (SELECT unnest($5))) AND
($2::bigint IS NULL OR price >= $2::bigint) AND
($3::bigint IS NULL OR price > $3::bigint) AND
($4::bigint IS NULL OR price <= $4::bigint) AND
($7::text[] IS NULL OR nft_entity.collection_id = ANY($7)) AND
($8::text IS NULL OR LOWER(nft_entity.name) LIKE LOWER('%' || $8 || '%'));
`
($1::text IS NULL OR ne.current_owner = $1) AND
($6::text IS NULL OR ne.issuer = $6) AND
($5::text[] IS NULL OR ne.issuer NOT IN (SELECT unnest($5))) AND
($2::bigint IS NULL OR ne.price >= $2::bigint) AND
($3::bigint IS NULL OR ne.price > $3::bigint) AND
($4::bigint IS NULL OR ne.price <= $4::bigint) AND
($7::text[] IS NULL OR ne.collection_id = ANY($7)) AND
($8::text IS NULL OR LOWER(ne.name) LIKE LOWER('%' || $8 || '%'));
`;

0 comments on commit ff1f755

Please sign in to comment.