import { TEST_ONT_URL } from '../../consts' ; import { Address } from '../../crypto/address' ; import { Deferred } from './deferred' ; import * as Builder from './websocketBuilder' ; import { WebsocketSender } from './websocketSender' ; /**
 * Websocket client.
 *
 * TODO: correlate request and response with id, so socket can be reused.
 */ export class WebsocketClient { sender : WebsocketSender ; autoClose : boolean ; promises : Map < string , Deferred < any > > ; constructor ( url = TEST_ONT_URL . SOCKET_URL , debug = false , autoClose = true ) { this . autoClose = autoClose ; this . promises = new Map ( ) ; this . sender = new WebsocketSender ( url , debug ) ; this . sender . addListener ( this . notifyListener . bind ( this ) ) ; } /**
     * Send heart beat request
     */ async sendHeartBeat ( ) { const raw = Builder . sendHeartBeat ( ) ; return this . send ( raw ) ; } param subscribeEvent param subscribeJsonBlock param subscribeRawBlock param subscribeBlockTxHashes async sendSubscribe ( subscribeEvent = false , subscribeJsonBlock = false , subscribeRawBlock = false , subscribeBlockTxHashes = false ) { const raw = Builder . sendSubscribe ( subscribeEvent , subscribeJsonBlock , subscribeRawBlock , subscribeBlockTxHashes ) ; return this . send ( raw ) ; } param hexData param preExec param waitNotify async sendRawTransaction ( hexData , preExec = false , waitNotify = false ) { const raw = Builder . sendRawTransaction ( hexData , preExec ) ; const sendResult = await this . send ( raw , this . autoClose && ! waitNotify ) ; if ( sendResult . Error !== 0 ) { throw new Error ( JSON . stringify ( sendResult ) ) ; } if ( waitNotify ) { const txHash = sendResult . Result ; const deferred = new Deferred < any > ( ) ; this . promises . set ( txHash , deferred ) ; return deferred . promise ; } else { return sendResult ; } } param txHash async getRawTransaction ( txHash ) { const raw = Builder . getRawTransaction ( txHash ) ; return this . send ( raw ) ; } param txHash async getRawTransactionJson ( txHash ) { const raw = Builder . getRawTransactionJson ( txHash ) ; return this . send ( raw ) ; } /** Deprecated
     * Get the generation time for each block.
     * If the blockchain node runs in vbft, the result is null.
     */ /**
     * Get Nodes count
     */ async getNodeCount ( ) { const raw = Builder . getNodeCount ( ) ; return this . send ( raw ) ; } /**
     * Get current block height
     */ async getBlockHeight ( ) { const raw = Builder . getBlockHeight ( ) ; return this . send ( raw ) ; } param value async getBlock ( value ) { const raw = Builder . getBlock ( value ) ; return this . send ( raw ) ; } param value async getBlockJson ( value ) { const raw = Builder . getBlockJson ( value ) ; return this . send ( raw ) ; } param address async getBalance ( address ) { const raw = Builder . getBalance ( address ) ; return this . send ( raw ) ; } param address async getBalanceV2 ( address ) { const raw = Builder . getBalanceV2 ( address ) ; return this . send ( raw ) ; } param address async getUnboundong ( address ) { const raw = Builder . getUnboundOng ( address ) ; return this . send ( raw ) ; } param hash async getContract ( hash ) { const raw = Builder . getContract ( hash ) ; return this . send ( raw ) ; } param hash async getContractJson ( hash ) { const raw = Builder . getContractJson ( hash ) ; return this . send ( raw ) ; } param value async getSmartCodeEvent ( value ) { const raw = Builder . getSmartCodeEvent ( value ) ; return this . send ( raw ) ; } param hash async getBlockHeightByTxHash ( hash ) { const raw = Builder . getBlockHeightByTxHash ( hash ) ; return this . send ( raw ) ; } param codeHash param key async getStorage ( codeHash , key ) { const raw = Builder . getStorage ( codeHash , key ) ; return this . send ( raw ) ; } param hash async getMerkleProof ( hash ) { const raw = Builder . getMerkleProof ( hash ) ; return this . send ( raw ) ; } param asset param from param to async getAllowance ( asset , from , to ) { const raw = Builder . getAllowance ( asset , from , to ) ; return this . send ( raw ) ; } param asset param from param to async getAllowanceV2 ( asset , from , to ) { const raw = Builder . getAllowanceV2 ( asset , from , to ) ; return this . send ( raw ) ; } param height async getBlockHash ( height ) { const raw = Builder . getBlockHash ( height ) ; return this . send ( raw ) ; } param height async getBlockTxsByHeight ( height ) { const raw = Builder . getBlockTxsByHeight ( height ) ; return this . send ( raw ) ; } /**
     * Return the state of transaction locate in memory
     */ async getGasPrice ( ) { const raw = Builder . getGasPrice ( ) ; return this . send ( raw ) ; } param address async getGrantOng ( address ) { const raw = Builder . getGrantOng ( address ) ; return this . send ( raw ) ; } /**
     * Query the transaction count in the memory pool
     */ async getMempoolTxCount ( ) { const raw = Builder . getMempoolTxCount ( ) ; return this . send ( raw ) ; } /**
     * Query the transaction state in the memory pool
     */ async getMempoolTxState ( txHash ) { const raw = Builder . getMempoolTxState ( txHash ) ; return this . send ( raw ) ; } /**
     * Get the version information of the node
     */ async getVersion ( ) { const raw = Builder . getVersion ( ) ; return this . send ( raw ) ; } /**
     * Get the network id
     */ async getNetworkId ( ) { const raw = Builder . getNetworkId ( ) ; return this . send ( raw ) ; } param listener addNotifyListener ( listener ) { this . sender . addListener ( ( result ) => { if ( result . Action === 'Notify' ) { listener ( result ) ; } } ) ; } /**
     * Close the websocket manually.
     */ close ( ) { this . sender . close ( ) ; } param raw param close private async send < T extends object > ( raw , close = this . autoClose ) { return this . sender . send ( raw , close ) ; } private notifyListener ( result ) { if ( result . Action === 'Notify' || result . Action === 'Log' ) { const txHash = result . Result . TxHash ; if ( txHash !== undefined ) { const promise = this . promises . get ( txHash ) ; if ( promise !== undefined ) { this . promises . delete ( txHash ) ; promise . resolve ( result ) ; } else { console . warn ( 'Received Notify event for unknown transaction' ) ; } if ( this . autoClose ) { this . sender . close ( ) ; } } } } }