Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v11.16.14 #25490

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [11.16.14]
### Fixed
- Fix bug preventing connection of Trezor hardware wallets on firefox ([#25487](https://github.com/MetaMask/metamask-extension/pull/25487))

## [11.16.13]
### Changed
- Update Blockaid feature for latest security protections ([#25442](https://github.com/MetaMask/metamask-extension/pull/25442))
Expand Down Expand Up @@ -4818,7 +4822,8 @@ Update styles and spacing on the critical error page ([#20350](https://github.c
- Added the ability to restore accounts from seed words.


[Unreleased]: https://github.com/MetaMask/metamask-extension/compare/v11.16.13...HEAD
[Unreleased]: https://github.com/MetaMask/metamask-extension/compare/v11.16.14...HEAD
[11.16.14]: https://github.com/MetaMask/metamask-extension/compare/v11.16.13...v11.16.14
[11.16.13]: https://github.com/MetaMask/metamask-extension/compare/v11.16.12...v11.16.13
[11.16.12]: https://github.com/MetaMask/metamask-extension/compare/v11.16.11...v11.16.12
[11.16.11]: https://github.com/MetaMask/metamask-extension/compare/v11.16.10...v11.16.11
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metamask-crx",
"version": "11.16.13",
"version": "11.16.14",
"private": true,
"repository": {
"type": "git",
Expand Down
36 changes: 36 additions & 0 deletions shared/lib/swaps-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { addHexPrefix } from '../../app/scripts/lib/util';
import { decimalToHex } from '../modules/conversion.utils';
import fetchWithCache from './fetch-with-cache';

const FALLBACK_GAS_MULTIPLIER = 1.5;

const TEST_CHAIN_IDS = [CHAIN_IDS.GOERLI, CHAIN_IDS.LOCALHOST];

const clientIdHeader = { 'X-Client-Id': SWAPS_CLIENT_ID };
Expand Down Expand Up @@ -317,3 +319,37 @@ export async function fetchTradesInfo(

return newQuotes;
}

/**
* Given a gas estimate, gas multiplier, max gas, and custom max gas, returns the max gas limit
* to use for a transaction.
*
* @param {string} gasEstimate - The gas estimate for the transaction.
* @param {number} gasMultiplier - The gas multiplier to use.
* @param {number} maxGas - The max gas limit to use.
* @param {string} customMaxGas - The custom max gas limit to use.
* @returns {string} The max gas limit to use for the transaction.
*/

export function calculateMaxGasLimit(
gasEstimate,
gasMultiplier = FALLBACK_GAS_MULTIPLIER,
maxGas,
customMaxGas,
) {
const gasLimitForMax = new BigNumber(gasEstimate || 0, 16)
.round(0)
.toString(16);

const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16)
.times(gasMultiplier, 10)
.round(0)
.toString(16);

const nonCustomMaxGasLimit = gasEstimate
? usedGasLimitWithMultiplier
: `0x${decimalToHex(maxGas || 0)}`;
const maxGasLimit = customMaxGas || nonCustomMaxGasLimit;

return maxGasLimit;
}
48 changes: 47 additions & 1 deletion shared/lib/swaps-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
TOKENS,
MOCK_TRADE_RESPONSE_2,
} from '../../ui/pages/swaps/swaps-util-test-constants';
import { fetchTradesInfo, shouldEnableDirectWrapping } from './swaps-utils';
import {
fetchTradesInfo,
shouldEnableDirectWrapping,
calculateMaxGasLimit,
} from './swaps-utils';

jest.mock('./storage-helpers', () => ({
getStorageItem: jest.fn(),
Expand Down Expand Up @@ -224,4 +228,46 @@ describe('Swaps Utils', () => {
expect(shouldEnableDirectWrapping(CHAIN_IDS.MAINNET)).toBe(false);
});
});

describe('calculateMaxGasLimit', () => {
const gasEstimate = '0x37b15';
const maxGas = 273740;
let expectedMaxGas = '42d4c';
let gasMultiplier = 1.2;
let customMaxGas = '';

it('should return the max gas limit', () => {
const result = calculateMaxGasLimit(
gasEstimate,
gasMultiplier,
maxGas,
customMaxGas,
);
expect(result).toStrictEqual(expectedMaxGas);
});

it('should return the custom max gas limit', () => {
customMaxGas = '46d4c';
const result = calculateMaxGasLimit(
gasEstimate,
gasMultiplier,
maxGas,
customMaxGas,
);
expect(result).toStrictEqual(customMaxGas);
});

it('should return the max gas limit with a gas multiplier of 4.5', () => {
gasMultiplier = 4.5;
expectedMaxGas = 'fa9df';
customMaxGas = '';
const result = calculateMaxGasLimit(
gasEstimate,
gasMultiplier,
maxGas,
customMaxGas,
);
expect(result).toStrictEqual(expectedMaxGas);
});
});
});
3 changes: 3 additions & 0 deletions shared/lib/ui-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ export const SECURITY_ALERTS_LEARN_MORE_LINK =

export const TRANSACTION_SIMULATIONS_LEARN_MORE_LINK =
'https://support.metamask.io/transactions-and-gas/transactions/simulations/';

export const GAS_FEES_LEARN_MORE_URL =
'https://community.metamask.io/t/what-is-gas-why-do-transactions-take-so-long/3172';
27 changes: 11 additions & 16 deletions ui/ducks/swaps/swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import {
} from '../../../shared/lib/transactions-controller-utils';
import { EtherDenomination } from '../../../shared/constants/common';
import { Numeric } from '../../../shared/modules/Numeric';
import { calculateMaxGasLimit } from '../../../shared/lib/swaps-utils';

export const GAS_PRICES_LOADING_STATES = {
INITIAL: 'INITIAL',
Expand All @@ -102,8 +103,6 @@ export const GAS_PRICES_LOADING_STATES = {
COMPLETED: 'COMPLETED',
};

export const FALLBACK_GAS_MULTIPLIER = 1.5;

const initialState = {
aggregatorMetadata: null,
approveTxId: null,
Expand Down Expand Up @@ -1106,20 +1105,16 @@ export const signAndSendTransactions = (
const usedQuote = getUsedQuote(state);
const usedTradeTxParams = usedQuote.trade;

const estimatedGasLimit = new BigNumber(
usedQuote?.gasEstimate || 0,
16,
).toString(16);

const maxGasLimit =
customSwapsGas ||
(usedQuote?.gasEstimate
? `0x${estimatedGasLimit}`
: `0x${decimalToHex(
new BigNumber(usedQuote?.maxGas)
.mul(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER)
.toString() || 0,
)}`);
const estimatedGasLimit = new BigNumber(usedQuote?.gasEstimate || 0, 16)
.round(0)
.toString(16);

const maxGasLimit = calculateMaxGasLimit(
usedQuote?.gasEstimate,
usedQuote?.gasMultiplier,
usedQuote?.maxGas,
customSwapsGas,
);

const usedGasPrice = getUsedSwapsGasPrice(state);
usedTradeTxParams.gas = maxGasLimit;
Expand Down
30 changes: 11 additions & 19 deletions ui/pages/swaps/prepare-swap-page/review-quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { usePrevious } from '../../../hooks/usePrevious';
import { useGasFeeInputs } from '../../confirmations/hooks/useGasFeeInputs';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
FALLBACK_GAS_MULTIPLIER,
getQuotes,
getSelectedQuote,
getApproveTxParams,
Expand Down Expand Up @@ -135,7 +134,11 @@ import {
toPrecisionWithoutTrailingZeros,
} from '../../../../shared/lib/transactions-controller-utils';
import { addHexPrefix } from '../../../../app/scripts/lib/util';
import { calcTokenValue } from '../../../../shared/lib/swaps-utils';
import {
calcTokenValue,
calculateMaxGasLimit,
} from '../../../../shared/lib/swaps-utils';
import { GAS_FEES_LEARN_MORE_URL } from '../../../../shared/lib/ui-utils';
import ExchangeRateDisplay from '../exchange-rate-display';
import InfoTooltip from '../../../components/ui/info-tooltip';
import useRamps from '../../../hooks/experiences/useRamps';
Expand All @@ -144,9 +147,6 @@ import SlippageNotificationModal from './slippage-notification-modal';

let intervalId;

const GAS_FEES_LEARN_MORE_URL =
'https://community.metamask.io/t/what-is-gas-why-do-transactions-take-so-long/3172';

export default function ReviewQuote({ setReceiveToAmount }) {
const history = useHistory();
const dispatch = useDispatch();
Expand Down Expand Up @@ -297,20 +297,12 @@ export default function ReviewQuote({ setReceiveToAmount }) {
usedQuote?.gasEstimateWithRefund ||
`0x${decimalToHex(usedQuote?.averageGas || 0)}`;

const estimatedGasLimit = new BigNumber(
usedQuote?.gasEstimate || 0,
16,
).toString(16);

const nonCustomMaxGasLimit = usedQuote?.gasEstimate
? `0x${estimatedGasLimit}`
: `0x${decimalToHex(
new BigNumber(usedQuote?.maxGas)
.mul(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER)
.toString() || 0,
)}`;

const maxGasLimit = customMaxGas || nonCustomMaxGasLimit;
const maxGasLimit = calculateMaxGasLimit(
usedQuote?.gasEstimate,
usedQuote?.gasMultiplier,
usedQuote?.maxGas,
customMaxGas,
);

let maxFeePerGas;
let maxPriorityFeePerGas;
Expand Down
23 changes: 10 additions & 13 deletions ui/pages/swaps/view-quote/view-quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { useGasFeeInputs } from '../../confirmations/hooks/useGasFeeInputs';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import FeeCard from '../fee-card';
import {
FALLBACK_GAS_MULTIPLIER,
getQuotes,
getSelectedQuote,
getApproveTxParams,
Expand Down Expand Up @@ -109,7 +108,10 @@ import {
toPrecisionWithoutTrailingZeros,
} from '../../../../shared/lib/transactions-controller-utils';
import { addHexPrefix } from '../../../../app/scripts/lib/util';
import { calcTokenValue } from '../../../../shared/lib/swaps-utils';
import {
calcTokenValue,
calculateMaxGasLimit,
} from '../../../../shared/lib/swaps-utils';
import {
addHexes,
decGWEIToHexWEI,
Expand Down Expand Up @@ -230,17 +232,12 @@ export default function ViewQuote() {
usedQuote?.gasEstimateWithRefund ||
`0x${decimalToHex(usedQuote?.averageGas || 0)}`;

const gasLimitForMax = usedQuote?.gasEstimate || `0x0`;

const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16)
.times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10)
.round(0)
.toString(16);

const nonCustomMaxGasLimit = usedQuote?.gasEstimate
? usedGasLimitWithMultiplier
: `0x${decimalToHex(usedQuote?.maxGas || 0)}`;
const maxGasLimit = customMaxGas || nonCustomMaxGasLimit;
const maxGasLimit = calculateMaxGasLimit(
usedQuote?.gasEstimate,
usedQuote?.gasMultiplier,
usedQuote?.maxGas,
customMaxGas,
);

let maxFeePerGas;
let maxPriorityFeePerGas;
Expand Down
Loading