-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathutils.ts
415 lines (374 loc) · 12.7 KB
/
utils.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
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
import {
Address,
BigInt,
Bytes,
crypto,
Entity,
ethereum,
log,
Value,
} from "@graphprotocol/graph-ts";
import {
IndexSubscription,
PoolMember,
Token,
} from "../generated/schema";
import { ISuperToken as SuperToken } from "../generated/templates/SuperToken/ISuperToken";
import { Resolver } from "../generated/templates/SuperToken/Resolver";
import { getIsLocalIntegrationTesting } from "./addresses";
/**************************************************************************
* Constants
*************************************************************************/
export const BIG_INT_ZERO = BigInt.fromI32(0);
export const BIG_INT_ONE = BigInt.fromI32(1);
export const ZERO_ADDRESS = Address.zero();
export const MAX_FLOW_RATE = BigInt.fromI32(2).pow(95).minus(BigInt.fromI32(1));
export const ORDER_MULTIPLIER = BigInt.fromI32(10000);
export const MAX_SAFE_SECONDS = BigInt.fromI64(8640000000000); //In seconds, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps
export const MAX_UINT256 = BigInt.fromString("115792089237316195423570985008687907853269984665640564039457584007913129639935");
/**************************************************************************
* Convenience Conversions
*************************************************************************/
export function bytesToAddress(bytes: Bytes): Address {
return Address.fromBytes(bytes);
}
/**
* Take an array of ethereum values and return the encoded bytes.
* @param values
* @returns the encoded bytes
*/
export function encode(values: Array<ethereum.Value>): Bytes {
return ethereum.encode(
// forcefully cast Value[] -> Tuple
ethereum.Value.fromTuple(changetype<ethereum.Tuple>(values))
)!;
}
/**************************************************************************
* Event entities util functions
*************************************************************************/
export function createEventID(
eventName: string,
event: ethereum.Event
): string {
return (
eventName +
"-" +
event.transaction.hash.toHexString() +
"-" +
event.logIndex.toString()
);
}
/**
* Initialize event and its base properties on Event interface.
* @param event the ethereum.Event object
* @param addresses the addresses array
* @returns Entity to be casted as original Event type
*/
export function initializeEventEntity(
entity: Entity,
event: ethereum.Event,
addresses: Bytes[]
): Entity {
const idValue = entity.get("id");
if (!idValue) return entity;
const stringId = idValue.toString();
const name = stringId.split("-")[0];
entity.set("blockNumber", Value.fromBigInt(event.block.number));
entity.set("logIndex", Value.fromBigInt(event.logIndex));
entity.set(
"order",
Value.fromBigInt(getOrder(event.block.number, event.logIndex))
);
entity.set("name", Value.fromString(name));
entity.set("addresses", Value.fromBytesArray(addresses));
entity.set("timestamp", Value.fromBigInt(event.block.timestamp));
entity.set("transactionHash", Value.fromBytes(event.transaction.hash));
entity.set("gasPrice", Value.fromBigInt(event.transaction.gasPrice));
const receipt = event.receipt;
if (receipt) {
entity.set("gasUsed", Value.fromBigInt(receipt.gasUsed));
} else {
// @note `gasUsed` is a non-nullable property in our `schema.graphql` file, so when we attempt to save
// the entity with a null field, it will halt the subgraph indexing.
// Nonetheless, we explicitly throw if receipt is null, as this can arise due forgetting to include
// `receipt: true` under `eventHandlers` in our manifest (`subgraph.template.yaml`) file.
log.critical("receipt MUST NOT be null", []);
}
return entity;
}
/**************************************************************************
* HOL entities util functions
*************************************************************************/
export function handleTokenRPCCalls(
token: Token
): Token {
// we must handle the case when the native token hasn't been initialized
// there is no name/symbol, but this may occur later
if (token.name.length == 0 || token.symbol.length == 0) {
token = getTokenInfoAndReturn(token);
}
return token;
}
export function getIsTokenListed(
token: Token,
resolverAddress: Address
): boolean {
const resolverContract = Resolver.bind(resolverAddress);
const isLocalIntegrationTesting = getIsLocalIntegrationTesting();
const version = isLocalIntegrationTesting ? "test" : "v1";
const result = resolverContract.try_get(
"supertokens." + version + "." + token.symbol
);
const superTokenAddress = result.reverted ? ZERO_ADDRESS : result.value;
return token.id == superTokenAddress.toHex();
}
export function getTokenInfoAndReturn(token: Token): Token {
const tokenAddress = Address.fromString(token.id);
const tokenContract = SuperToken.bind(tokenAddress);
const underlyingAddressResult = tokenContract.try_getUnderlyingToken();
const nameResult = tokenContract.try_name();
const symbolResult = tokenContract.try_symbol();
const decimalsResult = tokenContract.try_decimals();
token.underlyingAddress = underlyingAddressResult.reverted
? ZERO_ADDRESS
: underlyingAddressResult.value;
token.name = nameResult.reverted ? "" : nameResult.value;
token.symbol = symbolResult.reverted ? "" : symbolResult.value;
token.decimals = decimalsResult.reverted ? 0 : decimalsResult.value;
log.info("Got token info: underlying {}, name {}, symbol {}, decimals {}", [token.underlyingAddress.toHexString(), token.name, token.symbol, token.decimals.toString()]);
return token;
}
/**
* Helper function which finds out whether a token has a valid host address.
* If it does not, we should not create any HOL/events related to the token.
* @param hostAddress
* @param tokenAddress
* @returns
*/
export function tokenHasValidHost(
hostAddress: Address,
tokenAddress: Address
): boolean {
const tokenId = tokenAddress.toHex();
if (Token.load(tokenId) == null) {
const tokenContract = SuperToken.bind(tokenAddress);
const tokenHostAddressResult = tokenContract.try_getHost();
if (tokenHostAddressResult.reverted) {
log.error("REVERTED GET HOST = {}", [tokenId]);
return false;
}
return tokenHostAddressResult.value.toHex() == hostAddress.toHex();
}
return true;
}
// Get Higher Order Entity ID functions
// CFA Higher Order Entity
export function getStreamRevisionID(
senderAddress: Address,
receiverAddress: Address,
tokenAddress: Address
): string {
const values: Array<ethereum.Value> = [
ethereum.Value.fromAddress(senderAddress),
ethereum.Value.fromAddress(receiverAddress),
];
const flowId = crypto.keccak256(encode(values));
return flowId.toHex() + "-" + tokenAddress.toHex();
}
export function getStreamID(
senderAddress: Address,
receiverAddress: Address,
tokenAddress: Address,
revisionIndex: number
): string {
return (
senderAddress.toHex() +
"-" +
receiverAddress.toHex() +
"-" +
tokenAddress.toHex() +
"-" +
revisionIndex.toString()
);
}
export function getStreamPeriodID(
streamId: string,
periodRevisionIndex: number
): string {
return streamId + "-" + periodRevisionIndex.toString();
}
export function getFlowOperatorID(
flowOperatorAddress: Address,
tokenAddress: Address,
senderAddress: Address
): string {
return (
flowOperatorAddress.toHex() +
"-" +
tokenAddress.toHex() +
"-" +
senderAddress.toHex()
);
}
// IDA Higher Order Entity
export function getSubscriptionID(
subscriberAddress: Address,
publisherAddress: Address,
tokenAddress: Address,
indexId: BigInt
): string {
return (
subscriberAddress.toHex() +
"-" +
publisherAddress.toHex() +
"-" +
tokenAddress.toHex() +
"-" +
indexId.toString()
);
}
export function getIndexID(
publisherAddress: Address,
tokenAddress: Address,
indexId: BigInt
): string {
return (
publisherAddress.toHex() +
"-" +
tokenAddress.toHex() +
"-" +
indexId.toString()
);
}
export function getPoolMemberID(
poolAddress: Address,
poolMemberAddress: Address
): string {
return (
"poolMember-" + poolAddress.toHex() + "-" + poolMemberAddress.toHex()
);
}
export function getPoolDistributorID(
poolAddress: Address,
poolDistributorAddress: Address
): string {
return (
"poolDistributor-" +
poolAddress.toHex() +
"-" +
poolDistributorAddress.toHex()
);
}
// Get Aggregate ID functions
export function getAccountTokenSnapshotID(
accountAddress: Address,
tokenAddress: Address
): string {
return accountAddress.toHex() + "-" + tokenAddress.toHex();
}
// Get HOL Exists Functions
/**
* If your units get set to 0, you will still have a subscription
* entity, but your subscription technically no longer exists.
* Similarly, you may be approved, but the subscription by this
* definition does not exist.
* @param id
* @returns
*/
export function subscriptionWithUnitsExists(id: string): boolean {
const subscription = IndexSubscription.load(id);
return subscription != null && subscription.units.gt(BIG_INT_ZERO);
}
/**
* If your units get set to 0, you will still have a pool member
* entity, but your pool member technically no longer exists.
* Similarly, you may be approved, but the pool member by this
* definition does not exist.
* @param id
* @returns
*/
export function membershipWithUnitsExists(id: string): boolean {
const poolMembership = PoolMember.load(id);
return poolMembership != null && poolMembership.units.gt(BIG_INT_ZERO);
}
export function getAmountStreamedSinceLastUpdatedAt(
currentTime: BigInt,
lastUpdatedTime: BigInt,
flowRate: BigInt
): BigInt {
const timeDelta = currentTime.minus(lastUpdatedTime);
return timeDelta.times(flowRate);
}
export function getActiveStreamsDelta(
isCreate: boolean,
isDelete: boolean
): i32 {
return isCreate ? 1 : isDelete ? -1 : 0;
}
export function getClosedStreamsDelta(isDelete: boolean): i32 {
return isDelete ? 1 : 0;
}
/**
* calculateMaybeCriticalAtTimestamp will return optimistic date based on updatedAtTimestamp, balanceUntilUpdatedAt and totalNetFlowRate.
* @param updatedAtTimestamp
* @param balanceUntilUpdatedAt
* @param totalNetFlowRate
* @param previousMaybeCriticalAtTimestamp
*/
export function calculateMaybeCriticalAtTimestamp(
updatedAtTimestamp: BigInt,
balanceUntilUpdatedAt: BigInt,
totalNetFlowRate: BigInt,
previousMaybeCriticalAtTimestamp: BigInt | null
): BigInt | null {
// When the flow rate is not negative then there's no way to have a critical balance timestamp anymore.
if (totalNetFlowRate.ge(BIG_INT_ZERO)) return null;
// When there's no balance then that either means:
// 1. account is already critical, and we keep the existing timestamp when the liquidations supposedly started
// 2. it's a new account without a critical balance timestamp to begin with
if (balanceUntilUpdatedAt.le(BIG_INT_ZERO))
return previousMaybeCriticalAtTimestamp;
const secondsUntilCritical = balanceUntilUpdatedAt.div(
totalNetFlowRate.abs()
);
const calculatedCriticalTimestamp =
updatedAtTimestamp.plus(secondsUntilCritical);
if (calculatedCriticalTimestamp.gt(MAX_SAFE_SECONDS)) {
return MAX_SAFE_SECONDS;
}
return calculatedCriticalTimestamp;
}
/**
* getOrder calculate order based on {blockNumber.times(10000).plus(logIndex)}.
* @param blockNumber
* @param logIndex
*/
export function getOrder(blockNumber: BigInt, logIndex: BigInt): BigInt {
return blockNumber.times(ORDER_MULTIPLIER).plus(logIndex);
}
/**************************************************************************
* Log entities util functions
*************************************************************************/
export function createLogID(
logPrefix: string,
accountTokenSnapshotId: string,
event: ethereum.Event
): string {
return (
logPrefix +
"-" +
accountTokenSnapshotId +
"-" +
event.transaction.hash.toHexString() +
"-" +
event.logIndex.toString()
);
}
export function divideOrZero(
numerator: BigInt,
denominator: BigInt
): BigInt {
return denominator.equals(BIG_INT_ZERO)
? BIG_INT_ZERO
: numerator.div(denominator);
}