Skip to content

feat(launchpad_v7): make meteora liquidity configurable per launch - #509

Open
metapileks wants to merge 8 commits into
developfrom
pileks/configurable-launch-liquidity
Open

metapileks wants to merge 8 commits into
developfrom
pileks/configurable-launch-liquidity

Conversation

@metapileks

@metapileks metapileks commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Every v0.7 launch mints 900,000 tokens for its single-sided Meteora (DAMM v2) pool. The amount is a program constant, so a launch can't choose a different amount or go without the pool. After this change initialize_launch takes the amount and stores it on the Launch, and complete_launch creates the pool from it; 0 means no Meteora pool. The SDK defaults to 900,000 tokens, so existing callers behave as before.

What changed

  • initialize_launch takes damm_v2_liquidity_token_amount, mints that many tokens in place of the fixed 900,000, stores it on the Launch and reports it in LaunchInitializedEvent. It rejects an amount that isn't a whole number of tokens or is above 4,295,048,016 tokens, past which the pool maths at completion would overflow a u128.
  • complete_launch creates the Meteora pool from the stored amount. At 0 it creates no pool and skips the SOL top-up and the position-NFT check. Its account list is unchanged.
  • Launch accounts grow by 8 bytes, from 785 to 793. resize_launch now converts the 785-byte layout and writes 900,000 tokens into the new field, which is what those launches already minted; it no longer converts the 784-byte layout. Every other instruction that takes a Launch refuses one that hasn't been resized, because an unresized account can still deserialize, and would then read the new field as 0 and complete without its pool.
  • Version: the program is 0.7.1 at the same address.
  • SDK: initializeLaunchIx takes an optional dammV2LiquidityTokenAmount, default 900,000 tokens, and the v0.7 types are regenerated in place. initialize_launch gained an argument, so earlier SDK versions can't call it on the upgraded program. The package version moves to 0.1.3-alpha.2.
  • scripts/v0.7/resizeLaunches.ts prints the Launch account sizes after its run and fails unless they are all the same.

Confidence Score: 5/5 Tier: plusRetriggerView in Greptile

The PR appears safe to merge, with no new actionable issues found.

Summary

This PR makes each v0.7 launch choose 900,000 or 3,000,000 tokens for its Meteora pool.

  • Launch setup can choose 900,000 or 3,000,000 tokens for Meteora liquidity.
  • Existing launches must be resized before their instructions can use them.

No new actionable issues were found. Tests were inspected, not run.

Reviews (3) · Last reviewed commit: "feat(launchpad_v7): limit meteora liquid..." · Reviewed by Greptile

@metapileks metapileks self-assigned this Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Repository Guard

  • Cargo.lock: pass
  • yarn.lock (sdk): pass
  • yarn.lock (root): pass
  • Repo guard: pass

Repository Guard

