New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

python-infinity

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

python-infinity

Unofficial Infinity client packages

pipPyPI
Version
0.0.7
Maintainers
1

python-infinity

This is a Python3 connector for Infinity exchange's REST APIs and WebSocket.

Please note that the python-infinity package is an unofficial project and should be used at your own risk. It is NOT affiliated with the Infinity exchange and does NOT provide financial or investment advice.

Table of Contents

  • Overview
  • Features
  • Documentation
  • Quick Start
  • Example Usage
  • Disclaimer
  • Contact

Overview

The python-infinity package is a Python3 connector that allows you to interact with the Infinity Exchange. The package utilizes threads to handle concurrent execution of REST API requests and WebSocket subscriptions. This allows for efficient handling of multiple requests and real-time data streams without blocking the main execution thread.

The connector provides a REST client that allows you to make requests to all the available REST API endpoints of the Infinity exchange. You can perform actions such as retrieving user information, and more. It also includes a WebSocket client that enables you to subscribe to real-time data streams from the Infinity exchange. You can subscribe to channels like orderbook data, user order data etc.

To access private endpoints and perform actions on behalf of a user, the package includes a LoginClient class. This class handles the login process and manages the authentication using a JWT access token. You can create an instance of the LoginClient class and pass it to the REST or WebSocket client for private session usage.

This project is undergoing active development, ensuring that new changes to the Infinity API will be promptly integrated.

For feedback or suggestions, please reach out via one of the contact methods specified in Contact.

Features

  • REST API Handling
  • WebSocket Handling
  • Login Client
  • Thread-based Execution
  • Exception Handling and Reconnection
Release VersionChangelog
0.0.7Stable release for testnet (recommended)

Documentation

For more detailed information, please refer to the Infinity Exchange Docs.

Quick Start

Prerequisites

python-infinity is tested on python version: 3.11 to 3.11.6. Earlier or Later versions of Python might work, but they have not been thoroughly tested and could potentially conflict with the external web3 library.

python-infinity utilizes web3, threading, websocket-client and requests for its methods, alongside other built-in modules. As the package utilizes the web3 library, we suggest to use version >=6.0.0 here.

Installation

To get started with the python-infinity package, you can install it manually or via PyPI with pip:

pip install python-infinity

Known Issues

The getargspec Error (web3 version <= 5.31.4)

If you are using web3 version <=5.31.4 You may encounter the following error: ImportError: cannot import name 'getargspec' from 'inspect'

This error message is the result of a slight conflict between the various package versions we need.

We will continue to look for a more permanent solution to this. But for the time being, please do the following quick fix to resolve this issue.

You can update web3 version: >=6.0.0

Or apply fix on expressions.py

  • Find .../site-packages/parsimonious/expressions.py and double-click expressions.py to open it
  • Inside expressions.py file, change inspect import getargspec to inspect import getfullargspec
  • Error should be resolved, please run the code again

Example Usage

To be able to interact with private endpoints, an ethereum EOA account (wallet address) and the corresponding private key is required. We suggest to not include those information in your source code directly but alternatively store them in environment variables. Again, please note that the python-infinity package is an unofficial project and should be used at your own risk.

JWT access token handling is managed in this package for authentication. For interacting with private endpoints and private websocket, a login is required on the Infinity exchange side. If you are planning to interact with any private endpoints, please create an instance of the LoginClient class and parse it into the corresponding client (RestClient or WebSocketClient). If you want to only interact with public endpoints and public websocket, the login step 2. Create an instance of the LoginClient class can be skipped.

1. Import the required modules

# Infinity Exchange Login client
from infinity.login import LoginClient
# Infinity Exchange REST client
from infinity.rest_client import RestClient
# Infinity Exchange Websocket client
from infinity.websocket_client import WebSocketClient

2. Create an instance of the LoginClient class (Optional)

The login process will be carried out in the initializer of the LoginClient class. If you only interact with public session, this part can be skipped. Please refer to the documentation for information on how to use other parameters.

from infinity.login import LoginClient as InfinityLogin

infinity_login = InfinityLogin(rest_url="Infinity Exchange REST URL",
                               chain_id="Chain ID",
                               account_address=account_address,  # user wallet address
                               private_key=private_key)  # user private key

3. REST / WEBSOCKET client

3.1 REST client

Create a REST client instance, parse the infinity_login instance from 2. Create an instance of the LoginClient class for private endpoint usage. Please refer to the documentation for information on how to use other parameters.

from infinity.rest_client import RestClient as InfinityRestClient
# =============================================================================
# For public REST
# =============================================================================
infinity_public_rest = InfinityRestClient(rest_url="Infinity Exchange REST URL")
# =============================================================================
# For public and private REST
# =============================================================================
infinity_rest = InfinityRestClient(rest_url="Infinity Exchange REST URL",
                                   login=infinity_login)  # Infinity Exchange Login Client

Usage example:

# Public endpoint method example
market_info_floating_rate = infinity_rest.get_floating_rate(instrument_id="ETH-SPOT")

# Private endpoint method example
user_info = infinity_rest.get_user_info()

3.2 WEBSOCKET client

Create a WEBSOCKET client instance, parse the infinity_login instance from 2. Create an instance of the LoginClient class for private channel usage.

Please refer to the documentation for information on how to use other parameters.

from infinity.websocket_client import WebSocketClient as InfinityWebsocketClient

# =============================================================================
# For public WEBSOCKET channel
# =============================================================================
infinity_public_ws = InfinityWebsocketClient(ws_url="Infinity Exchange Websocket URL",
                                             auto_reconnect_retries=3)  # default is 0 which would disable reconnection retries
infinity_public_ws.run_all()
infinity_public_ws.disconnect_all_ws_connection()  # disconnect all the websocket connection
# =============================================================================
# For private and public WEBSOCKET channel
# =============================================================================
infinity_ws = InfinityWebsocketClient(ws_url="Infinity Exchange Websocket URL",
                                      login=infinity_login,  # Infinity Exchange Login Client
                                      auto_reconnect_retries=3)
infinity_ws.run_all()

infinity_ws.disconnect_all_ws_connection()  # disconnect all the websocket connection

Usage example - channel subscription / unsubscription:

# =============================================================================
# For public WEBSOCKET channel, e.g. orderbook data, recent trades
# =============================================================================

# Subscribing - recent trades
infinity_ws.subscribe_public_trades(instrument_id="USDC-SPOT")

# Unsubscribing - recent trades
infinity_ws.unsubscribe_public_trades(instrument_id="USDC-SPOT")

# Subscribing - orderbook data
infinity_ws.subscribe_orderbook(instrument_id="WBTC-2023-12-29")

# Unsubscribing - orderbook data
infinity_ws.unsubscribe_orderbook(instrument_id="WBTC-2023-12-29")

# =============================================================================
# For private WEBSOCKET channel, e.g. user order data, user trade data
# =============================================================================

# Subscribing - user order data
infinity_ws.subscribe_user_order(instrument_id="USDC-2023-11-03")

# Unsubscribing - user order data
infinity_ws.unsubscribe_user_order(instrument_id="USDC-2023-11-03")

# Subscribing - user trade data
infinity_ws.subscribe_user_trade(instrument_id="DAI-SPOT")

# Unsubscribing - user trade data
infinity_ws.unsubscribe_user_trade(instrument_id="DAI-SPOT")

Usage example - callbacks for extracting relevant data

while True:
    # =============================================================================
    # For public WEBSOCKET channel, e.g. orderbook data, recent trades
    # =============================================================================
    public_trade = infinity_ws.get_received_data(channel="recentTrades")
    orderbook = infinity_ws.get_received_data(channel="orderBook")

    # =============================================================================
    # For private WEBSOCKET channel, e.g. user order data, user trade data
    # =============================================================================

    user_order = infinity_ws.get_received_data(channel="userOrder")
    user_trade = infinity_ws.get_received_data(channel="userTrade")

