SphereX
  • SPHEREX
    • SphereX
    • Why choose us
  • PRODUCT
    • Portfolio
    • Leverage Trading
    • Referral System
    • Activity Center
      • Lucky Spin
      • Betting Wheel
    • Help Center
  • $HERE
    • What is $HERE
    • Tokenomics
    • Token Empowerment
  • MECHANISM
    • TraderDao
    • Liquidation
    • Deflation
  • FOR DEVS
    • API
  • PROGRAM
    • Grants
    • Ambassador Program
    • Bug Bounty
  • COMMUNITY
    • Social Media
    • Media Kit
  • TUTORIAL
    • Trade
  • USER PROTOCOL
    • Terms of Service
    • Private Policy
  • OTHERS
    • SphereX Team
    • Roadmap
    • Audit
    • Careers
Powered by GitBook
On this page
  1. FOR DEVS

API

PreviousDeflationNextGrants

Last updated 7 months ago

How to setup

What are the api services

How to add a service

What are needed before running

  1. Creating a Virtual Environment

    sudo apt update
    sudo apt install python3.12-venv
    python3 -m venv /home/ubuntu/myenv
  2. Automatically Activating the Virtual Environment

nano ~/.bashrc
source /home/ubuntu/myenv/bin/activate #add this code at the end of .bashrc file
source ~/.bashrc
  1. Git Cloning the Repository under the Branch feat/standalone-signer

Ask the administrator for permission

  1. Initiating Setup Files in the Directory /spherex-openapi-demo

Copy

sudo -i 
pip install .
  1. Running Demos in the Directory /spherex-openapi-demo/tests

Substituting Your Testnet or Mainnet gRPC Path in Demo Files(ask the administrator for grpc paths)

GrpcPrivate("spherex-testnet-grpc-gateway-ex-***************.elb.ap-southeast-1.amazonaws.com", 9001,) 
  1. you can find all services

**grpcurl -plaintext spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001 list**
grpcurl -plaintext [spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001](<http://spherex-testnet-grpc-gateway-ex-5802c5bf04e19798.elb.ap-southeast-1.amazonaws.com:9001/>) describe spherex.trade.OrderServices 
  1. There are 22 services, for example

spherex.quote.QuoteService
spherex.trade.OrderService
spherex.trade.TradeService

For example, OrderServices(you can create batch order and cancel orders using this service)

Quote Services(you can find market data and account position using this service)

  1. Adding a service

If you want to get open/active orders by account, you can first find the catagory(Account/Order/Quote/Trade etc.) and the service, then add the service.

# OrderService
rpc CreateOrderBatch ( .spherex.trade.CreateOrderBatchRequest ) returns ( .spherex.trade.CreateOrderBatchResponse ) {
    option (.spherex.common.rpc_method_ext_opt) = { is_write_operation:true };
  }
# /spherex-openapi-demo/spherex/grpc_private.py
def order_batch_create(self, **kwargs):
    resp = grpc_client.call_method("OrderService", "CreateOrderBatch", **kwargs)
    return resp
# /spherex-openapi-demo/spherex/order.py
class SpherexOrder:
    def __init__(self, grpc_client):
        self.grpc_client = grpc_client
		
    def create_batch_order(self, order_params_list):
		    ...
		    return create_order_batch_resp

What needs to be prepared before running

  • VPN or A Server

  • A permission for a github repo

  • An valid grpc path with a whitelisted ip

  • Account_id/L2_keys

Batch order demo

  order_params_list = []
  for order in orders:
      side = order['side']
      size = str(order['quantity'])
      price = str(order['price'])
      limit_fee = "0.0300000"
      type = "LIMIT"
      time_in_force = "GOOD_TIL_CANCEL"
      reduce_only = False
      is_position_tpsl = False
      is_set_open_tp = False
      is_set_open_sl = False
      contract_id = 10000001

      order_params = OrderParam(
          market,
          register_account_id,
          contract_id,
          side,
          size,
          price,
          limit_fee,
          position_id,
          synthetic_resolution,
          quote_resolution,
          synthetic_id,
          collateral_id,
          type,
          time_in_force,
          reduce_only,
          is_position_tpsl,
          is_set_open_tp,
          is_set_open_sl,
      )
      order_params_list.append(order_params)

  order_resp = self.spherexOrder.create_batch_order(order_params_list)
  print(order_resp)

https://github.com/spherex-code/spherex-openapi-demo
https://github.com/spherex-code/spherex-protos/blob/main/protos/spherex/trade/order_rpc.proto
https://github.com/spherex-code/spherex-protos/blob/main/protos/spherex/quote/quote_rpc.proto