qie-forge/packages/python/tests/test_amm_math.py
caelum0x 0e758bf547 feat: Python/Rust SDKs, deeper Solidity tests, live frontend on Vercel
Phase 2 of the QIE Forge build (owner directive: go deeper, ship a real
public app). Nothing here replaces or removes the phase-1 contracts/SDK —
this adds to it.

- Solidity: 25 -> 35 Foundry tests. New coverage: 3-pool multi-hop
  routing, swapTokensForExactTokens, oracle adapter edge cases
  (decimals-too-large, feed rotation, staleness-disable), and vault
  liquidation edge cases (zero-debt, over-repay, partial liquidation,
  sequential liquidators).
- packages/python: a real web3.py SDK mirroring @qie-forge/core
  (chain/oracle/qusdc/explorer/amm/vault). 23/23 tests pass, including
  2 integration tests that deploy the actual compiled contract
  bytecode to a local anvil devnet and prove the SDK's reads/quotes
  match real on-chain state exactly.
- packages/rust: a real ethers-rs SDK with an abigen!-generated
  binding for the corrected oracle ABI. 13/13 tests pass (9 offline,
  4 live against real testnet/mainnet). No anvil-integration test yet
  (honest gap, documented).
- apps/web: a Next.js 16 + viem frontend, deployed live and public at
  https://qie-forge.vercel.app. Server-rendered home page calls
  @qie-forge/core directly for real testnet/mainnet block numbers and
  real oracle prices on every request. /swap and /vault show an
  honest "not deployed yet" state, proven correct against a real local
  anvil deployment (the API route's quote matched a direct cast call
  to the deployed router exactly) before confirming that empty state
  is what ships today.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-27 00:09:34 +03:00

47 lines
1.9 KiB
Python

"""Pure math tests — cross-checked by hand against the same 30bps
constant-product formula QieForgeLibrary.sol and QieForgePair.sol implement
on-chain (see packages/contracts/test/QieForgePair.t.sol's equivalent
Solidity assertions for the same numbers)."""
import pytest
from qie_forge.amm import AmmQuoteError, get_amount_in, get_amount_out, quote
def test_get_amount_out_matches_hand_calculation():
# reserves 10_000/10_000, amountIn=100 -> same numbers used in the
# Solidity test_swap_respectsConstantProductWithFee test.
amount_in = 100 * 10**18
reserve_in = 10_000 * 10**18
reserve_out = 10_000 * 10**18
amount_in_with_fee = amount_in * 9970
expected = (amount_in_with_fee * reserve_out) // (reserve_in * 10_000 + amount_in_with_fee)
assert get_amount_out(amount_in, reserve_in, reserve_out) == expected
assert expected < amount_in # fee + slippage means strictly less than 1:1
def test_get_amount_out_get_amount_in_are_consistent():
reserve_in, reserve_out = 50_000 * 10**18, 20_000 * 10**18
amount_in = 1_000 * 10**18
amount_out = get_amount_out(amount_in, reserve_in, reserve_out)
# Integer floor division on both legs means the round trip isn't exact —
# the same characteristic any constant-product-with-fee integer formula
# has. Recovering the input for that exact output should land within a
# few wei of the original, never systematically short-changing the pool
# by more than rounding error.
recovered_in = get_amount_in(amount_out, reserve_in, reserve_out)
assert abs(recovered_in - amount_in) <= 2
def test_quote_is_proportional():
assert quote(100, 1000, 4000) == 400
def test_rejects_non_positive_inputs():
with pytest.raises(AmmQuoteError):
get_amount_out(0, 1000, 1000)
with pytest.raises(AmmQuoteError):
get_amount_in(1000, 1000, 500) # amountOut >= reserveOut
with pytest.raises(AmmQuoteError):
quote(0, 1000, 1000)