Infinity Websocket package also provides functions to be overridden for processing websocket data stream.

def process_orderbook_data(self, orderbook: dict) -> None:
    pass # please refer to websocket client's process_orderbook_data
def process_public_trade(self, public_trade: dict) -> None:
    pass # please refer to websocket client's process_public_trade
def process_user_order(self, user_order: dict) -> None:
    pass # please refer to websocket client's process_user_order
def process_user_trade(self, user_trade: dict) -> None:
    pass # please refer to websocket client's process_user_trade

Error Codes

Infinity Exchange uses the following error codes:

Error codeDescription
1Success
2Permission Denied
9Unexpected Error
1000Pemission Denied
1001Validation Error
1002Operation Failed
9999Unexpected Error
10000Invalid Wallet (Deposit)
10001Invalid Token (Deposit)
10010Invalid Wallet (Withdraw)
10011Invalid Token (Withdraw)
10012Not Enough Funds to Withdraw
10020Not Enough Tokens To Liquidate
10021No Liquidation Steps Found
10022Liquidation Swap Failed

Order API error codes table

Error codeDescription
102INVALID_ACCOUNT
104TIMEOUT
107MINIMUM_QUANTITY_NOT_REACH
108NO_ORDER
109ORDER_DONE_OR_CANCELLED
110TRADE_SERVER_PARSE
111TRADE_SERVER_ORDER_BOOK_ERROR
112TRADE_SERVER_DB
113TRADE_SERVER_DB_CONNECTION
114MARKET_EXPIRED
115RATE_TRANSACTION_ERROR
120QUANTITY_STEP_NOT_MATCH
121NO_TWAP
122INVALID_DEDUPLICATE_STRING
191ORDER_AUTO_CANCELLED
192ORDER_MANUALLY_CANCELLED

Orders

Order Sides

BooleanSide
0 (False)Lend
1 (True)Borrow

Order Status Codes

Status CodeStatusDescription
0STATUS_PENDINGPending (i.e. Not yet sent to market)
1STATUS_ONBOOKOn Book (i.e. Live order in market)
10STATUS_DONEDone (i.e. Fully executed)
11STATUS_MANUALLY_CANCELLEDManually cancelled
12STATUS_AUTO_CANCELLEDAuto cancelled
13STATUS_PARTIALLY_FILLEDPartially Filled
14STATUS_EXPIREDMarket expired
99STATUS_ERRORError

Order Types

Type CodeMeaning
1Market Order
2Limit Order

Response Codes

CodeDescription
200OK
403Forbidden
500Internal Server Error or Invalid Response
503Service Unavailable

Token IDs

CodeNameToken IDQuantity StepMinimum QuantityRate Step
ETHETH10.000010.0010.0001
USDTUSDT20.0110.0001
USDCUSDC30.0110.0001
DAIDAI40.0110.0001
WBTCWrapped BTC50.0000010.00010.0001

Token Valuation Protocols

CodeProtocol
0No protocol / Not relevant
1Aave
2Uniswap
3Compound
4Curve

Transaction Types

CodeMeaning
1Borrow
2Lend
3Repay
4Unlend
5Interest
6Transfer In (Between Wallets)
7Transfer Out (Between Wallets)
8External Transfer In
9External Transfer Out
10Trade
11System Airdrop
12LP Fee
13Swap Secondary Token
14Withdrawal Fee
15Transaction Fee
16LP Setup In
17LP Setup Out
18LP Unwind In
19LP Unwind Out
20Liquidation Unlend

Disclaimer

This is an unofficial Python project (the "Package") and made available for informational purpose only and does not constitute financial, investment or trading advice. You acknowledge and agree to comply with the Terms of service at https://www.infinity.exchange/terms-conditions. If you do not agree, please do not use this package. Any unauthorised use of the Package is strictly prohibited.

Contact

For support, suggestions or feedback please reach out to us via the following email address pydevbuzz@gmail.com

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts