Package: dexteritysdk
Dexterity client SDKDexterityis a derivatives decentralized exchange running on Solana.
This package provides the basic blocks in order to integrate and trade on Dexterity.You can learn more about Dexterityhere.PrerequisitesSolana.pyCreating the clientFirst, an instance of a Solana client is required.fromdexteritysdk.dex.sdk_contextimportSDKContext,SDKTraderfromsolana.rpc.apiimportClientfromsolana.keypairimportKeypairfromsolana.pubkeyimportPubkeydefmain():# use "https://api.mainnet-beta.solana.com" for mainnetnetwork="https://api.devnet.solana.com"client=Client(network)A Keypair for the payer has to also be provided.keypair=Keypair.from_secret_key(keypair_bytes)The SDK also requires theMarket Product Group (MPG)that we're going to trade on.The public key of the default MPG isHiCy6vzuN3yLXD3z35D6nV7bzNLcyrvGLf3uSKuutSLofor mainnet orHyWxreWnng9ZBDPYpuYugAfpCMkRkJ1oz93oyoybDFLBfor devnet.# mainnet MPGmpg=Pubkey("HiCy6vzuN3yLXD3z35D6nV7bzNLcyrvGLf3uSKuutSLo")# ** OR **# devnet MPGmpg=Pubkey("HyWxreWnng9ZBDPYpuYugAfpCMkRkJ1oz93oyoybDFLB")Now, we're ready to get an instance ofSDKContextfor this MPG.ctx=SDKContext.connect(client=client,market_product_group_key=mpg,payer=keypair,raise_on_error=True)Registering new trader / TRGIf you're a new trader, creation of a trading account is required.trader=ctx.register_trader(keypair)trg_key=trader.accountTheTrader Risk Group (TRG)is your trading account, that you can now use.Using an existing TRGIf you already have a trader set up, you need to provide the TRG public key for it.
The SDK allows you to list all your registered TRGs.# this will return a list of your all your TRG public keys# for example: [HeykeQWRh6DC2Tz5X3WBuWdMHicyECDEFGMjomV6LBye, HeyZNJ9gQVAEqHeCFFQ781E53d66DATKXHeynwnCFBye]trg_keys=ctx.list_trader_risk_groups()After finding your TRG, you can initialise theSDKTraderinstance.fromdexteritysdk.codegen.dex.typesimportTraderRiskGroupfromdexteritysdk.program_idsimport*fromdexteritysdk.utils.solanaimportexplore# ...trader=SDKTrader.connect(ctx,trg_pubkey,keypair)Funding the TRGTo start trading, you need to deposit funds.
The tokens deposited have to be MPG vault mint tokens (e.g. USDC).
The wallet you provided will be debited.# this will deposit 733.1 USDCtrader.deposit(ctx,733.1)You can also withdraw in a similar fashion.# this will withdraw 0.1 USDC back to your wallettrader.withdraw(ctx,0.1)You can check your balance at any time.trg,_=trader.get_trader_risk_group()trg.cash_balanceTrading on DexterityListing trading products and order booksTheproductsfield of aSDKContextinstance, is a list of the available trading products (SDKProduct).
EverySDKProducthas anameand aget_orderbook(ctx)function that will return its order book (SDKOrderBook).
TheSDKOrderBookcontains allSDKOrderson each side of the book.Here's an example:forproductinctx.products:book=product.get_orderbook(ctx,refresh=True)print(f"Printing the order book of{product.name}")fororderinbook.bids:print(f"Bid of size{order.qty}at{order.price}")fororderinbook.asks:print(f"Ask of size{order.qty}at{order.price}")Placing an orderPlacing an order is straightforward.fromdexteritysdk.utils.aobimportSide# ...order_summary=trader.place_order(ctx,product,Side.ASK,size,price)The returnedSDKOrderSummarycontains theorder_idof the new order, as well as theremaining_qtyandfilled_qtyif your order has been filled. If the order is immediately fully filled,order_idwill beNone.If there's any error, an exception will be raised.Cancelling ordersTo cancel a previous order you can calltrader.cancel(ctx, product, order_id).
Following the previous example:trader.cancel(ctx,product,order_summary.order_id)You can also cancel all your open orders for multiple products.# products is a list of SDKProduct# if products list is empty or None, then all your open orders will be cancelledproducts=[ctx.products[0],ctx.products[2]]trader.cancel_all_orders(ctx,products)If any cancel fails, an exception will be raised.PositionsPositions can also be listed, by iterating thetrader_positionsarray:trg,_=trader.get_trader_risk_group()trg.trader_positions