-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmappings.ts
More file actions
261 lines (220 loc) · 8.36 KB
/
mappings.ts
File metadata and controls
261 lines (220 loc) · 8.36 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { Address, BigInt, Bytes, Value, log } from "@graphprotocol/graph-ts";
import {
ProposalCreated,
ProposalCanceled,
ProposalQueued,
ProposalExecuted,
VoteCast
} from "../generated/GovernorAlpha/GovernorAlpha";
import {
DelegateChanged,
DelegateVotesChanged,
Transfer
} from "../generated/Token/Token";
import {
getOrCreateTokenHolder,
getOrCreateDelegate,
getOrCreateProposal,
getOrCreateVote,
getGovernanceEntity
} from "./utils/helpers";
import {
ZERO_ADDRESS,
BIGINT_ONE,
BIGINT_ZERO,
STATUS_ACTIVE,
STATUS_QUEUED,
STATUS_PENDING,
STATUS_EXECUTED,
STATUS_CANCELLED
} from "./utils/constants";
import { toDecimal } from "./utils/decimals";
// - event: ProposalCreated(uint256,address,address[],uint256[],string[],bytes[],uint256,uint256,string)
// handler: handleProposalCreated
export function handleProposalCreated(event: ProposalCreated): void {
let proposal = getOrCreateProposal(event.params.id.toString());
let proposer = getOrCreateDelegate(
event.params.proposer.toHexString(),
false
);
// checking if the proposer was a delegate already accounted for, if not we should log an error
// since it shouldn't be possible for a delegate to propose anything without first being "created"
if (proposer == null) {
log.error("Delegate {} not found on ProposalCreated. tx_hash: {}", [
event.params.proposer.toHexString(),
event.transaction.hash.toHexString()
]);
}
// Creating it anyway since we will want to account for this event data, even though it should've never happened
proposer = getOrCreateDelegate(event.params.proposer.toHexString());
proposal.proposer = proposer.id;
proposal.targets = event.params.targets as Bytes[];
proposal.values = event.params.values;
proposal.signatures = event.params.signatures;
proposal.calldatas = event.params.calldatas;
proposal.startBlock = event.params.startBlock;
proposal.endBlock = event.params.endBlock;
proposal.description = event.params.description;
proposal.status =
event.block.number >= proposal.startBlock ? STATUS_ACTIVE : STATUS_PENDING;
proposal.save();
}
// - event: ProposalCanceled(uint256)
// handler: handleProposalCanceled
export function handleProposalCanceled(event: ProposalCanceled): void {
let proposal = getOrCreateProposal(event.params.id.toString());
proposal.status = STATUS_CANCELLED;
proposal.save();
}
// - event: ProposalQueued(uint256,uint256)
// handler: handleProposalQueued
export function handleProposalQueued(event: ProposalQueued): void {
let governance = getGovernanceEntity();
let proposal = getOrCreateProposal(event.params.id.toString());
proposal.status = STATUS_QUEUED;
proposal.executionETA = event.params.eta;
proposal.save();
governance.proposalsQueued = governance.proposalsQueued + BIGINT_ONE;
governance.save();
}
// - event: ProposalExecuted(uint256)
// handler: handleProposalExecuted
export function handleProposalExecuted(event: ProposalExecuted): void {
let governance = getGovernanceEntity();
let proposal = getOrCreateProposal(event.params.id.toString());
proposal.status = STATUS_EXECUTED;
proposal.executionETA = null;
proposal.save();
governance.proposalsQueued = governance.proposalsQueued - BIGINT_ONE;
governance.save();
}
// - event: VoteCast(address,uint256,bool,uint256)
// handler: handleVoteCast
export function handleVoteCast(event: VoteCast): void {
let proposal = getOrCreateProposal(event.params.proposalId.toString());
let voteId = event.params.voter
.toHexString()
.concat("-")
.concat(event.params.proposalId.toString());
let vote = getOrCreateVote(voteId);
let voter = getOrCreateDelegate(event.params.voter.toHexString(), false);
// checking if the voter was a delegate already accounted for, if not we should log an error
// since it shouldn't be possible for a delegate to vote without first being "created"
if (voter == null) {
log.error("Delegate {} not found on VoteCast. tx_hash: {}", [
event.params.voter.toHexString(),
event.transaction.hash.toHexString()
]);
}
// Creating it anyway since we will want to account for this event data, even though it should've never happened
voter = getOrCreateDelegate(event.params.voter.toHexString());
vote.proposal = proposal.id;
vote.voter = voter.id;
vote.votesRaw = event.params.votes;
vote.votes = toDecimal(event.params.votes);
vote.support = event.params.support;
vote.save();
if (proposal.status == STATUS_PENDING) {
proposal.status = STATUS_ACTIVE;
proposal.save();
}
}
// - event: DelegateChanged(indexed address,indexed address,indexed address)
// handler: handleDelegateChanged
export function handleDelegateChanged(event: DelegateChanged): void {
let tokenHolder = getOrCreateTokenHolder(
event.params.delegator.toHexString()
);
let previousDelegate = getOrCreateDelegate(
event.params.fromDelegate.toHexString()
);
let newDelegate = getOrCreateDelegate(event.params.toDelegate.toHexString());
tokenHolder.delegate = newDelegate.id;
tokenHolder.save();
previousDelegate.tokenHoldersRepresentedAmount =
previousDelegate.tokenHoldersRepresentedAmount - 1;
newDelegate.tokenHoldersRepresentedAmount =
newDelegate.tokenHoldersRepresentedAmount + 1;
previousDelegate.save();
newDelegate.save();
}
// - event: DelegateVotesChanged(indexed address,uint256,uint256)
// handler: handleDelegateVotesChanged
export function handleDelegateVotesChanged(event: DelegateVotesChanged): void {
let governance = getGovernanceEntity();
let delegate = getOrCreateDelegate(event.params.delegate.toHexString());
let votesDifference = event.params.newBalance - event.params.previousBalance;
delegate.delegatedVotesRaw = event.params.newBalance;
delegate.delegatedVotes = toDecimal(event.params.newBalance);
delegate.save();
if (
event.params.previousBalance == BIGINT_ZERO &&
event.params.newBalance > BIGINT_ZERO
) {
governance.currentDelegates = governance.currentDelegates + BIGINT_ONE;
}
if (event.params.newBalance == BIGINT_ZERO) {
governance.currentDelegates = governance.currentDelegates - BIGINT_ONE;
}
governance.delegatedVotesRaw = governance.delegatedVotesRaw + votesDifference;
governance.delegatedVotes = toDecimal(governance.delegatedVotesRaw);
governance.save();
}
// - event: Transfer(indexed address,indexed address,uint256)
// handler: handleTransfer
export function handleTransfer(event: Transfer): void {
let fromHolder = getOrCreateTokenHolder(event.params.from.toHexString());
let toHolder = getOrCreateTokenHolder(event.params.to.toHexString());
let governance = getGovernanceEntity();
// fromHolder
if (event.params.from.toHexString() != ZERO_ADDRESS) {
let fromHolderPreviousBalance = fromHolder.tokenBalanceRaw;
fromHolder.tokenBalanceRaw =
fromHolder.tokenBalanceRaw - event.params.amount;
fromHolder.tokenBalance = toDecimal(fromHolder.tokenBalanceRaw);
if (fromHolder.tokenBalanceRaw < BIGINT_ZERO) {
log.error("Negative balance on holder {} with balance {}", [
fromHolder.id,
fromHolder.tokenBalanceRaw.toString()
]);
}
if (
fromHolder.tokenBalanceRaw == BIGINT_ZERO &&
fromHolderPreviousBalance > BIGINT_ZERO
) {
governance.currentTokenHolders =
governance.currentTokenHolders - BIGINT_ONE;
governance.save();
} else if (
fromHolder.tokenBalanceRaw > BIGINT_ZERO &&
fromHolderPreviousBalance == BIGINT_ZERO
) {
governance.currentTokenHolders =
governance.currentTokenHolders + BIGINT_ONE;
governance.save();
}
fromHolder.save();
}
// toHolder
let toHolderPreviousBalance = toHolder.tokenBalanceRaw;
toHolder.tokenBalanceRaw = toHolder.tokenBalanceRaw + event.params.amount;
toHolder.tokenBalance = toDecimal(toHolder.tokenBalanceRaw);
toHolder.totalTokensHeldRaw = toHolder.totalTokensHeldRaw + event.params.amount;
toHolder.totalTokensHeld = toDecimal(toHolder.totalTokensHeldRaw);
if (
toHolder.tokenBalanceRaw == BIGINT_ZERO &&
toHolderPreviousBalance > BIGINT_ZERO
) {
governance.currentTokenHolders =
governance.currentTokenHolders - BIGINT_ONE;
governance.save();
} else if (
toHolder.tokenBalanceRaw > BIGINT_ZERO &&
toHolderPreviousBalance == BIGINT_ZERO
) {
governance.currentTokenHolders =
governance.currentTokenHolders + BIGINT_ONE;
governance.save();
}
toHolder.save();
}