Skip to content

Commit

Permalink
chore: reorganize files to avoid circular import
Browse files Browse the repository at this point in the history
Revert "chore: reorganize files to avoid circular import"

This reverts commit 76b7afa.

chore: remove wip

Reapply "chore: reorganize files to avoid circular import"

This reverts commit 22c1fcc.
  • Loading branch information
chedieck committed Aug 13, 2024
1 parent e29941b commit 7387f6c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
1 change: 0 additions & 1 deletion pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 5 additions & 57 deletions services/blockchainService.ts
Original file line number Diff line number Diff line change
@@ -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<number>
syncTransactionsForAddress: (addressString: string) => AsyncGenerator<TransactionWithAddressAndPrices[]>
getBlockchainInfo: (networkSlug: string) => Promise<BlockchainInfo>
getBlockInfo: (networkSlug: string, height: number) => Promise<BlockInfo>
getTransactionDetails: (hash: string, networkSlug: string) => Promise<TransactionDetails>
subscribeAddresses: (addresses: Address[]) => Promise<void>
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion services/chronikService.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
59 changes: 59 additions & 0 deletions types/blockchainInterfaces.ts
Original file line number Diff line number Diff line change
@@ -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<number>
syncTransactionsForAddress: (addressString: string) => AsyncGenerator<TransactionWithAddressAndPrices[]>
getBlockchainInfo: (networkSlug: string) => Promise<BlockchainInfo>
getBlockInfo: (networkSlug: string, height: number) => Promise<BlockInfo>
getTransactionDetails: (hash: string, networkSlug: string) => Promise<TransactionDetails>
subscribeAddresses: (addresses: Address[]) => Promise<void>
getSubscribedAddresses: () => string[]
}

export type Networks = 'ecash' | 'bitcoincash'

export interface NetworkClients{
ecash?: BlockchainClient
bitcoincash?: BlockchainClient
}

export interface NodeJsGlobalChronik extends NodeJS.Global {
chronik?: NetworkClients
}

0 comments on commit 7387f6c

Please sign in to comment.