diff --git a/src/swap/utils/processSwapTransaction.test.ts b/src/swap/utils/processSwapTransaction.test.ts index c3e2fa9bb1..71745a8632 100644 --- a/src/swap/utils/processSwapTransaction.test.ts +++ b/src/swap/utils/processSwapTransaction.test.ts @@ -21,6 +21,11 @@ describe('processSwapTransaction', () => { .fn() .mockResolvedValueOnce('approveTxHash') .mockResolvedValueOnce('txHash'); + const sendTransactionAsyncPermit2 = vi + .fn() + .mockResolvedValueOnce('approveTxHash') + .mockResolvedValueOnce('permit2TxHash') + .mockResolvedValueOnce('txHash'); const onSuccess = vi.fn(); const onStart = vi.fn(); const onSuccessAsync = vi.fn().mockImplementation(async (_txHash: string) => { @@ -117,6 +122,7 @@ describe('processSwapTransaction', () => { sendTransactionAsync, onSuccess, onStart, + useAggregator: true, }); expect(setPendingTransaction).toHaveBeenCalledTimes(4); @@ -210,6 +216,7 @@ describe('processSwapTransaction', () => { sendTransactionAsync, onSuccess, onStart, + useAggregator: true, }); expect(setPendingTransaction).toHaveBeenCalledTimes(2); @@ -304,6 +311,7 @@ describe('processSwapTransaction', () => { sendTransactionAsync: sendTransactionAsync2, onSuccess: onSuccessAsync, onStart: onStartAsync, + useAggregator: true, }); expect(setPendingTransaction).toHaveBeenCalledTimes(4); @@ -320,4 +328,101 @@ describe('processSwapTransaction', () => { expect(onStartAsync).toHaveBeenNthCalledWith(1, 'approveTxHash'); expect(onStartAsync).toHaveBeenNthCalledWith(2, 'txHash'); }); + + it('should successfully use Permit2 approval process for `useAggregators`=false', async () => { + const swapTransaction: BuildSwapTransaction = { + transaction: { + to: '0x123', + value: 0n, + data: '0x', + chainId: 8453, + gas: 0n, + }, + approveTransaction: { + to: '0x456', + value: 0n, + data: '0x123', + chainId: 8453, + gas: 0n, + }, + quote: { + from: { + name: 'USDC', + address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', + symbol: 'USDC', + decimals: 6, + image: + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2', + chainId: 8453, + }, + to: { + address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed', + chainId: 8453, + decimals: 18, + image: + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm', + name: 'DEGEN', + symbol: 'DEGEN', + }, + fromAmount: '100000000000000', + toAmount: '19395353519910973703', + amountReference: 'from', + priceImpact: '0.94', + hasHighPriceImpact: false, + slippage: '3', + warning: undefined, + }, + fee: { + baseAsset: { + name: 'DEGEN', + address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed', + symbol: 'DEGEN', + decimals: 18, + image: + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm', + chainId: 8453, + }, + percentage: '1', + amount: '195912661817282562', + }, + }; + const config = createConfig({ + chains: [mainnet, sepolia], + connectors: [ + mock({ + accounts: [ + '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', + '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', + '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC', + ], + }), + ], + transports: { + [mainnet.id]: http(), + [sepolia.id]: http(), + }, + }); + + await processSwapTransaction({ + swapTransaction, + config, + setPendingTransaction, + setLoading, + sendTransactionAsync: sendTransactionAsyncPermit2, + onSuccess, + onStart, + useAggregator: false, + }); + + expect(setPendingTransaction).toHaveBeenCalledTimes(6); + expect(setPendingTransaction).toHaveBeenCalledWith(true); + expect(setPendingTransaction).toHaveBeenCalledWith(false); + expect(sendTransactionAsyncPermit2).toHaveBeenCalledTimes(3); + expect(waitForTransactionReceipt).toHaveBeenCalledTimes(3); + + expect(setLoading).toHaveBeenCalledTimes(1); + expect(setLoading).toHaveBeenCalledWith(true); + expect(onSuccess).toHaveBeenCalledTimes(1); + expect(onSuccess).toHaveBeenCalledWith({}); + }); }); diff --git a/src/swap/utils/processSwapTransaction.ts b/src/swap/utils/processSwapTransaction.ts index b357b17fc9..2767d11ddc 100644 --- a/src/swap/utils/processSwapTransaction.ts +++ b/src/swap/utils/processSwapTransaction.ts @@ -55,6 +55,7 @@ export async function processSwapTransaction({ // we also need to make an extra transaction to `Permit2` to approve the UniversalRouter to spend the funds // read more: https://blog.uniswap.org/permit2-and-universal-router if (!useAggregator) { + setPendingTransaction(true); const permit2ContractAbi = parseAbi([ 'function approve(address token, address spender, uint160 amount, uint48 expiration) external', ]); @@ -78,6 +79,7 @@ export async function processSwapTransaction({ hash: permitTxnHash, confirmations: 1, }); + setPendingTransaction(false); } }