-
Notifications
You must be signed in to change notification settings - Fork 55
/
OgmiosCardanoNode.ts
130 lines (115 loc) · 3.86 KB
/
OgmiosCardanoNode.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
import {
Cardano,
CardanoNode,
CardanoNodeErrors,
CardanoNodeUtil,
EraSummary,
HealthCheckResponse,
ProviderError,
ProviderFailure,
StakeDistribution
} from '@cardano-sdk/core';
import {
ConnectionConfig,
StateQuery,
createConnectionObject,
createStateQueryClient,
getServerHealth
} from '@cardano-ogmios/client';
import { Logger } from 'ts-log';
import { RunnableModule, contextLogger, stripStackTrace } from '@cardano-sdk/util';
import { createInteractionContextWithLogger } from '../util';
import { queryEraSummaries } from './queries';
/**
* Access cardano-node APIs via Ogmios
*
* @class OgmiosCardanoNode
*/
export class OgmiosCardanoNode extends RunnableModule implements CardanoNode {
#stateQueryClient: StateQuery.StateQueryClient;
#logger: Logger;
#connectionConfig: ConnectionConfig;
constructor(connectionConfig: ConnectionConfig, logger: Logger) {
super('OgmiosCardanoNode', logger);
this.#logger = contextLogger(logger, 'OgmiosCardanoNode');
this.#connectionConfig = connectionConfig;
}
public async initializeImpl(): Promise<void> {
this.#logger.info('Initializing CardanoNode');
this.#stateQueryClient = await createStateQueryClient(
await createInteractionContextWithLogger(this.#logger, { connection: this.#connectionConfig })
);
this.#logger.info('CardanoNode initialized');
}
public async shutdownImpl(): Promise<void> {
this.#logger.info('Shutting down CardanoNode');
await this.#stateQueryClient.shutdown();
}
public async eraSummaries(): Promise<EraSummary[]> {
if (this.state !== 'running') {
throw new CardanoNodeErrors.NotInitializedError('eraSummaries', this.name);
}
return queryEraSummaries(this.#stateQueryClient, this.#logger);
}
public async systemStart(): Promise<Date> {
if (this.state !== 'running') {
throw new CardanoNodeErrors.NotInitializedError('systemStart', this.name);
}
try {
this.#logger.info('Getting system start');
return await this.#stateQueryClient.systemStart();
} catch (error) {
const domainError =
CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);
stripStackTrace(domainError);
throw domainError;
}
}
public async stakeDistribution(): Promise<StakeDistribution> {
if (this.state !== 'running') {
throw new CardanoNodeErrors.NotInitializedError('stakeDistribution', this.name);
}
try {
this.#logger.info('Getting stake distribution');
const map = new Map();
for (const [key, value] of Object.entries(await this.#stateQueryClient.stakeDistribution())) {
const splitStake = value.stake.split('/');
map.set(Cardano.PoolId(key), {
...value,
stake: { pool: BigInt(splitStake[0]), supply: BigInt(splitStake[1]) }
});
}
return map;
} catch (error) {
const domainError =
CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);
stripStackTrace(domainError);
throw domainError;
}
}
async healthCheck(): Promise<HealthCheckResponse> {
try {
const { networkSynchronization, lastKnownTip } = await getServerHealth({
connection: createConnectionObject(this.#connectionConfig)
});
return {
localNode: {
ledgerTip: lastKnownTip,
networkSync: Cardano.Percent(networkSynchronization)
},
ok: networkSynchronization > 0.99
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.name === 'FetchError') {
return { ok: false };
}
const domainError = new ProviderError(ProviderFailure.Unknown, error);
stripStackTrace(domainError);
throw domainError;
}
}
async startImpl(): Promise<void> {
return Promise.resolve();
}
}