Skip to content

Commit

Permalink
chore: apply changes from #9565
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed May 15, 2024
1 parent 6a3d8e5 commit fe2ef4c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 18 deletions.
68 changes: 68 additions & 0 deletions app/components/Views/SmartTransactionStatus/useRemainingTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { selectSwapsChainFeatureFlags } from '../../../reducers/swaps';

export const FALLBACK_STX_ESTIMATED_DEADLINE_SEC = 45;
export const FALLBACK_STX_MAX_DEADLINE_SEC = 150;

interface Props {
creationTime: number | undefined;
isStxPending: boolean;
}

const useRemainingTime = ({ creationTime, isStxPending }: Props) => {
const swapFeatureFlags = useSelector(selectSwapsChainFeatureFlags);

const [isStxPastEstimatedDeadline, setIsStxPastEstimatedDeadline] =
useState(false);

const stxEstimatedDeadlineSec =
swapFeatureFlags?.smartTransactions?.expectedDeadline ||
FALLBACK_STX_ESTIMATED_DEADLINE_SEC;
const stxMaxDeadlineSec =
swapFeatureFlags?.smartTransactions?.maxDeadline ||
FALLBACK_STX_MAX_DEADLINE_SEC;

// Calc time left for progress bar and timer display
const stxDeadlineSec = isStxPastEstimatedDeadline
? stxMaxDeadlineSec
: stxEstimatedDeadlineSec;

const [timeLeftForPendingStxInSec, setTimeLeftForPendingStxInSec] = useState(
stxEstimatedDeadlineSec,
);

useEffect(() => {
let intervalId: NodeJS.Timeout;
if (isStxPending && creationTime) {
const calculateRemainingTime = () => {
const secondsAfterStxSubmission = Math.round(
(Date.now() - creationTime) / 1000,
);
if (secondsAfterStxSubmission > stxDeadlineSec) {
if (isStxPastEstimatedDeadline) {
setTimeLeftForPendingStxInSec(0);
clearInterval(intervalId);
return;
}
setIsStxPastEstimatedDeadline(true);
}
setTimeLeftForPendingStxInSec(
stxDeadlineSec - secondsAfterStxSubmission,
);
};
intervalId = setInterval(calculateRemainingTime, 1000);
calculateRemainingTime();
}

return () => clearInterval(intervalId);
}, [isStxPending, isStxPastEstimatedDeadline, creationTime, stxDeadlineSec]);

return {
timeLeftForPendingStxInSec,
stxDeadlineSec,
isStxPastEstimatedDeadline,
};
};

