Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 9x 6x 6x 40x 10x 10x 60x 11x 2x 9x 9x 10x 20x 10x 10x 10x 10x 10x 10x 1x 1x 1x 10x 10x 4x 1x 3x 10x 10x 10x 10x 10x 10x 10x 10x 1x 10x 10x 2x 2x 1x 1x 10x 2x 2x 1x 10x 10x 10x 1x 10x 10x 6x 1x 1x 1x 2x 5x 4x 40x 10x 10x 10x 10x 10x 10x 10x 10x 10x 16x 16x 5x 5x 5x | /* eslint-disable unicorn/consistent-function-scoping */ // cSpell:ignore impls /* eslint-disable complexity */ import { AssetProvider, CardanoNode, ChainHistoryProvider, HandleProvider, NetworkInfoProvider, Provider, RewardsProvider, Seconds, StakePoolProvider, UtxoProvider } from '@cardano-sdk/core'; import { BlockfrostAssetProvider, CardanoWsClient, TxSubmitApiProvider } from '@cardano-sdk/cardano-services-client'; import { Logger } from 'ts-log'; import { Observable } from 'rxjs'; import { OgmiosCardanoNode } from '@cardano-sdk/ogmios'; import { PgConnectionConfig } from '@cardano-sdk/projection-typeorm'; import { Pool } from 'pg'; import { SrvRecord } from 'dns'; import { createLogger } from 'bunyan'; import { isNotNil } from '@cardano-sdk/util'; import memoize from 'lodash/memoize.js'; /* eslint-disable sonarjs/cognitive-complexity */ import { AssetHttpService, CardanoTokenRegistry, DbSyncAssetProvider, DbSyncNftMetadataService, StubTokenMetadataService, TypeormAssetProvider } from '../../Asset'; import { BlockfrostChainHistoryProvider, ChainHistoryHttpService, DbSyncChainHistoryProvider } from '../../ChainHistory'; import { BlockfrostNetworkInfoProvider, DbSyncNetworkInfoProvider, NetworkInfoHttpService } from '../../NetworkInfo'; import { BlockfrostRewardsProvider, DbSyncRewardsProvider, RewardsHttpService } from '../../Rewards'; import { BlockfrostTxSubmitProvider, NodeTxSubmitProvider, TxSubmitHttpService } from '../../TxSubmit'; import { BlockfrostUtxoProvider, DbSyncUtxoProvider, UtxoHttpService } from '../../Utxo'; import { CommonOptionsDescriptions, ConnectionNames, HandlePolicyIdsOptionDescriptions, PostgresOptionDescriptions, ProviderImplementation, handlePolicyIdsFromFile, suffixType2Cli } from '../options'; import { DbPools, DbSyncEpochPollService, TypeormProvider, getBlockfrostApi, getBlockfrostClient } from '../../util'; import { DbSyncStakePoolProvider, StakePoolHttpService, TypeormStakePoolProvider, createHttpStakePoolMetadataService } from '../../StakePool'; import { DnsResolver, createDnsResolver, getCardanoNode, getDbPools, getGenesisData } from '../utils'; import { GenesisData } from '../../types'; import { HandleHttpService, TypeOrmHandleProvider } from '../../Handle'; import { HttpServer, HttpServerConfig, HttpService, getListen } from '../../Http'; import { InMemoryCache, NoCache } from '../../InMemoryCache'; import { MissingProgramOption, MissingServiceDependency, RunnableDependencies, UnknownServiceName } from '../errors'; import { ProviderServerArgs, ProviderServerOptionDescriptions, ServiceNames } from './types'; import { WarmCache } from '../../InMemoryCache/WarmCache'; import { createDbSyncMetadataService } from '../../Metadata'; import { getConnectionConfig, getOgmiosObservableCardanoNode } from '../services'; import { getEntities } from '../../Projection'; export const ALLOWED_ORIGINS_DEFAULT = false; export const DISABLE_STAKE_POOL_METRIC_APY_DEFAULT = false; export const PROVIDER_SERVER_API_URL_DEFAULT = new URL('http://localhost:3000'); export const PAGINATION_PAGE_SIZE_LIMIT_DEFAULT = 25; export const PAGINATION_PAGE_SIZE_LIMIT_ASSETS = 300; export const USE_BLOCKFROST_DEFAULT = false; export const USE_TYPEORM_STAKE_POOL_PROVIDER_DEFAULT = false; export const HANDLE_PROVIDER_SERVER_URL_DEFAULT = ''; export const USE_TYPEORM_ASSET_PROVIDER_DEFAULT = false; export interface LoadProviderServerDependencies { dnsResolver?: (serviceName: string) => Promise<SrvRecord>; logger?: Logger; } interface ServiceMapFactoryOptions { args: ProviderServerArgs; pools: Partial<DbPools>; dnsResolver: DnsResolver; genesisData?: GenesisData; logger: Logger; node?: OgmiosCardanoNode; } const serverName = 'provider-server'; const connectionConfigs: { [k in ConnectionNames]?: Observable<PgConnectionConfig> } = {}; let sharedHandleProvider: HandleProvider; const selectProviderImplementation = <T extends Provider>( impl: ProviderImplementation, impls: { typeorm?: () => T & TypeormProvider; dbsync?: () => T & Provider; blockfrost?: () => T; }, logger: Logger, service?: ServiceNames ) => { const selected = impl === ProviderImplementation.TYPEORM ? impls.typeorm!() : impl === ProviderImplementation.DBSYNC ? impls.dbsync!() : (impls.blockfrost!() as T); logger.info(`Selected ${typeof selected} for ${service} provider based on value ${impl}`); return selected; }; const serviceMapFactory = (options: ServiceMapFactoryOptions) => { const { args, pools, dnsResolver, genesisData, logger, node } = options; const withDbSyncProvider = <T>(factory: (dbPools: DbPools, cardanoNode: CardanoNode) => T, serviceName: ServiceNames) => () => { if (!pools.main || !pools.healthCheck) throw new MissingProgramOption(serviceName, [ PostgresOptionDescriptions.ConnectionString, PostgresOptionDescriptions.ServiceDiscoveryArgs ]); Iif (!node) throw new MissingServiceDependency(serviceName, RunnableDependencies.CardanoNode); return factory(pools as DbPools, node); }; const withTypeOrmProvider = <T>(suffix: ConnectionNames, factory: (connectionConfig$: Observable<PgConnectionConfig>) => T) => () => { const name = suffixType2Cli(suffix).slice(1); const connectionConfig$ = connectionConfigs[suffix] ?? (connectionConfigs[suffix] = getConnectionConfig(dnsResolver, name, suffix, args)); return factory(connectionConfig$); }; const getCache = (ttl: Seconds | 0) => (args.disableDbCache ? new NoCache() : new InMemoryCache(ttl)); const getWarmCache = (ttl: Seconds) => (args.disableDbCache ? new NoCache() : new WarmCache(ttl, Seconds(ttl / 10))); const getDbCache = () => getCache(args.dbCacheTtl); // Shared cache across all providers const healthCheckCache = getWarmCache(args.healthCheckCacheTtl); const getEpochMonitor = memoize((dbPool: Pool) => new DbSyncEpochPollService(dbPool, args.epochPollInterval!)); const getDbSyncChainHistoryProvider = withDbSyncProvider((dbPools, cardanoNode) => { const cache = { healthCheck: healthCheckCache }; const metadataService = createDbSyncMetadataService(dbPools.main, logger); return new DbSyncChainHistoryProvider( { paginationPageSizeLimit: args.paginationPageSizeLimit! }, { cache, cardanoNode, dbPools, logger, metadataService } ); }, ServiceNames.ChainHistory); const getWebSocketClient = () => { const url = args.webSocketApiUrl; Iif (!url) throw new MissingProgramOption('WebSocket', CommonOptionsDescriptions.WebSocketApiUrl); const chainHistoryProvider = getDbSyncChainHistoryProvider(); return new CardanoWsClient({ chainHistoryProvider, logger }, { url }); }; const getDbSyncStakePoolProvider = withDbSyncProvider((dbPools, cardanoNode) => { if (!genesisData) throw new MissingProgramOption(ServiceNames.StakePool, CommonOptionsDescriptions.CardanoNodeConfigPath); return new DbSyncStakePoolProvider( { paginationPageSizeLimit: args.paginationPageSizeLimit!, responseConfig: { search: { metrics: { apy: !args.disableStakePoolMetricApy } } }, useBlockfrost: args.useBlockfrost! }, { cache: { db: getDbCache(), healthCheck: healthCheckCache }, cardanoNode, dbPools, epochMonitor: getEpochMonitor(dbPools.main), genesisData, logger, metadataService: createHttpStakePoolMetadataService(logger) } ); }, ServiceNames.StakePool); const getTypeormStakePoolProvider = withTypeOrmProvider('StakePool', (connectionConfig$) => { const entities = getEntities(['currentPoolMetrics', 'poolDelisted', 'poolMetadata', 'poolRewards']); return new TypeormStakePoolProvider(args, { cache: getDbCache(), connectionConfig$, entities, healthCheckCache, logger }); }); const getHandleProvider = async () => { Iif (sharedHandleProvider) return sharedHandleProvider; Iif (!args.handlePolicyIds) throw new MissingProgramOption(ServiceNames.Handle, HandlePolicyIdsOptionDescriptions.HandlePolicyIds); sharedHandleProvider = await withTypeOrmProvider( 'Handle', async (connectionConfig$) => new TypeOrmHandleProvider({ connectionConfig$, entities: getEntities(['handle', 'handleMetadata']), healthCheckCache, logger }) )(); return sharedHandleProvider; }; const getSubmitApiProvider = () => { const { submitApiUrl } = args; Iif (!submitApiUrl) throw new MissingProgramOption(ServiceNames.TxSubmit, ProviderServerOptionDescriptions.SubmitApiUrl); return new TxSubmitApiProvider({ baseUrl: submitApiUrl }, { logger }); }; const getTypeormAssetProvider = withTypeOrmProvider('Asset', (connectionConfig$) => { const tokenMetadataService = args.tokenMetadataServerUrl?.startsWith('stub:') ? new StubTokenMetadataService() : new CardanoTokenRegistry({ logger }, args); return new TypeormAssetProvider( { paginationPageSizeLimit: Math.min(args.paginationPageSizeLimit! * 10, PAGINATION_PAGE_SIZE_LIMIT_ASSETS) }, { connectionConfig$, entities: getEntities(['asset']), healthCheckCache, logger, tokenMetadataService } ); }); const getDbSyncAssetProvider = withDbSyncProvider((dbPools, cardanoNode) => { const ntfMetadataService = new DbSyncNftMetadataService({ db: dbPools.main, logger, metadataService: createDbSyncMetadataService(dbPools.main, logger) }); const tokenMetadataService = args.tokenMetadataServerUrl?.startsWith('stub:') ? new StubTokenMetadataService() : new CardanoTokenRegistry({ logger }, args); return new DbSyncAssetProvider( { cacheTTL: args.assetCacheTTL, disableDbCache: args.disableDbCache, paginationPageSizeLimit: Math.min(args.paginationPageSizeLimit! * 10, PAGINATION_PAGE_SIZE_LIMIT_ASSETS) }, { cache: { healthCheck: healthCheckCache }, cardanoNode, dbPools, logger, ntfMetadataService, tokenMetadataService } ); }, ServiceNames.Asset); const getBlockfrostAssetProvider = () => new BlockfrostAssetProvider(getBlockfrostClient(), logger); const getBlockfrostUtxoProvider = () => new BlockfrostUtxoProvider({ blockfrost: getBlockfrostApi(), logger }); const getDbSyncUtxoProvider = withDbSyncProvider( (dbPools, cardanoNode) => new DbSyncUtxoProvider({ cache: { healthCheck: healthCheckCache }, cardanoNode, dbPools, logger }), ServiceNames.Utxo ); const getBlockfrostNetworkInfoProvider = () => new BlockfrostNetworkInfoProvider({ blockfrost: getBlockfrostApi(), logger }); const getDbSyncNetworkInfoProvider = withDbSyncProvider((dbPools, cardanoNode) => { Iif (args.useWebSocketApi) return getWebSocketClient().networkInfoProvider; if (!genesisData) throw new MissingProgramOption(ServiceNames.NetworkInfo, CommonOptionsDescriptions.CardanoNodeConfigPath); return new DbSyncNetworkInfoProvider({ cache: { db: getDbCache(), healthCheck: healthCheckCache }, cardanoNode, dbPools, epochMonitor: getEpochMonitor(dbPools.main), genesisData, logger }); }, ServiceNames.NetworkInfo); let networkInfoProvider: NetworkInfoProvider; const getNetworkInfoProvider = () => { if (!networkInfoProvider) networkInfoProvider = args.networkInfoProvider === ProviderImplementation.BLOCKFROST ? getBlockfrostNetworkInfoProvider() : getDbSyncNetworkInfoProvider(); return networkInfoProvider; }; const getBlockfrostChainHistoryProvider = (nInfoProvider: NetworkInfoProvider | DbSyncNetworkInfoProvider) => new BlockfrostChainHistoryProvider({ blockfrost: getBlockfrostApi(), logger, networkInfoProvider: nInfoProvider }); const getBlockfrostRewardsProvider = () => new BlockfrostRewardsProvider({ blockfrost: getBlockfrostApi(), logger }); const getDbSyncRewardsProvider = withDbSyncProvider( (dbPools, cardanoNode) => new DbSyncRewardsProvider( { paginationPageSizeLimit: args.paginationPageSizeLimit! }, { cache: { healthCheck: healthCheckCache }, cardanoNode, dbPools, logger } ), ServiceNames.Rewards ); const getBlockfrostTxSubmitProvider = () => new BlockfrostTxSubmitProvider({ blockfrost: getBlockfrostApi(), logger }); return { [ServiceNames.Asset]: async () => new AssetHttpService({ assetProvider: selectProviderImplementation<AssetProvider>( args.useTypeormAssetProvider ? ProviderImplementation.TYPEORM : args.assetProvider ?? ProviderImplementation.DBSYNC, { blockfrost: getBlockfrostAssetProvider, dbsync: getDbSyncAssetProvider, typeorm: getTypeormAssetProvider }, logger, ServiceNames.Asset ), logger }), [ServiceNames.StakePool]: async () => new StakePoolHttpService({ logger, stakePoolProvider: selectProviderImplementation<StakePoolProvider>( args.useTypeormStakePoolProvider ? ProviderImplementation.TYPEORM : args.stakePoolProvider ?? ProviderImplementation.DBSYNC, { dbsync: getDbSyncStakePoolProvider, typeorm: getTypeormStakePoolProvider }, logger, ServiceNames.StakePool ) }), [ServiceNames.Utxo]: async () => new UtxoHttpService({ logger, utxoProvider: selectProviderImplementation<UtxoProvider>( args.utxoProvider ?? ProviderImplementation.DBSYNC, { blockfrost: getBlockfrostUtxoProvider, dbsync: getDbSyncUtxoProvider }, logger, ServiceNames.Utxo ) }), [ServiceNames.ChainHistory]: async () => new ChainHistoryHttpService({ chainHistoryProvider: selectProviderImplementation<ChainHistoryProvider>( args.chainHistoryProvider ?? ProviderImplementation.DBSYNC, { blockfrost: () => getBlockfrostChainHistoryProvider(getNetworkInfoProvider()), dbsync: getDbSyncChainHistoryProvider }, logger, ServiceNames.ChainHistory ), logger }), [ServiceNames.Handle]: async () => new HandleHttpService({ handleProvider: await getHandleProvider(), logger }), [ServiceNames.Rewards]: async () => new RewardsHttpService({ logger, rewardsProvider: selectProviderImplementation<RewardsProvider>( args.rewardsProvider ?? ProviderImplementation.DBSYNC, { blockfrost: getBlockfrostRewardsProvider, dbsync: getDbSyncRewardsProvider }, logger, ServiceNames.Rewards ) }), [ServiceNames.NetworkInfo]: async () => new NetworkInfoHttpService({ logger, networkInfoProvider: getNetworkInfoProvider() }), [ServiceNames.TxSubmit]: async () => { const txSubmitProvider = args.useSubmitApi ? getSubmitApiProvider() : args.txSubmitProvider === ProviderImplementation.BLOCKFROST ? getBlockfrostTxSubmitProvider() : args.txSubmitProvider === ProviderImplementation.SUBMIT_API ? getSubmitApiProvider() : new NodeTxSubmitProvider({ cardanoNode: getOgmiosObservableCardanoNode(dnsResolver, logger, args), handleProvider: args.submitValidateHandles ? await getHandleProvider() : undefined, healthCheckCache, logger }); return new TxSubmitHttpService({ logger, txSubmitProvider }); } }; }; export const loadProviderServer = async ( args: ProviderServerArgs, deps: LoadProviderServerDependencies = {} ): Promise<HttpServer> => { const services: HttpService[] = []; const logger = deps?.logger || createLogger({ level: args.loggerMinSeverity, name: serverName }); const dnsResolver = deps?.dnsResolver || createDnsResolver( { factor: args.serviceDiscoveryBackoffFactor, maxRetryTime: args.serviceDiscoveryTimeout }, logger ); await handlePolicyIdsFromFile(args); const cardanoNode = await getCardanoNode(dnsResolver, logger, args); const genesisData = await getGenesisData(args); const pools: Partial<DbPools> = await getDbPools(dnsResolver, logger, args); const serviceMap = serviceMapFactory({ args, dnsResolver, genesisData, logger, node: cardanoNode, pools }); for (const serviceName of args.serviceNames) { if (serviceMap[serviceName]) { services.push(await serviceMap[serviceName]()); } else E{ throw new UnknownServiceName(serviceName, Object.values(ServiceNames)); } } const config: HttpServerConfig = { allowedOrigins: args.allowedOrigins, listen: getListen(args.apiUrl), meta: { ...args.buildInfo, startupTime: Date.now() } }; Iif (args.enableMetrics) { config.metrics = { enabled: args.enableMetrics }; } return new HttpServer(config, { logger, runnableDependencies: [cardanoNode].filter(isNotNil), services }); }; |