-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -12,6 +17,7 @@ export async function processSwapTransaction({ | |
sendTransactionAsync, | ||
onStart, | ||
onSuccess, | ||
useAggregator, | ||
}: { | ||
swapTransaction: BuildSwapTransaction; | ||
config: Config; | ||
|
@@ -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, | ||
|
@@ -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
|
||
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 | ||
|