-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanagedOracleV2.ts
More file actions
108 lines (87 loc) · 3.88 KB
/
managedOracleV2.ts
File metadata and controls
108 lines (87 loc) · 3.88 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
import { log, Bytes, crypto } from "@graphprotocol/graph-ts";
import { CustomBond, CustomLiveness, Resolver, ResolverHistory } from "../../generated/schema";
import { CustomBondSet, CustomLivenessSet, RoleGranted, RoleRevoked } from "../../generated/ManagedOracleV2/ManagedOracleV2";
import { createCustomBondIdFromEvent } from "../utils/helpers/managedOracleV2";
// RESOLVER_ROLE = keccak256("RESOLVER_ROLE")
const RESOLVER_ROLE = Bytes.fromHexString("0x92a19c77d2ea87c7f81d50c74403cb2f401780f3ad919571121efe2bdb427eb1");
/**
* Handles CustomBondSet events from the ManagedOracleV2 contract.
*
* Creates or updates a CustomBond entity with a unique ID that includes the currency.
* This ensures that custom bonds are tied to specific currencies and can only be
* found when the request currency matches the custom bond currency.
*/
export function handleCustomBondSet(event: CustomBondSet): void {
const managedRequestId = event.params.managedRequestId.toHexString();
log.debug("Custom Bond set event. Loading entity with managedRequestId, {}", [managedRequestId]);
// Generate unique ID that includes currency - this ensures currency matching
const id = createCustomBondIdFromEvent(event);
let entity = CustomBond.load(id);
if (entity == null) {
entity = new CustomBond(id);
entity.managedRequestId = managedRequestId;
entity.requester = event.params.requester;
entity.identifier = event.params.identifier.toString();
entity.ancillaryData = event.params.ancillaryData.toHex();
entity.currency = event.params.currency;
}
entity.customBond = event.params.bond;
entity.save();
}
export function handleCustomLivenessSet(event: CustomLivenessSet): void {
const managedRequestId = event.params.managedRequestId.toHexString();
log.debug("Custom Liveness set event. Loading entity with managedRequestId, {}", [managedRequestId]);
let entity = CustomLiveness.load(managedRequestId);
if (entity == null) {
entity = new CustomLiveness(managedRequestId);
entity.requester = event.params.requester;
entity.identifier = event.params.identifier.toString();
entity.ancillaryData = event.params.ancillaryData.toHex();
}
entity.customLiveness = event.params.customLiveness;
entity.save();
}
export function handleRoleGranted(event: RoleGranted): void {
if (event.params.role != RESOLVER_ROLE) return;
const id = event.params.account.toHexString();
let resolver = Resolver.load(id);
if (resolver == null) {
resolver = new Resolver(id);
resolver.address = event.params.account;
}
resolver.isActive = true;
resolver.addedAt = event.block.timestamp;
resolver.addedTx = event.transaction.hash;
resolver.save();
// Create history entry
const historyId = event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
let history = new ResolverHistory(historyId);
history.resolver = event.params.account;
history.action = "added";
history.timestamp = event.block.timestamp;
history.blockNumber = event.block.number;
history.transactionHash = event.transaction.hash;
history.save();
log.info("Resolver added: {}", [id]);
}
export function handleRoleRevoked(event: RoleRevoked): void {
if (event.params.role != RESOLVER_ROLE) return;
const id = event.params.account.toHexString();
let resolver = Resolver.load(id);
if (resolver != null) {
resolver.isActive = false;
resolver.removedAt = event.block.timestamp;
resolver.removedTx = event.transaction.hash;
resolver.save();
}
// Create history entry
const historyId = event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
let history = new ResolverHistory(historyId);
history.resolver = event.params.account;
history.action = "removed";
history.timestamp = event.block.timestamp;
history.blockNumber = event.block.number;
history.transactionHash = event.transaction.hash;
history.save();
log.info("Resolver removed: {}", [id]);
}