2022-04-11 17:14:19 -04:00
var _ _classPrivateFieldSet = ( this && this . _ _classPrivateFieldSet ) || function ( receiver , state , value , kind , f ) {
if ( kind === "m" ) throw new TypeError ( "Private method is not writable" ) ;
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a setter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot write private member to an object whose class did not declare it" ) ;
return ( kind === "a" ? f . call ( receiver , value ) : f ? f . value = value : state . set ( receiver , value ) ) , value ;
} ;
var _ _classPrivateFieldGet = ( this && this . _ _classPrivateFieldGet ) || function ( receiver , state , kind , f ) {
if ( kind === "a" && ! f ) throw new TypeError ( "Private accessor was defined without a getter" ) ;
if ( typeof state === "function" ? receiver !== state || ! f : ! state . has ( receiver ) ) throw new TypeError ( "Cannot read private member from an object whose class did not declare it" ) ;
return kind === "m" ? f : kind === "a" ? f . call ( receiver ) : f ? f . value : state . get ( receiver ) ;
} ;
var _PollingBlockSubscriber _instances , _PollingBlockSubscriber _provider , _PollingBlockSubscriber _poller , _PollingBlockSubscriber _interval , _PollingBlockSubscriber _blockNumber , _PollingBlockSubscriber _poll , _OnBlockSubscriber _provider , _OnBlockSubscriber _poll , _PollingOrphanSubscriber _filter , _PollingTransactionSubscriber _hash , _PollingEventSubscriber _instances , _PollingEventSubscriber _provider , _PollingEventSubscriber _filter , _PollingEventSubscriber _poller , _PollingEventSubscriber _blockNumber , _PollingEventSubscriber _poll ;
import { isHexString } from "@ethersproject/bytes" ;
import { logger } from "./logger.js" ;
function copy ( obj ) {
return JSON . parse ( JSON . stringify ( obj ) ) ;
}
export function getPollingSubscriber ( provider , event ) {
if ( event === "block" ) {
return new PollingBlockSubscriber ( provider ) ;
}
if ( isHexString ( event , 32 ) ) {
return new PollingTransactionSubscriber ( provider , event ) ;
}
return logger . throwError ( "unsupported polling event" , "UNSUPPORTED_OPERATION" , {
operation : "getPollingSubscriber" , info : { event }
} ) ;
}
// @TODO: refactor this
export class PollingBlockSubscriber {
constructor ( provider ) {
_PollingBlockSubscriber _instances . add ( this ) ;
_PollingBlockSubscriber _provider . set ( this , void 0 ) ;
_PollingBlockSubscriber _poller . set ( this , void 0 ) ;
_PollingBlockSubscriber _interval . set ( this , void 0 ) ;
// The most recent block we have scanned for events. The value -2
// indicates we still need to fetch an initial block number
_PollingBlockSubscriber _blockNumber . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _provider , provider , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _poller , null , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _interval , 4000 , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _blockNumber , - 2 , "f" ) ;
}
get pollingInterval ( ) { return _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _interval , "f" ) ; }
set pollingInterval ( value ) { _ _classPrivateFieldSet ( this , _PollingBlockSubscriber _interval , value , "f" ) ; }
start ( ) {
if ( _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _poller , "f" ) ) {
throw new Error ( "subscriber already running" ) ;
}
_ _classPrivateFieldGet ( this , _PollingBlockSubscriber _instances , "m" , _PollingBlockSubscriber _poll ) . call ( this ) ;
2022-04-14 04:51:35 -04:00
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _poller , _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _provider , "f" ) . _setTimeout ( _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _instances , "m" , _PollingBlockSubscriber _poll ) . bind ( this ) , _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _interval , "f" ) ) , "f" ) ;
2022-04-11 17:14:19 -04:00
}
stop ( ) {
if ( ! _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _poller , "f" ) ) {
throw new Error ( "subscriber not running" ) ;
}
2022-04-14 04:51:35 -04:00
_ _classPrivateFieldGet ( this , _PollingBlockSubscriber _provider , "f" ) . _clearTimeout ( _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _poller , "f" ) ) ;
2022-04-11 17:14:19 -04:00
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _poller , null , "f" ) ;
}
pause ( dropWhilePaused ) {
this . stop ( ) ;
if ( dropWhilePaused ) {
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _blockNumber , - 2 , "f" ) ;
}
}
resume ( ) {
this . start ( ) ;
}
}
_PollingBlockSubscriber _provider = new WeakMap ( ) , _PollingBlockSubscriber _poller = new WeakMap ( ) , _PollingBlockSubscriber _interval = new WeakMap ( ) , _PollingBlockSubscriber _blockNumber = new WeakMap ( ) , _PollingBlockSubscriber _instances = new WeakSet ( ) , _PollingBlockSubscriber _poll = async function _PollingBlockSubscriber _poll ( ) {
const blockNumber = await _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _provider , "f" ) . getBlockNumber ( ) ;
if ( _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _blockNumber , "f" ) === - 2 ) {
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _blockNumber , blockNumber , "f" ) ;
return ;
}
// @TODO: Put a cap on the maximum number of events per loop?
if ( blockNumber !== _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _blockNumber , "f" ) ) {
for ( let b = _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _blockNumber , "f" ) + 1 ; b <= blockNumber ; b ++ ) {
_ _classPrivateFieldGet ( this , _PollingBlockSubscriber _provider , "f" ) . emit ( "block" , b ) ;
}
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _blockNumber , blockNumber , "f" ) ;
}
2022-04-14 04:51:35 -04:00
_ _classPrivateFieldSet ( this , _PollingBlockSubscriber _poller , _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _provider , "f" ) . _setTimeout ( _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _instances , "m" , _PollingBlockSubscriber _poll ) . bind ( this ) , _ _classPrivateFieldGet ( this , _PollingBlockSubscriber _interval , "f" ) ) , "f" ) ;
2022-04-11 17:14:19 -04:00
} ;
export class OnBlockSubscriber {
constructor ( provider ) {
_OnBlockSubscriber _provider . set ( this , void 0 ) ;
_OnBlockSubscriber _poll . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _OnBlockSubscriber _provider , provider , "f" ) ;
_ _classPrivateFieldSet ( this , _OnBlockSubscriber _poll , ( blockNumber ) => {
this . _poll ( blockNumber , _ _classPrivateFieldGet ( this , _OnBlockSubscriber _provider , "f" ) ) ;
} , "f" ) ;
}
async _poll ( blockNumber , provider ) {
throw new Error ( "sub-classes must override this" ) ;
}
start ( ) {
_ _classPrivateFieldGet ( this , _OnBlockSubscriber _poll , "f" ) . call ( this , - 2 ) ;
_ _classPrivateFieldGet ( this , _OnBlockSubscriber _provider , "f" ) . on ( "block" , _ _classPrivateFieldGet ( this , _OnBlockSubscriber _poll , "f" ) ) ;
}
stop ( ) {
_ _classPrivateFieldGet ( this , _OnBlockSubscriber _provider , "f" ) . off ( "block" , _ _classPrivateFieldGet ( this , _OnBlockSubscriber _poll , "f" ) ) ;
}
pause ( dropWhilePaused ) { this . stop ( ) ; }
resume ( ) { this . start ( ) ; }
}
_OnBlockSubscriber _provider = new WeakMap ( ) , _OnBlockSubscriber _poll = new WeakMap ( ) ;
export class PollingOrphanSubscriber extends OnBlockSubscriber {
constructor ( provider , filter ) {
super ( provider ) ;
_PollingOrphanSubscriber _filter . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _PollingOrphanSubscriber _filter , copy ( filter ) , "f" ) ;
}
async _poll ( blockNumber , provider ) {
throw new Error ( "@TODO" ) ;
2022-04-14 04:51:35 -04:00
console . log ( _ _classPrivateFieldGet ( this , _PollingOrphanSubscriber _filter , "f" ) ) ;
2022-04-11 17:14:19 -04:00
}
}
_PollingOrphanSubscriber _filter = new WeakMap ( ) ;
export class PollingTransactionSubscriber extends OnBlockSubscriber {
constructor ( provider , hash ) {
super ( provider ) ;
_PollingTransactionSubscriber _hash . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _PollingTransactionSubscriber _hash , hash , "f" ) ;
}
async _poll ( blockNumber , provider ) {
const tx = await provider . getTransactionReceipt ( _ _classPrivateFieldGet ( this , _PollingTransactionSubscriber _hash , "f" ) ) ;
if ( tx ) {
provider . emit ( _ _classPrivateFieldGet ( this , _PollingTransactionSubscriber _hash , "f" ) , tx ) ;
}
}
}
_PollingTransactionSubscriber _hash = new WeakMap ( ) ;
export class PollingEventSubscriber {
constructor ( provider , filter ) {
_PollingEventSubscriber _instances . add ( this ) ;
_PollingEventSubscriber _provider . set ( this , void 0 ) ;
_PollingEventSubscriber _filter . set ( this , void 0 ) ;
_PollingEventSubscriber _poller . set ( this , void 0 ) ;
// The most recent block we have scanned for events. The value -2
// indicates we still need to fetch an initial block number
_PollingEventSubscriber _blockNumber . set ( this , void 0 ) ;
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _provider , provider , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _filter , copy ( filter ) , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _poller , _ _classPrivateFieldGet ( this , _PollingEventSubscriber _instances , "m" , _PollingEventSubscriber _poll ) . bind ( this ) , "f" ) ;
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _blockNumber , - 2 , "f" ) ;
}
start ( ) {
if ( _ _classPrivateFieldGet ( this , _PollingEventSubscriber _blockNumber , "f" ) === - 2 ) {
_ _classPrivateFieldGet ( this , _PollingEventSubscriber _provider , "f" ) . getBlockNumber ( ) . then ( ( blockNumber ) => {
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _blockNumber , blockNumber , "f" ) ;
} ) ;
}
_ _classPrivateFieldGet ( this , _PollingEventSubscriber _provider , "f" ) . on ( "block" , _ _classPrivateFieldGet ( this , _PollingEventSubscriber _poller , "f" ) ) ;
}
stop ( ) {
_ _classPrivateFieldGet ( this , _PollingEventSubscriber _provider , "f" ) . off ( "block" , _ _classPrivateFieldGet ( this , _PollingEventSubscriber _poller , "f" ) ) ;
}
pause ( dropWhilePaused ) {
this . stop ( ) ;
if ( dropWhilePaused ) {
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _blockNumber , - 2 , "f" ) ;
}
}
resume ( ) {
this . start ( ) ;
}
}
_PollingEventSubscriber _provider = new WeakMap ( ) , _PollingEventSubscriber _filter = new WeakMap ( ) , _PollingEventSubscriber _poller = new WeakMap ( ) , _PollingEventSubscriber _blockNumber = new WeakMap ( ) , _PollingEventSubscriber _instances = new WeakSet ( ) , _PollingEventSubscriber _poll = async function _PollingEventSubscriber _poll ( blockNumber ) {
// The initial block hasn't been determined yet
if ( _ _classPrivateFieldGet ( this , _PollingEventSubscriber _blockNumber , "f" ) === - 2 ) {
return ;
}
const filter = copy ( _ _classPrivateFieldGet ( this , _PollingEventSubscriber _filter , "f" ) ) ;
filter . fromBlock = _ _classPrivateFieldGet ( this , _PollingEventSubscriber _blockNumber , "f" ) + 1 ;
filter . toBlock = blockNumber ;
const logs = await _ _classPrivateFieldGet ( this , _PollingEventSubscriber _provider , "f" ) . getLogs ( filter ) ;
2022-04-14 04:51:35 -04:00
// No logs could just mean the node has not indexed them yet,
// so we keep a sliding window of 60 blocks to keep scanning
if ( logs . length === 0 ) {
if ( _ _classPrivateFieldGet ( this , _PollingEventSubscriber _blockNumber , "f" ) < blockNumber - 60 ) {
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _blockNumber , blockNumber - 60 , "f" ) ;
}
return ;
}
_ _classPrivateFieldSet ( this , _PollingEventSubscriber _blockNumber , blockNumber , "f" ) ;
2022-04-11 17:14:19 -04:00
for ( const log of logs ) {
_ _classPrivateFieldGet ( this , _PollingEventSubscriber _provider , "f" ) . emit ( _ _classPrivateFieldGet ( this , _PollingEventSubscriber _filter , "f" ) , log ) ;
}
} ;
//# sourceMappingURL=subscriber-polling.js.map