Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cadence/contracts/connectors/evm/UniswapV3SwapConnectors.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,26 @@ access(all) contract UniswapV3SwapConnectors {
erc20Address: outTokenEVMAddress
)

/// Quoting exact output then swapping exact input can overshoot by 0.00000001 (1 UFix64 quantum).
///
/// UFix64 has 8 decimals; EVM tokens have 18. One UFix64 step = 10^10 wei.
///
/// Example (pool price 1 FLOW = 2 USDC, want 10 USDC out):
/// 1. Quoter says need 5,000000002000000000 FLOW wei
/// 2. Ceil to UFix64: 5,000000010000000000 (overshoot: 8e9 wei)
/// 3. exactInput swaps the ceiled amount; extra 8e9 FLOW wei × 2 = 16e9 USDC wei extra
/// 4. Actual output: 10,000000016000000000 USDC wei
/// 5. Floor to UFix64: 10.00000001 USDC (quoted 10.00000000)
///
/// The overshoot is always non-negative (ceiled input >= what pool needs).
/// It surfaces when the extra output crosses a 10^10 wei quantum boundary.
/// Cap at amountOutMin so only the expected amount is bridged; dust stays in the COA.
let bridgeUFix = outUFix > amountOutMin && amountOutMin > 0.0 ? amountOutMin : outUFix
let safeAmountOut = FlowEVMBridgeUtils.convertCadenceAmountToERC20Amount(
outUFix,
bridgeUFix,
erc20Address: outTokenEVMAddress
)
// Withdraw output back to Flow
// Withdraw output back to Flow; sub-quantum remainder and any overshoot stay in COA
let outVault <- coa.withdrawTokens(type: outVaultType, amount: safeAmountOut, feeProvider: feeVaultRef)

// Handle leftover fee vault
Expand Down