import { H160 , H160Value , H256 , H256Value , H512 , H512Value , PlatformAddress , PlatformAddressValue , U64 , U64Value } from "codechain-primitives" ; import { Rpc } from "." ; import { Asset } from "../core/Asset" ; import { AssetScheme } from "../core/AssetScheme" ; import { Block } from "../core/Block" ; import { SignedTransaction } from "../core/SignedTransaction" ; import { Text } from "../core/Text" ; import { Transaction } from "../core/Transaction" ; import { fromJSONToSignedTransaction } from "../core/transaction/json" ; import { CommonParams , NetworkId } from "../core/types" ; export class ChainRpc { private rpc : Rpc ; private transactionSigner ? : string ; private fallbackServers ? : string [ ] ; hidden constructor ( rpc , options ) { const { transactionSigner , fallbackServers } = options ; this . rpc = rpc ; this . transactionSigner = transactionSigner ; this . fallbackServers = fallbackServers ; } param tx returns public sendSignedTransaction ( tx ) { if ( ! ( tx instanceof SignedTransaction ) ) { throw Error ( `Expected the first argument of sendSignedTransaction to be SignedTransaction but found ${ tx }` ) ; } return new Promise ( ( resolve , reject ) => { const bytes = tx . rlpBytes ( ) . toString ( "hex" ) ; const fallbackServers = this . fallbackServers ; this . rpc . sendRpcRequest ( "mempool_sendSignedTransaction" , [ `0x${ bytes }` ] , { fallbackServers } ) . then ( result => { try { resolve ( new H256 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected sendSignedTransaction() to return a value of H256, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param tx param options . account param options . passphrase param options . seq param options . fee returns throws throws throws throws throws public async sendTransaction ( tx , options = { } ) { if ( ! ( tx instanceof Transaction ) ) { throw Error ( `Expected the first argument of sendTransaction to be a Transaction but found ${ tx }` ) ; } const { account = this . transactionSigner , passphrase , blockNumber , fee = await this . getMinTransactionFee ( tx . type ( ) , blockNumber ) } = options ; if ( ! account ) { throw Error ( "The account to sign the tx is not specified" ) ; } else if ( ! PlatformAddress . check ( account ) ) { throw Error ( `Expected account param of sendTransaction to be a PlatformAddress value but found ${ account }` ) ; } const { seq = await this . getSeq ( account ) } = options ; tx . setSeq ( seq ! ) ; if ( ! fee ) { throw Error ( "The fee of the tx is not specified" ) ; } tx . setFee ( fee ) ; const address = PlatformAddress . ensure ( account ) ; const sig = await this . rpc . account . sign ( tx . unsignedHash ( ) , address , passphrase ) ; return this . sendSignedTransaction ( new SignedTransaction ( tx , sig ) ) ; } param hash returns public getTransaction ( hash ) { if ( ! H256 . check ( hash ) ) { throw Error ( `Expected the first argument of getTransaction to be an H256 value but found ${ hash }` ) ; } return new Promise ( ( resolve , reject ) => { const fallbackServers = this . fallbackServers ; this . rpc . sendRpcRequest ( "chain_getTransaction" , [ `0x${ H256 . ensure ( hash ) . value }` ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : fromJSONToSignedTransaction ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getTransaction to return either null or JSON of SignedTransaction, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param hash returns public async containsTransaction ( hash ) { if ( ! H256 . check ( hash ) ) { throw Error ( `Expected the first argument of containsTransaction to be an H256 value but found ${ hash }` ) ; } const result = await this . rpc . sendRpcRequest ( "chain_containsTransaction" , [ `0x${ H256 . ensure ( hash ) . value }` ] , { fallbackServers : this . fallbackServers } ) ; try { return JSON . parse ( result ) ; } catch ( e ) { throw Error ( `Expected chain_containsTransaction to return JSON of boolean, but an error occurred: ${ e . toString ( ) }` ) ; } } param address param blockNumber returns public getRegularKey ( address , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! PlatformAddress . check ( address ) ) { throw Error ( `Expected the first argument of getRegularKey to be a PlatformAddress value but found ${ address }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getRegularKey to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getRegularKey" , [ `${ PlatformAddress . ensure ( address ) . value }` , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : new H512 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getRegularKey to return either null or a value of H512, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param regularKey param blockNumber return public getRegularKeyOwner ( regularKey , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H512 . check ( regularKey ) ) { throw Error ( `Expected the first argument of getRegularKeyOwner to be an H512 value but found ${ regularKey }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getRegularKeyOwner to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getRegularKeyOwner" , [ `0x${ H512 . ensure ( regularKey ) . value }` , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : PlatformAddress . fromString ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getRegularKeyOwner to return a PlatformAddress string, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param hash param blockNumber returns public getShardIdByHash ( hash , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( hash ) ) { throw Error ( `Expected the first argument of getShardIdByHash to be an H256 value but found ${ hash }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getShardIdByHash to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getShardIdByHash" , [ H256 . ensure ( hash ) . toJSON ( ) , blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null || typeof result === "number" ) { resolve ( result ) ; } else { reject ( Error ( `Expected chain_getShardIdByHash to return either number or null but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param shardId returns public getShardOwners ( shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getShardOwners" , [ shardId , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : ( result as string [ ] ) . map ( str => PlatformAddress . ensure ( str ) ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getShardOwners to return either null or an array of PlatformAddress, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param shardId returns public getShardUsers ( shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getShardUsers" , [ shardId , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : ( result as string [ ] ) . map ( str => PlatformAddress . ensure ( str ) ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getShardUsers to return either null or an array of PlatformAddress, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param tracker returns public getTransactionByTracker ( tracker ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( tracker ) ) { throw Error ( `Expected the first argument of getTransactionByTracker to be an H256 value but found ${ tracker }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getTransactionByTracker" , [ `0x${ H256 . ensure ( tracker ) . value }` ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : fromJSONToSignedTransaction ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getTransactionByTracker to return either null or JSON of Transaction, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param tracker param options . timeout returns public async getTransactionResultsByTracker ( tracker , options = { } ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( tracker ) ) { throw Error ( `Expected the first argument of getTransactionResultsByTracker to be an H256 value but found ${ tracker }` ) ; } const attemptToGet = async ( ) => { return this . rpc . sendRpcRequest ( "mempool_getTransactionResultsByTracker" , [ `0x${ H256 . ensure ( tracker ) . value }` ] , { fallbackServers } ) ; } ; const startTime = Date . now ( ) ; const { timeout } = options ; if ( timeout !== undefined && ( typeof timeout !== "number" || timeout < 0 ) ) { throw Error ( `Expected timeout param of getTransactionResultsByTracker to be non-negative number but found ${ timeout }` ) ; } let result = await attemptToGet ( ) ; while ( result == null && timeout !== undefined && Date . now ( ) - startTime < timeout ) { await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; result = await attemptToGet ( ) ; } if ( result == null ) { return [ ] ; } try { return result . map ( JSON . parse ) ; } catch ( e ) { throw Error ( `Expected mempool_getTransactionResultsByTracker to return JSON of boolean[], but an error occurred: ${ e . toString ( ) }. received: ${ JSON . stringify ( result ) }` ) ; } } param address param blockNumber returns public getBalance ( address , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! PlatformAddress . check ( address ) ) { throw Error ( `Expected the first argument of getBalance to be a PlatformAddress value but found ${ address }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getBalance to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getBalance" , [ `${ PlatformAddress . ensure ( address ) . value }` , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? ( null as any ) : new U64 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getBalance to return a value of U64, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param transactionHash returns public async getErrorHint ( transactionHash ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( transactionHash ) ) { throw Error ( `Expected the first argument of getErrorHint to be an H256 value but found ${ transactionHash }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "mempool_getErrorHint" , [ `0x${ H256 . ensure ( transactionHash ) . value }` ] , { fallbackServers } ) . then ( result => { if ( typeof result === "string" || result == null ) { return resolve ( result ) ; } reject ( Error ( `Expected mempool_getErrorHint to return either null or value of string, but it returned ${ result }` ) ) ; } ) . catch ( reject ) ; } ) ; } param address param blockNumber returns public getSeq ( address , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! PlatformAddress . check ( address ) ) { throw Error ( `Expected the first argument of getSeq to be a PlatformAddress value but found ${ address }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getSeq to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getSeq" , [ `${ PlatformAddress . ensure ( address ) . value }` , blockNumber ] , { fallbackServers } ) . then ( async ( result ) => { if ( result == null ) { throw Error ( "chain_getSeq returns undefined" ) ; } resolve ( result ) ; } ) . catch ( reject ) ; } ) ; } returns public getBestBlockNumber ( ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getBestBlockNumber" , [ ] , { fallbackServers } ) . then ( result => { if ( typeof result === "number" ) { return resolve ( result ) ; } reject ( Error ( `Expected chain_getBestBlockNumber to return a number, but it returned ${ result }` ) ) ; } ) . catch ( reject ) ; } ) ; } param blockNumber returns public getBlockHash ( blockNumber ) { const fallbackServers = this . fallbackServers ; if ( ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the first argument of getBlockHash to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getBlockHash" , [ blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : new H256 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getBlockHash to return either null or a value of H256, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param hashOrNumber returns public async getBlock ( hashOrNumber ) { let result ; const fallbackServers = this . fallbackServers ; if ( hashOrNumber instanceof H256 || typeof hashOrNumber === "string" ) { result = await this . rpc . sendRpcRequest ( "chain_getBlockByHash" , [ `0x${ H256 . ensure ( hashOrNumber ) . value }` ] , { fallbackServers } ) ; } else if ( typeof hashOrNumber === "number" ) { result = await this . rpc . sendRpcRequest ( "chain_getBlockByNumber" , [ hashOrNumber ] , { fallbackServers } ) ; } else { throw Error ( `Expected the first argument of getBlock to be either a number or an H256 value but found ${ hashOrNumber }` ) ; } try { return result === null ? null : Block . fromJSON ( result ) ; } catch ( e ) { throw Error ( `Expected chain_getBlock to return either null or JSON of Block, but an error occurred: ${ e . toString ( ) }` ) ; } } param tracker param shardId param blockNumber returns public getAssetSchemeByTracker ( tracker , shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( tracker ) ) { throw Error ( `Expected the first arugment of getAssetSchemeByTracker to be an H256 value but found ${ tracker }` ) ; } if ( ! isShardIdValue ( shardId ) ) { throw Error ( `Expected the second argument of getAssetSchemeByTracker to be a shard ID value but found ${ shardId }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the third argument of getAssetSchemeByTracker to be non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getAssetSchemeByTracker" , [ `0x${ H256 . ensure ( tracker ) . value }` , shardId , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : AssetScheme . fromJSON ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getAssetSchemeByTracker to return either null or JSON of AssetScheme, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param assetType param shardId param blockNumber returns public getAssetSchemeByType ( assetType , shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H160 . check ( assetType ) ) { throw Error ( `Expected the first arugment of getAssetSchemeByType to be an H160 value but found ${ assetType }` ) ; } if ( ! isShardIdValue ( shardId ) ) { throw Error ( `Expected the second argument of getAssetSchemeByType to be a shard ID value but found ${ shardId }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the third argument of getAssetSchemeByType to be non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getAssetSchemeByType" , [ `0x${ H160 . ensure ( assetType ) . value }` , shardId , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : AssetScheme . fromJSON ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getAssetSchemeByType to return either null or JSON of AssetScheme, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param tracker param index param shardId param blockNumber returns public getAsset ( tracker , index , shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( tracker ) ) { throw Error ( `Expected the first argument of getAsset to be an H256 value but found ${ tracker }` ) ; } if ( ! isNonNegativeInterger ( index ) ) { throw Error ( `Expected the second argument of getAsset to be non-negative integer but found ${ index }` ) ; } if ( ! isShardIdValue ( shardId ) ) { throw Error ( `Expected the third argument of getAsset to be a shard ID value but found ${ shardId }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the forth argument of getAsset to be non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getAsset" , [ `0x${ H256 . ensure ( tracker ) . value }` , index , shardId , blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null ) { return resolve ( null ) ; } try { resolve ( Asset . fromJSON ( { ... result , shardId , tracker : H256 . ensure ( tracker ) . value , transactionOutputIndex : index } ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getAsset to return either null or JSON of Asset, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param txHash param blockNumber returns public getText ( txHash , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( txHash ) ) { throw Error ( `Expected the first arugment of getText to be an H256 value but found ${ txHash }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getText to be non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getText" , [ `0x${ H256 . ensure ( txHash ) . value }` , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result == null ? null : Text . fromJSON ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getText to return either null or JSON of Text, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param txhash param index param shardId param blockNumber returns public isAssetSpent ( txhash , index , shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( txhash ) ) { throw Error ( `Expected the first argument of isAssetSpent to be an H256 value but found ${ txhash }` ) ; } if ( ! isNonNegativeInterger ( index ) ) { throw Error ( `Expected the second argument of isAssetSpent to be a non-negative integer but found ${ index }` ) ; } if ( ! isShardIdValue ( shardId ) ) { throw Error ( `Expected the third argument of isAssetSpent to be a shard ID value but found ${ shardId }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_isAssetSpent" , [ `0x${ H256 . ensure ( txhash ) . value }` , index , shardId , blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null || typeof result === "boolean" ) { resolve ( result ) ; } else { reject ( Error ( `Expected chain_isAssetSpent to return either null or a boolean but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param from param to param futureIncluded returns public getPendingTransactions ( from ? , to ? , futureIncluded = false ) { const fallbackServers = this . fallbackServers ; if ( from != null && ! isNonNegativeInterger ( from ) ) { throw Error ( `Expected the first argument of getPendingTransactions to be a non-negative integer but found ${ from }` ) ; } if ( to != null && ! isNonNegativeInterger ( to ) ) { throw Error ( `Expected the second argument of getPendingTransactions to be a non-negative integer but found ${ to }` ) ; } if ( typeof futureIncluded !== "boolean" ) { throw Error ( `Expected the third argument to be a boolean but found ${ futureIncluded }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "mempool_getPendingTransactions" , [ from , to , futureIncluded ] , { fallbackServers } ) . then ( result => { try { const resultTransactions = result . transactions ; const resultLastTimestamp = result . lastTimestamp ; if ( ! Array . isArray ( resultTransactions ) ) { return reject ( Error ( `Expected mempool_getPendingTransactions to return an object whose property "transactions" is of array type but it is ${ resultTransactions }` ) ) ; } if ( resultLastTimestamp !== null && typeof resultLastTimestamp !== "number" ) { return reject ( Error ( `Expected mempool_getPendingTransactions to return an object containing a number but it returned ${ resultLastTimestamp }` ) ) ; } resolve ( { transactions : resultTransactions . map ( fromJSONToSignedTransaction ) , lastTimestamp : resultLastTimestamp } ) ; } catch ( e ) { reject ( Error ( `Expected mempool_getPendingTransactions to return an object who has transactions and lastTimestamp properties, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param transactionType param blockNumber returns public getMinTransactionFee ( transactionType , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getMinTransactionFee to be non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getMinTransactionFee" , [ transactionType , blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null || typeof result === "number" ) { resolve ( result ) ; } else { reject ( Error ( `Expected chain_getMinTransactionFee to return either null or a number but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } returns public getNetworkId ( ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getNetworkId" , [ ] , { fallbackServers } ) . then ( result => { if ( typeof result === "string" ) { resolve ( result ) ; } else { reject ( Error ( `Expected chain_getNetworkId to return a string but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param blockNumber returns public getNumberOfShards ( blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the first argument of getNumberOfShards to be a number but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getNumberOfShards" , [ blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null || typeof result === "number" ) { resolve ( result ) ; } else { reject ( Error ( `Expected chain_getNumberOfShards to return a number, but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } returns public getGenesisAccounts ( ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getGenesisAccounts" , [ ] , { fallbackServers } ) . then ( result => { try { resolve ( ( result as string [ ] ) . map ( str => PlatformAddress . ensure ( str ) ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getGenesisAccounts to return an array of PlatformAddress, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param shardId param blockNumber returns public getShardRoot ( shardId , blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( ! isShardIdValue ( shardId ) ) { throw Error ( `Expected the first argument of getShardRoot to be a shard ID value but found ${ shardId }` ) ; } if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the second argument of getShardRoot to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getShardRoot" , [ shardId , blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : new H256 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getShardRoot to return either null or a value of H256, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param blockNumber returns public getMiningReward ( blockNumber ) { const fallbackServers = this . fallbackServers ; if ( ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the argument of getMiningReward to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getMiningReward" , [ blockNumber ] , { fallbackServers } ) . then ( result => { try { resolve ( result === null ? null : new U64 ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getMiningReward to return either null or a value of U64, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param tx param sender returns public executeTransaction ( tx , sender ) { const fallbackServers = this . fallbackServers ; if ( ! ( tx instanceof Transaction ) ) { throw Error ( `Expected the first argument of executeTransaction to be a Transaction but found ${ tx }` ) ; } if ( ! PlatformAddress . check ( sender ) ) { throw Error ( `Expected the second argument of executeTransaction to be a PlatformAddress value but found ${ PlatformAddress }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_executeTransaction" , [ tx . toJSON ( ) , PlatformAddress . ensure ( sender ) . toString ( ) ] , { fallbackServers } ) . then ( resolve ) . catch ( reject ) ; } ) ; } returns public getBestBlockId ( ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getBestBlockId" , [ ] , { fallbackServers } ) . then ( result => { if ( result . hasOwnProperty ( "hash" ) && result . hasOwnProperty ( "number" ) ) { return resolve ( result ) ; } else { reject ( Error ( `Expected chain_getBestBlockId to return a number and an H256 value , but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param hash returns public getBlockTransactionCountByHash ( hash ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( hash ) ) { throw Error ( `Expected the first argument of getBlockTransactionCountByHash to be an H256 value but found ${ hash }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getBlockTransactionCountByHash" , [ hash ] , { fallbackServers } ) . then ( result => { if ( result == null || typeof result === "number" ) { return resolve ( result ) ; } else { reject ( Error ( `Expected chain_getBlockTransactionCountByHash to return either null or a number but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } /**
     * Deletes all pending transactions including future ones.
     */ public deleteAllPendingTransactions ( ) { const fallbackServers = this . fallbackServers ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "mempool_deleteAllPendingTransactions" , [ ] , { fallbackServers } ) . then ( result => { if ( result == null ) { return resolve ( ) ; } reject ( Error ( `Expected mempool_deleteAllPendingTransactions to return null but it returned ${ result }` ) ) ; } ) ; } ) ; } param from param to param futureIncluded returns public getPendingTransactionsCount ( from ? , to ? , futureIncluded = false ) { const fallbackServers = this . fallbackServers ; if ( from != null && ! isNonNegativeInterger ( from ) ) { throw Error ( `Expected the first argument of getPendingTransactions to be a non-negative integer but found ${ from }` ) ; } if ( to != null && ! isNonNegativeInterger ( to ) ) { throw Error ( `Expected the second argument of getPendingTransactions to be a non-negative integer but found ${ to }` ) ; } if ( typeof futureIncluded !== "boolean" ) { throw Error ( `Expected the third argument to be a boolean but found ${ futureIncluded }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "mempool_getPendingTransactionsCount" , [ from , to , futureIncluded ] , { fallbackServers } ) . then ( result => { if ( typeof result === "number" ) { resolve ( result ) ; } else { reject ( Error ( `Expected mempool_getPendingTransactionsCount to return a number but returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param transaction param parameters param indices returns public executeVM ( transaction , parameters , indices ) { const fallbackServers = this . fallbackServers ; if ( ! ( transaction instanceof SignedTransaction ) ) { throw Error ( `Expected the first argument of executeVM to be a Transaction but found ${ transaction }` ) ; } if ( parameters . length !== indices . length ) { throw Error ( `The length of paramters and indices must be equal` ) ; } const params = parameters . map ( parameter => parameter . map ( string => { if ( /^[0-9a-f]+$/g . test ( string ) ) { return [ ... Buffer . from ( string , "hex" ) ] ; } else { throw Error ( `Parameters should be array of array of hex string` ) ; } } ) ) ; return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_executeVM" , [ transaction , params , indices ] , { fallbackServers } ) . then ( result => { if ( result . every ( ( str ) => typeof str === "string" ) ) { resolve ( result ) ; } else { throw Error ( `Failed to execute VM` ) ; } } ) . catch ( reject ) ; } ) ; } param blockNumber returns public getCommonParams ( blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the argument of getCommonParams to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getCommonParams" , [ blockNumber ] , { fallbackServers } ) . then ( result => { try { if ( result === null ) { return resolve ( null ) ; } resolve ( { maxExtraDataSize : new U64 ( result . maxExtraDataSize ) , maxAssetSchemeMetadataSize : new U64 ( result . maxAssetSchemeMetadataSize ) , maxTransferMetadataSize : new U64 ( result . maxTransferMetadataSize ) , maxTextContentSize : new U64 ( result . maxTextContentSize ) , networkID : result . networkID , minPayCost : new U64 ( result . minPayCost ) , minSetRegularKeyCost : new U64 ( result . minSetRegularKeyCost ) , minCreateShardCost : new U64 ( result . minCreateShardCost ) , minSetShardOwnersCost : new U64 ( result . minSetShardOwnersCost ) , minSetShardUsersCost : new U64 ( result . minSetShardUsersCost ) , minWrapCccCost : new U64 ( result . minWrapCccCost ) , minCustomCost : new U64 ( result . minCustomCost ) , minStoreCost : new U64 ( result . minStoreCost ) , minRemoveCost : new U64 ( result . minRemoveCost ) , minMintAssetCost : new U64 ( result . minMintAssetCost ) , minTransferAssetCost : new U64 ( result . minTransferAssetCost ) , minChangeAssetSchemeCost : new U64 ( result . minChangeAssetSchemeCost ) , minIncreaseAssetSupplyCost : new U64 ( result . minIncreaseAssetSupplyCost ) , minComposeAssetCost : new U64 ( result . minComposeAssetCost ) , minDecomposeAssetCost : new U64 ( result . minDecomposeAssetCost ) , minUnwrapCccCost : new U64 ( result . minUnwrapCccCost ) , maxBodySize : new U64 ( result . maxBodySize ) , snapshotPeriod : new U64 ( result . snapshotPeriod ) , termSeconds : new U64 ( result . termSeconds ) , nominationExpiration : new U64 ( result . nominationExpiration ) , custodyPeriod : new U64 ( result . custodyPeriod ) , releasePeriod : new U64 ( result . releasePeriod ) , maxNumOfValidators : new U64 ( result . maxNumOfValidators ) , minNumOfValidators : new U64 ( result . minNumOfValidators ) , delegationThreshold : new U64 ( result . delegationThreshold ) , minDeposit : new U64 ( result . minDeposit ) , maxCandidateMetadataSize : new U64 ( result . maxCandidateMetadataSize ) } ) ; } catch ( e ) { reject ( Error ( `Expected chain_getCommonParams to return a common params struct, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } params returns public getPossibleAuthors ( blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the argument of getPossibleAuthors to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getPossibleAuthors" , [ blockNumber ] , { fallbackServers } ) . then ( result => { try { if ( result === null ) { return resolve ( null ) ; } resolve ( result . map ( ( address ) => PlatformAddress . fromString ( address ) ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getPossibleAuthors to return a list of platform addresses, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param hash returns public getTransactionSigner ( hash ) { const fallbackServers = this . fallbackServers ; if ( ! H256 . check ( hash ) ) { throw Error ( `Expected the first argument of getTransactionSigner to be an H256 value but found ${ hash }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getTransactionSigner" , [ hash ] , { fallbackServers } ) . then ( result => { try { if ( result === null ) { return resolve ( null ) ; } resolve ( PlatformAddress . fromString ( result ) ) ; } catch ( e ) { reject ( Error ( `Expected chain_getTransactionSigner to return a platform addresses, but an error occurred: ${ e . toString ( ) }` ) ) ; } } ) . catch ( reject ) ; } ) ; } param blockNumber returns public getMetadataSeq ( blockNumber ? ) { const fallbackServers = this . fallbackServers ; if ( blockNumber !== undefined && ! isNonNegativeInterger ( blockNumber ) ) { throw Error ( `Expected the argument of getMetadataSeq to be a non-negative integer but found ${ blockNumber }` ) ; } return new Promise ( ( resolve , reject ) => { this . rpc . sendRpcRequest ( "chain_getMetadataSeq" , [ blockNumber ] , { fallbackServers } ) . then ( result => { if ( result === null || typeof result === "number" ) { return resolve ( result ) ; } else { reject ( Error ( `Expected chain_getMetadataSeq to return a sequence number or null, but it returned ${ result }` ) ) ; } } ) . catch ( reject ) ; } ) ; } } function isNonNegativeInterger ( value ) { return typeof value === "number" && Number . isInteger ( value ) && value >= 0 ; } function isShardIdValue ( value ) { return ( typeof value === "number" && Number . isInteger ( value ) && value >= 0 && value <= 0xffff ) ; }