Python SDK
Python SDK: https://pypi.org/project/gfx-perp-sdk/ This SDK contains 3 classes to interact with the GooseFX on-chain perpetual futures.
Perp
Product
Trader
Perp
The Perp
class is required to initialize the connection and wallet that is going to be used for subsequent interaction.
Constructor
Methods
init()
Initializes the Perp instance by setting up the market product group and related data.
Returns: None
(Can raise exceptions for error handling)
place_order(product, order_type, trade_side, size, price)
Places an order in the perpetual market.
Returns: None
(Implementation-dependent, can return order details or raise exceptions)
Initializing the Perp
class should be the first step irrespective of the type of operation in the following manner:
Product
An instance of the product
class signifies one of the perp product we offer to trade.
Constructor
Methods
The Product class primarily serves as a data structure and does not contain specific methods for operations.
Initialization of the product
class can be done in one of two ways:
By index:
By name:
This product
instance will be useful for the following functions:
GET L2 Orderbook
: Get the latest layer 2 orderbook
GET L3 Orderbook
: Get the latest layer 3 orderbook. (Orders mapped to users)
Trader
The Trader
class is required to get instructions to send transactions to the program. Each wallet must have a unique trader account initialized to be able to place orders and deposit funds. This account needs to be created once using the create_trader_account_ixs
instruction. After it has been created once, for all subsequent interactions by the wallet, the Trader
class needs to be initialized using the init
function.
Constructor
Methods
place_order()
Places an order in the market.
Returns: Varies depending on implementation, typically order details or confirmation.
cancel_order(order_id)
Returns: Varies depending on implementation, typically cancellation confirmation.
update_order(order_id, new_size, new_price)
Updates an existing order.
Returns: aries depending on implementation, typically update confirmation.
To create a new
Trader
account on-chain:
where ixs
is an array of required instructions and signers
is an array of required wallet pairs for signature. The wallet must also sign the transaction along with the wallet pairs in the signers
array
Once the account is created successfully, the
Trader
instance must be initialized in the following way:
Fractional Datatype
The Fractional data type uses a simple formula to represent a fractional number based on its mantissa (m) and exponent (exp): number = mantissa / (10 ^ exponent)
Trader Instructions
Deposit Funds
To start placing new orders, traders need to deposit some collateral. This instruction will transfer the required USDC from the wallet to the trader account which will be used as collateral to place new orders.
The only parameter to this function is the amount of USDC to be deposited.
Withdraw Funds
Similar to deposit funds, this function takes the amount of USDC to be withdrawn as the only parameter. This instruction will transfer funds from the trader account to the wallet address.
NOTE: The above two instructions do not need a product
instance as a parameter since the market is cross-collateralized and the amount of USDC deposited can be used across products. The following two instructions to place a new order and cancel an order are specific to products and hence need a product
instance as one of the parameters.
Trader's open orders for a product
To get all open orders for a Trader
for a product
:
New Order
The New order instruction needs the following as parameters
Quantity (Fractional) Please note: 1 unit of the product is denoted by 1 * 100000 units. So to buy 1 unit, the parameter to pass as quantity should be
Price (Fractional)
Order side ('buy' or 'sell')
Order Type ('limit', 'market', 'immediateOrCancel', 'postOnly')
Product instance
Cancel Order
The cancel order instruction needs the orderId in string format to cancel the order. Use getOpenOrders()
to get open orders and its id's to pass as a parameter to cancel the order
Get All Trader Risk Group Accounts
get_all_trg_accounts()
will fetch all the trader accounts in your wallet
Withdraw Funds for Trader Risk Group
withdraw_funds_ix_for_trg()
will withdraw funds from specific trg(trader) account
Close Trader Risk Group
close_trader_risk_group_ix_for_trg()
will close a specific trg(trader) account (ideally it can be done after withdrawing funds)
Get Cash Balance
get_cash_balance()
will fetch available balances of the main trader account.
Get Cash Balance for Trader Risk Group
get_cash_balance_for_trg()
will fetch available balance of specific trader account
Get Deposited Amount
get_deposited_amount()
will fetch available deposited amounts
Get Withdrawn Amount
get_withdrawn_amount()
will fetch available withdrawn amounts
Get Trader Positions by Product Index
get_trader_positions_by_product_index()
will get all trader positions by product index
Get Trader Positions by Product Name
get_trader_positions_by_product_name()
will get all trader positions by product name
Get Trader Positions by Trader Risk Group
get_trader_positions_for_trg()
will get all trader positions for the Trader Risk Group
Checkout https://github.com/GooseFX1/gfx-perps-python-sdk/blob/dev/test_perp.py for examples on the above functionalities! Happy trading!
Last updated