Package: mango-explorer-v4
mango-explorer-v4Python client library for interacting with Mango Markets V4.Installationmango-explorer-v4is available as aPython package on PyPIand can be installed as:pip install mango-explorer-v4UsageAssuming that you have a SOL wallet already set up, visithttps://app.mango.marketsto create a Mango account and fund it so that you can place orders.importasynciofrommango_explorer_v4.mango_clientimportMangoClientfromsolana.keypairimportKeypairfrombase58importb58decodeasyncdefmain():mango_client=awaitMangoClient.connect()# General data functions:print(mango_client.symbols())print(awaitmango_client.orderbook_l2('SOL/USDC'))print(awaitmango_client.fills('SOL-PERP'))# It is possible to livestream both orderbook & fills, look for incremental_*.py in the examples folder# Fill in your Mango account public key e.g 9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2mango_account=awaitmango_client.get_mango_account('PUBLIC_KEY')# You can look up any Mango account using# https://app.mango.markets/?address=9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2print(awaitmango_client.balances(mango_account))print(awaitmango_client.equity(mango_account))# Account Value# Fill in output from Phantom's "Export Private Key" e.g 2pvKRVh ... 1fL5qGqkeypair=Keypair.from_secret_key(b58decode('SECRET_KEY'))# Place a limit orderprint(awaitmango_client.place_order(mango_account,keypair,'SOL/USDC','bid',10,0.1,'limit'))# Place an oracle pegged perp order: https://docs.mango.markets/mango-markets/oracle-peg-ordersprint(awaitmango_client.place_perp_pegged_order(mango_account,keypair,'SOL-PERP','bid',price_offset=-5,# Will always be $5 under oracle pricepeg_limit=10,# If the oracle price moves $10 or more, the order will expirequantity=1))# Cancel all ordersprint(awaitmango_client.cancel_all_orders(mango_account,keypair,'SOL-PERP'))# There's a simple quoter, using atomic cancel-replace in examples/market_maker.pyasyncio.run(main())Running the examplesPlease make sure you're running mango-explorer-v4@0.7.3 or above - these instructions don't apply to older versions.You can find a collection of example codehere. These are to be run as command-line tools, with the simplest being thesymbols retrieval function, executable as:python-mmango_explorer_v4.examples.symbols# Output:# [#     {#         'name': 'SOL/USDC',#         'type': 'spot',#         'base_currency': 'SOL',#         'quote_currency': 'USDC',#         'maker_fees': ...,#         'taker_fees': ...#     }#     ... all order symbols# ]Examples may take arguments, which are specified in their source files. Theequity functionfor example, takes a Mango account primary key, whilst theorder book retrievaltakes a symbol & (optional) depth. These can be specified in the shell like this:python-mmango_explorer_v4.examples.equity--mango-account9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2# Outputs the Account Balance at https://app.mango.markets/?address=9XJt2tvSZghsMAhWto1VuPBrwXsiimPtsTR8XwGgDxK2python-mmango_explorer_v4.examples.orderbook_l2--symbolSOL-PERP--depth5# Outputs the first 5 orders in the book at https://app.mango.markets/trade?name=SOL-PERPSome transactions, like order placement, require a keypair. OnPhantom walletit can be fetched as follows:https://user-images.githubusercontent.com/28162761/233431861-5db448c1-fdf9-4dd0-8df9-156b131dfd71.movWith the keypair at hand you can place orders using:python-mmango_explorer_v4.examples.place_order\--keypair[KEYPAIR]\--mango-account[MANGO_ACCOUNT]\--symbolSOL-PERP\--sidebids\--price1\--size0.1And cancel them later as:python-mmango_explorer_v4.examples.cancel_all_orders\--keypair[KEYPAIR]\--mango-account[MANGO_ACCOUNT]\--symbolSOL-PERPSupportSupport is available on theMango Markets Discord server- post in the#dev-discussionchannel for any questions or feature requests.