Skip to content

Commit

Permalink
permit2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Aug 6, 2024
1 parent bfea1bf commit 37cd77a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function SwapProvider({
sendTransactionAsync,
onStart,
onSuccess,
useAggregator,
});

// TODO: refresh balances
Expand Down
4 changes: 4 additions & 0 deletions src/swap/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export const TOO_MANY_REQUESTS_ERROR_CODE = 'TOO_MANY_REQUESTS_ERROR';
export const UNCAUGHT_SWAP_QUOTE_ERROR_CODE = 'UNCAUGHT_SWAP_QUOTE_ERROR';
export const UNCAUGHT_SWAP_ERROR_CODE = 'UNCAUGHT_SWAP_ERROR';
export const USER_REJECTED_ERROR_CODE = 'USER_REJECTED';
export const PERMIT2_CONTRACT_ADDRESS =
'0x000000000022D473030F116dDEE9F6B43aC78BA3';
export const UNIVERSALROUTER_CONTRACT_ADDRESS =
'0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD';
42 changes: 40 additions & 2 deletions src/swap/utils/processSwapTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { TransactionReceipt } from 'viem';
import type { Address, TransactionReceipt } from 'viem';
import type { Config } from 'wagmi';
import { waitForTransactionReceipt } from 'wagmi/actions';
import type { SendTransactionMutateAsync } from 'wagmi/query';
import {
UNIVERSALROUTER_CONTRACT_ADDRESS,
PERMIT2_CONTRACT_ADDRESS,
} from '../constants';
import type { BuildSwapTransaction } from '../types';
import { encodeFunctionData, parseAbi } from 'viem';

export async function processSwapTransaction({
swapTransaction,
Expand All @@ -12,6 +17,7 @@ export async function processSwapTransaction({
sendTransactionAsync,
onStart,
onSuccess,
useAggregator,
}: {
swapTransaction: BuildSwapTransaction;
config: Config;
Expand All @@ -22,8 +28,9 @@ export async function processSwapTransaction({
onSuccess:
| ((txReceipt: TransactionReceipt) => void | Promise<void>)
| undefined;
useAggregator: boolean;
}) {
const { transaction, approveTransaction } = swapTransaction;
const { transaction, approveTransaction, quote } = swapTransaction;

// for swaps from ERC-20 tokens,
// if there is an approveTransaction present,
Expand All @@ -41,6 +48,37 @@ export async function processSwapTransaction({
confirmations: 1,
});
setPendingTransaction(false);

// for the V2 API, we use Uniswap's UniversalRouter
// this adds an additional transaction/step to the swap process
// the `approveTx` on the response will be an approval for the amount of the `from` token against `Permit2`
// we also need to make an extra transaction to `Permit2` to approve the UniversalRouter to spend the funds
// see more: https://blog.uniswap.org/permit2-and-universal-router
if (!useAggregator) {
const permit2ContractAbi = parseAbi([
'function approve(address token, address spender, uint160 amount, uint48 expiration) external',
]);
const data = encodeFunctionData({

Check failure on line 61 in src/swap/utils/processSwapTransaction.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/swap/utils/processSwapTransaction.test.ts > processSwapTransaction > should request approval and make the swap for ERC-20 tokens

InvalidAddressError: Address "" is invalid. - Address must be a hex value of 20 bytes (40 hex characters). - Address must match its checksum counterpart. Version: [email protected] ❯ encodeAddress node_modules/viem/utils/abi/encodeAbiParameters.ts:219:32 ❯ prepareParam node_modules/viem/utils/abi/encodeAbiParameters.ts:159:12 ❯ prepareParams node_modules/viem/utils/abi/encodeAbiParameters.ts:124:25 ❯ encodeAbiParameters node_modules/viem/utils/abi/encodeAbiParameters.ts:97:26 ❯ Module.encodeFunctionData node_modules/viem/utils/abi/encodeFunctionData.ts:93:9 ❯ Module.processSwapTransaction src/swap/utils/processSwapTransaction.ts:61:20 ❯ src/swap/utils/processSwapTransaction.test.ts:112:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: undefined, metaMessages: [ '- Address must be a hex value of 20 bytes (40 hex characters).', '- Address must match its checksum counterpart.' ], shortMessage: 'Address "" is invalid.', version: '[email protected]', walk: 'Function<walk>' }

Check failure on line 61 in src/swap/utils/processSwapTransaction.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/swap/utils/processSwapTransaction.test.ts > processSwapTransaction > should successfully call relevant async lifecycle hooks

InvalidAddressError: Address "" is invalid. - Address must be a hex value of 20 bytes (40 hex characters). - Address must match its checksum counterpart. Version: [email protected] ❯ encodeAddress node_modules/viem/utils/abi/encodeAbiParameters.ts:219:32 ❯ prepareParam node_modules/viem/utils/abi/encodeAbiParameters.ts:159:12 ❯ prepareParams node_modules/viem/utils/abi/encodeAbiParameters.ts:124:25 ❯ encodeAbiParameters node_modules/viem/utils/abi/encodeAbiParameters.ts:97:26 ❯ Module.encodeFunctionData node_modules/viem/utils/abi/encodeFunctionData.ts:93:9 ❯ Module.processSwapTransaction src/swap/utils/processSwapTransaction.ts:61:20 ❯ src/swap/utils/processSwapTransaction.test.ts:299:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { details: undefined, docsPath: undefined, metaMessages: [ '- Address must be a hex value of 20 bytes (40 hex characters).', '- Address must match its checksum counterpart.' ], shortMessage: 'Address "" is invalid.', version: '[email protected]', walk: 'Function<walk>' }
abi: permit2ContractAbi,
functionName: 'approve',
args: [
quote.from.address as Address,
UNIVERSALROUTER_CONTRACT_ADDRESS,
BigInt(quote.fromAmount),
20_000_000_000_000,
],
});
const permitTxnHash = await sendTransactionAsync({
to: PERMIT2_CONTRACT_ADDRESS,
data: data,
value: 0n,
});
await Promise.resolve(onStart?.(permitTxnHash));
await waitForTransactionReceipt(config, {
hash: permitTxnHash,
confirmations: 1,
});
}
}

// make the swap
Expand Down

0 comments on commit 37cd77a

Please sign in to comment.