-
Notifications
You must be signed in to change notification settings - Fork 10
/
mint.ts
76 lines (66 loc) · 2.54 KB
/
mint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { warn } from 'node:console'
import { create, getOptional } from '@kodadot1/metasquid/entity'
import md5 from 'md5'
import { CollectionEntity as CE, NFTEntity as NE } from '../../model'
import { createEvent } from '../shared/event'
import { handleMetadata } from '../shared/metadata'
import { unwrap } from '../utils/extract'
import { debug, pending, success } from '../utils/logger'
import { Action, Context, createTokenId } from '../utils/types'
import { versionOf, calculateCollectionOwnerCountAndDistribution } from '../utils/helper'
import { mintHandler } from '../shared/token'
import { getCreateTokenEvent } from './getters'
const OPERATION = Action.MINT
export async function handleTokenCreate(context: Context): Promise<void> {
pending(OPERATION, context.block.height.toString())
const event = unwrap(context, getCreateTokenEvent)
debug(OPERATION, event)
const id = createTokenId(event.collectionId, event.sn)
const collection = await getOptional<CE>(context.store, CE, event.collectionId)
if (!collection) {
warn(OPERATION, `collection ${event.collectionId} not found`)
return
}
const final = create(NE, id, {})
// plsBe(real, collection);
// plsBe(remintable, final);
final.id = id
final.hash = md5(id)
final.issuer = event.caller
final.currentOwner = event.owner
final.blockNumber = BigInt(event.blockNumber)
final.collection = collection
final.sn = event.sn
final.metadata = event.metadata || collection.metadata
final.price = BigInt(0)
final.burned = false
final.createdAt = event.timestamp
final.updatedAt = event.timestamp
final.lewd = false
final.version = versionOf(context)
collection.updatedAt = event.timestamp
collection.nftCount += 1
collection.supply += 1
const { ownerCount, distribution } = await calculateCollectionOwnerCountAndDistribution(
context.store,
collection.id,
final.currentOwner
)
collection.ownerCount = ownerCount
collection.distribution = distribution
if (final.metadata) {
const metadata = await handleMetadata(final.metadata, context.store)
final.meta = metadata
final.name = metadata?.name
final.image = metadata?.image
final.media = metadata?.animationUrl
}
await mintHandler(context, collection, final)
success(OPERATION, `${final.id}`)
await context.store.save(final)
await context.store.save(collection)
await createEvent(final, OPERATION, event, '', context.store)
if (final.issuer !== final.currentOwner) {
await createEvent(final, Action.SEND, event, final.currentOwner, context.store, final.issuer)
}
}