import { AddressString , addressToScriptHash , ApplicationLogJSON , Base64String , BlockJSON , BufferString , CallReceiptJSON , ConfirmedTransactionJSON , ContractJSON , GetOptions , Hash256String , HeaderJSON , NativeContractJSON , Nep17BalancesJSON , Nep17TransfersJSON , NetworkSettingsJSON , Peer , PrivateNetworkSettings , RelayTransactionResultJSON , SendRawTransactionResultJSON , StorageItemJSON , TransactionDataJSON , TransactionReceiptJSON , TriggerTypeJSON , UnclaimedGASJSON , ValidateAddressJSON , ValidatorJSON , VerificationCostJSON , VersionJSON , } from '@neo-one/client-common' ; import { RelayTransactionError } from '../errors' ; import { JSONRPCProvider , JSONRPCProviderManager } from './JSONRPCProvider' ; export class JSONRPCClient { private readonly provider : JSONRPCProvider | JSONRPCProviderManager ; public constructor ( provider ) { this . provider = provider ; } public async getBlock ( hashOrIndex , options = { } ) { const { timeoutMS } = options ; return this . withInstance ( async ( provider ) => provider . request ( { method : 'getblock' , params : [ hashOrIndex , 1 ] , watchTimeoutMS : timeoutMS } ) ) ; } public async getNep17Balances ( address ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getnep17balances' , params : [ addressToScriptHash ( address ) ] } ) ) ; } public async getNep17Transfers ( address , startTime ? , endTime ? ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getnep17transfers' , params : [ addressToScriptHash ( address ) , startTime , endTime ] } ) ) ; } public async getBestBlockHash ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getbestblockhash' } ) ) ; } public async getBlockCount ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getblockcount' } ) ) ; } public async getBlockHeaderCount ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getblockheadercount' } ) ) ; } public async getFeePerByte ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getfeeperbyte' } ) ) ; } public async getExecFeeFactor ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getexecfeefactor' } ) ) ; } public async getContract ( address ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getcontractstate' , params : [ addressToScriptHash ( address ) ] } ) ) ; } public async getMemPool ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getrawmempool' } ) ) ; } public async getTransaction ( hash ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getrawtransaction' , params : [ hash , 1 ] } ) ) ; } public async testInvokeRaw ( script , verifications = [ ] ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'invokescript' , params : [ script , ... verifications ] } ) ) ; } public async getVerificationCost ( hash , transaction ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getverificationcost' , params : [ hash , transaction ] } ) ) ; } public async testTransaction ( transaction ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'testtransaction' , params : [ transaction ] } ) ) ; } public async relayTransaction ( value ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'relaytransaction' , params : [ value ] } ) . catch ( ( error ) => { const [ message , code ] = error . message . split ( ':' ) ; if ( error . code === 'JSON_RPC' && code === '-110' ) { throw new RelayTransactionError ( message ) ; } throw error ; } ) ) ; } public async sendRawTransaction ( value ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'sendrawtransaction' , params : [ value ] } ) . catch ( ( error ) => { const [ message , code ] = error . message . split ( ':' ) ; if ( error . code === 'JSON_RPC' && code === '-110' ) { throw new RelayTransactionError ( message ) ; } throw error ; } ) ) ; } public async getUnclaimedGas ( address ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getunclaimedgas' , params : [ address ] } ) ) ; } public async getAllStorage ( address ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getallstorage' , params : [ addressToScriptHash ( address ) ] } ) ) ; } public async getTransactionReceipt ( hash , options = { } ) { const { timeoutMS } = options ; return this . withInstance ( async ( provider ) => provider . request ( { method : 'gettransactionreceipt' , params : [ hash ] , watchTimeoutMS : timeoutMS } ) ) ; } public async getTransactionHeight ( hash ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'gettransactionheight' , params : [ hash ] } ) ) ; } public async getTransactionData ( hash ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'gettransactiondata' , params : [ hash ] } ) ) ; } public async getBlockHash ( index ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getblockhash' , params : [ index ] } ) ) ; } public async getBlockHeader ( hashOrIndex ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getblockheader' , params : [ hashOrIndex , 1 ] } ) ) ; } public async getValidators ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getnextblockvalidators' } ) ) ; } public async validateAddress ( address ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'validateaddress' , params : [ address ] } ) ) ; } public async getConnectionCount ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getconnectioncount' } ) ) ; } public async getStorage ( address , key ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getstorage' , params : [ address , key , 1 ] } ) ) ; } public async getVersion ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getversion' } ) ) ; } public async getApplicationLog ( hash , trigger ? ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getapplicationlog' , params : trigger === undefined ? [ hash ] : [ hash , trigger ] } ) ) ; } public async getConnectedPeers ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getpeers' } ) . then ( ( result ) => result . connected ) ) ; } public async getCommittee ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getcommittee' } ) ) ; } public async getNativeContracts ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getnativecontracts' } ) ) ; } public async getNetworkSettings ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getnetworksettings' } ) ) ; } public async runConsensusNow ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'runconsensusnow' } ) ) ; } public async updateSettings ( options ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'updatesettings' , params : [ options ] } ) ) ; } public async getSettings ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getsettings' } ) ) ; } public async fastForwardOffset ( seconds ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'fastforwardoffset' , params : [ seconds ] } ) ) ; } public async fastForwardToTime ( seconds ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'fastforwardtotime' , params : [ seconds ] } ) ) ; } public async reset ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'reset' } ) ) ; } public async getNEOTrackerURL ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'getneotrackerurl' } ) ) ; } public async resetProject ( ) { return this . withInstance ( async ( provider ) => provider . request ( { method : 'resetproject' } ) ) ; } private async withInstance < TResult > ( func ) ; private async withInstance < TResult > ( func ) { if ( this . provider instanceof JSONRPCProvider ) { return func ( this . provider as any ) ; } const instance = await this . provider . getInstance ( ) ; return func ( instance ) ; } }