-
Notifications
You must be signed in to change notification settings - Fork 27
/
access-control.ts
114 lines (105 loc) · 3.15 KB
/
access-control.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
107
108
109
110
111
112
113
114
import { Bytes, log, store } from '@graphprotocol/graph-ts';
// Event Imports [based on the yaml config]
import {
TokenRoleChanged as TokenRoleChangedEvent,
CollectionRoleChanged as CollectionRoleChangedEvent,
} from '../generated/FleekNFA/FleekNFA';
// Entity Imports [based on the schema]
import { Owner, Token, Verifier } from '../generated/schema';
import { CollectionRoles, TokenRoles } from './constants';
export function handleCollectionRoleChanged(
event: CollectionRoleChangedEvent
): void {
const toAddress = event.params.toAddress;
const byAddress = event.params.byAddress;
const role = event.params.role;
const status = event.params.status;
switch (role) {
case CollectionRoles.Owner:
// Owner role
if (status) {
// granted
let owner = Owner.load(toAddress);
if (!owner) {
owner = new Owner(toAddress);
}
owner.collection = true;
owner.save();
} else {
// revoked
const owner = Owner.load(toAddress);
if (!owner) {
log.error(
'Owner entity not found. Role: {}, byAddress: {}, toAddress: {}',
[role.toString(), byAddress.toHexString(), toAddress.toHexString()]
);
return;
}
owner.collection = false;
owner.save();
}
break;
case CollectionRoles.Verifier:
// Verifier role
if (status) {
// granted
let verifier = Verifier.load(toAddress);
if (!verifier) {
verifier = new Verifier(toAddress);
}
verifier.save();
} else {
// revoked
const verifier = Verifier.load(toAddress);
if (verifier) {
store.remove('Verifier', verifier.id.toString());
}
}
break;
default:
log.error('Role not supported. Role: {}, byAddress: {}, toAddress: {}', [
role.toString(),
byAddress.toHexString(),
toAddress.toHexString(),
]);
}
}
export function handleTokenRoleChanged(event: TokenRoleChangedEvent): void {
const tokenId = event.params.tokenId;
const toAddress = event.params.toAddress;
const byAddress = event.params.byAddress;
const role = event.params.role;
const status = event.params.status;
// load token
const token = Token.load(Bytes.fromByteArray(Bytes.fromBigInt(tokenId)));
if (!token) {
log.error('Token not found. TokenId: {}', [tokenId.toString()]);
return;
}
if (role === TokenRoles.Controller) {
// Controller role
// get the list of controllers.
let token_controllers = token.controllers;
if (!token_controllers) {
token_controllers = [];
}
if (status) {
// granted
token_controllers.push(toAddress);
} else {
// revoked
// remove address from the controllers list
const index = token_controllers.indexOf(event.params.toAddress, 0);
if (index > -1) {
token_controllers.splice(index, 1);
}
}
token.controllers = token_controllers;
} else {
log.error('Role not supported. Role: {}, byAddress: {}, toAddress: {}', [
role.toString(),
byAddress.toHexString(),
toAddress.toHexString(),
]);
}
}