Skip to content

Decimal normalization for production-scale deposits #42

Description

@hoddukzoa12

Summary

Q64.64 checked_mul overflow로 인해 현재 asset당 최대 ~740 토큰만 예치 가능.
Instruction handler 경계에서 decimal 정규화를 적용해 ~17.5억 토큰/asset 까지 확장.

Problem

  • initialize_pool에서 w_norm_squared()r² = radius.checked_mul(radius) 호출
  • deposit D에 대해 r ≈ D × 2.366 (n=3), 가 Q64.64 i128 범위 초과
  • 현재 workaround: deposit을 100 토큰으로 축소 (devnet bootstrap)

Solution: Decimal Normalization

온체인 instruction handler 진입/퇴출 경계에서 변환:

진입 (raw → normalized)

// instruction handler 시작부
let normalized = raw_amount / 10u64.pow(decimals as u32);
// FixedPoint 연산은 "1 token = 1.0" 단위로 수행

퇴출 (normalized → raw)

// instruction handler 종료부  
let raw_output = normalized_result * 10u64.pow(decimals as u32);

영향 받는 instruction handlers (4곳)

  1. initialize_poolinitial_deposit_per_asset 정규화
  2. add_liquidity — deposit amount 정규화
  3. swap — input amount 정규화, output amount 역정규화
  4. remove_liquidity — withdrawal amount 역정규화

내부 수학 변경 없음

  • fixed_point.rs, sphere.rs, torus.rs 등 수학 모듈은 그대로 유지
  • 기존 155 unit + 25 integration 테스트 영향 최소

Capacity After Fix

Decimals Max tokens/asset TVL 환산 (USD)
6 (USDC) ~1.75B ~$1.75B
9 ~1.75M N/A

Acceptance Criteria

  • 4개 instruction handler에 decimal normalization 적용
  • 1M 토큰 deposit으로 initialize_pool 성공
  • 기존 cargo test 전체 통과
  • 새 통합 테스트: large deposit → swap → withdraw round-trip

Context

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1-highCore feature, important for MVPctx:orbital-coreAMM 수학 엔진 (Sphere invariant, Tick, Swap)layer:mathFixed-point arithmetic, invariant computationlayer:smart-contractAnchor/Rust on-chain programtype:featureNew functionality implementation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions