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.17.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.17-py3-none-any.whl (9.1 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: avantis_trader_sdk-0.8.17.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.17.tar.gz
Algorithm Hash digest
SHA256 c765651fe61485d3574360bac54c4723e49d42a9f78659f8168833dc0f9c2606
MD5 507dd7c2c87dcb4a9d834ec513fd090c
BLAKE2b-256 b17a98aa36d441f04007f1d607d82bcc9d2d6846053754a30db68404e4f0540a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for avantis_trader_sdk-0.8.17-py3-none-any.whl
Algorithm Hash digest
SHA256 ca914be9c52942060d194e06e795c802fc883139f04089a6d60aeeeb8bb5aa22
MD5 6c174273242230a5066bc10538963d27
BLAKE2b-256 97b40f9825cc9ec4d4f42349f9790c6c4096613749812b6a19adffe292b561bd

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