@o1-labs/mina-archive-node-graphql 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/README.md +321 -0
  2. package/build/benchmark/setup.js +32 -0
  3. package/build/benchmark/setup.js.map +1 -0
  4. package/build/src/blockchain/constants.js +2 -0
  5. package/build/src/blockchain/constants.js.map +1 -0
  6. package/build/src/blockchain/types.js +7 -0
  7. package/build/src/blockchain/types.js.map +1 -0
  8. package/build/src/blockchain/utils.js +39 -0
  9. package/build/src/blockchain/utils.js.map +1 -0
  10. package/build/src/consensus/mina-consensus.js +59 -0
  11. package/build/src/consensus/mina-consensus.js.map +1 -0
  12. package/build/src/context.js +10 -0
  13. package/build/src/context.js.map +1 -0
  14. package/build/src/db/archive-node-adapter/archive-node-adapter.interface.js +2 -0
  15. package/build/src/db/archive-node-adapter/archive-node-adapter.interface.js.map +1 -0
  16. package/build/src/db/archive-node-adapter/archive-node-adapter.js +47 -0
  17. package/build/src/db/archive-node-adapter/archive-node-adapter.js.map +1 -0
  18. package/build/src/db/sql/events-actions/queries.js +438 -0
  19. package/build/src/db/sql/events-actions/queries.js.map +1 -0
  20. package/build/src/db/sql/events-actions/types.js +2 -0
  21. package/build/src/db/sql/events-actions/types.js.map +1 -0
  22. package/build/src/errors/error.js +17 -0
  23. package/build/src/errors/error.js.map +1 -0
  24. package/build/src/index.js +27 -0
  25. package/build/src/index.js.map +1 -0
  26. package/build/src/resolvers-types.js +8 -0
  27. package/build/src/resolvers-types.js.map +1 -0
  28. package/build/src/resolvers.js +67 -0
  29. package/build/src/resolvers.js.map +1 -0
  30. package/build/src/server/plugins.js +39 -0
  31. package/build/src/server/plugins.js.map +1 -0
  32. package/build/src/server/server.js +25 -0
  33. package/build/src/server/server.js.map +1 -0
  34. package/build/src/services/actions-service/actions-service.interface.js +2 -0
  35. package/build/src/services/actions-service/actions-service.interface.js.map +1 -0
  36. package/build/src/services/actions-service/actions-service.js +94 -0
  37. package/build/src/services/actions-service/actions-service.js.map +1 -0
  38. package/build/src/services/blocks-service/blocks-service.interface.js +2 -0
  39. package/build/src/services/blocks-service/blocks-service.interface.js.map +1 -0
  40. package/build/src/services/blocks-service/blocks-service.js +263 -0
  41. package/build/src/services/blocks-service/blocks-service.js.map +1 -0
  42. package/build/src/services/data-adapters/database-row-adapters.js +116 -0
  43. package/build/src/services/data-adapters/database-row-adapters.js.map +1 -0
  44. package/build/src/services/events-service/events-service.interface.js +2 -0
  45. package/build/src/services/events-service/events-service.interface.js.map +1 -0
  46. package/build/src/services/events-service/events-service.js +64 -0
  47. package/build/src/services/events-service/events-service.js.map +1 -0
  48. package/build/src/services/network-service/network-service.interface.js +2 -0
  49. package/build/src/services/network-service/network-service.interface.js.map +1 -0
  50. package/build/src/services/network-service/network-service.js +31 -0
  51. package/build/src/services/network-service/network-service.js.map +1 -0
  52. package/build/src/tracing/jaeger-setup.js +39 -0
  53. package/build/src/tracing/jaeger-setup.js.map +1 -0
  54. package/build/src/tracing/jaeger-tracing.js +29 -0
  55. package/build/src/tracing/jaeger-tracing.js.map +1 -0
  56. package/build/src/tracing/tracer.js +31 -0
  57. package/build/src/tracing/tracer.js.map +1 -0
  58. package/build/tests/__mocks__/generate_mock_data.js +13 -0
  59. package/build/tests/__mocks__/generate_mock_data.js.map +1 -0
  60. package/build/tests/consensus/run-compare.js +40 -0
  61. package/build/tests/consensus/run-compare.js.map +1 -0
  62. package/build/tests/consensus/select-consensus-precomputed.js +80 -0
  63. package/build/tests/consensus/select-consensus-precomputed.js.map +1 -0
  64. package/build/tests/consensus/types.js +15 -0
  65. package/build/tests/consensus/types.js.map +1 -0
  66. package/build/tests/devnet-dump/devnet-dump.test.js +311 -0
  67. package/build/tests/devnet-dump/devnet-dump.test.js.map +1 -0
  68. package/build/tests/devnet-dump/setup.js +58 -0
  69. package/build/tests/devnet-dump/setup.js.map +1 -0
  70. package/build/tests/integration/integration.test.js +226 -0
  71. package/build/tests/integration/integration.test.js.map +1 -0
  72. package/build/tests/integration/setup.js +82 -0
  73. package/build/tests/integration/setup.js.map +1 -0
  74. package/build/tests/live-api/actions.test.js +4 -0
  75. package/build/tests/live-api/actions.test.js.map +1 -0
  76. package/build/tests/live-api/blocks.test.js +116 -0
  77. package/build/tests/live-api/blocks.test.js.map +1 -0
  78. package/build/tests/live-api/events.test.js +97 -0
  79. package/build/tests/live-api/events.test.js.map +1 -0
  80. package/build/tests/live-api/networkState.test.js +27 -0
  81. package/build/tests/live-api/networkState.test.js.map +1 -0
  82. package/build/tests/live-network/live-network.test.js +144 -0
  83. package/build/tests/live-network/live-network.test.js.map +1 -0
  84. package/build/tests/live-network/setup.js +206 -0
  85. package/build/tests/live-network/setup.js.map +1 -0
  86. package/build/tests/resolvers.test.js +475 -0
  87. package/build/tests/resolvers.test.js.map +1 -0
  88. package/build/tests/services/actions-service/actions-service.test.js +124 -0
  89. package/build/tests/services/actions-service/actions-service.test.js.map +1 -0
  90. package/build/tests/services/blocks-service/blocks-service.test.js +348 -0
  91. package/build/tests/services/blocks-service/blocks-service.test.js.map +1 -0
  92. package/build/tests/services/events-service/events-service.test.js +185 -0
  93. package/build/tests/services/events-service/events-service.test.js.map +1 -0
  94. package/build/tests/test-helpers.js +11 -0
  95. package/build/tests/test-helpers.js.map +1 -0
  96. package/build/tests/unit/blockchain-utils.test.js +106 -0
  97. package/build/tests/unit/blockchain-utils.test.js.map +1 -0
  98. package/build/tests/unit/consensus.test.js +154 -0
  99. package/build/tests/unit/consensus.test.js.map +1 -0
  100. package/build/tests/unit/error-handling.test.js +52 -0
  101. package/build/tests/unit/error-handling.test.js.map +1 -0
  102. package/build/tests/utils.test.js +313 -0
  103. package/build/tests/utils.test.js.map +1 -0
  104. package/build/zkapp/contract.js +144 -0
  105. package/build/zkapp/contract.js.map +1 -0
  106. package/build/zkapp/utils.js +174 -0
  107. package/build/zkapp/utils.js.map +1 -0
  108. package/package.json +85 -0
  109. package/schema.graphql +221 -0
  110. package/src/blockchain/constants.ts +2 -0
  111. package/src/blockchain/types.ts +121 -0
  112. package/src/blockchain/utils.ts +54 -0
  113. package/src/consensus/mina-consensus.ts +137 -0
  114. package/src/context.ts +18 -0
  115. package/src/db/archive-node-adapter/archive-node-adapter.interface.ts +23 -0
  116. package/src/db/archive-node-adapter/archive-node-adapter.ts +101 -0
  117. package/src/db/sql/events-actions/queries.ts +490 -0
  118. package/src/db/sql/events-actions/types.ts +208 -0
  119. package/src/envionment.d.ts +18 -0
  120. package/src/errors/error.ts +20 -0
  121. package/src/index.ts +31 -0
  122. package/src/resolvers-types.ts +754 -0
  123. package/src/resolvers.ts +102 -0
  124. package/src/server/plugins.ts +56 -0
  125. package/src/server/server.ts +30 -0
  126. package/src/services/actions-service/actions-service.interface.ts +9 -0
  127. package/src/services/actions-service/actions-service.ts +195 -0
  128. package/src/services/blocks-service/blocks-service.interface.ts +11 -0
  129. package/src/services/blocks-service/blocks-service.ts +407 -0
  130. package/src/services/data-adapters/database-row-adapters.ts +261 -0
  131. package/src/services/events-service/events-service.interface.ts +6 -0
  132. package/src/services/events-service/events-service.ts +118 -0
  133. package/src/services/network-service/network-service.interface.ts +5 -0
  134. package/src/services/network-service/network-service.ts +53 -0
  135. package/src/tracing/jaeger-setup.ts +65 -0
  136. package/src/tracing/jaeger-tracing.ts +44 -0
  137. package/src/tracing/tracer.ts +52 -0
  138. package/tsconfig.json +104 -0
