1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Update quote selection for Optimism (#16998)

This commit is contained in:
Daniel 2022-12-21 20:59:34 +01:00 committed by GitHub
parent 3cfef11f71
commit 2832f8a97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 15 deletions

View File

@ -17,6 +17,7 @@ import {
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP, SWAPS_CHAINID_CONTRACT_ADDRESS_MAP,
} from '../../../shared/constants/swaps'; } from '../../../shared/constants/swaps';
import { GAS_ESTIMATE_TYPES } from '../../../shared/constants/gas'; import { GAS_ESTIMATE_TYPES } from '../../../shared/constants/gas';
import { CHAIN_IDS } from '../../../shared/constants/network';
import { import {
FALLBACK_SMART_TRANSACTIONS_REFRESH_TIME, FALLBACK_SMART_TRANSACTIONS_REFRESH_TIME,
FALLBACK_SMART_TRANSACTIONS_DEADLINE, FALLBACK_SMART_TRANSACTIONS_DEADLINE,
@ -24,6 +25,7 @@ import {
} from '../../../shared/constants/smartTransactions'; } from '../../../shared/constants/smartTransactions';
import { isSwapsDefaultTokenAddress } from '../../../shared/modules/swaps.utils'; import { isSwapsDefaultTokenAddress } from '../../../shared/modules/swaps.utils';
import { sumHexes } from '../../../ui/helpers/utils/transactions.util';
import { import {
fetchTradesInfo as defaultFetchTradesInfo, fetchTradesInfo as defaultFetchTradesInfo,
@ -36,6 +38,7 @@ import {
calcGasTotal, calcGasTotal,
calcTokenAmount, calcTokenAmount,
} from '../../../shared/lib/transactions-controller-utils'; } from '../../../shared/lib/transactions-controller-utils';
import fetchEstimatedL1Fee from '../../../ui/helpers/utils/optimism/fetchEstimatedL1Fee';
import { NETWORK_EVENTS } from './network'; import { NETWORK_EVENTS } from './network';
@ -291,6 +294,24 @@ export default class SwapsController {
destinationTokenInfo: fetchParamsMetaData.destinationTokenInfo, destinationTokenInfo: fetchParamsMetaData.destinationTokenInfo,
})); }));
if (chainId === CHAIN_IDS.OPTIMISM && Object.values(newQuotes).length > 0) {
await Promise.all(
Object.values(newQuotes).map(async (quote) => {
if (quote.trade) {
const multiLayerL1TradeFeeTotal = await fetchEstimatedL1Fee(
{
txParams: quote.trade,
chainId,
},
this.ethersProvider,
);
quote.multiLayerL1TradeFeeTotal = multiLayerL1TradeFeeTotal;
}
return quote;
}),
);
}
const quotesLastFetched = Date.now(); const quotesLastFetched = Date.now();
let approvalRequired = false; let approvalRequired = false;
@ -700,6 +721,7 @@ export default class SwapsController {
sourceToken, sourceToken,
trade, trade,
fee: metaMaskFee, fee: metaMaskFee,
multiLayerL1TradeFeeTotal,
} = quote; } = quote;
const tradeGasLimitForCalculation = gasEstimateWithRefund const tradeGasLimitForCalculation = gasEstimateWithRefund
@ -710,10 +732,16 @@ export default class SwapsController {
.plus(approvalNeeded?.gas || '0x0', 16) .plus(approvalNeeded?.gas || '0x0', 16)
.toString(16); .toString(16);
const gasTotalInWeiHex = calcGasTotal( let gasTotalInWeiHex = calcGasTotal(
totalGasLimitForCalculation, totalGasLimitForCalculation,
usedGasPrice, usedGasPrice,
); );
if (multiLayerL1TradeFeeTotal !== null) {
gasTotalInWeiHex = sumHexes(
gasTotalInWeiHex || '0x0',
multiLayerL1TradeFeeTotal || '0x0',
);
}
// trade.value is a sum of different values depending on the transaction. // trade.value is a sum of different values depending on the transaction.
// It always includes any external fees charged by the quote source. In // It always includes any external fees charged by the quote source. In

View File

@ -19,8 +19,10 @@ const OPTIMISM_GAS_PRICE_ORACLE_ABI = [
const OPTIMISM_GAS_PRICE_ORACLE_ADDRESS = const OPTIMISM_GAS_PRICE_ORACLE_ADDRESS =
'0x420000000000000000000000000000000000000F'; '0x420000000000000000000000000000000000000F';
export default async function fetchEstimatedL1Fee(txMeta) { export default async function fetchEstimatedL1Fee(txMeta, ethersProvider) {
const provider = new Web3Provider(global.ethereumProvider, 10); const provider = global.ethereumProvider
? new Web3Provider(global.ethereumProvider, 10)
: ethersProvider;
if (process.env.IN_TEST) { if (process.env.IN_TEST) {
provider.detectNetwork = async () => ({ provider.detectNetwork = async () => ({
name: 'optimism', name: 'optimism',

View File

@ -361,7 +361,7 @@ export function quotesToRenderableData(
chainId, chainId,
smartTransactionEstimatedGas, smartTransactionEstimatedGas,
nativeCurrencySymbol, nativeCurrencySymbol,
multiLayerL1FeeTotal, multiLayerL1ApprovalFeeTotal,
) { ) {
return Object.values(quotes).map((quote) => { return Object.values(quotes).map((quote) => {
const { const {
@ -376,7 +376,20 @@ export function quotesToRenderableData(
averageGas, averageGas,
fee, fee,
trade, trade,
multiLayerL1TradeFeeTotal,
} = quote; } = quote;
let multiLayerL1FeeTotal = null;
if (
multiLayerL1TradeFeeTotal !== null &&
multiLayerL1ApprovalFeeTotal !== null
) {
multiLayerL1FeeTotal = sumHexes(
multiLayerL1TradeFeeTotal || '0x0',
multiLayerL1ApprovalFeeTotal || '0x0',
);
} else if (multiLayerL1TradeFeeTotal !== null) {
multiLayerL1FeeTotal = multiLayerL1TradeFeeTotal;
}
const sourceValue = calcTokenAmount( const sourceValue = calcTokenAmount(
sourceAmount, sourceAmount,
sourceTokenInfo.decimals, sourceTokenInfo.decimals,

View File

@ -133,6 +133,8 @@ export default function ViewQuote() {
const [warningHidden, setWarningHidden] = useState(false); const [warningHidden, setWarningHidden] = useState(false);
const [originalApproveAmount, setOriginalApproveAmount] = useState(null); const [originalApproveAmount, setOriginalApproveAmount] = useState(null);
const [multiLayerL1FeeTotal, setMultiLayerL1FeeTotal] = useState(null); const [multiLayerL1FeeTotal, setMultiLayerL1FeeTotal] = useState(null);
const [multiLayerL1ApprovalFeeTotal, setMultiLayerL1ApprovalFeeTotal] =
useState(null);
// We need to have currentTimestamp in state, otherwise it would change with each rerender. // We need to have currentTimestamp in state, otherwise it would change with each rerender.
const [currentTimestamp] = useState(Date.now()); const [currentTimestamp] = useState(Date.now());
@ -314,7 +316,7 @@ export default function ViewQuote() {
smartTransactionsOptInStatus && smartTransactionsOptInStatus &&
smartTransactionFees?.tradeTxFees, smartTransactionFees?.tradeTxFees,
nativeCurrencySymbol, nativeCurrencySymbol,
multiLayerL1FeeTotal, multiLayerL1ApprovalFeeTotal,
); );
}, [ }, [
quotes, quotes,
@ -330,7 +332,7 @@ export default function ViewQuote() {
nativeCurrencySymbol, nativeCurrencySymbol,
smartTransactionsEnabled, smartTransactionsEnabled,
smartTransactionsOptInStatus, smartTransactionsOptInStatus,
multiLayerL1FeeTotal, multiLayerL1ApprovalFeeTotal,
]); ]);
const renderableDataForUsedQuote = renderablePopoverData.find( const renderableDataForUsedQuote = renderablePopoverData.find(
@ -891,15 +893,11 @@ export default function ViewQuote() {
]); ]);
useEffect(() => { useEffect(() => {
if (!isMultiLayerFeeNetwork) { if (!isMultiLayerFeeNetwork || !usedQuote?.multiLayerL1TradeFeeTotal) {
return; return;
} }
const getEstimatedL1Fee = async () => { const getEstimatedL1Fees = async () => {
try { try {
const l1TradeFeeTotal = await fetchEstimatedL1Fee({
txParams: unsignedTransaction,
chainId,
});
let l1ApprovalFeeTotal = '0x0'; let l1ApprovalFeeTotal = '0x0';
if (approveTxParams) { if (approveTxParams) {
l1ApprovalFeeTotal = await fetchEstimatedL1Fee({ l1ApprovalFeeTotal = await fetchEstimatedL1Fee({
@ -910,16 +908,27 @@ export default function ViewQuote() {
}, },
chainId, chainId,
}); });
setMultiLayerL1ApprovalFeeTotal(l1ApprovalFeeTotal);
} }
const l1FeeTotal = sumHexes(l1TradeFeeTotal, l1ApprovalFeeTotal); const l1FeeTotal = sumHexes(
usedQuote.multiLayerL1TradeFeeTotal,
l1ApprovalFeeTotal,
);
setMultiLayerL1FeeTotal(l1FeeTotal); setMultiLayerL1FeeTotal(l1FeeTotal);
} catch (e) { } catch (e) {
captureException(e); captureException(e);
setMultiLayerL1FeeTotal(null); setMultiLayerL1FeeTotal(null);
setMultiLayerL1ApprovalFeeTotal(null);
} }
}; };
getEstimatedL1Fee(); getEstimatedL1Fees();
}, [unsignedTransaction, approveTxParams, isMultiLayerFeeNetwork, chainId]); }, [
unsignedTransaction,
approveTxParams,
isMultiLayerFeeNetwork,
chainId,
usedQuote,
]);
useEffect(() => { useEffect(() => {
if (currentSmartTransactionsEnabled && smartTransactionsOptInStatus) { if (currentSmartTransactionsEnabled && smartTransactionsOptInStatus) {