export default useRemainingTime;
3 changes: 2 additions & 1 deletion app/core/AppConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export default {
PRIVACY_POLICY_2024: 'https://consensys.io/privacy-policy',
PRIVACY_BEST_PRACTICES:
'https://support.metamask.io/privacy-and-security/privacy-best-practices',
SMART_TXS: 'https://support.metamask.io/hc/en-us/articles/9184393821211',
SMART_TXS:
'https://support.metamask.io/transactions-and-gas/transactions/smart-transactions/',
},
ERRORS: {
INFURA_BLOCKED_MESSAGE:
Expand Down
7 changes: 6 additions & 1 deletion app/selectors/transactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const selectTransactionsStrict = createSelector(
(transactionControllerState) => transactionControllerState.transactions,
);

// eslint-disable-next-line import/prefer-default-export
export const selectTransactions = createDeepEqualSelector(
selectTransactionsStrict,
(transactions) => transactions,
);

export const selectNonReplacedTransactions = createDeepEqualSelector(
selectTransactionsStrict,
(transactions) =>
transactions.filter((tx) => !(tx.replacedBy && tx.replacedById && tx.hash)),
);
15 changes: 7 additions & 8 deletions locales/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2556,15 +2556,14 @@
"action_text": "Try it out"
},
"stx": {
"header": "Introducing Smart Transactions",
"description_1": "Unlock the safest, most reliable, and easiest transaction experience - a smarter way to navigate web3.",
"description_2": "Millions of dollars are lost every month due to failed transactions & frontrunning. Smart transactions fixes this.",
"description_3": "Right now, Smart Transactions are only available on ETH Mainnet. You can turn them off at any time in settings.",
"secondary_button": "Not right now",
"primary_button": "Enable Smart Transactions",
"header": "Transactions just got smarter",
"description_1": "Unlock higher success rates, frontrunning protection, and better visibility with Smart Transactions.",
"description_2": "Only available on Ethereum. Enable or disable any time in settings.",
"secondary_button": "No thanks",
"primary_button": "Enable",
"learn_more": "Learn more.",
"benefit_1_1": "82% fewer failed",
"benefit_1_2": "transactions",
"benefit_1_1": "99.5% success",
"benefit_1_2": "rate",
"benefit_2_1": "Transaction",
"benefit_2_2": "protection",
"benefit_3_1": "Real-time",
Expand Down
66 changes: 58 additions & 8 deletions patches/@metamask+preferences-controller+8.0.0.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/node_modules/@metamask/preferences-controller/dist/PreferencesController.d.ts b/node_modules/@metamask/preferences-controller/dist/PreferencesController.d.ts
index bfbfe66..c19399e 100644
index bfbfe66..e9c8232 100644
--- a/node_modules/@metamask/preferences-controller/dist/PreferencesController.d.ts
+++ b/node_modules/@metamask/preferences-controller/dist/PreferencesController.d.ts
@@ -71,7 +71,8 @@ export declare type PreferencesState = {
Expand All @@ -12,7 +12,18 @@ index bfbfe66..c19399e 100644
/**
* Controls whether "security alerts" are enabled
*/
@@ -216,11 +217,16 @@ export declare class PreferencesController extends BaseController<typeof name, P
@@ -98,6 +99,10 @@ export declare type PreferencesState = {
* Controls whether token detection is enabled
*/
useTokenDetection: boolean;
+ /**
+ * Controls whether smart transactions are opted into
+ */
+ smartTransactionsOptInStatus: boolean;
};
declare const name = "PreferencesController";
export declare type PreferencesControllerGetStateAction = ControllerGetStateAction<typeof name, PreferencesState>;
@@ -216,11 +221,16 @@ export declare class PreferencesController extends BaseController<typeof name, P
*/
setUseNftDetection(useNftDetection: boolean): void;
/**
Expand All @@ -32,11 +43,25 @@ index bfbfe66..c19399e 100644
/**
* Toggle the security alert enabled setting.
*
@@ -259,6 +269,12 @@ export declare class PreferencesController extends BaseController<typeof name, P
* @param isIncomingTransactionNetworkEnable - true to enable incoming transactions
*/
setEnableNetworkIncomingTransactions(chainId: EtherscanSupportedHexChainId, isIncomingTransactionNetworkEnable: boolean): void;
+ /**
+ * A setter for the user to opt into smart transactions
+ *
+ * @param smartTransactionsOptInStatus - true to opt into smart transactions
+ */
+ setSmartTransactionsOptInStatus(smartTransactionsOptInStatus: boolean): void;
}
export default PreferencesController;
//# sourceMappingURL=PreferencesController.d.ts.map
\ No newline at end of file
diff --git a/node_modules/@metamask/preferences-controller/dist/PreferencesController.js b/node_modules/@metamask/preferences-controller/dist/PreferencesController.js
index 228cd90..dac2587 100644
index 228cd90..ac62b59 100644
--- a/node_modules/@metamask/preferences-controller/dist/PreferencesController.js
+++ b/node_modules/@metamask/preferences-controller/dist/PreferencesController.js
@@ -18,7 +18,8 @@ const metadata = {
@@ -18,13 +18,15 @@ const metadata = {
isIpfsGatewayEnabled: { persist: true, anonymous: true },
isMultiAccountBalancesEnabled: { persist: true, anonymous: true },
lostIdentities: { persist: true, anonymous: false },
Expand All @@ -46,7 +71,14 @@ index 228cd90..dac2587 100644
securityAlertsEnabled: { persist: true, anonymous: true },
selectedAddress: { persist: true, anonymous: false },
showTestNetworks: { persist: true, anonymous: true },
@@ -43,7 +44,7 @@ function getDefaultPreferencesState() {
showIncomingTransactions: { persist: true, anonymous: true },
useNftDetection: { persist: true, anonymous: true },
useTokenDetection: { persist: true, anonymous: true },
+ smartTransactionsOptInStatus: { persist: true, anonymous: true },
};
const name = 'PreferencesController';
/**
@@ -43,7 +45,7 @@ function getDefaultPreferencesState() {
isIpfsGatewayEnabled: true,
isMultiAccountBalancesEnabled: true,
lostIdentities: {},
Expand All @@ -55,23 +87,24 @@ index 228cd90..dac2587 100644
securityAlertsEnabled: false,
selectedAddress: '',
showIncomingTransactions: {
@@ -61,6 +62,7 @@ function getDefaultPreferencesState() {
@@ -61,6 +63,7 @@ function getDefaultPreferencesState() {
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.FANTOM_TESTNET]: true,
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.SEPOLIA]: true,
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_GOERLI]: true,
+ [constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_SEPOLIA]: true,
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.LINEA_MAINNET]: true,
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM]: true,
[constants_1.ETHERSCAN_SUPPORTED_CHAIN_IDS.MOONBEAM_TESTNET]: true,
@@ -70,6 +72,7 @@ function getDefaultPreferencesState() {
@@ -70,6 +73,8 @@ function getDefaultPreferencesState() {
showTestNetworks: false,
useNftDetection: false,
useTokenDetection: true,
+ useSafeChainsListValidation: true,
+ smartTransactionsOptInStatus: false,
};
}
exports.getDefaultPreferencesState = getDefaultPreferencesState;
@@ -205,26 +208,41 @@ class PreferencesController extends base_controller_1.BaseController {
@@ -205,26 +210,41 @@ class PreferencesController extends base_controller_1.BaseController {
* @param useNftDetection - Boolean indicating user preference on NFT detection.
*/
setUseNftDetection(useNftDetection) {
Expand Down Expand Up @@ -120,6 +153,23 @@ index 228cd90..dac2587 100644
/**
* Toggle the security alert enabled setting.
*
@@ -291,6 +311,16 @@ class PreferencesController extends base_controller_1.BaseController {
});
}
}
+ /**
+ * A setter for the user to opt into smart transactions
+ *
+ * @param smartTransactionsOptInStatus - true to opt into smart transactions
+ */
+ setSmartTransactionsOptInStatus(smartTransactionsOptInStatus) {
+ this.update((state) => {
+ state.smartTransactionsOptInStatus = smartTransactionsOptInStatus;
+ });
+ }
}
exports.PreferencesController = PreferencesController;
_PreferencesController_instances = new WeakSet(), _PreferencesController_syncIdentities = function _PreferencesController_syncIdentities(addresses) {
diff --git a/node_modules/@metamask/preferences-controller/dist/constants.d.ts b/node_modules/@metamask/preferences-controller/dist/constants.d.ts
index cb9a3d4..5662d1c 100644
--- a/node_modules/@metamask/preferences-controller/dist/constants.d.ts
Expand Down

0 comments on commit fe2ef4c

Please sign in to comment.