Skip to main content

Python SDK for XCCY Protocol - Interest Rate Swap AMM on Polygon

Project description

xccy-py

Python SDK for the XCCY Protocol — an Interest Rate Swap AMM on Polygon.

┌─────────────────────────────────────────────────────────────────┐
│                         xccy-py SDK                             │
├─────────────┬─────────────┬─────────────┬─────────────┬─────────┤
│   account   │   margin    │   trading   │  position   │  oracle │
└──────┬──────┴──────┬──────┴──────┬──────┴──────┬──────┴────┬────┘
       │             │             │             │           │
       ▼             ▼             ▼             ▼           ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Polygon (On-Chain)                           │
│  CollateralEngine │ VAMMManager │ OracleHub │ AprOracle         │
└─────────────────────────────────────────────────────────────────┘

Installation

pip install xccy

Quick Start

from xccy import XccyClient
from xccy.tokens import PolygonTokens

# Initialize client
client = XccyClient(
    rpc_url="https://polygon-rpc.com",
    private_key="0x...",  # Optional, for signing transactions
)

# Create account identifier
account = client.account.create_account_id(
    owner="0xYourWallet...",
    account_id=0,
    isolated_margin_token=None  # Cross-margin mode
)

# Deposit margin
tx = client.margin.deposit(
    account=account,
    token=PolygonTokens.USDC,
    amount=1000 * 10**6  # 1000 USDC
)
print(f"Deposit tx: {tx.transactionHash.hex()}")

# Check health
obligations = client.position.get_obligations(account)
margin_value = client.margin.get_total_value_usd(account)
health = margin_value / obligations if obligations > 0 else float('inf')
print(f"Health factor: {health:.2f}")

Features

Multi-Account System

One wallet can own multiple sub-accounts with independent positions and margin:

# Cross-margin account
main = client.account.create_account_id(owner="0x...", account_id=0)

# Isolated margin account (USDC only)
isolated = client.account.create_account_id(
    owner="0x...", 
    account_id=1,
    isolated_margin_token=PolygonTokens.USDC
)

Trading (Swap & LP)

from xccy.math import fixed_rate_to_tick, notional_to_liquidity

# Execute a swap (pay fixed rate)
result = client.trading.swap(
    pool_key=pool,
    account=account,
    notional=10_000 * 10**6,
    is_fixed_taker=True,
    tick_lower=-6930,
    tick_upper=-6900,
)

# Provide liquidity with notional amount
tick_lower = fixed_rate_to_tick(0.06)  # 6%
tick_upper = fixed_rate_to_tick(0.04)  # 4%
liquidity = notional_to_liquidity(10_000 * 10**6, tick_lower, tick_upper)

client.trading.mint(pool, account, tick_lower, tick_upper, liquidity)

Oracle Data

# Get USD price
price = client.oracle.get_price_usd(PolygonTokens.USDC)

# Get current APR
apr = client.oracle.get_apr(PolygonTokens.A_USDC)

# Get rate between timestamps
rate = client.oracle.get_rate_from_to(
    asset=PolygonTokens.A_USDC,
    from_timestamp=1704067200,
    to_timestamp=1735689600
)

Math Utilities

from xccy.math import (
    tick_to_fixed_rate,
    fixed_rate_to_tick,
    liquidity_to_notional,
    notional_to_liquidity,
    wad_to_decimal,
)

# Tick ↔ Rate conversions
rate = tick_to_fixed_rate(-6930)  # ~5% APR
tick = fixed_rate_to_tick(0.05)

# Liquidity ↔ Notional
liquidity = notional_to_liquidity(10_000 * 10**6, tick_lower, tick_upper)
notional = liquidity_to_notional(liquidity, tick_lower, tick_upper)

Documentation

Full documentation available at docs.xccy.finance

Development

# Clone and install
git clone https://github.com/xccy-finance/xccy-sdk.git
cd xccy-sdk
pip install -e ".[dev,docs]"

# Run tests
pytest

# Run linter
ruff check xccy/

# Type check
mypy xccy/

# Build docs
mkdocs serve

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xccy-0.1.1.tar.gz (66.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xccy-0.1.1-py3-none-any.whl (57.0 kB view details)

Uploaded Python 3

File details

Details for the file xccy-0.1.1.tar.gz.

File metadata

  • Download URL: xccy-0.1.1.tar.gz
  • Upload date:
  • Size: 66.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for xccy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 912df5635ded73cf204a4955ca7afb6c5c3dbd1846e48ab0d96a631c247d41a7
MD5 f544b34565a1373b6c94511bb407cad8
BLAKE2b-256 5b13a0cae34cb4b24583f5f2971624515345a48095e4e14fc83975cba397604c

See more details on using hashes here.

File details

Details for the file xccy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xccy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 57.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for xccy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93aee2fe7ae665dcbb3c17e560ee1e4131cc2cd4edb6c9627592d6b6a747fc84
MD5 e8fe29d6782bacf163dc4810bb566b73
BLAKE2b-256 f26febd05b0830df89943d205543f40a091f4d82e701d29930f8b52e94f9773f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page