Skip to content

Commit

Permalink
chore: handle Eth USDT edge case with resetApproval
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed Sep 26, 2024
1 parent ece1718 commit 569c91d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
44 changes: 35 additions & 9 deletions ui/ducks/bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ import {
getSelectedNetworkClientId,
} from '../../selectors';
import { getGasFeeEstimates } from '../metamask/metamask';
import {
addHexes,
decGWEIToHexWEI,
} from '../../../shared/modules/conversion.utils';
import { decGWEIToHexWEI } from '../../../shared/modules/conversion.utils';
import { FEATURED_RPCS } from '../../../shared/constants/network';
import { MetaMetricsNetworkEventSource } from '../../../shared/constants/metametrics';
import { DEFAULT_TOKEN_ADDRESS } from '../../../shared/constants/swaps';
import {
DEFAULT_ERC20_APPROVE_GAS,
DEFAULT_TOKEN_ADDRESS,
} from '../../../shared/constants/swaps';
getEthUsdtApproveResetTx as getEthUsdtApproveResetTxParams,
isEthUsdt,
} from '../../pages/bridge/bridge.util';
import bridge, { bridgeSlice } from './bridge';
import {
DUMMY_QUOTES_APPROVAL,
Expand Down Expand Up @@ -139,8 +137,32 @@ export const signBridgeTransaction = (
// );
}

const handleUSDTAllowanceReset = () => {
const;
const handleUsdtAllowanceReset = async (hexChainId: Hex) => {
const gasLimit = quoteMeta.approval.gasLimit.toString();
const txParams = getEthUsdtApproveResetTxParams({
...quoteMeta.approval,
chainId: hexChainId,
gasLimit,
gas: gasLimit, // must set this field
maxFeePerGas,
maxPriorityFeePerGas,
});

await addTransactionAndWaitForPublish(txParams, {
requireApproval: false,
// @ts-expect-error Need TransactionController v37+, TODO add this type
type: 'bridgeApproval', // TransactionType.bridgeApproval,

// TODO update TransactionController to change this to a bridge field
// swaps.meta is of type Partial<TransactionMeta>, will get merged with TransactionMeta by the TransactionController
swaps: {
hasApproveTx: true,
meta: {
type: 'bridgeApproval', // TransactionType.bridgeApproval, // TODO
sourceTokenSymbol: quoteMeta.quote.srcAsset.symbol,
},
},
});
};

const handleApprovalTx = async () => {
Expand All @@ -154,6 +176,10 @@ export const signBridgeTransaction = (
throw new Error('Invalid chain ID');
}

if (isEthUsdt(hexChainId, quoteMeta.quote.srcAsset.address)) {
await handleUsdtAllowanceReset(hexChainId);
}

const gasLimit = quoteMeta.approval.gasLimit.toString();
const txParams = {
...quoteMeta.approval,
Expand Down
6 changes: 5 additions & 1 deletion ui/pages/bridge/bridge.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
isSwapsDefaultTokenAddress,
isSwapsDefaultTokenSymbol,
} from '../../../shared/modules/swaps.utils';
import { CHAIN_IDS } from '../../../shared/constants/network';
import { ETHEREUM_USDT_APPROVALS_ABI } from './EthUsdtApprovalsAbi';

const CLIENT_ID_HEADER = { 'X-Client-Id': BRIDGE_CLIENT_ID };
Expand Down Expand Up @@ -173,7 +174,7 @@ export async function fetchBridgeTokens(
* @param approval
* @returns
*/
export const getEthUsdtRemovalTx = (approval: TransactionParams) => {
export const getEthUsdtApproveResetTx = (approval: TransactionParams) => {
const USDTContractInterface = new Contract(
ETH_USDT_ADDRESS,
ETHEREUM_USDT_APPROVALS_ABI,
Expand All @@ -188,3 +189,6 @@ export const getEthUsdtRemovalTx = (approval: TransactionParams) => {
data,
};
};

export const isEthUsdt = (chainId: Hex, address: string) =>
chainId === CHAIN_IDS.MAINNET && address === ETH_USDT_ADDRESS;

0 comments on commit 569c91d

Please sign in to comment.