import axios from 'axios' ; import { TEST_ONT_URL } from '../../consts' ; import { Address } from '../../crypto/address' ; import { ERROR_CODE } from '../../error' ; /**
 * Wrapper class for RPC apis.
 */ export default class RpcClient { /**
     * Url of the blockchain node
     */ url : string ; constructor ( url ? ) { this . url = url || TEST_ONT_URL . RPC_URL ; } /**
     * Get the current blockchain node url.
     */ getUrl ( ) { return this . url ; } param method param params makeRequest ( method , ... params ) { const request = { jsonrpc : '2.0' , method , params , id : 1 } ; return request ; } param address getBalance ( address ) { const req = this . makeRequest ( 'getbalance' , address . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param address getBalanceV2 ( address ) { const req = this . makeRequest ( 'getbalancev2' , address . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param data param preExec sendRawTransaction ( data , preExec = false ) { let req ; if ( preExec ) { req = this . makeRequest ( 'sendrawtransaction' , data , 1 ) ; } else { req = this . makeRequest ( 'sendrawtransaction' , data ) ; } return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param txHash getRawTransaction ( txHash ) { const req = this . makeRequest ( 'getrawtransaction' , txHash ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param txHash getRawTransactionJson ( txHash ) { const req = this . makeRequest ( 'getrawtransaction' , txHash , 1 ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } /** Deprecated
     * Get the generation time for each block.
     * If the blockchain node runs in vbft, the result is null cause the time is not fixed.
     */ /**
     * Get the nodes count.
     */ getNodeCount ( ) { const req = this . makeRequest ( 'getconnectioncount' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } /**
     * Get the current block height.
     */ getBlockHeight ( ) { const req = this . makeRequest ( 'getblockcount' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { if ( res . data && res . data . result ) { return res . data . result - 1 ; } else { return 0 ; } } ) ; } /**
     * Get the all blocks count.
     */ getBlockCount ( ) { const req = this . makeRequest ( 'getblockcount' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param value getBlockJson ( value ) { const req = this . makeRequest ( 'getblock' , value , 1 ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param hash getContract ( hash ) { const req = this . makeRequest ( 'getcontractstate' , hash ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param codeHash getContractJson ( codeHash ) { const req = this . makeRequest ( 'getcontractstate' , codeHash , 1 ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param value getBlock ( value ) { const req = this . makeRequest ( 'getblock' , value ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param value getSmartCodeEvent ( value ) { const req = this . makeRequest ( 'getsmartcodeevent' , value ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param txHash getBlockHeightByTxHash ( txHash ) { const req = this . makeRequest ( 'getblockheightbytxhash' , txHash ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param codeHash param key getStorage ( codeHash , key ) { const req = this . makeRequest ( 'getstorage' , codeHash , key ) ; console . log ( req ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param hash getMerkleProof ( hash ) { const req = this . makeRequest ( 'getmerkleproof' , hash ) ; console . log ( this . url ) ; console . log ( req ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param asset param from param to getAllowance ( asset , from , to ) { if ( asset !== 'ont' && asset !== 'ong' ) { throw ERROR_CODE . INVALID_PARAMS ; } const req = this . makeRequest ( 'getallowance' , asset , from . toBase58 ( ) , to . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } param asset param from param to getAllowanceV2 ( asset , from , to ) { if ( asset !== 'ont' && asset !== 'ong' ) { throw ERROR_CODE . INVALID_PARAMS ; } const req = this . makeRequest ( 'getallowancev2' , asset , from . toBase58 ( ) , to . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getUnboundOng ( address ) { const req = this . makeRequest ( 'getunboundong' , 'ong' , address . toBase58 ( ) , address . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getBlockTxsByHeight ( height ) { const req = this . makeRequest ( 'getblocktxsbyheight' , height ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getGasPrice ( ) { const req = this . makeRequest ( 'getgasprice' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getGrantOng ( address ) { const req = this . makeRequest ( 'getgrantong' , address . toBase58 ( ) ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getMempoolTxCount ( ) { const req = this . makeRequest ( 'getmempooltxcount' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getMempoolTxState ( txHash ) { const req = this . makeRequest ( 'getmempooltxstate' , txHash ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } getVersion ( ) { const req = this . makeRequest ( 'getversion' ) ; return axios . post ( this . url , req ) . then ( ( res ) => { return res . data ; } ) ; } }