-
Notifications
You must be signed in to change notification settings - Fork 27
/
mint.ts
106 lines (93 loc) · 3.28 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { BigInt, Bytes, log } from '@graphprotocol/graph-ts';
// Event Imports [based on the yaml config]
import { NewMint as NewMintEvent } from '../generated/FleekNFA/FleekNFA';
// Entity Imports [based on the schema]
import {
Owner,
NewMint,
Token,
GitRepository,
Collection,
Verifier,
} from '../generated/schema';
export function handleNewMint(event: NewMintEvent): void {
const newMintEntity = new NewMint(
event.transaction.hash.concatI32(event.logIndex.toI32())
);
const name = event.params.name;
const description = event.params.description;
const externalURL = event.params.externalURL;
const ENS = event.params.ENS;
const gitRepository = event.params.gitRepository;
const ipfsHash = event.params.ipfsHash;
const commitHash = event.params.commitHash;
const logo = event.params.logo;
const color = event.params.color;
const accessPointAutoApproval = event.params.accessPointAutoApproval;
const tokenId = event.params.tokenId;
const ownerAddress = event.params.owner;
const verifierAddress = event.params.verifier;
newMintEntity.tokenId = tokenId;
newMintEntity.name = name;
newMintEntity.description = description;
newMintEntity.externalURL = externalURL;
newMintEntity.ENS = ENS;
newMintEntity.commitHash = commitHash;
newMintEntity.gitRepository = gitRepository;
newMintEntity.ipfsHash = ipfsHash;
newMintEntity.logo = logo;
newMintEntity.color = color;
newMintEntity.accessPointAutoApproval = accessPointAutoApproval;
newMintEntity.triggeredBy = event.params.minter;
newMintEntity.owner = ownerAddress;
newMintEntity.verifier = verifierAddress;
newMintEntity.blockNumber = event.block.number;
newMintEntity.blockTimestamp = event.block.timestamp;
newMintEntity.transactionHash = event.transaction.hash;
newMintEntity.save();
log.error('{}', [tokenId.toString()]);
// Create Token, Owner, and Controller entities
let owner = Owner.load(ownerAddress);
const token = new Token(Bytes.fromByteArray(Bytes.fromBigInt(tokenId)));
if (!owner) {
// Create a new owner entity
owner = new Owner(ownerAddress);
// Since no CollectionRoleChanged event was emitted before for this address, we can set `collection` to false.
owner.collection = false;
}
// Populate Token with data from the event
token.tokenId = tokenId;
token.name = name;
token.description = description;
token.externalURL = externalURL;
token.ENS = ENS;
token.logo = logo;
token.color = color;
token.accessPointAutoApproval = accessPointAutoApproval;
token.owner = ownerAddress;
token.verified = false;
token.mintTransaction = event.transaction.hash.concatI32(
event.logIndex.toI32()
);
token.mintedBy = event.params.minter;
token.controllers = [ownerAddress];
token.createdAt = event.block.timestamp;
if (Verifier.load(verifierAddress)) {
token.verifier = verifierAddress;
}
// Populate GitRepository entity
let repository = GitRepository.load(gitRepository);
if (!repository) {
repository = new GitRepository(gitRepository);
}
// Increase total tokens counter
const collection = Collection.load(event.address);
if (collection) {
collection.totalTokens = collection.totalTokens.plus(BigInt.fromU32(1));
collection.save();
}
// Save entities
owner.save();
token.save();
repository.save();
}