Skip to content

Commit

Permalink
Merge pull request #250 from kodadot/unlink-nft-from-token-on-nft-burn
Browse files Browse the repository at this point in the history
pass burn through tokenApi handler
  • Loading branch information
vikiival authored Mar 20, 2024
2 parents 13dcb85 + ab1806e commit 81d3f7b
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/mappings/shared/token/burn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { emOf, getOptional } from '@kodadot1/metasquid/entity'
import { getWith } from '@kodadot1/metasquid/entity'
import { Context } from '../../utils/types'
import { NFTEntity as NE, TokenEntity as TE } from '../../../model'
import { debug } from '../../utils/logger'
import { NFTEntity as NE } from '../../../model'
import { debug, warn } from '../../utils/logger'
import { OPERATION, generateTokenId } from './utils'
import { TokenAPI } from './tokenAPI'

export async function burnHandler(context: Context, nft: NE): Promise<void> {
debug(OPERATION, { handleBurn: `Handle Burn for NFT ${nft.id}` })
Expand All @@ -12,13 +13,13 @@ export async function burnHandler(context: Context, nft: NE): Promise<void> {
return
}

const token = await getOptional<TE>(context.store, TE, tokenId)

if (!token) {
return
const tokenAPI = new TokenAPI(context.store)
try {
const nftWithToken = await getWith(context.store, NE, nft.id, { token: true })
if (nftWithToken?.token) {
await tokenAPI.removeNftFromToken(nft, nftWithToken.token)
}
} catch (error) {
warn(OPERATION, `ERROR ${error}`)
}

debug(OPERATION, { BURN: `decrement Token's ${token.id} supply` })

await emOf(context.store).update(TE, token.id, { supply: token.supply - 1, updatedAt: nft.updatedAt })
}

0 comments on commit 81d3f7b

Please sign in to comment.