Skip to content

Conversation

luchobonatti
Copy link
Member

@luchobonatti luchobonatti commented Jun 10, 2025

Add Permit2 Single Support to SwapCallData

Overview

This PR adds support for Permit2 single token approvals in swap transactions, allowing users to perform gas-efficient token swaps without requiring separate approval transactions.

Changes

  • Added preparePermit2Data utility for handling single token approvals
  • Updated buildSwapCallData to support Permit2 single signatures
  • Consolidated Permit2 types into a single file
  • Added comprehensive test coverage for new features

Technical Details

  • preparePermit2Data handles:
    • Single token approvals
    • Native token (ETH) filtering
    • Custom deadline support
    • Current allowance fetching
  • buildSwapCallData now supports:
    • Basic swaps without approvals
    • Swaps with Permit2 single signatures
    • Universal Router integration
  • Consolidated types in permit2.ts:
    • Base interfaces for Permit2 data
    • Single and batch permit interfaces
    • Result interfaces with helper functions

Testing

Added test suites for:

  • preparePermit2Data.test.ts covering:
    • Native token validation
    • Default and custom deadlines
    • Signature building
    • Allowance fetching
  • Updated existing tests to maintain compatibility

Usage Example

// Basic swap
const { calldata, value } = await uniDevKit.buildSwapCallData({
  tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
  amountIn: parseUnits("100", 6), // 100 USDC
  pool: pool,
  slippageTolerance: 50, // 0.5%
  recipient: "0x..."
});

// Swap with permit2
const permitData = await uniDevKit.preparePermit2Data({
  token: tokenIn,
  spender: uniDevKit.getContractAddress('universalRouter'),
  owner: userAddress
});

const signature = await signer._signTypedData(permitData.toSign);
const permitWithSignature = permitData.buildPermit2DataWithSignature(signature);

const { calldata: calldataWithPermit, value: valueWithPermit } = await uniDevKit.buildSwapCallData({
  tokenIn: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  amountIn: parseUnits("100", 6),
  pool: pool,
  slippageTolerance: 50,
  recipient: "0x...",
  permit2Signature: permitWithSignature
});

const tx = await sendTransaction({
  to: uniDevKit.getContractAddress('universalRouter'),
  data: calldata,
  value
});

@luchobonatti luchobonatti merged commit 8f66c86 into main Jun 10, 2025
1 check passed
Copy link

🎉 This PR is included in version 1.0.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant