packageDocumentation module import { RPCMethod , Messenger , ResponseMiddleware , WSProvider , NewPendingTransactions , NewHeaders , LogSub , Syncing , } from '@harmony-js/network' ; import { assertObject , AssertType , DefaultBlockParams , } from '@harmony-js/utils' ; import { getAddress } from '@harmony-js/crypto' ; import { Transaction } from '@harmony-js/transaction' ; import { StakingTransaction } from '@harmony-js/staking' ; class Blockchain { hidden messenger : Messenger ; hidden constructor ( messenger ) { this . messenger = messenger ; } hidden setMessenger ( messenger ) { this . messenger = messenger ; } hidden getRpcResult ( result ) { if ( result instanceof ResponseMiddleware ) { return result . getRaw ; } else { return result ; } } param address param blockNumber param shardID returns hint example @ assertObject ( { address : [ 'isValidAddress' , AssertType . required ] , blockNumber : [ 'isBlockNumber' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBalance ( { address , blockNumber = DefaultBlockParams . latest , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBalance , [ getAddress ( address ) . checksum , blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param shardID return hint example async getBlockNumber ( shardID = this . messenger . currentShard ) { const result = await this . messenger . send ( RPCMethod . BlockNumber , [ ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockHash param returnObject param shardID returns example @ assertObject ( { blockHash : [ 'isHash' , AssertType . required ] , returnObject : [ 'isBoolean' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBlockByHash ( { blockHash , returnObject = true , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBlockByHash , [ blockHash , returnObject ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockNumber param returnObject param shardID returns example @ assertObject ( { blockNumber : [ 'isBlockNumber' , AssertType . optional ] , returnObject : [ 'isBoolean' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBlockByNumber ( { blockNumber = DefaultBlockParams . latest , returnObject = true , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBlockByNumber , [ blockNumber , returnObject ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param from param to param blockArgs param shardID returns example @ assertObject ( { from : [ 'isBlockNumber' , AssertType . required ] , to : [ 'isBlockNumber' , AssertType . required ] , blockArgs : [ 'isObject' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBlocks ( { from , to , blockArgs = { fullTx : false , withSigners : false , } , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBlocks , [ from , to , blockArgs ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockHash param shardID returns example @ assertObject ( { blockHash : [ 'isHash' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBlockTransactionCountByHash ( { blockHash , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBlockTransactionCountByHash , [ blockHash ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockNumber param shardID returns example @ assertObject ( { blockNumber : [ 'isBlockNumber' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getBlockTransactionCountByNumber ( { blockNumber , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetBlockTransactionCountByNumber , [ blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockHash param index param shardID returns example @ assertObject ( { blockHash : [ 'isHash' , AssertType . required ] , index : [ 'isHex' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getTransactionByBlockHashAndIndex ( { blockHash , index , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetTransactionByBlockHashAndIndex , [ blockHash , index ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param blockNumber param index param shardID returns example @ assertObject ( { blockNumber : [ 'isBlockNumber' , AssertType . optional ] , index : [ 'isHex' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getTransactionByBlockNumberAndIndex ( { blockNumber = DefaultBlockParams . latest , index , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetTransactionByBlockNumberAndIndex , [ blockNumber , index ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param txnHash param shardID returns example @ assertObject ( { txnHash : [ 'isHash' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getTransactionByHash ( { txnHash , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetTransactionByHash , [ txnHash ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param txnHash param shardID returns example @ assertObject ( { txnHash : [ 'isString' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getTransactionReceipt ( { txnHash , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetTransactionReceipt , [ txnHash ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param txnHash param shardID returns example @ assertObject ( { txnHash : [ 'isString' , AssertType . required ] , shardID : [ 'isNumber' , AssertType . required ] , } ) async getCxReceiptByHash ( { txnHash , shardID } ) { const result = await this . messenger . send ( RPCMethod . GetCXReceiptByHash , [ txnHash ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param address param blockNumber param shardID return example @ assertObject ( { address : [ 'isValidAddress' , AssertType . required ] , blockNumber : [ 'isBlockNumber' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getCode ( { address , blockNumber = DefaultBlockParams . latest , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetCode , [ getAddress ( address ) . checksum , blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param shardID returns example async net_peerCount ( shardID = this . messenger . currentShard ) { const result = await this . messenger . send ( RPCMethod . PeerCount , [ ] , 'net' , shardID ) ; return this . getRpcResult ( result ) ; } param shardID returns example async net_version ( shardID = this . messenger . currentShard ) { const result = await this . messenger . send ( RPCMethod . NetVersion , [ ] , 'net' , shardID ) ; return this . getRpcResult ( result ) ; } param shardID returns example async getProtocolVersion ( shardID = this . messenger . currentShard ) { const result = await this . messenger . send ( RPCMethod . ProtocolVersion , [ ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param address param position param blockNumber param shardID example @ assertObject ( { address : [ 'isValidAddress' , AssertType . required ] , position : [ 'isHex' , AssertType . required ] , blockNumber : [ 'isBlockNumber' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getStorageAt ( { address , position , blockNumber = DefaultBlockParams . latest , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetStorageAt , [ getAddress ( address ) . checksum , position , blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param address param blockNumber param shardID return example @ assertObject ( { address : [ 'isValidAddress' , AssertType . required ] , blockNumber : [ 'isBlockNumber' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async getTransactionCount ( { address , blockNumber = DefaultBlockParams . latest , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . GetTransactionCount , [ getAddress ( address ) . checksum , blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } return example async getShardingStructure ( ) { const result = await this . messenger . send ( RPCMethod . GetShardingStructure , [ ] , this . messenger . chainPrefix ) ; return this . getRpcResult ( result ) ; } param transaction return example async sendTransaction ( transaction ) { if ( ! transaction . isSigned ( ) || ! transaction ) { throw new Error ( 'transaction is not signed or not exist' ) ; } const result = await this . messenger . send ( RPCMethod . SendRawTransaction , [ transaction . getRawTransaction ( ) ] , this . messenger . chainPrefix , typeof transaction . txParams . shardID === 'string' ? Number . parseInt ( transaction . txParams . shardID , 10 ) : transaction . txParams . shardID ) ; return this . getRpcResult ( result ) ; } param transaction return example async sendRawTransaction ( transaction ) { if ( ! transaction . isSigned ( ) || ! transaction ) { throw new Error ( 'transaction is not signed or not exist' ) ; } const [ txn , result ] = await transaction . sendTransaction ( ) ; if ( txn . isPending ( ) ) { return result ; } } param transaction return example createObservedTransaction ( transaction ) { try { transaction . sendTransaction ( ) . then ( ( response ) => { const [ txReturned , TranID ] = response ; txReturned . confirm ( TranID ) . then ( ( txConfirmed ) => { transaction . emitter . resolve ( txConfirmed ) ; } ) ; } ) ; return transaction . emitter ; } catch ( err ) { throw err ; } } param staking ignore warning async sendRawStakingTransaction ( staking ) { if ( ! staking . isSigned ( ) || ! staking ) { throw new Error ( 'staking transaction is not signed or not exist' ) ; } const [ txn , result ] = await staking . sendTransaction ( ) ; if ( txn . isPending ( ) ) { return result ; } } param staking ignore warning createObservedStakingTransaction ( staking ) { try { staking . sendTransaction ( ) . then ( ( response ) => { const [ txReturned , TranID ] = response ; txReturned . confirm ( TranID ) . then ( ( txConfirmed ) => { staking . emitter . resolve ( txConfirmed ) ; } ) ; } ) ; return staking . emitter ; } catch ( err ) { throw err ; } } param to param data param shardID return warning example @ assertObject ( { to : [ 'isValidAddress' , AssertType . optional ] , data : [ 'isHex' , AssertType . optional ] , shardID : [ 'isNumber' , AssertType . optional ] , } ) async estimateGas ( { to , data , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . EstimateGas , [ { to : getAddress ( to ) . checksum , data } ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param shardID return example async gasPrice ( shardID = this . messenger . currentShard ) { const result = await this . messenger . send ( RPCMethod . GasPrice , [ ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param payload param blockNumber param shardID example async call ( { payload , blockNumber = DefaultBlockParams . latest , shardID = this . messenger . currentShard , } ) { const result = await this . messenger . send ( RPCMethod . Call , [ payload , blockNumber ] , this . messenger . chainPrefix , shardID ) ; return this . getRpcResult ( result ) ; } param shardID example newPendingTransactions ( shardID = this . messenger . currentShard ) { if ( this . messenger . provider instanceof WSProvider ) { return new NewPendingTransactions ( this . messenger , shardID ) ; } else { throw new Error ( 'HttpProvider does not support this feature' ) ; } } param shardID example newBlockHeaders ( shardID = this . messenger . currentShard ) { if ( this . messenger . provider instanceof WSProvider ) { return new NewHeaders ( this . messenger , shardID ) ; } else { throw new Error ( 'HttpProvider does not support this feature' ) ; } } param shardID example syncing ( shardID = this . messenger . currentShard ) { if ( this . messenger . provider instanceof WSProvider ) { return new Syncing ( this . messenger , shardID ) ; } else { throw new Error ( 'HttpProvider does not support this feature' ) ; } } param shardID example logs ( options , shardID = this . messenger . currentShard ) { if ( this . messenger . provider instanceof WSProvider ) { return new LogSub ( options , this . messenger , shardID ) ; } else { throw new Error ( 'HttpProvider does not support this feature' ) ; } } } export { Blockchain } ;