Skip to content

Commit 0556088

Browse files
authored
fix: replace hardcoded slippage + expire bridge quotes (#29028)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Changes - Replaces hardcoded slippage with the slippage percentage included in the quote request - Disables tx submission 30s after final quote fetch <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29028?quickstart=1) ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/MMS-1771 ## **Manual testing steps** 1. Submit a tx 2. Request quotes with sufficient balance 3. 30 seconds after the last fetch, the CTA should be disabled ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [X] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 07d4265 commit 0556088

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

app/_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/pages/bridge/hooks/useSubmitBridgeTransaction.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { useDispatch } from 'react-redux';
1+
import { useDispatch, useSelector } from 'react-redux';
22
import { zeroAddress } from 'ethereumjs-util';
33
import { useHistory } from 'react-router-dom';
44
import { TransactionMeta } from '@metamask/transaction-controller';
55
import { QuoteMetadata, QuoteResponse } from '../types';
66
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
77
import { setDefaultHomeActiveTabName } from '../../../store/actions';
88
import { startPollingForBridgeTxStatus } from '../../../ducks/bridge-status/actions';
9+
import { getQuoteRequest } from '../../../ducks/bridge/selectors';
910
import useAddToken from './useAddToken';
1011
import useHandleApprovalTx from './useHandleApprovalTx';
1112
import useHandleBridgeTx from './useHandleBridgeTx';
@@ -16,6 +17,7 @@ export default function useSubmitBridgeTransaction() {
1617
const { addSourceToken, addDestToken } = useAddToken();
1718
const { handleApprovalTx } = useHandleApprovalTx();
1819
const { handleBridgeTx } = useHandleBridgeTx();
20+
const { slippage } = useSelector(getQuoteRequest);
1921

2022
const submitBridgeTransaction = async (
2123
quoteResponse: QuoteResponse & QuoteMetadata,
@@ -51,7 +53,7 @@ export default function useSubmitBridgeTransaction() {
5153
bridgeTxMeta,
5254
statusRequest,
5355
quoteResponse,
54-
slippagePercentage: 0, // TODO pull this from redux/bridgecontroller once it's implemented. currently hardcoded in quoteRequest.slippage right now
56+
slippagePercentage: slippage ?? 0,
5557
startTime: bridgeTxMeta.time,
5658
}),
5759
);

ui/pages/bridge/prepare/bridge-cta-button.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo, useState } from 'react';
22
import { useSelector } from 'react-redux';
33
import { Button } from '../../../components/component-library';
44
import {
@@ -8,6 +8,7 @@ import {
88
getToToken,
99
getBridgeQuotes,
1010
getValidationErrors,
11+
getBridgeQuotesConfig,
1112
} from '../../../ducks/bridge/selectors';
1213
import { useI18nContext } from '../../../hooks/useI18nContext';
1314
import useSubmitBridgeTransaction from '../hooks/useSubmitBridgeTransaction';
@@ -29,7 +30,9 @@ export const BridgeCTAButton = () => {
2930

3031
const fromAmount = useSelector(getFromAmount);
3132

32-
const { isLoading, activeQuote } = useSelector(getBridgeQuotes);
33+
const { isLoading, activeQuote, isQuoteGoingToRefresh, quotesRefreshCount } =
34+
useSelector(getBridgeQuotes);
35+
const { maxRefreshCount, refreshRate } = useSelector(getBridgeQuotesConfig);
3336

3437
const { submitBridgeTransaction } = useSubmitBridgeTransaction();
3538

@@ -44,7 +47,28 @@ export const BridgeCTAButton = () => {
4447
const requestMetadataProperties = useRequestMetadataProperties();
4548
const tradeProperties = useTradeProperties();
4649

50+
const [isQuoteExpired, setIsQuoteExpired] = useState(false);
51+
useEffect(() => {
52+
let timeout: NodeJS.Timeout;
53+
// Reset the isQuoteExpired if quote fethching restarts
54+
if (quotesRefreshCount === 0) {
55+
setIsQuoteExpired(false);
56+
return () => clearTimeout(timeout);
57+
}
58+
// After the last quote refresh, set a timeout to expire the quote and disable the CTA
59+
if (quotesRefreshCount >= maxRefreshCount && !isQuoteGoingToRefresh) {
60+
timeout = setTimeout(() => {
61+
setIsQuoteExpired(true);
62+
}, refreshRate);
63+
}
64+
return () => clearTimeout(timeout);
65+
}, [isQuoteGoingToRefresh, quotesRefreshCount]);
66+
4767
const label = useMemo(() => {
68+
if (isQuoteExpired) {
69+
return t('bridgeQuoteExpired');
70+
}
71+
4872
if (isLoading && !isTxSubmittable) {
4973
return t('swapFetchingQuotes');
5074
}
@@ -76,6 +100,7 @@ export const BridgeCTAButton = () => {
76100
isTxSubmittable,
77101
balanceAmount,
78102
isInsufficientBalance,
103+
isQuoteExpired,
79104
]);
80105

81106
return (
@@ -97,7 +122,7 @@ export const BridgeCTAButton = () => {
97122
submitBridgeTransaction(activeQuote);
98123
}
99124
}}
100-
disabled={!isTxSubmittable}
125+
disabled={!isTxSubmittable || isQuoteExpired}
101126
>
102127
{label}
103128
</Button>

0 commit comments

Comments
 (0)