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

unique tokenEntity id by both image and media #225

Merged
merged 3 commits into from
Feb 24, 2024
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
8 changes: 4 additions & 4 deletions src/mappings/shared/token/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { emOf, getOptional } from '@kodadot1/metasquid/entity'
import { Context } from '../../utils/types'
import { NFTEntity as NE, TokenEntity as TE } from '../../../model'
import { debug } from '../../utils/logger'
import { OPERATION, generateTokenId, mediaOf } from './utils'
import { OPERATION, generateTokenId } from './utils'

export async function burnHandler(context: Context, nft: NE): Promise<void> {
debug(OPERATION, { handleBurn: `Handle Burn for NFT ${nft.id}` })

const nftMedia = mediaOf(nft)
if (!nftMedia) {
const tokenId = generateTokenId(nft.collection.id, nft)
if (!tokenId) {
return
}

const token = await getOptional<TE>(context.store, TE, generateTokenId(nft.collection.id, nftMedia))
const token = await getOptional<TE>(context.store, TE, tokenId)

if (!token) {
return
Expand Down
8 changes: 4 additions & 4 deletions src/mappings/shared/token/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { getOptional } from '@kodadot1/metasquid/entity'
import { Context } from '../../utils/types'
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model'
import { debug } from '../../utils/logger'
import { OPERATION, generateTokenId, mediaOf } from './utils'
import { OPERATION, generateTokenId } from './utils'
import { TokenAPI } from './tokenAPI'

export async function mintHandler(context: Context, collection: CE, nft: NE): Promise<TE | undefined> {
debug(OPERATION, { mintHandler: `Handle mint for NFT ${nft.id}` })

const nftMedia = mediaOf(nft)
if (!nftMedia) {
const tokenId = generateTokenId(collection.id, nft)
if (!tokenId) {
return
}

const tokenApi = new TokenAPI(context.store)

const existingToken = await getOptional<TE>(context.store, TE, generateTokenId(collection.id, nftMedia))
const existingToken = await getOptional<TE>(context.store, TE, tokenId)
return await (existingToken ? tokenApi.addNftToToken(nft, existingToken) : tokenApi.create(collection, nft))
}
10 changes: 6 additions & 4 deletions src/mappings/shared/token/setMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { getOptional, getWith } from '@kodadot1/metasquid/entity'
import { Context } from '../../utils/types'
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model'
import { debug, warn } from '../../utils/logger'
import { OPERATION, generateTokenId, mediaOf } from './utils'
import { OPERATION, generateTokenId } from './utils'
import { TokenAPI } from './tokenAPI'

export async function setMetadataHandler(context: Context, collection: CE, nft: NE): Promise<TE | undefined> {
debug(OPERATION, { handleMetadataSet: `Handle set metadata for NFT ${nft.id}` })
const nftMedia = mediaOf(nft)
if (!nftMedia) {

const tokenId = generateTokenId(collection.id, nft)
if (!tokenId) {
return
}

const tokenAPI = new TokenAPI(context.store)

try {
Expand All @@ -23,6 +25,6 @@ export async function setMetadataHandler(context: Context, collection: CE, nft:
return
}

const existingToken = await getOptional<TE>(context.store, TE, generateTokenId(collection.id, nftMedia))
const existingToken = await getOptional<TE>(context.store, TE, tokenId)
return await (existingToken ? tokenAPI.addNftToToken(nft, existingToken) : tokenAPI.create(collection, nft))
}
8 changes: 3 additions & 5 deletions src/mappings/shared/token/tokenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import md5 from 'md5'
import { Store } from '../../utils/types'
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model'
import { debug } from '../../utils/logger'
import { OPERATION, generateTokenId, mediaOf, tokenName } from './utils'
import { OPERATION, generateTokenId, tokenName } from './utils'

export class TokenAPI {
constructor(private store: Store) {}

async create(collection: CE, nft: NE): Promise<TE | undefined> {
const nftMedia = mediaOf(nft)
if (!nftMedia) {
const tokenId = generateTokenId(collection.id, nft)
if (!tokenId) {
return
}
const tokenId = generateTokenId(collection.id, nftMedia)
debug(OPERATION, { createToken: `Create TOKEN ${tokenId} for NFT ${nft.id}` })

const token = createEntity(TE, tokenId, {
Expand Down Expand Up @@ -67,7 +66,6 @@ export class TokenAPI {
nft.token = token
await this.store.save(token)
await this.store.save(nft)

return token
}
}
15 changes: 5 additions & 10 deletions src/mappings/shared/token/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import { CHAIN } from '../../../environment'

export const OPERATION = 'TokenEntity' as any

export function generateTokenId(collectionId: string, nftMedia: string): string {
return `${collectionId}-${md5(nftMedia)}`
}

export const mediaOf = (nft: NE): string | undefined => {
const nftMedia = nft.image ?? nft.media

if (!nftMedia || nftMedia === '') {
export function generateTokenId(collectionId: string, nft: NE): string | undefined {
if (!nft.image && !nft.media) {
warn(OPERATION, `MISSING NFT MEDIA ${nft.id}`)
return undefined
}

return nftMedia
const image = nft.image ?? ''
const media = nft.media ?? ''
return `${collectionId}-${md5(image)}:${md5(media)}`
}

export const collectionsToKeepNameAsIs: Record<string, string[]> = {
Expand Down