-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathHasuraBackgroundClient.ts
305 lines (290 loc) · 8.87 KB
/
HasuraBackgroundClient.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
import { exec } from 'child_process'
import util, { ModuleState } from '@cardano-graphql/util'
import { GraphQLSchema } from 'graphql'
import { GraphQLClient, gql } from 'graphql-request'
import pRetry from 'p-retry'
import path from 'path'
import { dummyLogger, Logger } from 'ts-log'
import { Asset, Block } from './graphql_types'
import { AssetMetadataAndHash, AssetMetadataHashAndId, AssetWithoutTokens } from './typeAliases'
import { Schema } from '@cardano-ogmios/client'
const epochInformationNotYetAvailable = 'Epoch information not yet available. This is expected during the initial chain-sync.'
const withHexPrefix = (value: string) => `\\x${value !== undefined ? value : ''}`
export class HasuraBackgroundClient {
private client: GraphQLClient
private applyingSchemaAndMetadata: boolean
private state: ModuleState
public schema: GraphQLSchema
constructor (
readonly hasuraCliPath: string,
readonly hasuraCliExtPath: string,
readonly hasuraUri: string,
private logger: Logger = dummyLogger
) {
this.state = null
this.applyingSchemaAndMetadata = false
this.client = new GraphQLClient(
`${this.hasuraUri}/v1/graphql`,
{
headers: {
'X-Hasura-Role': 'cardano-graphql'
}
}
)
}
private async hasuraCli (command: string) {
return new Promise((resolve, reject) => {
exec(
`${this.hasuraCliPath} --cli-ext-path ${this.hasuraCliExtPath} --skip-update-check --project ${path.resolve(__dirname, '..', 'hasura', 'project')} --endpoint ${this.hasuraUri} ${command}`,
(error, stdout) => {
if (error) {
reject(error)
}
if (stdout !== '') this.logger.debug({ module: 'HasuraBackgroundClient' }, stdout)
resolve({ module: 'HasuraBackgroundClient' })
}
)
})
}
public async initialize () {
if (this.state !== null) return
this.state = 'initializing'
this.logger.info({ module: 'HasuraBackgroundClient' }, 'Initializing')
await this.applySchemaAndMetadata()
this.logger.debug({ module: 'HasuraBackgroundClient' }, 'graphql-engine setup')
await pRetry(async () => {
const result = await this.client.request(
gql`query {
epochs (limit: 1, order_by: { number: desc }) {
number
}
}`
)
if (result.epochs.length === 0) {
this.logger.debug({ module: 'HasuraBackgroundClient' }, epochInformationNotYetAvailable)
throw new Error(epochInformationNotYetAvailable)
}
}, {
factor: 1.05,
retries: 10,
onFailedAttempt: util.onFailedAttemptFor(
'Detecting DB sync state has reached minimum progress',
this.logger
)
})
this.logger.debug({ module: 'HasuraBackgroundClient' }, 'DB sync state has reached minimum progress')
this.state = 'initialized'
this.logger.info({ module: 'HasuraBackgroundClient' }, 'Initialized')
}
public async shutdown () {
this.state = null
}
public async applySchemaAndMetadata (): Promise<void> {
if (this.applyingSchemaAndMetadata) return
this.applyingSchemaAndMetadata = true
await pRetry(async () => {
await this.hasuraCli('migrate --database-name default apply --down all')
await this.hasuraCli('migrate --database-name default apply --up all')
}, {
factor: 1.75,
retries: 9,
onFailedAttempt: util.onFailedAttemptFor(
'Applying PostgreSQL schema migrations',
this.logger
)
})
await pRetry(async () => {
await this.hasuraCli('metadata clear')
await this.hasuraCli('metadata apply')
}, {
factor: 1.75,
retries: 9,
onFailedAttempt: util.onFailedAttemptFor('Applying Hasura metadata', this.logger)
})
this.applyingSchemaAndMetadata = false
}
public async deleteAssetsAfterSlot (slotNo: Block['slotNo']): Promise<number> {
this.logger.debug(
{ module: 'HasuraClient', slotNo },
'deleting assets found in tokens after slot'
)
const result = await this.client.request(
gql`mutation DeleteAssetsAfterSlot($slotNo: Int!) {
delete_assets(
where: {
firstAppearedInSlot: {
_gt: $slotNo
}
}
) {
affected_rows
}
}`,
{
slotNo
}
)
return result.delete_assets.affected_rows
}
public async hasAsset (assetId: Asset['assetId']): Promise<boolean> {
const result = await this.client.request(
gql`query HasAsset (
$assetId: bytea!
) {
assets (
where: { assetId: { _eq: $assetId }}
) {
assetId
}
}`, {
assetId: withHexPrefix(assetId)
}
)
const response = result.assets.length > 0
this.logger.debug(
{ module: 'HasuraClient', assetId, hasAsset: response },
'Has asset?'
)
return response
}
public async getMostRecentPointWithNewAsset (): Promise<Schema.Point | null> {
let point: Schema.Point | null
// Handles possible race condition between the internal chain-follower, which manages the Asset table,
// and cardano-db-sync's which managed the block table.
await pRetry(async () => {
// An offset of 1 is applied to ensure a partial block extraction is not skipped
const result = await this.client.request(
gql`query {
assets (
limit: 1
offset: 1
order_by: { firstAppearedInBlock: { slotNo: desc }}
) {
firstAppearedInBlock {
hash
slotNo
}
}
}`
)
if (result.errors !== undefined) {
throw new Error(result.errors)
}
if (result.assets.length !== 0) {
if (result.assets[0].firstAppearedInBlock === null) {
throw new Error('cardano-db-sync is lagging behind the asset sync operation.')
}
const { hash, slotNo } = result.assets[0].firstAppearedInBlock
point = {
slot: Number(slotNo),
id: hash.substring(2)
}
} else {
point = null
}
}, {
factor: 1.5,
retries: 1000,
onFailedAttempt: util.onFailedAttemptFor(
'Getting the most recent point with a new asset',
this.logger
)
})
return point
}
public async addAssetMetadata (asset: AssetMetadataAndHash) {
this.logger.info(
{ module: 'HasuraClient', assetId: asset.assetId },
'Adding metadata to asset'
)
const result = await this.client.request(
gql`mutation AddAssetMetadata(
$assetId: bytea!
$decimals: Int
$description: String
$logo: String
$metadataHash: bpchar!
$name: String
$ticker: String
$url: String
) {
update_assets(
where: {
assetId: { _eq: $assetId }
},
_set: {
decimals: $decimals
description: $description
logo: $logo
metadataHash: $metadataHash
name: $name
ticker: $ticker
url: $url
}
) {
affected_rows
returning {
assetId
}
}
}`,
{
...asset,
...{ assetId: withHexPrefix(asset.assetId) }
}
)
if (result.errors !== undefined) {
throw new Error(result.errors)
}
}
public async insertAssets (assets: AssetWithoutTokens[]) {
this.logger.debug(
{ module: 'HasuraClient', qty: assets.length },
'inserting assets found in tokens'
)
const result = await this.client.request(
gql`mutation InsertAssets($assets: [Asset_insert_input!]!) {
insert_assets(objects: $assets) {
returning {
name
policyId
description
assetName
assetId
}
affected_rows
}
}`,
{
assets: assets.map(asset => ({
...asset,
...{
assetId: withHexPrefix(asset.assetId),
assetName: withHexPrefix(asset.assetName),
policyId: withHexPrefix(asset.policyId)
}
}))
}
)
return result
}
public async getAssetMetadataHashesById (assetIds: Asset['assetId'][]): Promise<AssetMetadataHashAndId[]> {
const result = await this.client.request(
gql`query AssetMetadataHashes (
$assetIds: [bytea!]!
){
assets (
where: {
assetId: { _in: $assetIds }
}) {
assetId
metadataHash
}
}`,
{
assetIds: assetIds.map(id => withHexPrefix(id))
}
)
return result.assets
}
}