-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.ts
149 lines (134 loc) · 3.76 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
import {
Address,
BigInt,
ByteArray,
Bytes,
crypto,
DataSourceTemplate,
ethereum,
log,
store,
Value,
} from '@graphprotocol/graph-ts';
import {
BlacklistedDAO,
ContractInfo,
ControllerScheme,
Debug,
TemplateInfo,
} from './types/schema';
export function concat(a: ByteArray, b: ByteArray): ByteArray {
let out = new Uint8Array(a.length + b.length);
for (let i = 0; i < a.length; i++) {
out[i] = a[i];
}
for (let j = 0; j < b.length; j++) {
out[a.length + j] = b[j];
}
return out as ByteArray;
}
export const CLOSING_AT_TIME_DECREASE_GSMC = 32000000;
export const CLOSING_AT_TIME_INCREASE = 2147483647;
export function eventId(event: ethereum.Event): string {
return crypto
.keccak256(
concat(event.transaction.hash, event.transactionLogIndex as ByteArray),
)
.toHex();
}
export function hexToAddress(hex: string): Address {
return Address.fromString(hex.substr(2));
}
/**
* WORKAROUND: there's no `console.log` functionality in mapping.
* so we use `debug(..)` which writes a `Debug` entity to the store so you can see them in graphiql.
*/
let debugId = 0;
export function debug(msg: string): void {
let id = BigInt.fromI32(debugId).toHex();
let ent = new Debug(id);
ent.set('message', Value.fromString(msg));
store.set('Debug', id, ent);
debugId++;
}
export function equalsBytes(a: Bytes, b: Bytes): boolean {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
export function equalStrings(a: string, b: string): boolean {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i].charCodeAt(0) !== b[i].charCodeAt(0)) {
return false;
}
}
return true;
}
export function setContractInfo(address: string, name: string, alias: string, version: string): void {
let contractInfo = ContractInfo.load(address);
if (contractInfo == null) {
contractInfo = new ContractInfo(address);
contractInfo.address = Address.fromString(address);
contractInfo.name = name;
contractInfo.alias = alias;
contractInfo.version = version;
contractInfo.save();
}
}
export function setTemplateInfo(name: string, version: string, templateName: string): void {
let id = name.concat(version);
let templateInfo = TemplateInfo.load(id);
if (templateInfo == null) {
templateInfo = new TemplateInfo(id);
templateInfo.templateName = templateName;
templateInfo.save();
}
}
export function fetchTemplateName(name: string, version: string): string | null {
let id = name.concat(version);
let templateInfo = TemplateInfo.load(id);
if (templateInfo == null) {
return null;
} else {
return templateInfo.templateName;
}
}
export function createTemplate(templateName: string, address: Address): void {
DataSourceTemplate.create(templateName, [address.toHex()]);
}
export function setBlacklistedDAO(address: string): void {
let blacklistedDAO = BlacklistedDAO.load(address);
if (blacklistedDAO == null) {
blacklistedDAO = new BlacklistedDAO(address);
blacklistedDAO.save();
}
}
export function fixJsonQuotes(target: string): string {
let targetIndex = 0;
let result = '';
for (targetIndex = 0; targetIndex < target.length; targetIndex++) {
if (target[targetIndex] === '"') {
result += '\\"';
} else {
result += target[targetIndex];
}
}
return result;
}
export function setSchemeError(schemeId: string, errorCode: BigInt, errorMsg: string): void {
let controllerScheme = ControllerScheme.load(schemeId);
if (controllerScheme != null) {
controllerScheme.error = errorCode;
controllerScheme.save();
}
log.info(errorMsg, []);
}