-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptimisticOracleV2.ts
More file actions
416 lines (367 loc) · 13.8 KB
/
optimisticOracleV2.ts
File metadata and controls
416 lines (367 loc) · 13.8 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import {
DisputePrice,
ManagedOracleV2,
ProposePrice,
RequestPrice,
SetBondCall,
SetCustomLivenessCall,
SetEventBasedCall,
Settle,
} from "../../generated/ManagedOracleV2/ManagedOracleV2";
import { ManagedOracleV2Legacy } from "../../generated/ManagedOracleV2/ManagedOracleV2Legacy";
import { createOptimisticPriceRequestId, getManagedRequestId, getOrCreateOptimisticPriceRequest } from "../utils/helpers";
import { CustomBond, CustomLiveness } from "../../generated/schema";
import { Address, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts";
import { createCustomBondId } from "../utils/helpers/managedOracleV2";
/**
* Retrieves a custom bond entity if one exists for the given parameters.
*
* IMPORTANT: Custom bonds are stored with a unique ID that includes the currency.
* This means we can only find custom bonds that match the EXACT currency used in the request.
* If a custom bond was set for a different currency, it will NOT be found here.
*
* This ensures that custom bonds are only applied when the currency matches,
* preventing incorrect bond amounts from being applied to requests with different currencies.
*/
function getCustomBond(
requester: Address,
identifier: Bytes,
ancillaryData: Bytes,
currency: Bytes
): CustomBond | null {
const id = createCustomBondId(requester, identifier, ancillaryData, currency);
let customBondEntity = CustomBond.load(id);
return customBondEntity ? customBondEntity : null;
}
function getCustomLiveness(requester: Address, identifier: Bytes, ancillaryData: Bytes): CustomLiveness | null {
const managedRequestId = getManagedRequestId(requester, identifier, ancillaryData).toHexString();
let customLivenessEntity = CustomLiveness.load(managedRequestId);
return customLivenessEntity ? customLivenessEntity : null;
}
function getState(
ooAddress: Address,
requester: Address,
identifier: Bytes,
timestamp: BigInt,
ancillaryData: Bytes
): string {
const states = [
"Invalid", // Never requested.
"Requested", // Requested, no other actions taken.
"Proposed", // Proposed, but not expired or disputed yet.
"Expired", // Proposed, not disputed, past liveness.
"Disputed", // Disputed, but no DVM price returned yet.
"Resolved", // Disputed and DVM price is available.
"Settled", // Final price has been set in the contract (can get here from Expired or Resolved).
];
let oov2 = ManagedOracleV2.bind(ooAddress);
let state = oov2.try_getState(requester, identifier, timestamp, ancillaryData);
if (state.reverted) {
log.warning("getState call reverted, returning Invalid state", []);
return states[0]; // Return "Invalid" if the call fails
}
return states[state.value];
}
// - event: RequestPrice(indexed address,bytes32,uint256,bytes,address,uint256,uint256)
// handler: handleOptimisticRequestPrice
// - event RequestPrice(
// address indexed requester,
// bytes32 identifier,
// uint256 timestamp,
// bytes ancillaryData,
// address currency,
// uint256 reward,
// uint256 finalFee
// );
export function handleOptimisticRequestPrice(event: RequestPrice): void {
let network = dataSource.network();
let isMainnet = network == "mainnet";
let isGoerli = network == "goerli";
log.warning(`(ancillary) OOV2 PriceRequest params: {},{},{}`, [
event.params.timestamp.toString(),
event.params.identifier.toString(),
event.params.ancillaryData.toHex(),
]);
let requestId = createOptimisticPriceRequestId(
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.identifier = event.params.identifier.toString();
request.time = event.params.timestamp;
request.ancillaryData = event.params.ancillaryData.toHex();
request.requester = event.params.requester;
request.currency = event.params.currency;
request.reward = event.params.reward;
request.finalFee = event.params.finalFee;
request.requestTimestamp = event.block.timestamp;
request.requestBlockNumber = event.block.number;
request.requestLogIndex = event.logIndex;
request.requestHash = event.transaction.hash;
request.state = getState(
event.address,
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
// workaround for L2 chains that don't support `callHandlers`
// see readme for more info
if (!isMainnet && !isGoerli) {
let oov2 = ManagedOracleV2.bind(event.address);
let result = oov2.try_getRequest(
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
if (!result.reverted) {
// New ABI succeeded
let requestSettings = result.value.requestSettings;
request.bond = requestSettings.bond;
request.eventBased = requestSettings.eventBased;
request.customLiveness = requestSettings.customLiveness;
} else {
// Try legacy ABI (for contracts not yet upgraded)
let oov2Legacy = ManagedOracleV2Legacy.bind(event.address);
let legacyResult = oov2Legacy.try_getRequest(
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
if (!legacyResult.reverted) {
let requestSettings = legacyResult.value.requestSettings;
request.bond = requestSettings.bond;
request.eventBased = requestSettings.eventBased;
request.customLiveness = requestSettings.customLiveness;
} else {
log.warning("Both new and legacy getRequest calls failed for request {}", [requestId]);
}
}
}
// Look up custom bond and liveness values that may have been set before the request
// Custom bonds are stored with a unique ID that includes the currency, so we only find
// custom bonds that match the exact currency used in this request
let customBond = getCustomBond(
event.params.requester,
event.params.identifier,
event.params.ancillaryData,
event.params.currency
);
if (customBond !== null) {
const bond = customBond.customBond;
const currency = customBond.currency;
log.debug("custom bond of {} of currency {} was set for request Id: {}", [
bond.toString(),
currency.toHexString(),
requestId,
]);
// Apply the custom bond amount - the currency is guaranteed to match since
// the custom bond ID includes the currency and we looked it up using the request's currency
request.bond = bond;
}
let customLiveness = getCustomLiveness(event.params.requester, event.params.identifier, event.params.ancillaryData);
if (customLiveness !== null) {
const liveness = customLiveness.customLiveness;
log.debug("custom liveness of {} was set for request Id: {}", [liveness.toString(), requestId]);
request.customLiveness = customLiveness.customLiveness;
}
request.save();
}
// - event: ProposePrice(indexed address,indexed address,bytes32,uint256,bytes,int256,uint256,address)
// handler: handleOptimisticProposePrice
// - event ProposePrice(
// address indexed requester,
// address indexed proposer,
// bytes32 identifier,
// uint256 timestamp,
// bytes ancillaryData,
// int256 proposedPrice,
// uint256 expirationTimestamp,
// address currency
// );
export function handleOptimisticProposePrice(event: ProposePrice): void {
log.warning(`(ancillary) OOV2 PriceProposed params: {},{},{}`, [
event.params.timestamp.toString(),
event.params.identifier.toString(),
event.params.ancillaryData.toHex(),
]);
let requestId = createOptimisticPriceRequestId(
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.proposer = event.params.proposer;
request.proposedPrice = event.params.proposedPrice;
request.proposalExpirationTimestamp = event.params.expirationTimestamp;
request.proposalTimestamp = event.block.timestamp;
request.proposalBlockNumber = event.block.number;
request.proposalLogIndex = event.logIndex;
request.proposalHash = event.transaction.hash;
request.state = getState(
event.address,
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
// Look up custom bond and liveness values that may have been set before the request
// Custom bonds are stored with a unique ID that includes the currency, so we only find
// custom bonds that match the exact currency used in this request
let customBond = getCustomBond(
event.params.requester,
event.params.identifier,
event.params.ancillaryData,
event.params.currency
);
if (customBond !== null) {
const bond = customBond.customBond;
const currency = customBond.currency;
log.debug("custom bond of {} of currency {} was set for request Id: {}", [
bond.toString(),
currency.toHexString(),
requestId,
]);
// Apply the custom bond amount - the currency is guaranteed to match since
// the custom bond ID includes the currency and we looked it up using the request's currency
request.bond = bond;
}
let customLiveness = getCustomLiveness(event.params.requester, event.params.identifier, event.params.ancillaryData);
if (customLiveness !== null) {
const liveness = customLiveness.customLiveness;
log.debug("custom liveness of {} was set for request Id: {}", [liveness.toString(), requestId]);
request.customLiveness = customLiveness.customLiveness;
}
request.save();
}
// - event: DisputePrice(indexed address,indexed address,indexed address,bytes32,uint256,bytes,int256)
// handler: handleOptimisticDisputePrice
// - event DisputePrice(
// address indexed requester,
// address indexed proposer,
// address indexed disputer,
// bytes32 identifier,
// uint256 timestamp,
// bytes ancillaryData,
// int256 proposedPrice
// );
export function handleOptimisticDisputePrice(event: DisputePrice): void {
log.warning(`(ancillary) OOV2 PriceDisputed params: {},{},{}`, [
event.params.timestamp.toString(),
event.params.identifier.toString(),
event.params.ancillaryData.toHex(),
]);
let requestId = createOptimisticPriceRequestId(
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.disputer = event.params.disputer;
request.disputeTimestamp = event.block.timestamp;
request.disputeBlockNumber = event.block.number;
request.disputeLogIndex = event.logIndex;
request.disputeHash = event.transaction.hash;
request.state = getState(
event.address,
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
request.save();
}
// - event: Settle(indexed address,indexed address,indexed address,bytes32,uint256,bytes,int256,uint256)
// handler: handleOptimisticSettle
// - event Settle(
// address indexed requester,
// address indexed proposer,
// address indexed disputer,
// bytes32 identifier,
// uint256 timestamp,
// bytes ancillaryData,
// int256 price,
// uint256 payout
// );
export function handleOptimisticSettle(event: Settle): void {
log.warning(`(ancillary) OOV2 Settled params: {},{},{}`, [
event.params.timestamp.toString(),
event.params.identifier.toString(),
event.params.ancillaryData.toHex(),
]);
let requestId = createOptimisticPriceRequestId(
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.settlementPrice = event.params.price;
request.settlementPayout = event.params.payout;
if (!request.disputer || request.proposedPrice!.equals(event.params.price)) {
request.settlementRecipient = request.proposer;
} else {
request.settlementRecipient = request.disputer;
}
request.settlementTimestamp = event.block.timestamp;
request.settlementBlockNumber = event.block.number;
request.settlementLogIndex = event.logIndex;
request.settlementHash = event.transaction.hash;
request.state = getState(
event.address,
event.params.requester,
event.params.identifier,
event.params.timestamp,
event.params.ancillaryData
);
request.save();
}
export function handleSetCustomLiveness(call: SetCustomLivenessCall): void {
log.warning(`OOV2 set custom liveness inputs: {},{},{},{}`, [
call.inputs.timestamp.toString(),
call.inputs.identifier.toString(),
call.inputs.ancillaryData.toHex(),
call.inputs.customLiveness.toString(),
]);
let requestId = createOptimisticPriceRequestId(
call.inputs.identifier,
call.inputs.timestamp,
call.inputs.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.customLiveness = call.inputs.customLiveness;
request.save();
}
export function handleSetBond(call: SetBondCall): void {
log.warning(`OOV2 set bond inputs: {},{},{},{}`, [
call.inputs.timestamp.toString(),
call.inputs.identifier.toString(),
call.inputs.ancillaryData.toHex(),
call.inputs.bond.toString(),
]);
let requestId = createOptimisticPriceRequestId(
call.inputs.identifier,
call.inputs.timestamp,
call.inputs.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.bond = call.inputs.bond;
request.save();
}
export function handleSetEventBased(call: SetEventBasedCall): void {
log.warning(`OOV2 set event based inputs: {},{},{}`, [
call.inputs.timestamp.toString(),
call.inputs.identifier.toString(),
call.inputs.ancillaryData.toHex(),
]);
let requestId = createOptimisticPriceRequestId(
call.inputs.identifier,
call.inputs.timestamp,
call.inputs.ancillaryData
);
let request = getOrCreateOptimisticPriceRequest(requestId);
request.eventBased = true;
request.save();
}