Orderly Open API Connector Python is a connector to Orderly open API
Note: This connector is for Orderly EVM. It is not compatible with Orderly NEAR.
pip install orderly-evm-connector
https://orderly.network/docs/build-on-evm/building-on-evm
Usage examples:
from orderly_evm_connector.rest import Rest as Client
from orderly_evm_connector.lib.utils import get_account_info
(
orderly_key,
orderly_secret,
orderly_account_id,
orderly_testnet,
wallet_secret,
wss_id,
) = get_account_info('config.ini')
client = Client(
orderly_key=orderly_key,
orderly_secret=orderly_secret,
orderly_account_id=orderly_account_id,
orderly_testnet=True,
timeout=5
)
# Orders APIs
response = client.create_order(
symbol="PERP_NEAR_USDC",
order_type="LIMIT",
side="BUY",
order_price=1.95,
order_quantity=1,
)
Please find examples
folder in github to check for more endpoints.
config.ini
with your keys.# examples/config.ini
[keys]
orderly_key=ed25519:xxxx
orderly_secret=ed25519:xxxx
orderly_account_id=0xaaaa
orderly_testnet=False
wallet_secret=xxxx
wss_id=ClientID
debug=False
Setting the debug=True
will log the request URL, payload and response text.
Requests to Orderly API needs to be signed using orderly-key
and orderly-secret
.
Orderly Network uses the ed25519
elliptic curve standard for request authentication. The lib.utils
class provides methods for signing and generating request signatures.
from orderly_evm_connector.lib.utils import generate_signature
orderly_secret = "YOUR_ORDERLY_SECRET_HERE"
# A normalized orderly request string, see Authentication section of the Orderly API Doc for details
request_str = """1649920583000POST/v1/order{"symbol": "SPOT_NEAR_USDC", "order_type": "LIMIT", "order_price": 15.23, "order_quantity": 23.11, "side": "BUY"}"""
sginature = generate_signature(orderly_secret, request_str)
Once connected, the websocket server sends a ping frame every 10 seconds and is asked to return a response pong frame within 1 minute. This package automatically handles pong responses.
Once the connection is abnormal, the websocket connection tries a maximum of 30 times every 5s(WEBSOCKET_RETRY_SLEEP_TIME = 5
,WEBSOCKET_FAILED_MAX_RETRIES = 30
). After the connection is established, the subscription is completed again
When creating a Rest or Websocket client, set the orderly_testnet
parameter to true to use Testnet.
orderly_testnet = True
# Private Websocket Client on Testnet
wss_client = WebsocketPrivateAPIClient(
orderly_testnet=orderly_testnet,
orderly_account_id=orderly_account_id,
wss_id=wss_id,
orderly_key=orderly_key,
orderly_secret=orderly_secret,
on_message=message_handler,
on_close=on_close,
)
# Private REST API Client
client = Client(
orderly_key=orderly_key,
orderly_secret=orderly_secret,
orderly_account_id=orderly_account_id,
orderly_testnet=orderly_testnet,
timeout=5
)
There are 2 types of error returned from the library:
orderly_evm_connector.error.ClientError
4XX
, it's an issue from client side.status_code
- HTTP status codeerror_code
- Server's error code, e.g. -1102
error_message
- Server's error message, e.g. Unknown order sent.
header
- Full response header.error_data
* - Additional detailed data which supplements the error_message
.
cancelReplace
orderly_evm_connector.error.ServerError
5XX
, it's an issue from server side.
In addition, there are 3 types of Parameter Error:orderly_evm_connector.error.ParameterRequiredError
orderly_evm_connector.error.ParameterValueError
order_type
parameter is an ENUM with a fixed set of values users could pass in. Passing a value out of the ENUM definition will result in this error.orderly_evm_connector.error.ParameterTypeError
WebsocketClientError
Orderly has two Websocket endpoints, the Market Data Base Endpoint(public endpoint) and the Private User Data Stream Base Endpoint(private endpoint).
orderly-connector
supports connecting to both endpoints in both Mainnet and Testnet. See below for example:
from orderly_evm_connector.lib.utils import get_account_info
import time, logging
from orderly_evm_connector.websocket.websocket_api import WebsocketPublicAPIClient
(
orderly_key,
orderly_secret,
orderly_account_id,
orderly_testnet,
wallet_secret,
wss_id,
) = get_account_info('config.ini')
def on_close(_):
logging.info("Do custom stuff when connection is closed")
def message_handler(_, message):
logging.info(message)
# Public websocket does not need to pass orderly_key and orderly_secret arguments
wss_client = WebsocketPublicAPIClient(
orderly_testnet=orderly_testnet,
orderly_account_id=orderly_account_id,
wss_id=wss_id,
on_message=message_handler,
on_close=on_close,
debug=True,
)
wss_client.get_24h_tickers()
time.sleep(1000)
wss_client.stop()
For private endpoint, user will need to pass in the orderly_key
and orderly_secret
to the orderly.websocket.WebsocketPrivateAPIClient
class.
Private endpoint also requires signature of the message sent using orderly_key
and orderly_secret
. This function is encapsulated by the WebsocketPrivateAPIClient
class. See Orderly API Docs for more detai.
from orderly_evm_connector.websocket.websocket_api import WebsocketPrivateAPIClient
wss_client = WebsocketPrivateAPIClient(
orderly_testnet=orderly_testnet,
orderly_account_id=orderly_account_id,
wss_id=wss_id,
orderly_key=orderly_key,
orderly_secret=orderly_secret,
on_message=message_handler,
on_close=on_close,
debug=True,
)
# wss_client.get_liquidator_liquidations()
wss_client.get_notifications()
time.sleep(1000)
wss_client.stop()
wss_id
is the request id of included in each of websocket request to orderly. This is defined by user and has a max length of 64 bytes.
Contributions are welcome.
If you've found a bug within this project, please open an issue to discuss what you would like to change.
If it's an issue with the API, please open a topic at [Orderly Developer Community]()