-
Notifications
You must be signed in to change notification settings - Fork 15
/
erc20Templates.ts
319 lines (287 loc) · 9.99 KB
/
erc20Templates.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { Order, Nft, OrderReuse } from '../@types/schema'
import { BigInt, BigDecimal, Address, log } from '@graphprotocol/graph-ts'
import {
NewPaymentCollector,
OrderStarted,
PublishMarketFee,
PublishMarketFeeChanged,
ConsumeMarketFee,
AddedMinter,
AddedPaymentManager,
RemovedMinter,
RemovedPaymentManager,
CleanedPermissions,
OrderReused,
ProviderFee
} from '../@types/templates/ERC20Template/ERC20Template'
import { integer } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { addOrder } from './utils/globalUtils'
import { getToken, getUSDValue } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
import {
getOrderId,
searchOrderForEvent,
searchOrderReusedForEvent
} from './utils/orderUtils'
export function handleOrderStarted(event: OrderStarted): void {
const order = new Order(
getOrderId(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex(),
event.logIndex.toI32()
)
)
const token = getToken(event.address, true)
order.datatoken = token.id
token.orderCount = token.orderCount.plus(integer.ONE)
const consumer = getUser(event.params.consumer.toHex())
order.consumer = consumer.id
if (token.nft) {
const nft = Nft.load(token.nft as string) as Nft
const nftOwner = getUser(nft.owner)
order.nftOwner = nftOwner.id
}
const payer = getUser(event.params.payer.toHex())
payer.totalOrders = payer.totalOrders.plus(integer.ONE)
payer.save()
order.payer = payer.id
order.amount = weiToDecimal(
event.params.amount.toBigDecimal(),
token.decimals
)
order.serviceIndex = event.params.serviceIndex.toI32()
const publishMarket = getUser(event.params.publishMarketAddress.toHex())
order.publishingMarket = publishMarket.id
order.createdTimestamp = event.block.timestamp.toI32()
order.tx = event.transaction.hash.toHex()
order.eventIndex = event.logIndex.toI32()
order.block = event.block.number.toI32()
const tokenId = token.lastPriceToken
if (tokenId) {
const priceToken = getToken(Address.fromString(tokenId), false)
order.lastPriceToken = priceToken.id
order.lastPriceValue = token.lastPriceValue
order.estimatedUSDValue = getUSDValue(
priceToken.id,
order.lastPriceValue,
order.createdTimestamp
)
}
if (event.receipt !== null && event.receipt!.gasUsed) {
order.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else {
order.gasUsed = BigDecimal.zero()
}
if (event.transaction.gasPrice) {
order.gasPrice = event.transaction.gasPrice
} else {
order.gasPrice = BigInt.zero()
}
order.save()
token.save()
addOrder()
if (token.nft) {
const nft = Nft.load(token.nft as string) as Nft
if (nft) {
nft.orderCount = nft.orderCount.plus(integer.ONE)
nft.save()
}
const owner = getUser(nft.owner)
owner.totalSales = owner.totalSales.plus(integer.ONE)
owner.save()
}
}
export function handlerOrderReused(event: OrderReused): void {
const order = searchOrderForEvent(
event.params.orderTxId.toHexString(),
event.address.toHex(),
event.params.caller.toHex(),
event.logIndex.toI32()
)
if (!order) return
const eventIndex: number = event.logIndex.toI32()
const reuseOrder = new OrderReuse(
`${event.transaction.hash.toHex()}-${eventIndex}`
)
if (event.transaction.gasPrice)
reuseOrder.gasPrice = event.transaction.gasPrice
else reuseOrder.gasPrice = BigInt.zero()
if (event.receipt !== null && event.receipt!.gasUsed) {
reuseOrder.gasUsed = event.receipt!.gasUsed.toBigDecimal()
} else reuseOrder.gasUsed = BigDecimal.zero()
reuseOrder.order = order.id
reuseOrder.caller = event.params.caller.toHexString()
reuseOrder.createdTimestamp = event.params.timestamp.toI32()
reuseOrder.tx = event.transaction.hash.toHex()
reuseOrder.eventIndex = event.logIndex.toI32()
reuseOrder.block = event.params.number.toI32()
reuseOrder.save()
}
export function handlePublishMarketFee(event: PublishMarketFee): void {
const order = searchOrderForEvent(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex(),
event.logIndex.toI32()
)
if (!order) return
const publishMarket = getUser(event.params.PublishMarketFeeAddress.toHex())
order.publishingMarket = publishMarket.id
const publishMarketToken = getToken(event.params.PublishMarketFeeToken, true)
order.publishingMarketToken = publishMarketToken.id
order.publishingMarketAmmount = weiToDecimal(
event.params.PublishMarketFeeAmount.toBigDecimal(),
publishMarketToken.decimals
)
order.save()
}
export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged
): void {
const token = getToken(event.address, true)
if (!token) return
token.publishMarketFeeAddress =
event.params.PublishMarketFeeAddress.toHexString()
token.publishMarketFeeToken = event.params.PublishMarketFeeToken.toHexString()
let decimals = BigInt.fromI32(18).toI32()
if (
token.publishMarketFeeToken != '0x0000000000000000000000000000000000000000'
) {
const token = getToken(event.params.PublishMarketFeeToken, false)
decimals = BigInt.fromI32(token.decimals).toI32()
}
token.publishMarketFeeAmount = weiToDecimal(
event.params.PublishMarketFeeAmount.toBigDecimal(),
decimals
)
token.eventIndex = event.logIndex.toI32()
token.save()
// TODO - shold we have a history
}
export function handleConsumeMarketFee(event: ConsumeMarketFee): void {
const order = searchOrderForEvent(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex(),
event.logIndex.toI32()
)
if (!order) return
const consumeMarket = getUser(event.params.consumeMarketFeeAddress.toHex())
order.consumerMarket = consumeMarket.id
const consumeMarketToken = getToken(event.params.consumeMarketFeeToken, false)
order.consumerMarketToken = consumeMarketToken.id
order.consumerMarketAmmount = weiToDecimal(
event.params.consumeMarketFeeAmount.toBigDecimal(),
consumeMarketToken.decimals
)
order.save()
}
// roles
// roles
export function handleAddedMinter(event: AddedMinter): void {
const token = getToken(event.address, true)
let existingRoles: string[]
if (!token.minter) existingRoles = []
else existingRoles = token.minter as string[]
if (!existingRoles.includes(event.params.user.toHexString()))
existingRoles.push(event.params.user.toHexString())
token.minter = existingRoles
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleRemovedMinter(event: RemovedMinter): void {
const token = getToken(event.address, true)
const newList: string[] = []
let existingRoles: string[]
if (!token.minter) existingRoles = []
else existingRoles = token.minter as string[]
if (!existingRoles || existingRoles.length < 1) return
while (existingRoles.length > 0) {
const role = existingRoles.shift().toString()
if (!role) break
if (role !== event.params.user.toHexString()) newList.push(role)
}
token.minter = newList
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleAddedPaymentManager(event: AddedPaymentManager): void {
const token = getToken(event.address, true)
let existingRoles: string[]
if (!token.paymentManager) existingRoles = []
else existingRoles = token.paymentManager as string[]
if (!existingRoles.includes(event.params.user.toHexString()))
existingRoles.push(event.params.user.toHexString())
token.paymentManager = existingRoles
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleRemovedPaymentManager(
event: RemovedPaymentManager
): void {
const token = getToken(event.address, true)
const newList: string[] = []
let existingRoles: string[]
if (!token.paymentManager) existingRoles = []
else existingRoles = token.paymentManager as string[]
if (!existingRoles || existingRoles.length < 1) return
while (existingRoles.length > 0) {
const role = existingRoles.shift().toString()
if (!role) break
if (role !== event.params.user.toHexString()) newList.push(role)
}
token.paymentManager = newList
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleCleanedPermissions(event: CleanedPermissions): void {
const token = getToken(event.address, true)
const newList: string[] = []
token.paymentManager = newList
token.minter = newList
const nft = Nft.load(token.nft as string)
if (nft) token.paymentCollector = nft.owner
else token.paymentCollector = '0x0000000000000000000000000000000000000000'
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleNewPaymentCollector(event: NewPaymentCollector): void {
const token = getToken(event.address, true)
token.paymentCollector = event.params._newPaymentCollector.toHexString()
token.eventIndex = event.logIndex.toI32()
token.save()
}
export function handleProviderFee(event: ProviderFee): void {
const providerFee: string = `{"providerFeeAddress": "${event.params.providerFeeAddress.toHex()}", "providerFeeToken": "${event.params.providerFeeToken.toHex()}", "providerFeeAmount": "${
event.params.providerFeeAmount
}", "providerData": "${event.params.providerData.toHexString()}", "v": "${
event.params.v
}", "r": "${event.params.r.toHexString()}", "s": "${event.params.s.toHexString()}", "validUntil": "${
event.params.validUntil
}"}`
const order = searchOrderForEvent(
event.transaction.hash.toHex(),
event.address.toHex(),
event.transaction.from.toHex(),
event.logIndex.toI32()
)
if (order) {
order.providerFee = providerFee
order.providerFeeValidUntil = event.params.validUntil
order.save()
return
}
const orderReuse = searchOrderReusedForEvent(
event.transaction.hash.toHex(),
event.address.toHex(),
event.logIndex.toI32()
)
if (orderReuse) {
log.info('order reuse id in provider fee handler: {}', [orderReuse.id])
orderReuse.providerFee = providerFee
orderReuse.providerFeeValidUntil = event.params.validUntil
orderReuse.save()
}
}