/
indexer.ts
527 lines (486 loc) · 16.2 KB
/
indexer.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import axios from "axios";
import { AnyNumber } from "../bcs/types";
import { HexString, MaybeHexString, CUSTOM_REQUEST_HEADER } from "../utils";
import {
GetAccountTokensCountQuery,
GetAccountCoinsDataQuery,
GetAccountCurrentTokensQuery,
GetAccountTransactionsCountQuery,
GetAccountTransactionsDataQuery,
GetNumberOfDelegatorsQuery,
GetDelegatedStakingActivitiesQuery,
GetIndexerLedgerInfoQuery,
GetTokenActivitiesCountQuery,
GetTokenActivitiesQuery,
GetTokenDataQuery,
GetTokenOwnersDataQuery,
GetTopUserTransactionsQuery,
GetUserTransactionsQuery,
GetOwnedTokensQuery,
GetTokenOwnedFromCollectionQuery,
GetCollectionDataQuery,
GetCollectionsWithOwnedTokensQuery,
} from "../indexer/generated/operations";
import {
GetAccountTokensCount,
GetAccountCoinsData,
GetAccountCurrentTokens,
GetAccountTransactionsCount,
GetAccountTransactionsData,
GetNumberOfDelegators,
GetDelegatedStakingActivities,
GetIndexerLedgerInfo,
GetTokenActivities,
GetTokenActivitiesCount,
GetTokenData,
GetTokenOwnersData,
GetTopUserTransactions,
GetUserTransactions,
GetOwnedTokens,
GetTokenOwnedFromCollection,
GetCollectionData,
GetCollectionsWithOwnedTokens,
} from "../indexer/generated/queries";
/**
* Controls the number of results that are returned and the starting position of those results.
* limit specifies the maximum number of items or records to return in a query result.
* offset parameter specifies the starting position of the query result within the set of data.
* For example, if you want to retrieve records 11-20,
* you would set the offset parameter to 10 (i.e., the index of the first record to retrieve is 10)
* and the limit parameter to 10 (i.e., the number of records to retrieve is 10))
*/
interface PaginationArgs {
offset?: AnyNumber;
limit?: number;
}
type TokenStandard = "v1" | "v2";
type GraphqlQuery = {
query: string;
variables?: {};
};
/**
* Provides methods for retrieving data from Aptos Indexer.
* For more detailed Queries specification see
* {@link https://cloud.hasura.io/public/graphiql?endpoint=https://indexer.mainnet.aptoslabs.com/v1/graphql}
*/
export class IndexerClient {
endpoint: string;
/**
* @param endpoint URL of the Aptos Indexer API endpoint.
*/
constructor(endpoint: string) {
this.endpoint = endpoint;
}
/**
* Indexer only accepts address in the long format, i.e a 66 chars long -> 0x<64 chars>
* This method makes sure address is 66 chars long.
* @param address
*/
static validateAddress(address: string): void {
if (address.length < 66) {
throw new Error(`${address} is less than 66 chars long.`);
}
}
/**
* Builds a axios client call to fetch data from Aptos Indexer.
*
* @param graphqlQuery A GraphQL query to pass in the `data` axios call.
*/
async queryIndexer<T>(graphqlQuery: GraphqlQuery): Promise<T> {
const { data } = await axios.post(this.endpoint, graphqlQuery, {
headers: CUSTOM_REQUEST_HEADER,
});
if (data.errors) {
throw new Error(`Indexer data error ${JSON.stringify(data.errors, null, " ")}`);
}
return data.data;
}
/**
* Queries Indexer Ledger Info
*
* @returns GetLedgerInfoQuery response type
*/
async getIndexerLedgerInfo(): Promise<GetIndexerLedgerInfoQuery> {
const graphqlQuery = {
query: GetIndexerLedgerInfo,
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries an Aptos account's NFTs by owner address
*
* @param ownerAddress Hex-encoded 32 byte Aptos account address
* @returns GetAccountCurrentTokensQuery response type
*/
async getAccountNFTs(ownerAddress: MaybeHexString, options?: PaginationArgs): Promise<GetAccountCurrentTokensQuery> {
const address = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetAccountCurrentTokens,
variables: { address, offset: options?.offset, limit: options?.limit },
};
return this.queryIndexer<GetAccountCurrentTokensQuery>(graphqlQuery);
}
/**
* Queries a token activities by token id hash
*
* @param idHash token id hash
* @returns GetTokenActivitiesQuery response type
*/
async getTokenActivities(idHash: string, options?: PaginationArgs): Promise<GetTokenActivitiesQuery> {
const graphqlQuery = {
query: GetTokenActivities,
variables: { idHash, offset: options?.offset, limit: options?.limit },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries an account coin data
*
* @param ownerAddress Owner address
* @returns GetAccountCoinsDataQuery response type
*/
async getAccountCoinsData(ownerAddress: MaybeHexString, options?: PaginationArgs): Promise<GetAccountCoinsDataQuery> {
const address = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetAccountCoinsData,
variables: { owner_address: address, offset: options?.offset, limit: options?.limit },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Gets the count of tokens owned by an account
*
* @param ownerAddress Owner address
* @returns AccountTokensCountQuery response type
*/
async getAccountTokensCount(ownerAddress: MaybeHexString): Promise<GetAccountTokensCountQuery> {
const address = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetAccountTokensCount,
variables: { owner_address: address },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Gets the count of transactions submitted by an account
*
* @param address Account address
* @returns GetAccountTransactionsCountQuery response type
*/
async getAccountTransactionsCount(accountAddress: MaybeHexString): Promise<GetAccountTransactionsCountQuery> {
const address = HexString.ensure(accountAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetAccountTransactionsCount,
variables: { address },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries an account transactions data
*
* @param address Account address
* @returns GetAccountTransactionsDataQuery response type
*/
async getAccountTransactionsData(
accountAddress: MaybeHexString,
options?: PaginationArgs,
): Promise<GetAccountTransactionsDataQuery> {
const address = HexString.ensure(accountAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetAccountTransactionsData,
variables: { address, offset: options?.offset, limit: options?.limit },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries delegated staking activities
*
* @param delegatorAddress Delegator address
* @param poolAddress Pool address
* @returns GetDelegatedStakingActivitiesQuery response type
*/
async getDelegatedStakingActivities(
delegatorAddress: MaybeHexString,
poolAddress: MaybeHexString,
): Promise<GetDelegatedStakingActivitiesQuery> {
const delegator = HexString.ensure(delegatorAddress).hex();
const pool = HexString.ensure(poolAddress).hex();
IndexerClient.validateAddress(delegator);
IndexerClient.validateAddress(pool);
const graphqlQuery = {
query: GetDelegatedStakingActivities,
variables: {
delegatorAddress: delegator,
poolAddress: pool,
},
};
return this.queryIndexer(graphqlQuery);
}
/**
* Gets the count of token's activities
*
* @param tokenId Token ID
* @returns GetTokenActivitiesCountQuery response type
*/
async getTokenActivitiesCount(tokenId: string): Promise<GetTokenActivitiesCountQuery> {
const graphqlQuery = {
query: GetTokenActivitiesCount,
variables: { token_id: tokenId },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries token data
*
* @param tokenId Token ID
* @returns GetTokenDataQuery response type
*/
async getTokenData(tokenId: string): Promise<GetTokenDataQuery> {
const graphqlQuery = {
query: GetTokenData,
variables: { token_id: tokenId },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries token owners data
*
* @param tokenId Token ID
* @param propertyVersion Property version
* @returns GetTokenOwnersDataQuery response type
*/
async getTokenOwnersData(tokenId: string, propertyVersion: number): Promise<GetTokenOwnersDataQuery> {
const graphqlQuery = {
query: GetTokenOwnersData,
variables: { token_id: tokenId, property_version: propertyVersion },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries top user transactions
*
* @param limit
* @returns GetTopUserTransactionsQuery response type
*/
async getTopUserTransactions(limit: number): Promise<GetTopUserTransactionsQuery> {
const graphqlQuery = {
query: GetTopUserTransactions,
variables: { limit },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries top user transactions
*
* @returns GetUserTransactionsQuery response type
*/
async getUserTransactions(startVersion?: number, options?: PaginationArgs): Promise<GetUserTransactionsQuery> {
const graphqlQuery = {
query: GetUserTransactions,
variables: { start_version: startVersion, offset: options?.offset, limit: options?.limit },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries current number of delegators in a pool
*
* @returns GetNumberOfDelegatorsQuery response type
*/
async getNumberOfDelegators(poolAddress: MaybeHexString): Promise<GetNumberOfDelegatorsQuery> {
const address = HexString.ensure(poolAddress).hex();
IndexerClient.validateAddress(address);
const graphqlQuery = {
query: GetNumberOfDelegators,
variables: { poolAddress: address },
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries account's current owned tokens.
* This query returns all tokens (v1 and v2 standards) an account owns, including NFTs, fungible, soulbound, etc.
* If you want to get only the token from a specific standrd, you can pass an optional tokenStandard param
* @example An example of how to pass a specific token standard
* ```
* {
* tokenStandard:"v2"
* }
* ```
* @param ownerAddress The token owner address we want to get the tokens for
* @returns GetOwnedTokensQuery response type
*/
async getOwnedTokens(
ownerAddress: MaybeHexString,
extraArgs?: {
tokenStandard?: TokenStandard;
options?: PaginationArgs;
},
): Promise<GetOwnedTokensQuery> {
const address = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(address);
const whereCondition: any = {
owner_address: { _eq: address },
amount: { _gt: 0 },
};
if (extraArgs?.tokenStandard) {
whereCondition.token_standard = { _eq: extraArgs?.tokenStandard };
}
const graphqlQuery = {
query: GetOwnedTokens,
variables: {
where_condition: whereCondition,
offset: extraArgs?.options?.offset,
limit: extraArgs?.options?.limit,
},
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries all tokens of a specific collection that an account owns by the collection address
*
* @param ownerAddress owner address that owns the tokens
* @param collectionAddress the collection address
* @returns GetTokenOwnedFromCollectionQuery response type
*/
async getTokenOwnedFromCollectionAddress(
ownerAddress: MaybeHexString,
collectionAddress: string,
extraArgs?: {
tokenStandard?: TokenStandard;
options?: PaginationArgs;
},
): Promise<GetTokenOwnedFromCollectionQuery> {
const ownerHexAddress = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(ownerHexAddress);
const collectionHexAddress = HexString.ensure(collectionAddress).hex();
IndexerClient.validateAddress(collectionHexAddress);
const whereCondition: any = {
owner_address: { _eq: ownerHexAddress },
current_token_data: { collection_id: { _eq: collectionHexAddress } },
amount: { _gt: 0 },
};
if (extraArgs?.tokenStandard) {
whereCondition.token_standard = { _eq: extraArgs?.tokenStandard };
}
const graphqlQuery = {
query: GetTokenOwnedFromCollection,
variables: {
where_condition: whereCondition,
offset: extraArgs?.options?.offset,
limit: extraArgs?.options?.limit,
},
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries all tokens of a specific collection that an account owns by the collection name and collection
* creator address
*
* @param ownerAddress owner address that owns the tokens
* @param collectionName the collection name
* @param creatorAddress the collection creator address
* @returns GetTokenOwnedFromCollectionQuery response type
*/
async getTokenOwnedFromCollectionNameAndCreatorAddress(
ownerAddress: MaybeHexString,
collectionName: string,
creatorAddress: MaybeHexString,
extraArgs?: {
tokenStandard?: TokenStandard;
options?: PaginationArgs;
},
): Promise<GetTokenOwnedFromCollectionQuery> {
const collectionAddress = await this.getCollectionAddress(creatorAddress, collectionName, extraArgs);
const tokens = await this.getTokenOwnedFromCollectionAddress(ownerAddress, collectionAddress, extraArgs);
return tokens;
}
/**
* Queries data of a specific collection by the collection creator address and the collection name.
*
* if, for some reason, a creator account has 2 collections with the same name in v1 and v2,
* can pass an optional `tokenStandard` parameter to query a specific standard
*
* @param creatorAddress the collection creator address
* @param collectionName the collection name
* @returns GetCollectionDataQuery response type
*/
async getCollectionData(
creatorAddress: MaybeHexString,
collectionName: string,
extraArgs?: {
tokenStandard?: TokenStandard;
options?: PaginationArgs;
},
): Promise<GetCollectionDataQuery> {
const address = HexString.ensure(creatorAddress).hex();
IndexerClient.validateAddress(address);
const whereCondition: any = {
collection_name: { _eq: collectionName },
creator_address: { _eq: address },
};
if (extraArgs?.tokenStandard) {
whereCondition.token_standard = { _eq: extraArgs?.tokenStandard };
}
const graphqlQuery = {
query: GetCollectionData,
variables: {
where_condition: whereCondition,
offset: extraArgs?.options?.offset,
limit: extraArgs?.options?.limit,
},
};
return this.queryIndexer(graphqlQuery);
}
/**
* Queries a collection address.
*
* @param creatorAddress the collection creator address
* @param collectionName the collection name
* @returns the collection address
*/
async getCollectionAddress(
creatorAddress: MaybeHexString,
collectionName: string,
extraArgs?: {
tokenStandard?: TokenStandard;
},
): Promise<string> {
return (await this.getCollectionData(creatorAddress, collectionName, extraArgs)).current_collections_v2[0]
.collection_id;
}
/**
* Queries for all collections that an account has tokens for.
*
* @param ownerAddress the account address that owns the tokens
* @returns GetCollectionsWithOwnedTokensQuery response type
*/
async getCollectionsWithOwnedTokens(
ownerAddress: MaybeHexString,
extraArgs?: {
tokenStandard?: TokenStandard;
options?: PaginationArgs;
},
): Promise<GetCollectionsWithOwnedTokensQuery> {
const ownerHexAddress = HexString.ensure(ownerAddress).hex();
IndexerClient.validateAddress(ownerHexAddress);
const whereCondition: any = {
owner_address: { _eq: ownerHexAddress },
};
if (extraArgs?.tokenStandard) {
whereCondition.current_collection = { token_standard: { _eq: extraArgs?.tokenStandard } };
}
const graphqlQuery = {
query: GetCollectionsWithOwnedTokens,
variables: {
where_condition: whereCondition,
offset: extraArgs?.options?.offset,
limit: extraArgs?.options?.limit,
},
};
return this.queryIndexer(graphqlQuery);
}
}