src/blockchain/pledges.js
// fetches all necessary data to determine what happened for this Transfer event
fetches all necessary data to determine what happened for this Transfer event
fetches all necessary data to determine what happened for this Transfer event
async function transfer(from, to, amount, ts, txHash) {
    try {
      const [fromPledge, toPledge, receipt] = await executeRequestsAsBatch(web3, [
        liquidPledging.$contract.methods.getPledge(from).call.request,
        liquidPledging.$contract.methods.getPledge(to).call.request,
        web3.eth.getTransactionReceipt.request.bind(null, txHash),
      ]);
      const fromPledgeAdmin = await getPledgeAdmin(fromPledge.owner);
      const initialTransfer = isInitialTransfer(from, amount, receipt);
      const promises = [
        getPledgeAdmin(toPledge.owner),
        getDonations(from, amount, initialTransfer, txHash),
      ];
      if (toPledge.nDelegates > 0) {
        promises.push(
          liquidPledging
            .getPledgeDelegate(to, toPledge.nDelegates)
            .then(delegate => getPledgeAdmin(delegate.idDelegate)),
        );
      } else {
        promises.push(undefined);
      }
      if (Number(toPledge.intendedProject) > 0) {
        promises.push(getPledgeAdmin(toPledge.intendedProject));
      } else {
        promises.push(undefined);
      }
      const [toPledgeAdmin, donations, delegate, intendedProject] = await Promise.all(promises);
      const transferInfo = {
        fromPledgeAdmin,
        toPledgeAdmin,
        fromPledge,
        toPledge,
        toPledgeId: to,
        initialTransfer,
        delegate,
        intendedProject,
        donations,
        amount,
        ts,
        txHash,
      };
      if (
        (fromPledgeAdmin.type === AdminTypes.TRACE &&
          fromPledgeAdmin.admin.status === TraceStatus.CANCELED) ||
        (fromPledgeAdmin.type === AdminTypes.CAMPAIGN &&
          fromPledgeAdmin.admin.status === CampaignStatus.CANCELED)
      ) {
        transferInfo.isReturn = true;
      }
      if (donations.length === 0) {
        logTransferInfo(transferInfo);
        return;
      }
      const hasSimilar = await hasSimilarDonation(transferInfo);
      if (!hasSimilar) {
        await spendAndUpdateExistingDonations(transferInfo);
        await createToDonation(transferInfo);
      } else {
        logger.warn('Ignore repetitive transfer:', transferInfo);
      }
    } catch (err) {
      logger.error('Handling transfer event error:', err);
    }
  }
async function transfer ( from , to , amount , ts , txHash ) { try { const [ fromPledge , toPledge , receipt ] = await executeRequestsAsBatch ( web3 , [ liquidPledging . $contract . methods . getPledge ( from ) . call . request , liquidPledging . $contract . methods . getPledge ( to ) . call . request , web3 . eth . getTransactionReceipt . request . bind ( null , txHash ) , ] ) ; const fromPledgeAdmin = await getPledgeAdmin ( fromPledge . owner ) ; const initialTransfer = isInitialTransfer ( from , amount , receipt ) ; const promises = [ getPledgeAdmin ( toPledge . owner ) , getDonations ( from , amount , initialTransfer , txHash ) , ] ; if ( toPledge . nDelegates > 0 ) { promises . push ( liquidPledging . getPledgeDelegate ( to , toPledge . nDelegates ) . then ( delegate => getPledgeAdmin ( delegate . idDelegate ) ) , ) ; } else { promises . push ( undefined ) ; } if ( Number ( toPledge . intendedProject ) > 0 ) { promises . push ( getPledgeAdmin ( toPledge . intendedProject ) ) ; } else { promises . push ( undefined ) ; } const [ toPledgeAdmin , donations , delegate , intendedProject ] = await Promise . all ( promises ) ; const transferInfo = { fromPledgeAdmin , toPledgeAdmin , fromPledge , toPledge , toPledgeId : to , initialTransfer , delegate , intendedProject , donations , amount , ts , txHash , } ; if ( ( fromPledgeAdmin . type === AdminTypes . TRACE && fromPledgeAdmin . admin . status === TraceStatus . CANCELED ) || ( fromPledgeAdmin . type === AdminTypes . CAMPAIGN && fromPledgeAdmin . admin . status === CampaignStatus . CANCELED ) ) { transferInfo . isReturn = true ; } if ( donations . length === 0 ) { logTransferInfo ( transferInfo ) ; return ; } const hasSimilar = await hasSimilarDonation ( transferInfo ) ; if ( ! hasSimilar ) { await spendAndUpdateExistingDonations ( transferInfo ) ; await createToDonation ( transferInfo ) ; } else { logger . warn ( 'Ignore repetitive transfer:' , transferInfo ) ; } } catch ( err ) { logger . error ( 'Handling transfer event error:' , err ) ; } }
fetches all necessary data to determine what happened for this Transfer event
fetches all necessary data to determine what happened for this Transfer event
// In lp any delegate in the chain can delegate, but currently we only allow last delegate // to have that ability // fetch intendedProject pledgeAdmin // if from donation is missing, we can't do anything