import { Connector } from '../../connectors/Connector' ; import { BlockHeader , BlockMessages , ChainEpoch , Cid , HeadChange , Message , MessageReceipt , ObjStat , TipSet , TipSetKey , WrappedMessage , } from '../Types' ; import { WsJsonRpcConnector } from '../../connectors/WsJsonRpcConnector' ; import { HttpJsonRpcConnector } from '../../connectors/HttpJsonRpcConnector' ; const CHAIN_NOTIFY_INTERVAL = 2000 ; /**
 * The Chain method group contains methods for interacting with the blockchain, but that do not require any form of state computation.
 */ export class JsonRpcChainMethodGroup { private conn : Connector ; constructor ( conn ) { this . conn = conn ; } param cid public async readObj ( cid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainReadObj' , params : [ cid ] } ) ; return ret as string ; } param cid public async deleteObj ( cid ) { const error = await this . conn . request ( { method : 'Filecoin.ChainDeleteObj' , params : [ cid ] } ) ; return error as string ; } param blockCid public async getBlockMessages ( blockCid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetBlockMessages' , params : [ blockCid ] } ) ; return ret as BlockMessages ; } /**
     * returns the current head of the chain
     */ public async getHead ( ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainHead' } ) ; return ret as TipSet ; } param cb returns public async chainNotify ( cb ) { if ( this . conn instanceof WsJsonRpcConnector ) { const subscriptionId = await this . conn . request ( { method : 'Filecoin.ChainNotify' } ) ; this . conn . on ( subscriptionId , cb ) ; } else if ( this . conn instanceof HttpJsonRpcConnector ) { let head ; return setInterval ( async ( ) => { const currentHead = await this . getHead ( ) ; if ( head !== currentHead ) { head = currentHead ; cb ( [ { Type : '' , Val : currentHead } ] ) ; } } , CHAIN_NOTIFY_INTERVAL ) ; } } public stopChainNotify ( id ? ) { if ( this . conn instanceof HttpJsonRpcConnector && id ) { clearInterval ( id ) ; } else if ( this . conn instanceof WsJsonRpcConnector ) { this . conn . closeSubscription ( id ) ; } } param blockCid public async getBlock ( blockCid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetBlock' , params : [ blockCid ] } ) ; return ret as BlockHeader ; } param messageCid public async getMessage ( messageCid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetMessage' , params : [ messageCid ] } ) ; return ret as Message ; } param blockCid public async getParentReceipts ( blockCid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetParentReceipts' , params : [ blockCid ] } ) ; return ret as MessageReceipt [ ] ; } param blockCid public async getParentMessages ( blockCid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetParentMessages' , params : [ blockCid ] } ) ; return ret as WrappedMessage [ ] ; } param cid public async hasObj ( cid ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainHasObj' , params : [ cid ] } ) ; return ret as boolean ; } remarks public async statObj ( obj , base ? ) { const stat = await this . conn . request ( { method : 'Filecoin.ChainStatObj' , params : [ obj , base ] } ) ; return stat ; } param tipSetKey public async setHead ( tipSetKey ) { await this . conn . request ( { method : 'Filecoin.ChainSetHead' , params : [ tipSetKey ] } ) ; } param tipSet public async getGenesis ( ) { const genesis = await this . conn . request ( { method : 'Filecoin.ChainGetGenesis' , params : [ ] } ) ; return genesis ; } param tipSetKey public async getTipSetWeight ( tipSetKey ? ) { const weight = await this . conn . request ( { method : 'Filecoin.ChainTipSetWeight' , params : [ tipSetKey ] } ) ; return weight ; } param epochNumber public async getTipSetByHeight ( epochNumber ) { const ret = await this . conn . request ( { method : 'Filecoin.ChainGetTipSetByHeight' , params : [ epochNumber , [ ] ] } ) ; return ret ; } param from param to public async getPath ( from , to ) { const path = await this . conn . request ( { method : 'Filecoin.ChainGetPath' , params : [ from , to ] } ) ; return path ; } param nroots param tipSetKey remarks public async export ( nroots , oldmsgskip , tipSetKey ) { const path = await this . conn . request ( { method : 'Filecoin.ChainExport' , params : [ nroots , oldmsgskip , tipSetKey ] } ) ; return path ; } }