Package: gfx-perp-sdk
GooseFX Perpetual Futures SDKWebsite|Docs|Discord|Telegram|MediumGooseFX Perpetual Futures SDKThis SDK contains 3 classes to interact with the GooseFX on-chain perpetual futures.PerpProductTraderThePerpclass is required to initialise the connection and wallet that is going to be used for subsequent interaction.
Initialising thePerpclass should be the first step irrespective of the type of operation in the following manner:perp=Perp(rpc_client,'devnet',wallet)perp.init()ProductAn instance of theproductclass signfies one of the perp product we offer to trade. Initialization of theproductclass can be done in one of two ways:By index:perp=Perp(rpc_client,'devnet',wallet)perp.init()product=Product(perp)product.initByIndex(0)By name:perp=Perp(rpc_client,'devnet',wallet)perp.init()product=Product(perp)product.initByName('SOL-PERP')Thisproductinstance will be useful for the following functions:GET L2 Orderbook: Get the latest layer 2 orderbookorderbook=product.get_orderbook_L2()GET L3 Orderbook: Get the latest layer 3 orderbook. (Orders mapped to users)orderbook=product.get_orderbook_L3()TraderTheTraderclass 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 thecreate_trader_account_ixsinstruction. After it has been created once, for all subsequent interactions by the wallet, theTraderclass needs to be initialized using theinitfunction.To create a newTraderaccount on-chain:perp=Perp(rpc_client,'devnet',wallet)perp.init()trader=Trader(perp)[ixs,signers]=trader.create_trader_account_ixs()whereixsis an array of required instructions andsignersis an array of required walletairs for signature. The wallet must also sign the transaction along with the walletairs in thesignersarrayOnce the account is created successfully, theTraderinstance must be initialised in the following way:perp=Perp(rpc_client,'devnet',wallet)perp.init()trader=Trader(perp)trader.init()Fractional DatatypeThe 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 InstructionsDeposit FundsTo 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 depositted.perp=Perp(rpc_client,'devnet',wallet)perp.init()trader=Trader(perp)trader.init()[ix,signers]=trader.deposit_funds_ix(Fractional.to_decimal(100))Withdraw FundsSimilar 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.perp=Perp(rpc_client,'devnet',wallet)perp.init()trader=Trader(perp)trader.init()[ix,signers]=awaittrader.withdraw_funds_ix(Fractional.to_decimal(100))NOTE: The above two instructions do not need aproductinstance 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 aproductinstance as one of the parameters.Trader's open orders for a prouctTo get all open orders for aTraderfor aproduct:perp=Perp(rpc_client,'devnet',wallet)perp.init()product=Product(perp)product.initByIndex(0)trader=Trader(perp)trader.init()orderbookData=trader.getOpenOrders(product)New OrderThe New order instruction needs the following as parametersQuantity (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 beFractional.to_decimal(100000)Price (Fractional)Order side ('buy' or 'sell')Order Type ('limit', 'market', 'immediateOrCancel', 'postOnly')Product instanceperp=Perp(rpc_client,'devnet',wallet)perp.init()product=Product(perp)product.initByIndex(0)trader=Trader(perp)trader.init()[ix,signers]=trader.new_order_ix(product,Fractional.to_decimal(50000),Fractional.to_decimal(35),Side.ASK,OrderType.Limit)Cancel OrderThe cancel order instruction needs the orderId in string format to cancel the order. UsegetOpenOrders()to get open orders and its id's to pass as a parameter to cancel the orderperp=Perp(rpc_client,'devnet',wallet)perp.init()product=Product(perp)product.initByIndex(0)trader=Trader(perp)trader.init()[ix,signers]=trader.cancel_order_ix(product,269375752548498747818049433352371)# Get this order id from t.get_open_orders()Checkouthttps://github.com/GooseFX1/gfx-perps-python-sdk/blob/dev/test_perp.pyfor examples on the above functionalities!Subscribe to asksThe subscribe to asks feature needs a callback function which can process added_asks which tracks the new asks at new prices and size changes which tracks if the size of ask is changeddefon_ask_change(updated_asks):print("Updated Asks:",updated_asks)perp=Perp(rpc_client,'devnet',keyp)perp.init()product=Product(perp)product.init_by_name('SOL-PERP')orderbook=product.get_orderbook_L2()awaitproduct.subscribe_to_asks(on_ask_change)Subscribe to bidsThe subscribe to bids feature needs a callback function which can process added_bids which tracks the new bids at new prices and size changes which tracks if the size of bid is changeddefon_bid_change(updated_bids):print("Updated Bids:",updated_bids)perp=Perp(rpc_client,'devnet',keyp)perp.init()product=Product(perp)product.init_by_name('SOL-PERP')orderbook=product.get_orderbook_L2()awaitproduct.subscribe_to_bids(on_bid_change)Checkouthttps://github.com/GooseFX1/gfx-perps-python-sdk/test_subscribe_accounts.pyfor examples on the subscription functionalities!Happy trading!