diff --git a/pages/admin/index.tsx b/pages/admin/index.tsx index d9f288f3b..cd5258d2c 100644 --- a/pages/admin/index.tsx +++ b/pages/admin/index.tsx @@ -62,7 +62,6 @@ export default function Admin ({ user, isAdmin }: IProps): JSX.Element { useEffect(() => { void (async () => { const ok = await (await fetch('chronikStatus')).json() - console.log('ok', ok) // WIP const subscribedEcashAddresses = ok.ecash.map((value: string) => ({ address: value })) const subscribedBitcoincashAddresses = ok.bitcoincash.map((value: string) => ({ address: value })) setEcashSubscribedAddresses(subscribedEcashAddresses) diff --git a/services/blockchainService.ts b/services/blockchainService.ts index 37a34945e..ee39c7e0b 100644 --- a/services/blockchainService.ts +++ b/services/blockchainService.ts @@ -1,71 +1,19 @@ -import { ChronikBlockchainClient } from './chronikService' +import { ChronikBlockchainClient } from 'services/chronikService' import { getObjectValueForAddress, getObjectValueForNetworkSlug } from '../utils/index' import { RESPONSE_MESSAGES, KeyValueT, NETWORK_IDS, NETWORK_TICKERS } from '../constants/index' import { TransactionWithAddressAndPrices } from './transactionService' -import { Address, Prisma } from '@prisma/client' +import { Address } from '@prisma/client' import config, { BlockchainClientOptions } from 'config' import { PHASE_PRODUCTION_BUILD } from 'next/dist/shared/lib/constants' +import { BlockchainClient, Networks, NodeJsGlobalChronik, TransactionDetails } from 'types/blockchainInterfaces' -export interface BlockchainInfo { - height: number - hash: Uint8Array | string -} - -export interface BlockInfo extends BlockchainInfo { - timestamp: number -} - -export interface GetAddressTransactionsParameters { - addressString: string - start: number -} - -interface InputOutput { - value: Prisma.Decimal - address?: string -} - -export interface TransactionDetails { - hash: string - version: number - inputs: InputOutput[] - outputs: InputOutput[] - block: { - height?: number - hash?: string - timestamp?: string - } -} - -export interface AddressWithTransaction { - address: Address - transaction: Prisma.TransactionUncheckedCreateInput -} - -export interface BlockchainClient { - getBalance: (address: string) => Promise - syncTransactionsForAddress: (addressString: string) => AsyncGenerator - getBlockchainInfo: (networkSlug: string) => Promise - getBlockInfo: (networkSlug: string, height: number) => Promise - getTransactionDetails: (hash: string, networkSlug: string) => Promise - subscribeAddresses: (addresses: Address[]) => Promise -} - -interface NetworkClients{ - ecash?: ChronikBlockchainClient - bitcoincash?: ChronikBlockchainClient -} - -export type Networks = 'ecash' | 'bitcoincash' - -export interface NodeJsGlobalChronik extends NodeJS.Global { - chronik?: NetworkClients -} declare const global: NodeJsGlobalChronik function getBlockchainClient (networkSlug: Networks): BlockchainClient { if (!Object.keys(config.networkBlockchainClients).includes(networkSlug)) { throw new Error(RESPONSE_MESSAGES.MISSING_BLOCKCHAIN_CLIENT_400.message) } + console.log('porra meu', ChronikBlockchainClient) + console.log('porra meu2', typeof ChronikBlockchainClient) switch (config.networkBlockchainClients[networkSlug]) { case 'chronik' as BlockchainClientOptions: if (global.chronik === undefined || global.chronik[networkSlug] === undefined) { diff --git a/services/chronikService.ts b/services/chronikService.ts index 757497263..37883396c 100644 --- a/services/chronikService.ts +++ b/services/chronikService.ts @@ -1,7 +1,7 @@ import { BlockInfo_InNode, ChronikClientNode, ScriptType_InNode, ScriptUtxo_InNode, Tx_InNode, WsConfig_InNode, WsEndpoint_InNode, WsMsgClient, WsSubScriptClient } from 'chronik-client' import { encode, decode } from 'ecashaddrjs' import bs58 from 'bs58' -import { AddressWithTransaction, BlockchainClient, BlockchainInfo, BlockInfo, NodeJsGlobalChronik, TransactionDetails, Networks } from './blockchainService' +import { AddressWithTransaction, BlockchainClient, BlockchainInfo, BlockInfo, TransactionDetails, Networks, NodeJsGlobalChronik } from 'types/blockchainInterfaces' import { CHRONIK_MESSAGE_CACHE_DELAY, RESPONSE_MESSAGES, XEC_TIMESTAMP_THRESHOLD, XEC_NETWORK_ID, BCH_NETWORK_ID, BCH_TIMESTAMP_THRESHOLD, FETCH_DELAY, FETCH_N, KeyValueT, NETWORK_IDS_FROM_SLUGS, NETWORK_SLUGS_FROM_IDS, SOCKET_MESSAGES } from 'constants/index' import { TransactionWithAddressAndPrices, diff --git a/types/blockchainInterfaces.ts b/types/blockchainInterfaces.ts new file mode 100644 index 000000000..7f1e7347d --- /dev/null +++ b/types/blockchainInterfaces.ts @@ -0,0 +1,59 @@ +import { Address, Prisma } from '@prisma/client' +import { TransactionWithAddressAndPrices } from 'services/transactionService' + +export interface BlockchainInfo { + height: number + hash: Uint8Array | string +} + +export interface BlockInfo extends BlockchainInfo { + timestamp: number +} + +export interface GetAddressTransactionsParameters { + addressString: string + start: number +} + +interface InputOutput { + value: Prisma.Decimal + address?: string +} + +export interface TransactionDetails { + hash: string + version: number + inputs: InputOutput[] + outputs: InputOutput[] + block: { + height?: number + hash?: string + timestamp?: string + } +} + +export interface AddressWithTransaction { + address: Address + transaction: Prisma.TransactionUncheckedCreateInput +} + +export interface BlockchainClient { + getBalance: (address: string) => Promise + syncTransactionsForAddress: (addressString: string) => AsyncGenerator + getBlockchainInfo: (networkSlug: string) => Promise + getBlockInfo: (networkSlug: string, height: number) => Promise + getTransactionDetails: (hash: string, networkSlug: string) => Promise + subscribeAddresses: (addresses: Address[]) => Promise + getSubscribedAddresses: () => string[] +} + +export type Networks = 'ecash' | 'bitcoincash' + +export interface NetworkClients{ + ecash?: BlockchainClient + bitcoincash?: BlockchainClient +} + +export interface NodeJsGlobalChronik extends NodeJS.Global { + chronik?: NetworkClients +}