Skip to main content

A Pythonic SDK for the f(x) Protocol

Project description

f(x) Protocol Python SDK

Creator: Christopher Stampar (@cstampar)
Date: December 20, 2025

A Pythonic, production-grade SDK for interacting with the f(x) Protocol. This client abstracts Web3.py boilerplate and provides human-readable interfaces for both V1 and V2 products.

โœจ Key Features

  • Modular Design: Clean separation of read and write operations.
  • Precision First: Uses Python Decimal for all human-readable amounts.
  • Full Protocol Coverage: Supports fxUSD, fETH, rUSD, veFXN, Gauges, and all x tokens (xETH, xCVX, xWBTC, xeETH, xezETH, xstETH, xfrxETH).
  • Infrastructure-Aware: Direct integration with the Pool Manager, Treasury, and Multi-Path Converter.
  • V1 & V2 Support: Robust support for both legacy and current protocol versions.
  • Convex Finance Integration: Complete vault management, staking, and rewards for 30+ f(x) Protocol pools.
  • Curve Finance Integration: Pool operations, swaps, liquidity management, and gauge staking.

๐Ÿš€ Quick Start

Read-Only Mode (No Private Key Required)

from fx_sdk.client import ProtocolClient

# Initialize in read-only mode
client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY"
)

# Read balance
fxusd_balance = client.get_fxusd_balance()
print(f"fxUSD Balance: {fxusd_balance}")

# Fetch protocol NAV
nav = client.get_treasury_nav()
print(f"Base NAV: {nav['base_nav']}")

Write Mode (Secure Authentication Options)

The SDK supports multiple secure methods for wallet authentication. Never hardcode private keys in your scripts!

Option 1: Environment Variable (Recommended for Production)

# Set in your shell or deployment environment
export FX_PROTOCOL_PRIVATE_KEY="0x..."
from fx_sdk.client import ProtocolClient

client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY"
    # Private key automatically loaded from FX_PROTOCOL_PRIVATE_KEY
)

Option 2: .env File (Recommended for Local Development)

Create a .env file in your project root:

FX_PROTOCOL_PRIVATE_KEY=0x...
from fx_sdk.client import ProtocolClient

client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY"
    # Private key automatically loaded from .env file
)

Important: Add .env to your .gitignore to prevent committing secrets!

Option 3: Google Colab Secrets

from fx_sdk.client import ProtocolClient

# Store your private key in Colab secrets: 'fx_protocol_private_key'
client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY"
    # Private key automatically loaded from Colab secrets
)

Option 4: Browser Wallet (MetaMask, etc.)

from fx_sdk.client import ProtocolClient

client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY",
    use_browser_wallet=True  # Connects to MetaMask or other browser extension
)

# Transactions will prompt user approval in browser
tx_hash = client.mint_f_token(market_address="0x...", base_in=1.0)

Option 5: Explicit Private Key (Not Recommended)

# โš ๏ธ Only use for testing! Never commit this to version control.
client = ProtocolClient(
    rpc_url="https://mainnet.infura.io/v3/YOUR_API_KEY",
    private_key="0x..."  # Explicit parameter (lowest priority)
)

Authentication Priority Order

The SDK checks for credentials in this order:

  1. Explicit private_key parameter
  2. FX_PROTOCOL_PRIVATE_KEY environment variable
  3. .env file (FX_PROTOCOL_PRIVATE_KEY)
  4. Google Colab secret (fx_protocol_private_key)
  5. Browser wallet (if use_browser_wallet=True)

๐Ÿ“‚ Project Structure

fx_api/
โ”œโ”€โ”€ fx_sdk/              # Core SDK package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ client.py        # Main ProtocolClient class
โ”‚   โ”œโ”€โ”€ constants.py     # Contract addresses and configurations
โ”‚   โ”œโ”€โ”€ utils.py          # Unit conversion utilities
โ”‚   โ”œโ”€โ”€ exceptions.py    # Custom exception classes
โ”‚   โ””โ”€โ”€ abis/            # Contract ABIs (JSON files)
โ”œโ”€โ”€ tests/               # Test suite
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ tests.py         # Main unit tests
โ”‚   โ”œโ”€โ”€ test_sdk.py      # SDK integration tests
โ”‚   โ””โ”€โ”€ ...              # Additional test files
โ”œโ”€โ”€ docs/                # Documentation
โ”‚   โ”œโ”€โ”€ features.md     # Detailed feature list
โ”‚   โ”œโ”€โ”€ roadmap.md      # Development roadmap
โ”‚   โ””โ”€โ”€ ...             # Additional documentation
โ”œโ”€โ”€ scripts/             # Utility scripts
โ”‚   โ””โ”€โ”€ upload_pypi.sh  # PyPI upload helper
โ”œโ”€โ”€ README.md            # This file
โ”œโ”€โ”€ setup.py            # Package setup
โ”œโ”€โ”€ pyproject.toml      # Modern Python project config
โ”œโ”€โ”€ requirements.txt    # Python dependencies
โ””โ”€โ”€ LICENSE             # MIT License

For detailed SDK capabilities, see docs/features.md.

๐Ÿ›  Project Status & Progress

The SDK is currently in a feature-complete state for initial release.

Done:

  • Scaffolding & Core Architecture
  • Data Normalization (Wei <-> Decimal)
  • Contract Registry (V1 & V2)
  • Governance & veFXN support
  • Gauge & Reward claiming
  • V2 Pool Manager (Operate, Liquidate, Rebalance)
  • V1 Treasury & Market support
  • Multi-Path Converter & stETH Gateway
  • Rebalance Pool (Stability Pool) support
  • Integrated ABIs for all core components
  • Convex Finance Integration (v0.2.0): Vault management, staking, APY calculations, 30+ pools
  • Curve Finance Integration (v0.2.0): Swaps, liquidity, gauge staking, pool discovery

๐Ÿ”’ Security Best Practices

  1. Never commit private keys to version control. Always use .gitignore to exclude .env files.
  2. Use environment variables in production deployments (AWS, Heroku, etc.).
  3. Use .env files for local development, but never commit them.
  4. Use Colab secrets when working in Google Colab notebooks.
  5. Use browser wallets for interactive applications where users control their own keys.
  6. Read-only mode is available for analytics and monitoring without any credentials.

๐Ÿ“ Documentation

For a full list of features and supported products, see docs/features.md.

โš–๏ธ License

MIT

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

fx_sdk-0.4.0.tar.gz (92.4 kB view details)

Uploaded Source

Built Distribution

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

fx_sdk-0.4.0-py3-none-any.whl (95.5 kB view details)

Uploaded Python 3

File details

Details for the file fx_sdk-0.4.0.tar.gz.

File metadata

  • Download URL: fx_sdk-0.4.0.tar.gz
  • Upload date:
  • Size: 92.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.2

File hashes

Hashes for fx_sdk-0.4.0.tar.gz
Algorithm Hash digest
SHA256 bf6d555fa6744698882f0438e6aebac46f8345f0f1baffb354f0e8ed8296642f
MD5 3be401165ed7f03c2e90aa84d716719f
BLAKE2b-256 74ea1d82338c869b56c0221be2b658386d18ccd400d321690190ec6be0b0b851

See more details on using hashes here.

File details

Details for the file fx_sdk-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: fx_sdk-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 95.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.2

File hashes

Hashes for fx_sdk-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50c85014fb37b34aee47951edfda8f41268c5ce0a714d1362093feba391ac0e4
MD5 bf04a07a7985a170752a1e377b0fc82e
BLAKE2b-256 c00f09bc4e179166a6d6d5dda8d7e5a7072a7e8c42a01eae41d061c20db54601

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