fix add liquidity#11
Merged
Merged
Conversation
gabririgo
commented
May 25, 2026
Contributor
- correctly add liquidity
- prompt user to initialize pool from user's wallet if pool has not been initialized (smart pools can only add liquidity to already-initialized liquidity pools)
There was a problem hiding this comment.
Pull request overview
This PR updates the Uniswap v4 LP flow so liquidity adds fail fast on uninitialized pools and introduces a new initialize_pool tool to build a PoolManager initialize transaction (signed from the user wallet) before adding liquidity via the vault.
Changes:
- Add
initialize_poolas a first-class tool (LLM definition, prompts, routing/categorying, dispatch) and include it in required-tool tests. - Update
add_liquidityto explicitly block pools withsqrtPriceX96 = 0and improve error guidance around initialization. - Extend
get_pool_infomessaging to suggest initialization when appropriate.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
tests/tools.test.ts |
Adds initialize_pool to the required tool list to prevent accidental removal. |
src/services/uniswapLP.ts |
Adds buildInitializePoolTx, exports POOL_MANAGER, and changes pool-state/“initialized” handling for add-liquidity + pool info. |
src/routes/tools.ts |
Categorizes initialize_pool under “Uniswap v4 LP”. |
src/llm/tools.ts |
Defines the new initialize_pool tool and updates LP tool guidance around tickSpacing/initialization. |
src/llm/prompts.ts |
Updates LP workflow instructions to include initialization. |
src/llm/client.ts |
Wires the new tool into execution and updates get_pool_info output messaging. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Replace floating-point sqrtPriceX96 math with pure BigInt integer arithmetic (Newton-Raphson isqrt) to avoid precision loss and Infinity/overflow for large ERC-20 base-unit amounts - Add input validation: throw when amount0 or amount1 is zero - Add TickMath bounds check on computed sqrtPriceX96 - Export computeSqrtPriceX96FromAmounts for unit testing - Add @uniswap/v3-sdk and jsbi as direct package.json dependencies (previously only transitive via @uniswap/v4-sdk) - Fix inaccurate catch comment in buildInitializePoolTx: readPoolState returns sqrtPriceX96=0 for uninitialized pools (does not throw); the catch block handles RPC/StateView deployment failures - Fix getPoolInfoById to return structured PoolInfo with initialized=false instead of throwing when pool is uninitialized, enabling get_pool_info to surface the initialization guidance message in client.ts - Add unit tests for computeSqrtPriceX96FromAmounts covering precision, edge cases, zero inputs, out-of-bounds ratios, and symmetry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Distinguish infrastructure errors (StateView not deployed, RPC failures) from pool-key mismatches in the readPoolState catch block so users are not misled into retrying with a different pool key when the backend simply cannot read on-chain state - Add poolKeyKnown: boolean to PoolInfo interface; set false for uninitialized pools (where fee/tickSpacing/hooks/currency0/currency1 are zero placeholders) and true when values come from the on-chain Initialize event - Update get_pool_info message in client.ts: when poolKeyKnown is false, ask the user for the full pool key instead of presenting placeholder values as valid initialize_pool parameters - Tighten symmetry test tolerance from Q96 (~7.9e28) to 2n, matching the comment intent of allowing ±1 floor rounding Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When params.sqrtPriceX96 is supplied directly, add explicit bounds validation (>0, within [MIN_SQRT_RATIO, MAX_SQRT_RATIO)) before passing the value to TickMath/encodeFunctionData, consistent with the checks already present in computeSqrtPriceX96FromAmounts. Throws a user-friendly error rather than a low-level revert when the value is out of range. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Encode the mutual-exclusion between sqrtPriceX96 and (amountA+amountB) directly in the JSON Schema parameters object. The top-level required array continues to mandate tokenA, tokenB, and fee; a oneOf clause adds that callers must also supply either sqrtPriceX96 OR both amountA AND amountB. This prevents the LLM from invoking the tool with an argument combination that would cause a runtime error in buildInitializePoolTx. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…in initialized check
Two fixes:
1. tools.ts: restore the leading sentence of remove_liquidity's description
('Remove liquidity from a Uniswap v4 LP position through the vault's
modifyLiquidities adapter.') that was accidentally dropped when the
oneOf clause was added to the adjacent initialize_pool tool in the
previous commit.
2. uniswapLP.ts: in getPoolInfoById, treat hasInitializeEvent as
authoritative for the initialized flag. Changed:
initialized: sqrtPriceX96 !== 0n
to:
initialized: hasInitializeEvent || sqrtPriceX96 !== 0n
An on-chain Initialize event is definitive proof the pool exists.
If sqrtPriceX96 reads back as 0 due to a transient RPC/StateView
issue, the pool should not be incorrectly classified as uninitialized
(which would generate misleading 'call initialize_pool' guidance that
would result in an on-chain revert).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Two fixes in uniswapLP.ts: 1. JSDoc for computeSqrtPriceX96FromAmounts: add a second @throws entry documenting that the function also throws when the computed sqrtPriceX96 falls outside TickMath bounds (extreme price ratios). Previously only the zero-amount throw was documented. 2. buildInitializePoolTx catch block: only swallow StateView-unavailable errors (expected on chains without StateView deployment). Any other error — e.g. transient RPC failures — is now re-thrown as a user- friendly 'Could not verify pool initialization state' message. This prevents silently proceeding to build a transaction that would revert when the pre-initialization check failed for a non-structural reason. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Set poolKeyKnown: hasInitializeEvent (not unconditionally true) so callers know when pool key fields come from the on-chain Initialize event vs. are placeholder/estimated values - Improve sqrtPriceX96=0 error in buildAddLiquidityTx to reflect both possible causes: pool uninitialized OR wrong pool key (fee/tickSpacing/hooks mismatch), and suggest get_pool_info to verify - Update package-lock.json and yarn.lock to include @uniswap/v3-sdk and jsbi as tracked direct runtime dependencies Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…InitializePoolTx that warns pool key fields may be placeholders (Initialize event not found) and asks user to verify before calling add_liquidity, instead of silently suggesting potentially wrong fee/tickSpacing/hooks values - In buildInitializePoolTx: add explicit check that tokenA and tokenB resolve to different addresses after resolution, with a clear error message Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add validatePoolKeyNumerics() helper that checks fee (uint24: 0-16,777,215, integer) and tickSpacing (int24: >0 and ≤8,388,607, integer) before building pool key / pool ID. Called in both buildAddLiquidityTx and buildInitializePoolTx to catch invalid inputs with a clear message instead of low-level ABI encoding errors or on-chain reverts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.