import { ERROR_CODE } from './../../error' ; import axios from 'axios' ; import { TEST_ONT_URL } from '../../consts' ; import { Address } from '../../crypto/address' ; import UrlConsts from './urlConsts' ; /**
 * Wrapper class for restful api.
 */ export default class RestClient { /**
     * Url of the blockchain node
     */ url : string ; /**
     * Version of restful api
     */ version : string = 'v1.0.0' ; /**
     * Action name of the request
     */ action : string = 'sendrawtransaction' ; constructor ( url ? ) { this . url = url || TEST_ONT_URL . REST_URL ; if ( this . url [ this . url . length - 1 ] === '/' ) { this . url = this . url . substring ( 0 , this . url . length - 1 ) ; } } param params concatParams ( params ) { let result = '' ; if ( params . size === 0 ) { return '' ; } for ( const key of params . keys ( ) ) { let value = params . get ( key ) ; if ( value ) { value = encodeURIComponent ( value ) ; } result += `&${ key }=${ value }` ; } return '?' + result . substr ( 1 ) ; } /**
     * Get the current blockchain node url
     */ getUrl ( ) { return this . url ; } param hexData param preExec param userId sendRawTransaction ( hexData , preExec = false , userId ? ) { const param = new Map < string , string > ( ) ; if ( userId ) { param . set ( 'userid' , userId ) ; } if ( preExec ) { param . set ( 'preExec' , '1' ) ; } let url = this . url + UrlConsts . Url_send_transaction ; url += this . concatParams ( param ) ; const body = { Action : this . action , Version : this . version , Data : hexData } ; return axios . post ( url , body ) . then ( ( res ) => { return res . data ; } ) ; } param txHash example getRawTransaction ( txHash ) { const param = new Map < string , string > ( ) ; param . set ( 'raw' , '1' ) ; let url = this . url + UrlConsts . Url_get_transaction + txHash ; url += this . concatParams ( param ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param txHash getRawTransactionJson ( txHash ) { const param = new Map < string , string > ( ) ; param . set ( 'raw' , '0' ) ; let url = this . url + UrlConsts . Url_get_transaction + txHash ; url += this . concatParams ( param ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } /** Deprecated
     * Get the generation time for each block.
     * If the blockchain node runs in vbft, the result is null.
     */ /**
     * Get the nodes count of the blockchain.
     */ getNodeCount ( ) { const url = this . url + UrlConsts . Url_get_node_count ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } /**
     * Get the current height of the blockchain.
     */ getBlockHeight ( ) { const url = this . url + UrlConsts . Url_get_block_height ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param value getBlock ( value ) { const params = new Map < string , string > ( ) ; params . set ( 'raw' , '1' ) ; let url = '' ; if ( typeof value === 'number' ) { url = this . url + UrlConsts . Url_get_block_by_height + value ; } else if ( typeof value === 'string' ) { url = this . url + UrlConsts . Url_get_block_by_hash + value ; } url += this . concatParams ( params ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param codeHash getContract ( codeHash ) { const params = new Map < string , string > ( ) ; params . set ( 'raw' , '1' ) ; let url = this . url + UrlConsts . Url_get_contract_state + codeHash ; url += this . concatParams ( params ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param codeHash getContractJson ( codeHash ) { const params = new Map < string , string > ( ) ; params . set ( 'raw' , '0' ) ; let url = this . url + UrlConsts . Url_get_contract_state + codeHash ; url += this . concatParams ( params ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param value getSmartCodeEvent ( value ) { let url = '' ; if ( typeof value === 'string' ) { url = this . url + UrlConsts . Url_get_smartcodeevent_by_txhash + value ; } else if ( typeof value === 'number' ) { url = this . url + UrlConsts . Url_get_smartcodeevent_txs_by_height + value ; } return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param hash getBlockHeightByTxHash ( hash ) { const url = this . url + UrlConsts . Url_get_block_height_by_txhash + hash ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param codeHash param key getStorage ( codeHash , key ) { const url = this . url + UrlConsts . Url_get_storage + codeHash + '/' + key ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param hash getMerkleProof ( hash ) { const url = this . url + UrlConsts . Url_get_merkleproof + hash ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param address getBalance ( address ) { const url = this . url + UrlConsts . Url_get_account_balance + address . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param address getBalanceV2 ( address ) { const url = this . url + UrlConsts . Url_get_account_balance_v2 + address . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param value getBlockJson ( value ) { let url = '' ; if ( typeof value === 'number' ) { url = this . url + UrlConsts . Url_get_block_by_height + value ; } else if ( typeof value === 'string' ) { url = this . url + UrlConsts . Url_get_block_by_hash + value ; } return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param asset param from param to getAllowance ( asset , from , to ) { asset = asset . toLowerCase ( ) ; if ( asset !== 'ont' && asset !== 'ong' ) { throw ERROR_CODE . INVALID_PARAMS ; } const url = this . url + UrlConsts . Url_get_allowance + asset . toLowerCase ( ) + '/' + from . toBase58 ( ) + '/' + to . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } param asset param from param to getAllowanceV2 ( asset , from , to ) { asset = asset . toLowerCase ( ) ; if ( asset !== 'ont' && asset !== 'ong' ) { throw ERROR_CODE . INVALID_PARAMS ; } const url = this . url + UrlConsts . Url_get_allowance_v2 + asset . toLowerCase ( ) + '/' + from . toBase58 ( ) + '/' + to . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getUnboundOng ( address ) { const url = this . url + UrlConsts . Url_get_unbound_ong + address . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getBlockTxsByHeight ( height ) { const url = this . url + UrlConsts . Url_get_block_txs_by_height + height ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getGasPrice ( ) { const url = this . url + UrlConsts . Url_get_gasprice ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getGrantOng ( address ) { const url = this . url + UrlConsts . Url_get_grant_ong + address . toBase58 ( ) ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getMempoolTxCount ( ) { const url = this . url + UrlConsts . Url_get_mempool_txcount ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getMempoolTxState ( hash ) { const url = this . url + UrlConsts . Url_get_mempool_txstate + hash ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } getVersion ( ) { const url = this . url + UrlConsts . Url_get_version ; return axios . get ( url ) . then ( ( res ) => { return res . data ; } ) ; } }