Skip to content

Conversation

@liobrasil
Copy link
Contributor

@liobrasil liobrasil commented Nov 3, 2025

Fix computation limits in Uniswap V3 connector

Problem

getMaxAmount() was hitting computation limits due to expensive tick bitmap scanning (20-30+ EVM calls per transaction).

Solution

Replaced tick-scanning with lightweight price impact heuristic:

  • 2 EVM calls instead of 20-30 (slot0 + liquidity)
  • Calculates max amount based on configurable price impact (default 6%)

Changes

  • Simplified getMaxAmount() calculation
  • Added configurable maxPriceImpactBps parameter
  • Removed tick bitmap scanning functions
  • All existing APIs unchanged

Trade-off

Less precise but significantly faster. Sufficient for preventing oversized swap requests.​​​​​​​​​​​​​​​​

@liobrasil liobrasil changed the title [DRAFT] for optimization [DRAFT] Simplify max amount calculation using price impact Nov 13, 2025
@liobrasil
Copy link
Contributor Author

Please hold off on merging this PR for now, as I still need to complete testing and performance benchmarking.

refactor(UniswapV3SwapConnectors.cdc): rename variable slot0Res to s0Res for brevity and consistency in naming conventions

refactor(UniswapV3SwapConnectors.cdc): change maxAmount type from UInt to UFix128 for improved precision in calculations
fix(UniswapV3SwapConnectors.cdc): update calculations to use UFix128 for consistent handling of decimal values in price impact logic
@liobrasil liobrasil force-pushed the lionel/simplify-max-amount-calculation branch from a58fd64 to a8dd82c Compare November 21, 2025 17:10
…ns to use UInt256 to prevent overflow and improve accuracy in arithmetic operations
@liobrasil liobrasil force-pushed the lionel/simplify-max-amount-calculation branch 2 times, most recently from 4253daf to ce17ca4 Compare November 25, 2025 16:13
…m 500 to 600 basis points to reflect new configuration requirements
@liobrasil liobrasil force-pushed the lionel/simplify-max-amount-calculation branch from ce17ca4 to 6fe99aa Compare November 25, 2025 16:13
@liobrasil liobrasil changed the title [DRAFT] Simplify max amount calculation using price impact Simplify max amount calculation using price impact Nov 26, 2025
…s for better project structure

fix(UniswapV3SwapConnectors.cdc): change maxPriceImpactBps from let to var to allow updates and add setter function for maxPriceImpactBps with validation
@liobrasil liobrasil force-pushed the lionel/simplify-max-amount-calculation branch from 6943f61 to c44e449 Compare November 27, 2025 17:54
Comment on lines 116 to 122
access(all) fun setMaxPriceImpactBps(_ bps: UInt) {
pre {
bps > 0 && bps <= 5000: "maxPriceImpactBps must be between 1 and 5000 (0.01% to 50%)"
}
self.maxPriceImpactBps = bps
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to guard this method?

…variable to constant to ensure immutability

chore(UniswapV3SwapConnectors.cdc): remove setMaxPriceImpactBps function as it is no longer needed with constant maxPriceImpactBps
@liobrasil liobrasil closed this Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants