src/blockchain/watcher.js
/**
   * Ensures that no donations occur after the last event.
   *
   * If we reprocess events w/o clearing the donations, this will cause
   * issues with how we calculate which donation to transfer, etc.
   */
Ensures that no donations occur after the last event.
If we reprocess events w/o clearing the donations, this will cause
issues with how we calculate which donation to transfer, etc.
Ensures that no donations occur after the last event . If we reprocess events w / o clearing the donations this will cause issues with how we calculate which donation to transfer etc .
async function checkDonations() {
    const lastEvent = await eventService.find({
      paginate: false,
      query: { $limit: 1, $sort: { blockNumber: -1 } },
    });
    const lastDonation = await app.service('donations').find({
      paginate: false,
      query: {
        $limit: 1,
        mined: true,
        status: { $nin: [DonationStatus.PENDING, DonationStatus.FAILED] },
        $sort: { createdAt: -1 },
      },
    });
    if (lastDonation.length > 0) {
      const receipt = await web3.eth.getTransactionReceipt(lastDonation[0].txHash);
      if (receipt.blockNumber > lastEvent.blockNumber) {
        logger.error(
          `It appears that you are attempting to reprocess events, or the events table has
          been altered and there are donations. In order to correctly sync/re-sync, the
          'donations' and 'events' tables must both be cleared, otherwise the donations
          will not be an accurate representation of the blockchain txs`,
        );
        process.exit(1);
      }
    }
  }
async function checkDonations ( ) { const lastEvent = await eventService . find ( { paginate : false , query : { $limit : 1 , $sort : { blockNumber : - 1 } } , } ) ; const lastDonation = await app . service ( 'donations' ) . find ( { paginate : false , query : { $limit : 1 , mined : true , status : { $nin : [ DonationStatus . PENDING , DonationStatus . FAILED ] } , $sort : { createdAt : - 1 } , } , } ) ; if ( lastDonation . length > 0 ) { const receipt = await web3 . eth . getTransactionReceipt ( lastDonation [ 0 ] . txHash ) ; if ( receipt . blockNumber > lastEvent . blockNumber ) { logger . error ( ` ` , ) ; process . exit ( 1 ) ; } } }
Ensures that no donations occur after the last event.
Ensures that no donations occur after the last event .