1- import { BlockInfo_InNode , ChronikClientNode , ScriptType_InNode , ScriptUtxo_InNode , Tx_InNode , WsConfig_InNode , WsEndpoint_InNode , WsMsgClient , WsSubScriptClient } from 'chronik-client-cashtokens'
1+ import { BlockInfo , ChronikClient , ScriptType , ScriptUtxo , Tx , WsConfig , WsEndpoint , WsMsgClient , WsSubScriptClient } from 'chronik-client-cashtokens'
22import { encode , decode } from 'ecashaddrjs'
33import bs58 from 'bs58'
4- import { AddressWithTransaction , BlockchainInfo , BlockInfo , TransactionDetails , ProcessedMessages , SubbedAddressesLog , SyncAndSubscriptionReturn , SubscriptionReturn } from 'types/chronikTypes'
4+ import { AddressWithTransaction , BlockchainInfo , TransactionDetails , ProcessedMessages , SubbedAddressesLog , SyncAndSubscriptionReturn , SubscriptionReturn , SimpleBlockInfo } from 'types/chronikTypes'
55import { 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 , SOCKET_MESSAGES , NETWORK_IDS , NETWORK_TICKERS , MainNetworkSlugsType , MAX_MEMPOOL_TXS_TO_PROCESS_AT_A_TIME , MEMPOOL_PROCESS_DELAY , CHRONIK_INITIALIZATION_DELAY } from 'constants/index'
66import { productionAddresses } from 'prisma/seeds/addresses'
77import {
@@ -107,10 +107,10 @@ export function getNullDataScriptData (outputScript: string): OpReturnData | nul
107107}
108108
109109export class ChronikBlockchainClient {
110- chronik : ChronikClientNode
110+ chronik : ChronikClient
111111 networkId : number
112112 networkSlug : string
113- chronikWSEndpoint : WsEndpoint_InNode
113+ chronikWSEndpoint : WsEndpoint
114114 confirmedTxsHashesFromLastBlock : string [ ]
115115 wsEndpoint : Socket
116116 CHRONIK_MSG_PREFIX : string
@@ -127,7 +127,7 @@ export class ChronikBlockchainClient {
127127 this . mempoolTxsBeingProcessed = 0
128128 this . networkSlug = networkSlug
129129 this . networkId = NETWORK_IDS_FROM_SLUGS [ networkSlug ]
130- this . chronik = new ChronikClientNode ( [ config . networkBlockchainURLs [ networkSlug ] ] )
130+ this . chronik = new ChronikClient ( [ config . networkBlockchainURLs [ networkSlug ] ] )
131131 this . chronikWSEndpoint = this . chronik . ws ( this . getWsConfig ( ) )
132132 this . confirmedTxsHashesFromLastBlock = [ ]
133133 void this . chronikWSEndpoint . waitForOpen ( )
@@ -197,14 +197,14 @@ export class ChronikBlockchainClient {
197197 return { height : blockchainInfo . tipHeight , hash : blockchainInfo . tipHash }
198198 }
199199
200- async getBlockInfo ( networkSlug : string , height : number ) : Promise < BlockInfo > {
200+ async getBlockInfo ( networkSlug : string , height : number ) : Promise < SimpleBlockInfo > {
201201 this . validateNetwork ( networkSlug )
202- const blockInfo : BlockInfo_InNode = ( await this . chronik . block ( height ) ) . blockInfo
202+ const blockInfo : BlockInfo = ( await this . chronik . block ( height ) ) . blockInfo
203203 return { hash : blockInfo . hash , height : blockInfo . height , timestamp : blockInfo . timestamp }
204204 }
205205
206206 private txThesholdFilter ( address : Address ) {
207- return ( t : Tx_InNode , _index : number , _array : Tx_InNode [ ] ) : boolean => {
207+ return ( t : Tx , _index : number , _array : Tx [ ] ) : boolean => {
208208 return (
209209 t . block === undefined ||
210210 ( t . block ?. timestamp >= XEC_TIMESTAMP_THRESHOLD && address . networkId === XEC_NETWORK_ID ) ||
@@ -213,16 +213,16 @@ export class ChronikBlockchainClient {
213213 }
214214 }
215215
216- private async getTransactionAmountAndData ( transaction : Tx_InNode , addressString : string ) : Promise < { amount : Prisma . Decimal , opReturn : string } > {
217- let totalOutput = 0
218- let totalInput = 0
216+ private async getTransactionAmountAndData ( transaction : Tx , addressString : string ) : Promise < { amount : Prisma . Decimal , opReturn : string } > {
217+ let totalOutput = 0n
218+ let totalInput = 0n
219219 const addressFormat = xecaddr . detectAddressFormat ( addressString )
220220 const script = toHash160 ( addressString ) . hash160
221221 let opReturn = ''
222222
223223 for ( const output of transaction . outputs ) {
224224 if ( output . outputScript . includes ( script ) ) {
225- totalOutput += output . value
225+ totalOutput += output . sats
226226 }
227227 if ( opReturn === '' ) {
228228 const nullScriptData = getNullDataScriptData ( output . outputScript )
@@ -235,17 +235,17 @@ export class ChronikBlockchainClient {
235235 }
236236 for ( const input of transaction . inputs ) {
237237 if ( input ?. outputScript ?. includes ( script ) === true ) {
238- totalInput += input . value
238+ totalInput += input . sats
239239 }
240240 }
241- const satoshis = new Prisma . Decimal ( totalOutput ) . minus ( totalInput )
241+ const satoshis = totalOutput - totalInput
242242 return {
243243 amount : await satoshisToUnit ( satoshis , addressFormat ) ,
244244 opReturn
245245 }
246246 }
247247
248- private async getTransactionFromChronikTransaction ( transaction : Tx_InNode , address : Address ) : Promise < Prisma . TransactionUncheckedCreateInput > {
248+ private async getTransactionFromChronikTransaction ( transaction : Tx , address : Address ) : Promise < Prisma . TransactionUncheckedCreateInput > {
249249 const { amount, opReturn } = await this . getTransactionAmountAndData ( transaction , address . address )
250250 return {
251251 hash : transaction . txid ,
@@ -257,7 +257,7 @@ export class ChronikBlockchainClient {
257257 }
258258 }
259259
260- public async getPaginatedTxs ( addressString : string , page : number , pageSize : number ) : Promise < Tx_InNode [ ] > {
260+ public async getPaginatedTxs ( addressString : string , page : number , pageSize : number ) : Promise < Tx [ ] > {
261261 const { type, hash160 } = toHash160 ( addressString )
262262 return ( await this . chronik . script ( type , hash160 ) . history ( page , pageSize ) ) . txs
263263 }
@@ -314,15 +314,15 @@ export class ChronikBlockchainClient {
314314 await updateLastSynced ( addressString )
315315 }
316316
317- private async getUtxos ( address : string ) : Promise < ScriptUtxo_InNode [ ] > {
317+ private async getUtxos ( address : string ) : Promise < ScriptUtxo [ ] > {
318318 const { type, hash160 } = toHash160 ( address )
319319 const scriptsUtxos = await this . chronik . script ( type , hash160 ) . utxos ( )
320320 return scriptsUtxos . utxos
321321 }
322322
323- public async getBalance ( address : string ) : Promise < number > {
323+ public async getBalance ( address : string ) : Promise < bigint > {
324324 const utxos = await this . getUtxos ( address )
325- return utxos . reduce ( ( acc , utxo ) => acc + utxo . value , 0 )
325+ return utxos . reduce ( ( acc , utxo ) => acc + utxo . sats , 0n )
326326 }
327327
328328 async getTransactionDetails ( hash : string ) : Promise < TransactionDetails > {
@@ -341,20 +341,20 @@ export class ChronikBlockchainClient {
341341 }
342342 for ( const input of tx . inputs ) {
343343 details . inputs . push ( {
344- value : new Prisma . Decimal ( input . value ) ,
344+ value : input . sats ,
345345 address : outputScriptToAddress ( this . networkSlug , input . outputScript )
346346 } )
347347 }
348348 for ( const output of tx . outputs ) {
349349 details . outputs . push ( {
350- value : new Prisma . Decimal ( output . value ) ,
350+ value : output . sats ,
351351 address : outputScriptToAddress ( this . networkSlug , output . outputScript )
352352 } )
353353 }
354354 return details
355355 }
356356
357- private getWsConfig ( ) : WsConfig_InNode {
357+ private getWsConfig ( ) : WsConfig {
358358 return {
359359 onMessage : ( msg : WsMsgClient ) => { void this . processWsMessage ( msg ) } ,
360360 onError : ( e : ws . ErrorEvent ) => { console . log ( `${ this . CHRONIK_MSG_PREFIX } : Chronik webSocket error, type: ${ e . type } | message: ${ e . message } | error: ${ e . error as string } ` ) } ,
@@ -365,19 +365,19 @@ export class ChronikBlockchainClient {
365365 }
366366 }
367367
368- private getSortedInputAddresses ( transaction : Tx_InNode ) : string [ ] {
369- const addressValueMap = new Map < string , number > ( )
368+ private getSortedInputAddresses ( transaction : Tx ) : string [ ] {
369+ const addressSatsMap = new Map < string , bigint > ( )
370370
371371 transaction . inputs . forEach ( ( inp ) => {
372372 const address = outputScriptToAddress ( this . networkSlug , inp . outputScript )
373373 if ( address !== undefined && address !== '' ) {
374- const currentValue = addressValueMap . get ( address ) ?? 0
375- addressValueMap . set ( address , currentValue + inp . value )
374+ const currentValue = addressSatsMap . get ( address ) ?? 0n
375+ addressSatsMap . set ( address , currentValue + inp . sats )
376376 }
377377 } )
378378
379- const sortedInputAddresses = Array . from ( addressValueMap . entries ( ) )
380- . sort ( ( [ , valueA ] , [ , valueB ] ) => valueB - valueA )
379+ const sortedInputAddresses = Array . from ( addressSatsMap . entries ( ) )
380+ . sort ( ( [ , valueA ] , [ , valueB ] ) => Number ( valueB - valueA ) )
381381 . map ( ( [ address ] ) => address )
382382
383383 return sortedInputAddresses
@@ -458,7 +458,7 @@ export class ChronikBlockchainClient {
458458 let page = 0
459459 const pageSize = 200
460460 let blockPageTxs = ( await this . chronik . blockTxs ( blockHash , page , pageSize ) ) . txs
461- let blockTxsToSync : Tx_InNode [ ] = [ ]
461+ let blockTxsToSync : Tx [ ] = [ ]
462462 while ( blockPageTxs . length > 0 && blockTxsToSync . length !== this . confirmedTxsHashesFromLastBlock . length ) {
463463 const thisBlockTxsToSync = blockPageTxs . filter ( tx => this . confirmedTxsHashesFromLastBlock . includes ( tx . txid ) )
464464 blockTxsToSync = [ ...blockTxsToSync , ...thisBlockTxsToSync ]
@@ -481,13 +481,13 @@ export class ChronikBlockchainClient {
481481 }
482482 }
483483
484- private getRelatedAddressesForTransaction ( transaction : Tx_InNode ) : string [ ] {
484+ private getRelatedAddressesForTransaction ( transaction : Tx ) : string [ ] {
485485 const inputAddresses = transaction . inputs . map ( inp => outputScriptToAddress ( this . networkSlug , inp . outputScript ) )
486486 const outputAddresses = transaction . outputs . map ( out => outputScriptToAddress ( this . networkSlug , out . outputScript ) )
487487 return [ ...inputAddresses , ...outputAddresses ] . filter ( a => a !== undefined )
488488 }
489489
490- private async getAddressesForTransaction ( transaction : Tx_InNode ) : Promise < AddressWithTransaction [ ] > {
490+ private async getAddressesForTransaction ( transaction : Tx ) : Promise < AddressWithTransaction [ ] > {
491491 const relatedAddresses = this . getRelatedAddressesForTransaction ( transaction )
492492 const addressesFromStringArray = await fetchAddressesArray ( relatedAddresses )
493493 const addressesWithTransactions : AddressWithTransaction [ ] = await Promise . all ( addressesFromStringArray . map (
@@ -628,14 +628,14 @@ export function fromHash160 (networkSlug: string, type: string, hash160: string)
628628 )
629629}
630630
631- export function toHash160 ( address : string ) : { type : ScriptType_InNode , hash160 : string } {
631+ export function toHash160 ( address : string ) : { type : ScriptType , hash160 : string } {
632632 try {
633633 const { type, hash } = decode ( address )
634634 const legacyAdress = bs58 . encode ( hash )
635635 const addrHash160 = Buffer . from ( bs58 . decode ( legacyAdress ) ) . toString (
636636 'hex'
637637 )
638- return { type : type . toLowerCase ( ) as ScriptType_InNode , hash160 : addrHash160 }
638+ return { type : type . toLowerCase ( ) as ScriptType , hash160 : addrHash160 }
639639 } catch ( err ) {
640640 console . log ( '[CHRONIK]: Error converting address to hash160' )
641641 throw err
@@ -781,7 +781,7 @@ class MultiBlockchainClient {
781781 return await this . clients [ networkSlug as MainNetworkSlugsType ] . getLastBlockTimestamp ( )
782782 }
783783
784- public async getBalance ( address : string ) : Promise < number > {
784+ public async getBalance ( address : string ) : Promise < bigint > {
785785 const networkSlug = getAddressPrefix ( address )
786786 return await this . clients [ networkSlug as MainNetworkSlugsType ] . getBalance ( address )
787787 }
0 commit comments