Skip to main content

Autonomous Futures Protocol Python SDK

Project description

Autonomous Futures Protocol Python SDK

Decentralized clearing and creation of Forecast Futures on any timeseries.

CI

Installation

This library is published on PyPI as the afp-sdk package. It can be installed in a virtualenv with:

pip install afp-sdk

Documentation

See afp.autonity.org for the Autonomous Futures Protocol documentation, including the Python SDK reference.

Authentication

The SDK supports 3 methods for authenticating with AutEx and signing blockchain transactions.

  • Private key: Use an Autonity account's private key as a hex-string with 0x prefix.
  • Key file: Use a Geth / Clef key file.
  • Trezor device: Use a Trezor hardware wallet. The derivation path of an Ethereum account needs to be specified, which can be found in the Account Settings in the Trezor Suite.

Overview

The afp package consists of the following:

  • afp top-level module: High-level API for interacting with the AFP Clearing System and the AutEx exchange.
  • afp.bindings submodule: Low-level API that provides typed Python bindings for the Clearing System smart contracts.

Configuration

By default the SDK communicates with the AFP Clearing System contracts on Autonity Mainnet, and the AutEx Exchange. Connection parameters can be overridden via the afp.AFP constructor and environment variables; see the documentation of the afp.AFP class for the available parameters.

Usage

Preparation

In order to trade in the AFP system, traders need to prepare the following:

  • The ID of a product to be traded.
  • The address of the product's collateral token.
  • An Autonity account for managing the margin account. It needs to hold a balance in ATN (for paying gas fee) and in the product's collateral token.
  • An Autonity account for signing intents and submitting trades to the exchange. The two accounts can be the same.
  • The address of an Autonity RPC provider. They can be found on Chainlist.

We can store those in the following constants (using random example IDs):

import os

PRODUCT_ID = "0x38d502bb683f53ec7c3d7a14b4aa47ac717659e121426131c0189c15bf4b9460"
COLLATERAL_ASSET = "0xD1A1e4035a164cF42228A8aAaBC2c0Ac9e49687B"
PRIVATE_KEY = os.environ["PRIVATE_KEY"]
AUTONITY_RPC_URL = "https://bakerloo.autonity-apis.com"

Configuration

An application instance can be created with the afp.AFP() constructor. An instance is associated with a trading venue and a margin account.

The required constructor arguments are the RPC provider URL and the authenticator of the blockchain account that manages the margin account.

An "authenticator" is a service that implements the afp.Authenticator protocol. Available options are afp.PrivateKeyAuthenticator that reads the private key from a constructor argument, and afp.KeyfileAuthenticator that reads the private key from an encrypted keyfile.

import afp

app = afp.AFP(
    rpc_url=AUTONITY_RPC_URL,
    authenticator=afp.PrivateKeyAuthenticator(PRIVATE_KEY),
)

Margin Account API

Margin accounts can be managed via the MarginAccount session object. It connects to the specified Autonity RPC provider and communicates with the Clearing System smart contracts.

margin_account = app.MarginAccount()

Collateral can be deposited into the margin account with margin_account.deposit().

from decimal import Decimal

margin_account.deposit(COLLATERAL_ASSET, Decimal("100.00"))
print(margin_account.capital(COLLATERAL_ASSET))

Trading API

Functions of the trading API can be accessed via the Trading session object. It communicates with the AutEx exchange and authenticates on creation with the intent account's private key. The intent account authenticator is optional, it defaults to the authenticator set in the AFP constructor.

trading = app.Trading()

To start trading a product, its parameters shall be retrieved from the server.

product = trading.product(PRODUCT_ID)

Intents can be created with trading.create_intent(). Intent creation involves hashing and signing the intent data. (The intent account's address is derived from the private key specified in the Trading constructor.)

from datetime import datetime, timedelta
from decimal import Decimal

intent = trading.create_intent(
    margin_account_id=MARGIN_ACCOUNT_ID,
    product=product,
    side="bid",
    limit_price=Decimal("1.23"),
    quantity=2,
    max_trading_fee_rate=Decimal("0.1"),
    good_until_time=datetime.now() + timedelta(hours=1),
)

The intent expressing a limit order can then be submitted to the exchange with trading.submit_limit_order() that returns the created order object.

order = trading.submit_limit_order(intent)
print(order)

The exchange then performs various checks to ensure that the order is valid. To ensure that the order has been accepted, its state can be polled with trading.order().

order = trading.order(order.id)
print(order.state)

Fills of orders submitted by the authenticated intent account can be queried with trading.order_fills().

fills = trading.order_fills(product_id=PRODUCT_ID)
print(fills)

Product API

Use the Product session object to validate a product schema, pin the specification to IPFS, and register it on-chain.

In order to use the product API for product building, connection parameters of an IPFS pinning service like Filebase should be included in the AFP constructor parameters.

IPFS_API_URL = "https://rpc.filebase.io"
IPFS_API_KEY = os.environ["IPFS_API_KEY"]

app = afp.AFP(
    rpc_url=AUTONITY_RPC_URL,
    authenticator=afp.PrivateKeyAuthenticator(PRIVATE_KEY),
    ipfs_api_url=IPFS_API_URL,
    ipfs_api_key=IPFS_API_KEY,
)

product = app.Product()

A JSON product specification can be parsed and validated with product.validate_json().

with open("product-spec.json") as spec_file:
    specification = product.validate_json(spec_file.read())

Alternatively, it can also be validated from a Python dictionary.

spec_dict = {
    "product": {...},
    "outcome_space": {...},
    "outcome_point": {...},
    "oracle_config": {...},
    "oracle_fallback": {...},
}
specification = product.validate(spec_dict)

Product specifications are stored at two different places, the PredictionProductV1 object is stored on-chain in the Product Registry contract, while the rest of the product specification is referred to as extended metadata and it is uploaded to IPFS.

The first step therefore is to upload the extended metadata of the product to IPFS and to pin the root node of the DAG. product.pin() returns a modified copy of the specification that incudes the extended metadata CID.

pinned_specification = product.pin(specification)

The product can then be registered with the Product Registry contract via a blockchain transaction.

tx = product.register(
    pinned_specification, initial_builder_stake=Decimal("10")
)

See further code examples in the examples directory.

Development

See DEVELOPMENT.md for developer documentation.

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

afp_sdk-0.7.0.tar.gz (202.7 kB view details)

Uploaded Source

Built Distribution

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

afp_sdk-0.7.0-py3-none-any.whl (87.0 kB view details)

Uploaded Python 3

File details

Details for the file afp_sdk-0.7.0.tar.gz.

File metadata

  • Download URL: afp_sdk-0.7.0.tar.gz
  • Upload date:
  • Size: 202.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for afp_sdk-0.7.0.tar.gz
Algorithm Hash digest
SHA256 2ed7c117d8608c3e0452313a5a1f8a7de8608bf9490009eebe12a214d74daa95
MD5 25351e62e594ade4fcfa7d6173fbe025
BLAKE2b-256 9764375ea52124f8cacf827be79841c5eef57e72d05a40168b0ca476b2dd74b2

See more details on using hashes here.

File details

Details for the file afp_sdk-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: afp_sdk-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 87.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for afp_sdk-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5619b0a0f895f7a9bf93f74a9256674681cdaa089f5858e68e80c7b34a6cbb0
MD5 3700d3d54130406bf53649f06510990a
BLAKE2b-256 7990cc7e66e9345ca726cd03e706c470e862f49d8ac6583c509256348f2c6d67

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