-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathallocations.ts
308 lines (287 loc) · 8.66 KB
/
allocations.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
import { SubgraphDeploymentID, formatGRT } from '@graphprotocol/common-ts'
import yaml from 'yaml'
import { GluegunPrint } from 'gluegun'
import { table, getBorderCharacters } from 'table'
import { BigNumber, utils } from 'ethers'
import { OutputFormat, parseOutputFormat, pickFields } from './command-helpers'
import { IndexerManagementClient } from '@graphprotocol/indexer-common'
import gql from 'graphql-tag'
import {
CloseAllocationResult,
CreateAllocationResult,
ReallocateAllocationResult,
} from '@graphprotocol/indexer-common'
export interface IndexerAllocation {
id: number
indexer: string
subgraphDeployment: string
allocatedTokens: BigNumber
signalledTokens: BigNumber
stakedTokens: BigNumber
createdAtEpoch: number
closedAtEpoch: number | null
ageInEpochs: number
closeDeadlineEpoch: number
closeDeadlineBlocksRemaining: number
closeDeadlineTimeRemaining: number
indexingRewards: BigNumber
queryFeesCollected: BigNumber
status: string
}
const ALLOCATION_CONVERTERS_FROM_GRAPHQL: Record<
keyof IndexerAllocation,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(x: never) => any
> = {
id: x => x,
indexer: x => x,
subgraphDeployment: (d: SubgraphDeploymentID) =>
typeof d === 'string' ? d : d.ipfsHash,
allocatedTokens: nullPassThrough((x: string) => BigNumber.from(x)),
signalledTokens: nullPassThrough((x: string) => BigNumber.from(x)),
stakedTokens: nullPassThrough((x: string) => BigNumber.from(x)),
createdAtEpoch: nullPassThrough((x: string) => parseInt(x)),
closedAtEpoch: nullPassThrough((x: string) => parseInt(x)),
ageInEpochs: nullPassThrough((x: string) => parseInt(x)),
closeDeadlineEpoch: nullPassThrough((x: string) => parseInt(x)),
closeDeadlineBlocksRemaining: nullPassThrough((x: string) => parseInt(x)),
closeDeadlineTimeRemaining: nullPassThrough((x: string) => parseInt(x)),
indexingRewards: nullPassThrough((x: string) => BigNumber.from(x)),
queryFeesCollected: nullPassThrough((x: string) => BigNumber.from(x)),
status: x => x,
}
const ALLOCATION_FORMATTERS: Record<
keyof IndexerAllocation,
(x: never) => string | null
> = {
id: nullPassThrough(x => x),
indexer: nullPassThrough(x => x),
subgraphDeployment: (d: SubgraphDeploymentID) =>
typeof d === 'string' ? d : d.ipfsHash,
allocatedTokens: x => utils.commify(formatGRT(x)),
signalledTokens: x => utils.commify(formatGRT(x)),
stakedTokens: x => utils.commify(formatGRT(x)),
createdAtEpoch: x => x,
closedAtEpoch: x => x,
ageInEpochs: x => x,
closeDeadlineEpoch: x => x,
closeDeadlineBlocksRemaining: x => x,
closeDeadlineTimeRemaining: x => x,
indexingRewards: x => utils.commify(formatGRT(x)),
queryFeesCollected: x => utils.commify(formatGRT(x)),
status: x => x,
}
/**
* Formats an indexer allocation for display in the console.
*/
export const formatIndexerAllocation = (
allocation: Partial<IndexerAllocation>,
): Partial<IndexerAllocation> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const obj = {} as any
for (const [key, value] of Object.entries(allocation)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
obj[key] = (ALLOCATION_FORMATTERS as any)[key](value)
}
return obj as Partial<IndexerAllocation>
}
/**
* Parses an indexer allocation returned from the indexer management GraphQL
* API into normalized form.
*/
export const indexerAllocationFromGraphQL = (
allocation: Partial<IndexerAllocation>,
): Partial<IndexerAllocation> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const obj = {} as any
for (const [key, value] of Object.entries(pickFields(allocation, []))) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
obj[key] = (ALLOCATION_CONVERTERS_FROM_GRAPHQL as any)[key](value)
}
return obj as Partial<IndexerAllocation>
}
export const printIndexerAllocations = (
print: GluegunPrint,
outputFormat: OutputFormat,
allocationOrAllocations:
| Partial<IndexerAllocation>
| Partial<IndexerAllocation>[]
| null,
keys: (keyof IndexerAllocation)[],
): void => {
parseOutputFormat(print, outputFormat)
if (Array.isArray(allocationOrAllocations)) {
const allocations = allocationOrAllocations.map(allocation =>
formatIndexerAllocation(pickFields(allocation, keys)),
)
print.info(displayIndexerAllocations(outputFormat, allocations))
} else if (allocationOrAllocations) {
const allocation = formatIndexerAllocation(pickFields(allocationOrAllocations, keys))
print.info(displayIndexerAllocation(outputFormat, allocation))
} else {
print.error(`No allocations found`)
}
}
export const displayIndexerAllocations = (
outputFormat: OutputFormat,
allocations: Partial<IndexerAllocation>[],
): string =>
outputFormat === OutputFormat.Json
? JSON.stringify(allocations, null, 2)
: outputFormat === OutputFormat.Yaml
? yaml.stringify(allocations).trim()
: allocations.length === 0
? 'No allocations found'
: table(
[
Object.keys(allocations[0]),
...allocations.map(allocation => Object.values(allocation)),
],
{
border: getBorderCharacters('norc'),
},
).trim()
export const displayIndexerAllocation = (
outputFormat: OutputFormat,
allocation: Partial<IndexerAllocation>,
): string =>
outputFormat === OutputFormat.Json
? JSON.stringify(allocation, null, 2)
: outputFormat === OutputFormat.Yaml
? yaml.stringify(allocation).trim()
: table([Object.keys(allocation), Object.values(allocation)], {
border: getBorderCharacters('norc'),
}).trim()
function nullPassThrough<T, U>(fn: (x: T) => U): (x: T | null) => U | null {
return (x: T | null) => (x === null ? null : fn(x))
}
export const createAllocation = async (
client: IndexerManagementClient,
deployment: string,
amount: BigNumber,
indexNode: string | undefined,
): Promise<CreateAllocationResult> => {
const result = await client
.mutation(
gql`
mutation createAllocation(
$deployment: String!
$amount: String!
$indexNode: String
) {
createAllocation(
deployment: $deployment
amount: $amount
indexNode: $indexNode
) {
allocation
deployment
allocatedTokens
}
}
`,
{
deployment,
amount: amount.toString(),
indexNode,
},
)
.toPromise()
if (result.error) {
throw result.error
}
return result.data.createAllocation
}
export const closeAllocation = async (
client: IndexerManagementClient,
allocationID: string,
poi: string | undefined,
force: boolean,
): Promise<CloseAllocationResult> => {
const result = await client
.mutation(
gql`
mutation closeAllocation($allocation: String!, $poi: String, $force: Boolean) {
closeAllocation(allocation: $allocation, poi: $poi, force: $force) {
allocation
allocatedTokens
indexingRewards
receiptsWorthCollecting
}
}
`,
{
allocation: allocationID,
poi,
force,
},
)
.toPromise()
if (result.error) {
throw result.error
}
return result.data.closeAllocation
}
export const reallocateAllocation = async (
client: IndexerManagementClient,
allocationID: string,
poi: string | undefined,
amount: BigNumber,
force: boolean,
): Promise<ReallocateAllocationResult> => {
const result = await client
.mutation(
gql`
mutation reallocateAllocation(
$allocation: String!
$poi: String
$amount: String!
$force: Boolean
) {
reallocateAllocation(
allocation: $allocation
poi: $poi
amount: $amount
force: $force
) {
closedAllocation
indexingRewardsCollected
receiptsWorthCollecting
createdAllocation
createdAllocationStake
}
}
`,
{
allocation: allocationID,
poi,
amount: amount.toString(),
force,
},
)
.toPromise()
if (result.error) {
throw result.error
}
return result.data.reallocateAllocation
}
export const submitCollectReceiptsJob = async (
client: IndexerManagementClient,
allocationID: string,
): Promise<void> => {
const result = await client
.mutation(
gql`
mutation submitCollectReceiptsJob($allocation: String!) {
submitCollectReceiptsJob(allocation: $allocation)
}
`,
{
allocation: allocationID,
},
)
.toPromise()
if (result.error) {
throw result.error
}
}