Skip to main content

SDK for interacting with Avantis trading contracts.

Project description

Avantis Trader SDK

Python SDK for trading on Avantis - a perpetual trading platform on Base.

Installation

pip install avantis-trader-sdk

Quick Start

import asyncio
from avantis_trader_sdk import TraderClient
from avantis_trader_sdk.types import TradeInput, TradeInputOrderType

async def main():
    # Initialize client
    trader_client = TraderClient("https://mainnet.base.org")
    trader_client.set_local_signer("0xYOUR_PRIVATE_KEY")
    
    trader = trader_client.get_signer().get_ethereum_address()
    
    # Check and approve USDC allowance
    allowance = await trader_client.get_usdc_allowance_for_trading(trader)
    if allowance < 100:
        await trader_client.approve_usdc_for_trading(100)
    
    # Open a 10x long ETH position with $100 collateral
    trade_input = TradeInput(
        trader=trader,
        pair_index=1,  # ETH/USD
        collateral_in_trade=100,
        is_long=True,
        leverage=10,
        tp=5000,  # take profit
        sl=2500,  # stop loss
    )
    
    tx = await trader_client.trade.build_trade_open_tx(
        trade_input, TradeInputOrderType.MARKET, slippage_percentage=1
    )
    receipt = await trader_client.sign_and_get_receipt(tx)
    print("Trade opened!", receipt.transactionHash.hex())

asyncio.run(main())

Get Open Trades

trades, pending_orders = await trader_client.trade.get_trades(trader)

for trade in trades:
    print(f"Pair: {trade.trade.pair_index}, Leverage: {trade.trade.leverage}x")
    print(f"Entry: {trade.trade.open_price}, Liq: {trade.liquidation_price}")

Close a Trade

trade = trades[0]
close_tx = await trader_client.trade.build_trade_close_tx(
    pair_index=trade.trade.pair_index,
    trade_index=trade.trade.trade_index,
    collateral_to_close=trade.trade.collateral_in_trade,
    trader=trader,
)
await trader_client.sign_and_get_receipt(close_tx)

AI-Assisted Development

Building with AI tools? We provide optimized documentation:

  • AGENT.md - Comprehensive guide for AI agents. Copy to your project or paste into AI chat.
  • .cursorrules - Auto-loaded by Cursor IDE.
curl -o AGENT.md https://raw.githubusercontent.com/Avantis-Labs/avantis_trader_sdk/main/AGENT.md

Resources

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

avantis_trader_sdk-0.8.15.tar.gz (8.7 MB view details)

Uploaded Source

Built Distribution

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

avantis_trader_sdk-0.8.15-py3-none-any.whl (9.1 MB view details)

Uploaded Python 3

File details

Details for the file avantis_trader_sdk-0.8.15.tar.gz.

File metadata

  • Download URL: avantis_trader_sdk-0.8.15.tar.gz
  • Upload date:
  • Size: 8.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for avantis_trader_sdk-0.8.15.tar.gz
Algorithm Hash digest
SHA256 b8c6cce8c443b331c665257697aec1725cc5a2caff2474a1b9df172742170673
MD5 76934551edccf951e0e52528e8c784ef
BLAKE2b-256 e67ed62f797512e0dc5779c8e655c87bf5e03e0c7d9a9a17641a4b3f960478d7

See more details on using hashes here.

File details

Details for the file avantis_trader_sdk-0.8.15-py3-none-any.whl.

File metadata

File hashes

Hashes for avantis_trader_sdk-0.8.15-py3-none-any.whl
Algorithm Hash digest
SHA256 664e8bc5893560d67dbb89350a7d6149426ffc05a58678c17fc5e02c4c3bdff0
MD5 72cc3b44980d57d2c79b7adf8271ecfa
BLAKE2b-256 62fa27483168ae4007de9a670875b7dae687261b11f62e21c32d89d1c85cc4b3

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