Cargo dependency pinning

  • Status: pass
  • Every programs/*/Cargo.toml dep uses =x.y.z, a path = .. workspace ref, or a git dep with a 40-char rev.

Cross-program Anchor/Solana version consistency

  • Status: pass
  • anchor-lang and anchor-spl are pinned to the version declared in repo-guard.toml across every program.

Anchor.toml solana_version

  • Status: pass
  • Anchor.toml declares solana_version = "4.3.0" (local-dev install for anchor test).

Crate minimum age

  • Status: pass
  • All Cargo deps changed by this PR are at least 14 days old on crates.io.

Yarn package.json pinning

  • Status: pass
  • All package.json deps use exact versions (no ^, ~, ranges).

npm minimum age

  • Status: pass
  • All npm deps changed by this PR are at least 14 days old.

Workflow toolchain consistency

  • Status: pass
  • Every workflow declares anchor-version: 0.32.2.
  • Per-file solana-cli-version values match [toolchain.workflow_solana_cli] in repo-guard.toml.

GitHub Action SHA pinning

  • Status: pass
  • Every third-party action is pinned to a SHA in [actions.sha_allowlist].

Sensitive program / config changes

  • Status: warn
  • Review hint only (CODEOWNERS is the merge gate). Lines below match heuristics for security-sensitive changes:
  • High-sensitivity files touched: Cargo.lock

Overall status: pass

Lockfile freshness (Cargo.lock + yarn.lock) is checked by the workflow directly and cannot be bypassed. The sensitive-diff section is a review hint - CODEOWNERS handles the actual merge gate.

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

security Security Review

This PR appears safe to merge from a security standpoint; the only issue is that any launch still on the oldest 784-byte account layout becomes permanently unusable after the upgrade.

What was checked
  • The new damm_v2_liquidity_token_amount argument is validated on-chain to exactly 900,000 or 3,000,000 tokens, so a caller can't set any other value for their own launch.
  • The amount minted at initialize_launch is the same value complete_launch puts in the pool, and resize_launch only writes the fixed 900,000-token default that old launches actually minted.
  • The u128 pool math at completion cannot overflow for either allowed amount, even at maximum raise sizes, and overflow-checks are on in the release build.
  • Every instruction that reads a Launch now rejects an unmigrated account, so an unresized launch fails closed instead of completing without its pool.
  • resize_launch is permissionless but writes only the fixed default amount; it no longer accepts 784-byte accounts, which is the one gap found.

Findings

  • P2 Pre-bid-wall launches get bricked

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Comments Outside Diff

  • P2 security Pre-bid-wall launches get bricked — programs/v07_launchpad/src/instructions/resize_launch.rs:35

    784-byte Launches can never be migrated after this upgrade. resize_launch now only accepts the 785-byte layout (BEFORE_REALLOC_SIZE at line 29) and the 793-byte migrated size; the deployed 0.7.0 program could still grow a 784-byte launch, which is the layout from before has_bid_wall existed. If any mainnet Launch is still 784 bytes — for example one that missed the earlier migration run — then after this upgrade no instruction can read it and it can never be resized, so its funders can't refund or claim and the launch can't complete; only a future program upgrade could recover it. The size check added to scripts/v0.7/resizeLaunches.ts reports leftover non-793 accounts, but it runs after the upgrade, when the on-chain fix path is already gone. If every launch on mainnet is already 785 bytes, nothing is lost; the team should scan for sub-785 accounts before deploying, or keep the 784-byte conversion path until that's confirmed.

Comment thread scripts/v0.7/resizeLaunches.ts Resolved
@metanallok

Copy link
Copy Markdown
Contributor

Formal verification: Meteora liquidity maths (Lean 4 + Mathlib)

This is a machine-checked proof of the arithmetic complete_launch uses to seed the single-sided Meteora pool, for both allowed amounts (900,000 and 3,000,000 tokens), at 21418b87. The proofs build with no sorry. #print axioms shows only Lean's standard axioms (propext, Classical.choice, Quot.sound), with no native_decide.

Result: the maths is safe for both amounts. For every possible raise, Meteora takes at most the amount minted for it and at most one token less, no u128 step overflows, and the 10M participant tokens plus the additional and performance-package tokens stay in the launch vault.

What is proved

Theorem Statement
scale_exact tokens_to_lp / TOKEN_SCALE * TOKEN_SCALE = tokens_to_lp for both amounts, so dividing by TOKEN_SCALE loses nothing.
no_u128_overflow For any sqrt_price ≤ 2^86: MAX_SQRT_PRICE * tokens < 2^128, MAX_SQRT_PRICE - sqrt_price neither underflows nor is zero, and both later products are < 2^128. At the bound, the final liquidity is at most 0.68 × u128::MAX.
sqrt_price_lt_2_pow_75 For any raise < 2^64, the real sqrt(raise / 10M tokens) * 2^64 is < 2^75. That leaves 2048× headroom before the overflow bound, far more than f64 rounding can use.
meteora_le_amount Meteora's base-token pull, ceil(L·(√Pmax − √P) / (√P·√Pmax)) (get_delta_amount_a_unsigned, Rounding::Up), is ≤ damm_v2_liquidity_token_amount.
meteora_dust_lt_one_token That pull is > damm_v2_liquidity_token_amount − 1 token, so under 1 token is left behind.
meteora_amount_in_range The pull is nonzero and below u64::MAX, so Meteora's AmountIsZero and MathOverflow checks can't fail.
vault_covers_claims Starting from what initialize_launch mints, and after 2M tokens go to the futarchy AMM and the Meteora pull, the vault still holds ≥ 10M + additional_tokens_amount + performance_package_token_amount. So verify_vaults can't fail because of the Meteora step.

For a $1M raise, the model gives a pool deposit of 2,999,999,999,780 base units for the 3M option (220 short) and 899,999,999,934 for the 900k option (66 short).

Trust boundary

  • Rust ↔ Lean: values are modelled as ℕ. Release builds have overflow-checks = true, so each u128 operation either equals the ℕ result or panics, and no_u128_overflow shows none panics.
  • Meteora: the pull formula is transcribed from MeteoraAg/damm-v2 main (ConcentratedLiquidity::get_initial_pool_information and get_delta_amount_a_unsigned → mul_div_u256(.., Rounding::Up)). Not proved: that tests/fixtures/cp_amm.so and the mainnet program match that source. As a cross-check, CI's run-anchor-test passes this PR's 3M test against cp_amm.so, which asserts the same bounds (2,999,999e6 < pool ≤ 3,000,000e6).
  • f64: the conversion from raise to sqrt_price is bounded over the reals, not modelled bit-for-bit.
  • Sanity check: changing the model so the quotient rounds up by one makes meteora_le_amount, meteora_dust_lt_one_token and no_u128_overflow fail to build, so the theorems are not vacuous.

Reproduce

Save the file below as LaunchpadLiquidity/Liquidity.lean in a new Mathlib project (lake new LaunchpadLiquidity math), then run lake exe cache get && lake build. It was checked with Lean v4.37.0-rc1 and the matching Mathlib.

Liquidity.lean
import Mathlib

/-!
# Meteora liquidity maths in `launchpad_v7` (PR #509)

Models the arithmetic that `complete_launch` uses to seed the single-sided Meteora DAMM v2 pool,
and proves it is safe for both amounts a launch can choose (900,000 or 3,000,000 tokens).

Rust source (`programs/v07_launchpad/src/instructions/complete_launch.rs`):

```rust
let liquidity = ((MAX_SQRT_PRICE * (tokens_to_lp / TOKEN_SCALE) as u128)
    / (MAX_SQRT_PRICE - sqrt_price))
    * TOKEN_SCALE as u128
    * sqrt_price;

Meteora (cp-amm, ConcentratedLiquidity::get_initial_pool_information and
get_delta_amount_a_unsigned) then pulls this many base tokens from the launch vault, rounding up:

token_a = ceil(liquidity * (sqrt_max_price - sqrt_price) / (sqrt_price * sqrt_max_price))

with sqrt_max_price = MAX_SQRT_PRICE. With sqrt_min_price = sqrt_price, it pulls no quote
tokens.

Values are natural numbers. The release build has overflow-checks = true, so a Rust u128
operation either equals the ℕ operation or panics. no_u128_overflow shows that no step panics,
so the ℕ model matches the program exactly.
-/

namespace LaunchpadLiquidity

/-- damm_v2_cpi::constants::MAX_SQRT_PRICE -/
def MAX : ℕ := 79226673521066979257578248091

/-- TOKEN_SCALE (6 decimals) -/
def SCALE : ℕ := 10 ^ 6

/-- TOKENS_TO_DAMM_V2_LIQUIDITY_DEFAULT -/
def D_DEFAULT : ℕ := 900000 * SCALE

/-- TOKENS_TO_DAMM_V2_LIQUIDITY_LARGE -/
def D_LARGE : ℕ := 3000000 * SCALE

/-- The only values initialize_launch accepts, and the value resize_launch writes. -/
def ValidAmount (d : ℕ) : Prop := d = D_DEFAULT ∨ d = D_LARGE

def U64 : ℕ := 2 ^ 64
def U128 : ℕ := 2 ^ 128

/-- MAX_SQRT_PRICE * (tokens_to_lp / TOKEN_SCALE) / (MAX_SQRT_PRICE - sqrt_price) -/
def quotient (d s : ℕ) : ℕ := MAX * (d / SCALE) / (MAX - s)

/-- The liquidity that complete_launch passes to Meteora. -/
def liquidity (d s : ℕ) : ℕ := quotient d s * SCALE * s

/-- mul_div_u256(.., Rounding::Up), i.e. prod.div_ceil(denominator) -/
def ceilDiv (a b : ℕ) : ℕ := (a + b - 1) / b

/-- Base tokens Meteora pulls from the launch vault for liquidity at sqrt_price. -/
def meteoraTokenA (L s : ℕ) : ℕ := ceilDiv (L * (MAX - s)) (s * MAX)

/-! ## Helpers -/

lemma ceilDiv_le {x b k : ℕ} (hb : 0 < b) (h : x ≤ k * b) : ceilDiv x b ≤ k := by
unfold ceilDiv
have : (x + b - 1) / b < k + 1 := by
rw [Nat.div_lt_iff_lt_mul hb]
have := Nat.add_mul k 1 b
omega
omega

lemma le_ceilDiv {x b m : ℕ} (hb : 0 < b) (h : m * b ≤ x + b - 1) : m ≤ ceilDiv x b := by
unfold ceilDiv
exact (Nat.le_div_iff_mul_le hb).2 h

lemma valid_cases {d : ℕ} (hd : ValidAmount d) : d / SCALE = 900000 ∨ d / SCALE = 3000000 := by
rcases hd with rfl | rfl <;> simp [D_DEFAULT, D_LARGE, SCALE]

lemma tokens_le {d : ℕ} (hd : ValidAmount d) : d / SCALE ≤ 3000000 := by
rcases valid_cases hd with h | h <;> omega

lemma tokens_pos {d : ℕ} (hd : ValidAmount d) : 1 ≤ d / SCALE := by
rcases valid_cases hd with h | h <;> omega

/-! ## 1. Dividing by TOKEN_SCALE loses nothing -/

/-- tokens_to_lp / TOKEN_SCALE is exact for both allowed amounts, so the pool is sized from the
full amount stored on the launch. -/
theorem scale_exact {d : ℕ} (hd : ValidAmount d) : d / SCALE * SCALE = d := by
rcases hd with rfl | rfl <;> simp [D_DEFAULT, D_LARGE, SCALE]

/-! ## 2. No u128 overflow, underflow or division by zero -/

/-- Every intermediate u128 value in the liquidity expression stays in range, and the divisor
is nonzero, for any sqrt_price ≤ 2^86. sqrt_price_lt_2_pow_75 shows the real sqrt_price is
below 2^75, a 2048× margin. -/
theorem no_u128_overflow {d s : ℕ} (hd : ValidAmount d) (hs : s ≤ 2 ^ 86) :
MAX * (d / SCALE) < U128 ∧
s < MAX ∧
quotient d s * SCALE < U128 ∧
liquidity d s < U128 := by
have ht := tokens_le hd
have hMAX : (2 : ℕ) ^ 86 < MAX := by simp [MAX]
have hsub : MAX - 2 ^ 86 ≤ MAX - s := by omega
have hq : quotient d s ≤ MAX * 3000000 / (MAX - 2 ^ 86) := by
unfold quotient
exact Nat.div_le_div (Nat.mul_le_mul_left _ ht) hsub (by simp [MAX])
have hC : MAX * 3000000 / (MAX - 2 ^ 86) = 3002932 := by simp [MAX]
rw [hC] at hq
refine ⟨?, by omega, ?, ?_⟩
· calc MAX * (d / SCALE) ≤ MAX * 3000000 := Nat.mul_le_mul_left _ ht
_ < U128 := by simp [MAX, U128]
· calc quotient d s * SCALE ≤ 3002932 * SCALE := Nat.mul_le_mul_right _ hq
_ < U128 := by simp [SCALE, U128]
· unfold liquidity
calc quotient d s * SCALE * s ≤ 3002932 * SCALE * 2 ^ 86 :=
Nat.mul_le_mul (Nat.mul_le_mul_right _ hq) hs
_ < U128 := by simp [SCALE, U128]

/-! ## 3. Meteora never takes more than the launch minted for it -/

/-- Meteora pulls at most tokens_to_lp base tokens, despite rounding up. -/
theorem meteora_le_amount {d s : ℕ} (hd : ValidAmount d) (hs0 : 0 < s) (hs : s < MAX) :
meteoraTokenA (liquidity d s) s ≤ d := by
set t := d / SCALE
set w := MAX - s with hw
have hw0 : 0 < w := by omega
have hb : 0 < s * MAX := Nat.mul_pos hs0 (by simp [MAX])
have hqw : quotient d s * w ≤ MAX * t := Nat.div_mul_le_self _ _
have h : meteoraTokenA (liquidity d s) s ≤ t * SCALE := by
apply ceilDiv_le hb
change quotient d s * SCALE * s * w ≤ t * SCALE * (s * MAX)
calc quotient d s * SCALE * s * w = quotient d s * w * (SCALE * s) := by ring
_ ≤ MAX * t * (SCALE * s) := Nat.mul_le_mul_right _ hqw
_ = t * SCALE * (s * MAX) := by ring
exact h.trans (le_of_eq (scale_exact hd))

/-! ## 4. The shortfall is less than one token -/

/-- Meteora pulls more than tokens_to_lp - 1 token, so at most 999_999 base units
(under one whole token) stay behind in the launch vault. -/
theorem meteora_dust_lt_one_token {d s : ℕ} (hd : ValidAmount d) (hs0 : 0 < s) (hs : s < MAX) :
d < meteoraTokenA (liquidity d s) s + SCALE := by
set t := d / SCALE with ht
set w := MAX - s with hw
set q := quotient d s with hq
have hw0 : 0 < w := by omega
have hwle : w ≤ MAX := by omega
have ht1 : 1 ≤ t := tokens_pos hd
have hb : 0 < s * MAX := Nat.mul_pos hs0 (by simp [MAX])
-- floor division loses less than one divisor: MAX * t < (q + 1) * w
have hlt : MAX * t < w * (q + 1) := Nat.lt_mul_div_succ _ hw0
-- hence (t - 1) * MAX < q * w
have key : (t - 1) * MAX < q * w := by
have : (t - 1) * MAX + MAX = MAX * t := by
rw [Nat.sub_mul, one_mul, Nat.sub_add_cancel (Nat.le_mul_of_pos_left _ ht1), mul_comm]
nlinarith
-- so the floored amount already exceeds (t - 1) * SCALE
have hmain : (t * SCALE - SCALE + 1) * (s * MAX) ≤ q * SCALE * s * w + s * MAX - 1 := by
have e1 : t * SCALE - SCALE = (t - 1) * SCALE := by rw [Nat.sub_mul, one_mul]
rw [e1]
have h2 : (t - 1) * SCALE * (s * MAX) < q * SCALE * s * w := by
have hSs : 0 < SCALE * s := Nat.mul_pos (by simp [SCALE]) hs0
calc (t - 1) * SCALE * (s * MAX) = (t - 1) * MAX * (SCALE * s) := by ring
_ < q * w * (SCALE * s) := Nat.mul_lt_mul_of_pos_right key hSs
_ = q * SCALE * s * w := by ring
have : ((t - 1) * SCALE + 1) * (s * MAX) = (t - 1) * SCALE * (s * MAX) + s * MAX := by ring
omega
have hge := le_ceilDiv hb hmain
have hd' : d = t * SCALE := (scale_exact hd).symm
have hTS : SCALE ≤ t * SCALE := Nat.le_mul_of_pos_left _ ht1
have hA : meteoraTokenA (liquidity d s) s = ceilDiv (q * SCALE * s * w + 0) (s * MAX) := rfl
rw [hA]
simp only [Nat.add_zero] at hA ⊢
omega

/-! ## 5. The amount fits Meteora's u64 check and is nonzero -/

theorem meteora_amount_in_range {d s : ℕ} (hd : ValidAmount d) (hs0 : 0 < s) (hs : s < MAX) :
0 < meteoraTokenA (liquidity d s) s ∧ meteoraTokenA (liquidity d s) s < U64 := by
have h1 := meteora_le_amount hd hs0 hs
have h2 := meteora_dust_lt_one_token hd hs0 hs
have hd' : SCALE ≤ d ∧ d < U64 := by
rcases hd with rfl | rfl <;> simp [D_DEFAULT, D_LARGE, SCALE, U64]
omega

/-! ## 6. The launch vault still covers participants after completion -/

/-- initialize_launch mints perf + add + 10M + 2M + d base tokens into the launch vault.
complete_launch sends 2M to the futarchy AMM and Meteora pulls meteoraTokenA. What stays is
at least what verify_vaults requires: 10M + additional_tokens_amount + performance_package_token_amount. So the Meteora step can never trip InvariantViolated or
take tokens owed to participants. -/
theorem vault_covers_claims {d s perf add : ℕ} (hd : ValidAmount d) (hs0 : 0 < s) (hs : s < MAX) :
let P := 10000000 * SCALE
let F := 2000000 * SCALE
let minted := perf + add + P + F + d
P + add + perf ≤ minted - F - meteoraTokenA (liquidity d s) s := by
have := meteora_le_amount hd hs0 hs
intro P F minted
omega

/-! ## 7. The real sqrt_price is far below the 2^86 overflow bound -/

set_option maxRecDepth 4000 in
/-- complete_launch computes sqrt_price = (sqrt(raise / 10M tokens) * 2^64) as u128 with
raise : u64, so raise < 2^64 = 18446744073709551616. Over the reals, sqrt_price is below
2^75 for every possible raise. f64 rounding changes it by a relative error near 2^-52, far
less than the factor of 2048 of headroom before 2^86. -/
theorem sqrt_price_lt_2_pow_75 (raise : ℕ) (h : raise < 18446744073709551616) :
Real.sqrt ((raise : ℝ) / (10000000 * 10 ^ 6)) * 2 ^ 64 < 2 ^ 75 := by
have hr : (raise : ℝ) < 2 ^ 64 := by
have := (Nat.cast_lt (α := ℝ)).mpr h
norm_num at this ⊢
exact this
clear h
have hx : (raise : ℝ) / (10000000 * 10 ^ 6) < 2048 ^ 2 := by
rw [div_lt_iff₀ (by positivity)]
linarith [show (2 : ℝ) ^ 64 < 2048 ^ 2 * (10000000 * 10 ^ 6) by norm_num]
have hsq : Real.sqrt ((raise : ℝ) / (10000000 * 10 ^ 6)) < 2048 := by
rw [Real.sqrt_lt' (by positivity)]
exact hx
calc Real.sqrt ((raise : ℝ) / (10000000 * 10 ^ 6)) * 2 ^ 64 < 2048 * 2 ^ 64 := by
exact mul_lt_mul_of_pos_right hsq (by positivity)
_ = 2 ^ 75 := by norm_num

end LaunchpadLiquidity


</details>

_Posted from an Amp security review thread. The Lean project is not committed to this branch._

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.

3 participants