@@ -0,0 +1,2 @@
1
+ export const DEFAULT_TOKEN_ID =
2
+ 'wSHV2S4qX9jFsLjQo8r1BsMLH2ZRKsZx6EJd1sbozGPieEC4Jf' as const;
@@ -0,0 +1,121 @@
1
+ import { ArchiveNodeDatabaseRow } from '../db/sql/events-actions/types.js';
2
+
3
+ export type Transactions = Map<string, ArchiveNodeDatabaseRow[]>;
4
+
5
+ export type BlocksWithTransactionsMap = Map<string, Transactions>;
6
+
7
+ export type FieldElementIdWithValueMap = Map<string, string>;
8
+
9
+ export enum BlockStatusFilter {
10
+ all = 'ALL',
11
+ pending = 'PENDING',
12
+ canonical = 'CANONICAL',
13
+ }
14
+
15
+ export type Event = {
16
+ accountUpdateId: string;
17
+ transactionInfo: TransactionInfo;
18
+ data: string[];
19
+ };
20
+
21
+ export type Action = {
22
+ accountUpdateId: string;
23
+ transactionInfo: TransactionInfo;
24
+ data: string[];
25
+ };
26
+
27
+ export type NetworkState = {
28
+ maxBlockHeight: MaxBlockHeightInfo;
29
+ };
30
+
31
+ export type MaxBlockHeightInfo = {
32
+ canonicalMaxBlockHeight: number;
33
+ pendingMaxBlockHeight: number;
34
+ };
35
+
36
+ export type BlockInfo = {
37
+ height: number;
38
+ stateHash: string;
39
+ parentHash: string;
40
+ ledgerHash: string;
41
+ chainStatus: string;
42
+ timestamp: string;
43
+ globalSlotSinceHardfork: number;
44
+ globalSlotSinceGenesis: number;
45
+ distanceFromMaxBlockHeight: number;
46
+ lastVrfOutput: string;
47
+ };
48
+
49
+ export type TransactionInfo = {
50
+ status: string;
51
+ hash: string;
52
+ memo: string;
53
+ authorizationKind: string;
54
+ sequenceNumber: number;
55
+ zkappAccountUpdateIds: number[];
56
+ };
57
+
58
+ export type Events = {
59
+ eventData: Event[];
60
+ blockInfo: BlockInfo;
61
+ }[];
62
+
63
+ export type ActionStates = {
64
+ actionStateOne: string;
65
+ actionStateTwo: string;
66
+ actionStateThree: string;
67
+ actionStateFour: string;
68
+ actionStateFive: string;
69
+ };
70
+
71
+ export type Actions = {
72
+ actionState: ActionStates;
73
+ actionData: Action[];
74
+ blockInfo: BlockInfo;
75
+ }[];
76
+
77
+ export type UserCommand = {
78
+ hash: string;
79
+ kind: string;
80
+ from: string;
81
+ to: string;
82
+ amount: string;
83
+ fee: string;
84
+ memo: string;
85
+ nonce: number;
86
+ status: string;
87
+ failureReason: string | null;
88
+ };
89
+
90
+ export type ZkAppCommand = {
91
+ hash: string;
92
+ feePayer: string;
93
+ fee: string;
94
+ memo: string;
95
+ status: string;
96
+ failureReason: string | null;
97
+ };
98
+
99
+ export type FeeTransfer = {
100
+ recipient: string;
101
+ fee: string;
102
+ type: string;
103
+ };
104
+
105
+ export type BlockTransactions = {
106
+ coinbase: string;
107
+ userCommands: UserCommand[];
108
+ zkappCommands: ZkAppCommand[];
109
+ feeTransfer: FeeTransfer[];
110
+ };
111
+
112
+ export type Block = {
113
+ blockHeight: number;
114
+ creator: string;
115
+ stateHash: string;
116
+ parentHash: string;
117
+ dateTime: string;
118
+ transactions: BlockTransactions;
119
+ };
120
+
121
+ export type Blocks = Block[];
@@ -0,0 +1,54 @@
1
+ import type { BlockInfo, TransactionInfo, Event, Action } from './types.js';
2
+ import type { ArchiveNodeDatabaseRow } from '../db/sql/events-actions/types.js';
3
+
4
+ export function createBlockInfo(row: ArchiveNodeDatabaseRow): BlockInfo {
5
+ return {
6
+ height: Number(row.height),
7
+ stateHash: row.state_hash,
8
+ parentHash: row.parent_hash,
9
+ ledgerHash: row.ledger_hash,
10
+ chainStatus: row.chain_status,
11
+ timestamp: row.timestamp,
12
+ globalSlotSinceHardfork: Number(row.global_slot_since_hard_fork),
13
+ globalSlotSinceGenesis: Number(row.global_slot_since_genesis),
14
+ distanceFromMaxBlockHeight: Number(row.distance_from_max_block_height),
15
+ lastVrfOutput: row.last_vrf_output,
16
+ };
17
+ }
18
+
19
+ export function createTransactionInfo(
20
+ row: ArchiveNodeDatabaseRow
21
+ ): TransactionInfo {
22
+ return {
23
+ status: row.status,
24
+ hash: row.hash,
25
+ memo: row.memo,
26
+ authorizationKind: row.authorization_kind,
27
+ sequenceNumber: row.sequence_number,
28
+ zkappAccountUpdateIds: row.zkapp_account_updates_ids,
29
+ };
30
+ }
31
+
32
+ export function createEvent(
33
+ accountUpdateId: string,
34
+ data: string[],
35
+ transactionInfo: TransactionInfo
36
+ ): Event {
37
+ return {
38
+ accountUpdateId,
39
+ data,
40
+ transactionInfo,
41
+ };
42
+ }
43
+
44
+ export function createAction(
45
+ accountUpdateId: string,
46
+ data: string[],
47
+ transactionInfo: TransactionInfo
48
+ ): Action {
49
+ return {
50
+ accountUpdateId,
51
+ data,
52
+ transactionInfo,
53
+ };
54
+ }
@@ -0,0 +1,137 @@
1
+ import blakejs from 'blakejs';
2
+ const { blake2bHex } = blakejs;
3
+ import { type BlockInfo } from '../blockchain/types.js';
4
+
5
+ export { select, getAllPredicate, filterBestTip };
6
+
7
+ function getAllPredicate<T>(array: T[], predicate: (arg: T) => boolean) {
8
+ const data: T[] = [];
9
+ for (let i = 0; i < array.length; i++) {
10
+ if (predicate(array[i])) {
11
+ data.push(array[i]);
12
+ }
13
+ }
14
+ return data;
15
+ }
16
+
17
+ function filterBestTip<T extends { blockInfo: BlockInfo }>(
18
+ eventOrActionData: T[]
19
+ ) {
20
+ const highestTipBlocks = getAllPredicate(
21
+ eventOrActionData,
22
+ (e) => e.blockInfo.distanceFromMaxBlockHeight === 0
23
+ );
24
+ if (highestTipBlocks.length > 1) {
25
+ const selectedBlock = chainSelect(highestTipBlocks);
26
+ const startIndex = eventOrActionData.length - highestTipBlocks.length;
27
+ eventOrActionData.splice(
28
+ startIndex,
29
+ highestTipBlocks.length,
30
+ selectedBlock
31
+ );
32
+ }
33
+ }
34
+
35
+ function chainSelect<T extends { blockInfo: BlockInfo }>(blocks: T[]) {
36
+ if (blocks.length === 1) return blocks[0];
37
+ let existing = blocks[0];
38
+ for (let i = 1; i < blocks.length; i++) {
39
+ existing = select(existing, blocks[i]);
40
+ }
41
+ return existing;
42
+ }
43
+
44
+ /**
45
+ * Selects the best chain based on Mina Protocol's short range tie breaker rules.
46
+ *
47
+ * This function mirrors the OCaml implementation and utilizes the short range rules specified
48
+ * here: https://github.com/MinaProtocol/mina/tree/develop/docs/specs/consensus#521-short-range-fork-rule
49
+ *
50
+ * The tie breaker rules are also more formally documented here: https://github.com/MinaProtocol/mina/tree/develop/docs/specs/consensus
51
+ *
52
+ * When comparing two chains:
53
+ * 1. Compare based on state hash.
54
+ * 2. If equal, fallback to VRF comparisons.
55
+ * 3. If still equal, use the chain length as the final determinant.
56
+ *
57
+ * @param {T} existing - The current chain.
58
+ * @param {T} candidate - The new chain to be compared against the current chain.
59
+ * @returns {T} - The chain that is considered the "best" based on the aforementioned criteria.
60
+ */
61
+ function select<T extends { blockInfo: BlockInfo }>(
62
+ existing: T,
63
+ candidate: T
64
+ ): T {
65
+ const candidateHashIsBigger = compareWithCondition(
66
+ existing.blockInfo.stateHash,
67
+ candidate.blockInfo.stateHash,
68
+ compareStrings,
69
+ true
70
+ );
71
+
72
+ const candidateVRFIsBigger = compareWithCondition(
73
+ existing.blockInfo,
74
+ candidate.blockInfo,
75
+ compareVRF,
76
+ candidateHashIsBigger
77
+ );
78
+
79
+ const candidateLengthIsBigger = compareWithCondition(
80
+ existing.blockInfo,
81
+ candidate.blockInfo,
82
+ compareBlockChainLength,
83
+ candidateVRFIsBigger
84
+ );
85
+
86
+ return candidateLengthIsBigger ? candidate : existing;
87
+ }
88
+
89
+ /**
90
+ * Takes two values and compares them using the provided compare function.
91
+ *
92
+ * In our compare function, the following semantics should apply:
93
+ * - If `a` is less than `b`, then a negative number should be returned.
94
+ * - If `a` is greater than `b`, then a positive number should be returned.
95
+ * - If `a` is equal to `b`, then 0 should be returned.
96
+ *
97
+ * When we call `compare`, if the result is negative, then `a` is returned.A positive result means `b` is returned.
98
+ * If the result is 0, then the `condition` is used to determine which value is returned.
99
+ * Meaning, if the `condition` is true, then `b` is returned. Otherwise, `a` is returned.
100
+ *
101
+ * @param a - The first value to compare
102
+ * @param b - The second value to compare
103
+ * @param compare - The function used to compare the two values
104
+ * @param condition - The condition used to determine which value is returned if the two values are equal
105
+ * @returns
106
+ */
107
+ function compareWithCondition<T>(
108
+ a: T,
109
+ b: T,
110
+ compare: (a: T, b: T) => number,
111
+ condition: boolean
112
+ ): boolean {
113
+ const c = compare(a, b);
114
+ return c < 0 || (c === 0 && condition);
115
+ }
116
+
117
+ function compareStrings(a: string, b: string): number {
118
+ if (a > b) return 1;
119
+ if (a < b) return -1;
120
+ return 0;
121
+ }
122
+
123
+ function getHashOfVRF(vrfOutput: string): string {
124
+ /* last_vrf_output is a sequence of hex-digit pairs derived from a bitstring */
125
+ const vrfBuf = Buffer.from(vrfOutput, 'hex');
126
+ return blake2bHex(vrfBuf);
127
+ }
128
+
129
+ function compareVRF(existing: BlockInfo, candidate: BlockInfo): number {
130
+ const existingVRFHash = getHashOfVRF(existing.lastVrfOutput);
131
+ const candidateVRFHash = getHashOfVRF(candidate.lastVrfOutput);
132
+ return compareStrings(existingVRFHash, candidateVRFHash);
133
+ }
134
+
135
+ function compareBlockChainLength(existing: BlockInfo, candidate: BlockInfo) {
136
+ return existing.height - candidate.height;
137
+ }
package/src/context.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { Span } from '@opentelemetry/sdk-trace-base';
2
+ import { ArchiveNodeAdapter } from './db/archive-node-adapter/archive-node-adapter.js';
3
+ import { DatabaseAdapter } from './db/archive-node-adapter/archive-node-adapter.interface.js';
4
+
5
+ export { buildContext, GraphQLContext };
6
+
7
+ interface GraphQLContext {
8
+ db_client: DatabaseAdapter;
9
+ [OPEN_TELEMETRY_GRAPHQL: symbol]: Span | undefined; // Will only be set if we are in the scope of a GraphQL request
10
+ }
11
+
12
+ async function buildContext(connectionString: string | undefined) {
13
+ const db_client = new ArchiveNodeAdapter(connectionString);
14
+ await db_client.checkSQLSchema();
15
+ return {
16
+ db_client,
17
+ };
18
+ }
@@ -0,0 +1,23 @@
1
+ import type { EventFilterOptionsInput } from '../../resolvers-types.js';
2
+ import type {
3
+ Actions,
4
+ Events,
5
+ NetworkState,
6
+ Blocks,
7
+ } from '../../blockchain/types.js';
8
+ import type { BlockQueryInput, BlockSortByInput } from '../../resolvers-types.js';
9
+
10
+ export interface DatabaseAdapter {
11
+ getEvents(input: EventFilterOptionsInput, options?: unknown): Promise<Events>;
12
+ getActions(
13
+ input: EventFilterOptionsInput,
14
+ options?: unknown
15
+ ): Promise<Actions>;
16
+ getNetworkState(options?: unknown): Promise<NetworkState>;
17
+ getBlocks(
18
+ query: BlockQueryInput | null | undefined,
19
+ limit: number | null | undefined,
20
+ sortBy: BlockSortByInput | null | undefined,
21
+ options: unknown
22
+ ): Promise<Blocks>;
23
+ }
@@ -0,0 +1,101 @@
1
+ import postgres from 'postgres';
2
+ import type {
3
+ Actions,
4
+ Events,
5
+ NetworkState,
6
+ Blocks,
7
+ } from '../../blockchain/types.js';
8
+ import type { DatabaseAdapter } from './archive-node-adapter.interface.js';
9
+ import type {
10
+ ActionFilterOptionsInput,
11
+ EventFilterOptionsInput,
12
+ BlockQueryInput,
13
+ BlockSortByInput,
14
+ } from '../../resolvers-types.js';
15
+ import { getTables, USED_TABLES } from '../../db/sql/events-actions/queries.js';
16
+ import { EventsService } from '../../services/events-service/events-service.js';
17
+ import { IEventsService } from '../../services/events-service/events-service.interface.js';
18
+ import { ActionsService } from '../../services/actions-service/actions-service.js';
19
+ import { IActionsService } from '../../services/actions-service/actions-service.interface.js';
20
+ import { NetworkService } from '../../services/network-service/network-service.js';
21
+ import { INetworkService } from '../../services/network-service/network-service.interface.js';
22
+ import { BlocksService } from '../../services/blocks-service/blocks-service.js';
23
+ import { IBlocksService } from '../../services/blocks-service/blocks-service.interface.js';
24
+
25
+ export class ArchiveNodeAdapter implements DatabaseAdapter {
26
+ /**
27
+ * Connections are created lazily once a query is created.
28
+ * This means that simply doing const sql = postgres(...) won't have any
29
+ * effect other than instantiating a new sql instance. Because of this, sharing the
30
+ * `postgres.Sql` instance across the adapter is safe.
31
+ */
32
+ private client: postgres.Sql;
33
+ private eventsService: IEventsService;
34
+ private actionsService: IActionsService;
35
+ private networkService: INetworkService;
36
+ private blocksService: IBlocksService;
37
+
38
+ constructor(connectionString: string | undefined) {
39
+ if (!connectionString)
40
+ throw new Error(
41
+ 'Missing Postgres Connection String. Please provide a valid connection string in the environment variables or in your configuration file to connect to the Postgres database.'
42
+ );
43
+ this.client = postgres(connectionString);
44
+ this.eventsService = new EventsService(this.client);
45
+ this.actionsService = new ActionsService(this.client);
46
+ this.networkService = new NetworkService(this.client);
47
+ this.blocksService = new BlocksService(this.client);
48
+ }
49
+
50
+ async getEvents(
51
+ input: EventFilterOptionsInput,
52
+ options: unknown
53
+ ): Promise<Events> {
54
+ return this.eventsService.getEvents(input, options);
55
+ }
56
+
57
+ async getActions(
58
+ input: ActionFilterOptionsInput,
59
+ options: unknown
60
+ ): Promise<Actions> {
61
+ return this.actionsService.getActions(input, options);
62
+ }
63
+
64
+ async getNetworkState(options: unknown): Promise<NetworkState> {
65
+ return this.networkService.getNetworkState(options);
66
+ }
67
+
68
+ async getBlocks(
69
+ query: BlockQueryInput | null | undefined,
70
+ limit: number | null | undefined,
71
+ sortBy: BlockSortByInput | null | undefined,
72
+ options: unknown
73
+ ): Promise<Blocks> {
74
+ return this.blocksService.getBlocks(query, limit, sortBy, options);
75
+ }
76
+
77
+ async checkSQLSchema() {
78
+ let tables;
79
+ try {
80
+ tables = await (
81
+ await getTables(this.client)
82
+ ).map((table) => table.tablename);
83
+ } catch (e) {
84
+ throw new Error(
85
+ `Could not connect to Postgres with the specified connection string. Please check that Postgres is available and that your connection string is correct and try again.\nReason: ${e}`
86
+ );
87
+ }
88
+
89
+ for (const table of USED_TABLES) {
90
+ if (!tables.includes(table)) {
91
+ throw new Error(
92
+ `Missing table ${table}. Please make sure the table exists in the database.`
93
+ );
94
+ }
95
+ }
96
+ }
97
+
98
+ async close() {
99
+ return this.client.end();
100
+ }
101
+ }