Advertisement
Brord

Untitled

Oct 12th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const promoteTransaction = (bundleHash, accountName, powFn) => (dispatch, getState) => {
  2.     dispatch(promoteTransactionRequest(bundleHash));
  3.  
  4.     const remotePoW = getRemotePoWFromState(getState());
  5.  
  6.     if (!remotePoW) {
  7.         dispatch(
  8.             generateAlert(
  9.                 'info',
  10.                 i18next.t('global:promotingTransaction'),
  11.                 i18next.t('global:deviceMayBecomeUnresponsive'),
  12.             ),
  13.         );
  14.     }
  15.  
  16.     let accountState = {};
  17.  
  18.     try {
  19.         accountState = await syncAccount()(selectedAccountStateFactory(accountName)(getState()));
  20.         dispatch(syncAccountBeforeManualPromotion(accountState));
  21.  
  22.         const transaction = accountState.transfers[bundleHash];
  23.  
  24.         if (transaction.persistence) {
  25.             throw new Error(Errors.TRANSACTION_ALREADY_CONFIRMED);
  26.         }
  27.  
  28.         let isValid = await isStillAValidTransaction()(transaction, accountState.addresses);
  29.         if (!isValid) {
  30.             throw new Error(Errors.BUNDLE_NO_LONGER_VALID);
  31.         }
  32.  
  33.         const tailTransactions = accountState.transfers[bundleHash].tailTransactions;
  34.  
  35.         let consistentTail = await findPromotableTail()(tailTransactions, 0);
  36.         let hash = await dispatch(
  37.             forceTransactionPromotion(
  38.                 accountName,
  39.                 consistentTail,
  40.                 accountState.transfers[bundleHash].tailTransactions,
  41.                 true,
  42.                 // If proof of work configuration is set to remote, pass proof of work function as null
  43.                 remotePoW ? null : powFn,
  44.             ),
  45.         );
  46.  
  47.         dispatch(
  48.             generateAlert(
  49.                 'success',
  50.                 i18next.t('global:promoted'),
  51.                 i18next.t('global:promotedExplanation', { hash }),
  52.             ),
  53.         );
  54.  
  55.         return dispatch(promoteTransactionSuccess());
  56.     } catch(err) {
  57.         if (err.message === Errors.BUNDLE_NO_LONGER_VALID) {
  58.             dispatch(generateAlert('error', i18next.t('global:promotionError'), i18next.t('global:noLongerValid')));
  59.         } else if (err.message.includes(Errors.ATTACH_TO_TANGLE_UNAVAILABLE)) {
  60.             dispatch(
  61.                 generateAlert(
  62.                     'error',
  63.                     i18next.t('global:attachToTangleUnavailable'),
  64.                     i18next.t('global:attachToTangleUnavailableExplanationShort'),
  65.                     10000,
  66.                 ),
  67.             );
  68.         } else if (err.message === Errors.TRANSACTION_ALREADY_CONFIRMED) {
  69.             dispatch(
  70.                 generateAlert(
  71.                     'success',
  72.                     i18next.t('global:transactionAlreadyConfirmed'),
  73.                     i18next.t('global:transactionAlreadyConfirmedExplanation'),
  74.                 ),
  75.             );
  76.         } else {
  77.             dispatch(generatePromotionErrorAlert(err));
  78.         }
  79.  
  80.         return dispatch(promoteTransactionError());
  81.     }
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement