From 0fed0c544c48260dfe78eac16b436bb2309d7aef Mon Sep 17 00:00:00 2001 From: microwavedcola1 Date: Fri, 17 Nov 2023 17:09:42 +0100 Subject: [PATCH] mass rename Signed-off-by: microwavedcola1 --- .github/.dependabot.yml | 2 +- .gitignore | 16 +++--- .gitmodules | 3 -- README.md | 29 ++++++---- docker-compose.yml | 13 ++--- mango-bowl | 1 - .../.dockerignore | 0 .../.vscode/settings.json | 0 .../Dockerfile | 2 +- .../README.md | 10 ++-- .../package.json | 2 +- .../service-v3.postman_collection.json | 2 +- .../service-v3.yml | 2 +- .../src/account.controller.ts | 0 .../src/app.ts | 0 .../src/coin.controller.ts | 0 .../src/controller.interface.ts | 0 .../src/dtos.ts | 0 .../src/fills.ts | 0 .../src/mango.simple.client.ts | 2 +- .../src/markets.controller.ts | 0 .../src/orders.controller.ts | 0 .../src/positionsController.ts | 0 .../src/server.ts | 0 .../src/types.ts | 0 .../src/utils.ts | 0 .../src/wallet.controller.ts | 4 +- .../test/test.ts | 0 .../tsconfig.json | 0 .../tslint.json | 0 .../yarn.lock | 0 nginx.conf | 6 +-- py/README.md | 4 +- py/example1_basic.py | 44 +++++++-------- py/example1_cancel_all_orders.py | 10 ++-- py/example2_set_stink_bids.py | 22 ++++---- py/example3_market_maker.py | 25 +++++---- py/mango_service_v3_py/api.py | 8 +-- py/test-devnet.py | 54 +++++++++---------- py/test-mainnet.py | 34 ++++++------ 40 files changed, 144 insertions(+), 151 deletions(-) delete mode 160000 mango-bowl rename {mango-service-v3 => mango-service-v4}/.dockerignore (100%) rename {mango-service-v3 => mango-service-v4}/.vscode/settings.json (100%) rename {mango-service-v3 => mango-service-v4}/Dockerfile (80%) rename {mango-service-v3 => mango-service-v4}/README.md (76%) rename {mango-service-v3 => mango-service-v4}/package.json (97%) rename {mango-service-v3 => mango-service-v4}/service-v3.postman_collection.json (99%) rename {mango-service-v3 => mango-service-v4}/service-v3.yml (99%) rename {mango-service-v3 => mango-service-v4}/src/account.controller.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/app.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/coin.controller.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/controller.interface.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/dtos.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/fills.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/mango.simple.client.ts (99%) rename {mango-service-v3 => mango-service-v4}/src/markets.controller.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/orders.controller.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/positionsController.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/server.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/types.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/utils.ts (100%) rename {mango-service-v3 => mango-service-v4}/src/wallet.controller.ts (98%) rename {mango-service-v3 => mango-service-v4}/test/test.ts (100%) rename {mango-service-v3 => mango-service-v4}/tsconfig.json (100%) rename {mango-service-v3 => mango-service-v4}/tslint.json (100%) rename {mango-service-v3 => mango-service-v4}/yarn.lock (100%) diff --git a/.github/.dependabot.yml b/.github/.dependabot.yml index 64e335c..73fe69a 100644 --- a/.github/.dependabot.yml +++ b/.github/.dependabot.yml @@ -1,7 +1,7 @@ version: 2 updates: - package-ecosystem: npm - directory: "/mango-service-v3" + directory: "/mango-service-v4" schedule: interval: daily time: "01:00" diff --git a/.gitignore b/.gitignore index 9574ab5..330b8cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,12 @@ .idea __pycache__/ -mango-service-v3/node_modules -mango-service-v3/dist -mango-service-v3/run-dev-server.sh -mango-service-v3/run-dev-server-devnet.sh -mango-service-v3/run-build-and-publish-docker-image.sh -mango-service-v3/run-build-and-test-docker-locally.sh -mango-service-v3/run-update-api-docs.sh +mango-service-v4/node_modules +mango-service-v4/dist +mango-service-v4/run-dev-server.sh +mango-service-v4/run-dev-server-devnet.sh +mango-service-v4/run-build-and-publish-docker-image.sh +mango-service-v4/run-build-and-test-docker-locally.sh +mango-service-v4/run-update-api-docs.sh py/.venv py/.idea -py/mango_service_v3_py/__pycache__/ +py/mango_service_v4_py/__pycache__/ diff --git a/.gitmodules b/.gitmodules index 05a1e4c..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "mango-bowl"] - path = mango-bowl - url = https://github.com/tardis-dev/mango-bowl diff --git a/README.md b/README.md index 91595d2..bd8d12f 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,36 @@ # Introduction -REST and WEBSOCKET API Services for mango markets version 3, and some simple clients and examples. Aimed to follow spec as close as possible to popular exchanges like ftx, etc. Current motivation is to enable traders to bring their existing tools to mango markets. + +REST and WEBSOCKET API Services for mango markets version 3, and some simple clients and examples. Aimed to follow spec as close as possible to popular exchanges like ftx, etc. Current motivation is to enable traders to bring their existing tools to mango markets. # Note + REST Service requires the user to run a local copy with his/her own private key. An alternative approach which is known and was not taken is to prepare solana transactions in a centrally hosted REST API Service and send them back to the client for signing using their wallet. The advantages of this would be that we could have a centrally hosted service, and would save local hosting, the disadvantages of this would be complicating the REST clients users want to use with solana specific signing code and would need us to ship and maintain clients for various programming languages. Also such a centrally hosted service would then need authorization, authentication, rate limiting, etc. to prevent abuse of the configured RPC node, which so far is not the aim of this project. WEBSOCKET API currently streams L1, and L2 level orderbook data. # Documentation -See https://microwavedcola1.github.io/mango-service-v3/#tag/default + +See https://microwavedcola1.github.io/mango-service-v4/#tag/default Directory structure + ``` . ├── README.md -├── mango-service-v3 - REST API Service for mango markets version 3 -├── mango-bowl - WEBSOCKET API Service for L1, L2 orderbook data for mango markets version 3 +├── mango-service-v4 - REST API Service for mango markets version 3 └── py - python3 client for above REST API Service ``` # How to run -* `docker-compose up` starts the REST API Service and the WEBSOCKET API Service, and a ngninx reverse proxy -* The REST API is then available e.g. `curl http://localhost/api/wallet/balances` -* the WEBSOCKET API is then available e.g.`wscat --connect ws://localhost/ws` (note by default the websocket program when run in isolation is available at `ws://localhost/v1/ws` ) +- `docker-compose up` starts the REST API Service and the WEBSOCKET API Service, and a ngninx reverse proxy +- The REST API is then available e.g. `curl http://localhost/api/wallet/balances` +- the WEBSOCKET API is then available e.g.`wscat --connect ws://localhost/ws` (note by default the websocket program when run in isolation is available at `ws://localhost/v1/ws` ) # Todos + losely sorted in order of importance/priority + - rpc node related issues - ensure that order has been placed or definitely not placed on the server side - some off chain services are used, these might use other nodes, mixing data from various nodes, might be problematic, what if one node is behind? @@ -37,18 +42,20 @@ losely sorted in order of importance/priority - cleanup tsconfig.json - add pre commit tools e.g. husky/pre-commit for code formatting and linting -# Feedback +# Feedback + so far from beta tasters, from twitter, discord, etc. + - auto create mango account if none exists - deposit collateral using cross-chain bridges - new endpoints - - stop loss, + - stop loss, - market orders - modify order - funding payments -- advanced order types e.g. split +- advanced order types e.g. split - cache various mango related things which change infrequently like e.g. spot+perp markets, placed orders for user, etc. -- identify which endpoints are still slow, comparison with ftx, can use https://ftx.com/latency-stats, +- identify which endpoints are still slow, comparison with ftx, can use https://ftx.com/latency-stats, - how often to load/reload certain mango things e.g. account, cache, rootbanks, etc.? - integration with freqtrade and/or ccxt https://github.com/ccxt/ccxt/blob/master/js/ftx.js - integration with tradingview or https://github.com/thibaultyou/tradingview-alerts-processor/blob/master/docs/2_Alerts.md & https://www.tradingview.com/support/solutions/43000529348-about-webhooks/ diff --git a/docker-compose.yml b/docker-compose.yml index b8ff2ae..8ada98e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: "3.9" services: - mango-service-v3: - build: mango-service-v3 + mango-service-v4: + build: mango-service-v4 ports: - "3000:3000" volumes: @@ -11,16 +11,9 @@ services: - GROUP_NAME=mainnet.1 - CLUSTER_URL=${CLUSTER_URL} # configure custom RPC node - PRIVATE_KEY_PATH=/root/.config/solana/id.json # configure path to private keypair here - mango-bowl: - build: "mango-bowl" - ports: - - "8000:8000" - environment: - - MB_PORT=8000 - - MB_ENDPOINT=${CLUSTER_URL} # configure custom RPC node reverse-proxy: build: . ports: - "80:80" volumes: - - ./nginx.conf/:/etc/nginx/nginx.conf # custom config which unifies mango-service-v3 and mango-bowl \ No newline at end of file + - ./nginx.conf/:/etc/nginx/nginx.conf # custom config which unifies mango-service-v4 \ No newline at end of file diff --git a/mango-bowl b/mango-bowl deleted file mode 160000 index 7865688..0000000 --- a/mango-bowl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7865688be6b261adc7ffb8f67e579e7efd5ad74b diff --git a/mango-service-v3/.dockerignore b/mango-service-v4/.dockerignore similarity index 100% rename from mango-service-v3/.dockerignore rename to mango-service-v4/.dockerignore diff --git a/mango-service-v3/.vscode/settings.json b/mango-service-v4/.vscode/settings.json similarity index 100% rename from mango-service-v3/.vscode/settings.json rename to mango-service-v4/.vscode/settings.json diff --git a/mango-service-v3/Dockerfile b/mango-service-v4/Dockerfile similarity index 80% rename from mango-service-v3/Dockerfile rename to mango-service-v4/Dockerfile index 0e3dd5a..991433c 100644 --- a/mango-service-v3/Dockerfile +++ b/mango-service-v4/Dockerfile @@ -11,4 +11,4 @@ RUN yarn build ENV PORT=3000 -CMD ["pm2-docker", "start", "dist/server.js", "-i", "2", "-n", "mango-service-v3", "--max-memory-restart", "200M"] +CMD ["pm2-docker", "start", "dist/server.js", "-i", "2", "-n", "mango-service-4", "--max-memory-restart", "200M"] diff --git a/mango-service-v3/README.md b/mango-service-v4/README.md similarity index 76% rename from mango-service-v3/README.md rename to mango-service-v4/README.md index d750eb8..89eade7 100644 --- a/mango-service-v3/README.md +++ b/mango-service-v4/README.md @@ -1,5 +1,5 @@ -A REST API server on top of mango markets v3, written in typescript + expressjs + using mango client and some other off chain REST services. +A REST API server on top of mango markets v4, written in typescript + expressjs + using mango client and some other off chain REST services. # Environment variables All environment variables are optional. @@ -14,9 +14,9 @@ All environment variables are optional. * `yarn ts-node ./src/server.ts` or if you have `nodemon` installed then, `nodemon ./src/server.ts` # How to run using docker -* `docker pull microwavedcola/mango-service-v3` -* `docker run -p 3000:3000 -v ~/.config:/root/.config microwavedcola/mango-service-v3`, assumes private key to be present at ~/.config/solana/id.json +* `docker pull microwavedcola/mango-service-v4` +* `docker run -p 3000:3000 -v ~/.config:/root/.config microwavedcola/mango-service-v4`, assumes private key to be present at ~/.config/solana/id.json # How to test -* via postman, see `service-v3.postman_collection.json` -* python client, see https://github.com/microwavedcola1/mango-v3-service/blob/master/py/README.md +* via postman, see `service-v4.postman_collection.json` +* python client, see https://github.com/microwavedcola1/mango-v4-service/blob/master/py/README.md diff --git a/mango-service-v3/package.json b/mango-service-v4/package.json similarity index 97% rename from mango-service-v3/package.json rename to mango-service-v4/package.json index 1c85756..c7fae69 100644 --- a/mango-service-v3/package.json +++ b/mango-service-v4/package.json @@ -1,5 +1,5 @@ { - "name": "mango-service-v3", + "name": "mango-service-v4", "version": "0.0.1", "description": "REST API for trading against mango markets", "main": "./src/server.ts", diff --git a/mango-service-v3/service-v3.postman_collection.json b/mango-service-v4/service-v3.postman_collection.json similarity index 99% rename from mango-service-v3/service-v3.postman_collection.json rename to mango-service-v4/service-v3.postman_collection.json index ff5f80f..4a166d6 100644 --- a/mango-service-v3/service-v3.postman_collection.json +++ b/mango-service-v4/service-v3.postman_collection.json @@ -1,7 +1,7 @@ { "info": { "_postman_id": "6400c7b4-eaa6-4a86-b7df-19857192378e", - "name": "service-v3", + "name": "service-4", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ diff --git a/mango-service-v3/service-v3.yml b/mango-service-v4/service-v3.yml similarity index 99% rename from mango-service-v3/service-v3.yml rename to mango-service-v4/service-v3.yml index e635439..af95fc7 100644 --- a/mango-service-v3/service-v3.yml +++ b/mango-service-v4/service-v3.yml @@ -1,6 +1,6 @@ openapi: 3.0.0 info: - title: service-v3 + title: service-v4 version: 1.0.0 servers: - url: http://{{baseurl}} diff --git a/mango-service-v3/src/account.controller.ts b/mango-service-v4/src/account.controller.ts similarity index 100% rename from mango-service-v3/src/account.controller.ts rename to mango-service-v4/src/account.controller.ts diff --git a/mango-service-v3/src/app.ts b/mango-service-v4/src/app.ts similarity index 100% rename from mango-service-v3/src/app.ts rename to mango-service-v4/src/app.ts diff --git a/mango-service-v3/src/coin.controller.ts b/mango-service-v4/src/coin.controller.ts similarity index 100% rename from mango-service-v3/src/coin.controller.ts rename to mango-service-v4/src/coin.controller.ts diff --git a/mango-service-v3/src/controller.interface.ts b/mango-service-v4/src/controller.interface.ts similarity index 100% rename from mango-service-v3/src/controller.interface.ts rename to mango-service-v4/src/controller.interface.ts diff --git a/mango-service-v3/src/dtos.ts b/mango-service-v4/src/dtos.ts similarity index 100% rename from mango-service-v3/src/dtos.ts rename to mango-service-v4/src/dtos.ts diff --git a/mango-service-v3/src/fills.ts b/mango-service-v4/src/fills.ts similarity index 100% rename from mango-service-v3/src/fills.ts rename to mango-service-v4/src/fills.ts diff --git a/mango-service-v3/src/mango.simple.client.ts b/mango-service-v4/src/mango.simple.client.ts similarity index 99% rename from mango-service-v3/src/mango.simple.client.ts rename to mango-service-v4/src/mango.simple.client.ts index 0a34e19..a179b2e 100644 --- a/mango-service-v3/src/mango.simple.client.ts +++ b/mango-service-v4/src/mango.simple.client.ts @@ -406,7 +406,7 @@ class MangoSimpleClient { perpMarketConfig.baseDecimals, perpMarketConfig.quoteDecimals ); - // TODO: this is a workaround, mango-v3 has a assertion for price>0 for all order types + // TODO: this is a workaround, mango-v4 has a assertion for price>0 for all order types // this will be removed soon hopefully price = orderType !== "market" ? price : 1; return await this.client.placePerpOrder( diff --git a/mango-service-v3/src/markets.controller.ts b/mango-service-v4/src/markets.controller.ts similarity index 100% rename from mango-service-v3/src/markets.controller.ts rename to mango-service-v4/src/markets.controller.ts diff --git a/mango-service-v3/src/orders.controller.ts b/mango-service-v4/src/orders.controller.ts similarity index 100% rename from mango-service-v3/src/orders.controller.ts rename to mango-service-v4/src/orders.controller.ts diff --git a/mango-service-v3/src/positionsController.ts b/mango-service-v4/src/positionsController.ts similarity index 100% rename from mango-service-v3/src/positionsController.ts rename to mango-service-v4/src/positionsController.ts diff --git a/mango-service-v3/src/server.ts b/mango-service-v4/src/server.ts similarity index 100% rename from mango-service-v3/src/server.ts rename to mango-service-v4/src/server.ts diff --git a/mango-service-v3/src/types.ts b/mango-service-v4/src/types.ts similarity index 100% rename from mango-service-v3/src/types.ts rename to mango-service-v4/src/types.ts diff --git a/mango-service-v3/src/utils.ts b/mango-service-v4/src/utils.ts similarity index 100% rename from mango-service-v3/src/utils.ts rename to mango-service-v4/src/utils.ts diff --git a/mango-service-v3/src/wallet.controller.ts b/mango-service-v4/src/wallet.controller.ts similarity index 98% rename from mango-service-v3/src/wallet.controller.ts rename to mango-service-v4/src/wallet.controller.ts index 8386d15..51b2ae8 100644 --- a/mango-service-v3/src/wallet.controller.ts +++ b/mango-service-v4/src/wallet.controller.ts @@ -78,7 +78,7 @@ class WalletController implements Controller { mangoGroup.loadRootBanks(this.mangoSimpleClient.connection), ]); - ////// copy pasta block from mango-ui-v3 + ////// copy pasta block from mango-ui-v4 /* tslint:disable */ const balances: Balances[][] = new Array(); @@ -218,7 +218,7 @@ class WalletController implements Controller { const tokenIndex = mangoGroup.getTokenIndex(token.mintKey); const value = net.mul(mangoGroup.getPrice(tokenIndex, mangoCache)); /* tslint:enable */ - ////// end of copy pasta block from mango-ui-v3 + ////// end of copy pasta block from mango-ui-v4 // append balances for base symbols const balanceDtos = baseBalances.map((baseBalance) => { return { diff --git a/mango-service-v3/test/test.ts b/mango-service-v4/test/test.ts similarity index 100% rename from mango-service-v3/test/test.ts rename to mango-service-v4/test/test.ts diff --git a/mango-service-v3/tsconfig.json b/mango-service-v4/tsconfig.json similarity index 100% rename from mango-service-v3/tsconfig.json rename to mango-service-v4/tsconfig.json diff --git a/mango-service-v3/tslint.json b/mango-service-v4/tslint.json similarity index 100% rename from mango-service-v3/tslint.json rename to mango-service-v4/tslint.json diff --git a/mango-service-v3/yarn.lock b/mango-service-v4/yarn.lock similarity index 100% rename from mango-service-v3/yarn.lock rename to mango-service-v4/yarn.lock diff --git a/nginx.conf b/nginx.conf index 694de5f..7b49926 100644 --- a/nginx.conf +++ b/nginx.conf @@ -13,11 +13,7 @@ http { } upstream rest-api { - server mango-service-v3:3000; - } - - upstream ws-api { - server mango-bowl:8000; + server mango-service-v4:3000; } server { diff --git a/py/README.md b/py/README.md index d246c9a..72a7b0a 100644 --- a/py/README.md +++ b/py/README.md @@ -1,9 +1,9 @@ -A python3 client with some examples for the mango markets v3 REST Service +A python3 client with some examples for the mango markets v4 REST Service # Pre-requisites * you have [poetry](https://python-poetry.org/docs/#installation) installed -* you have the REST API service from `../mango-service-v3 running` locally +* you have the REST API service from `../mango-service-v4 running` locally # How to test examples while developing diff --git a/py/example1_basic.py b/py/example1_basic.py index 763c538..20bcc9b 100644 --- a/py/example1_basic.py +++ b/py/example1_basic.py @@ -1,41 +1,41 @@ -from mango_service_v3_py.api import MangoServiceV3Client -from mango_service_v3_py.dtos import PlaceOrder +from mango_service_v4_py.api import MangoServiceV4Client +from mango_service_v4_py.dtos import PlaceOrder if __name__ == "__main__": - mango_service_v3_client = MangoServiceV3Client() + mango_service_v4_client = MangoServiceV4Client() - for position in mango_service_v3_client.get_open_positions(): + for position in mango_service_v4_client.get_open_positions(): print(position.json(indent=4, sort_keys=True)) - for balance in mango_service_v3_client.get_balances(): + for balance in mango_service_v4_client.get_balances(): print(balance.json(indent=4, sort_keys=True)) - for market in mango_service_v3_client.get_markets(): + for market in mango_service_v4_client.get_markets(): print(market.json(indent=4, sort_keys=True)) print( - mango_service_v3_client.get_market_by_market_name("BTC-PERP").json( + mango_service_v4_client.get_market_by_market_name("BTC-PERP").json( indent=4, sort_keys=True ) ) - for order in mango_service_v3_client.get_orderbook("BTC-PERP"): + for order in mango_service_v4_client.get_orderbook("BTC-PERP"): print(order.json(indent=4, sort_keys=True)) - for trade in mango_service_v3_client.get_trades("BTC-PERP"): + for trade in mango_service_v4_client.get_trades("BTC-PERP"): print(trade.json(indent=4, sort_keys=True)) - for candle in mango_service_v3_client.get_candles( + for candle in mango_service_v4_client.get_candles( "BTC-PERP", 60, 1625922900, 1631214960 ): print(candle.json(indent=4, sort_keys=True)) - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) - for order in mango_service_v3_client.get_orders_by_market_name("BTC-PERP"): + for order in mango_service_v4_client.get_orders_by_market_name("BTC-PERP"): print(order.json(indent=4, sort_keys=True)) - mango_service_v3_client.place_order( + mango_service_v4_client.place_order( PlaceOrder( market="BTC-PERP", side="buy", @@ -48,14 +48,14 @@ if __name__ == "__main__": client_id=123, ) ) - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) - mango_service_v3_client.cancel_order_by_client_id("123") - for order in mango_service_v3_client.get_orders(): + mango_service_v4_client.cancel_order_by_client_id("123") + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) - mango_service_v3_client.place_order( + mango_service_v4_client.place_order( PlaceOrder( market="BTC/USDC", side="buy", @@ -68,13 +68,13 @@ if __name__ == "__main__": client_id=123, ) ) - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) - mango_service_v3_client.cancel_order_by_client_id("123") - for order in mango_service_v3_client.get_orders(): + mango_service_v4_client.cancel_order_by_client_id("123") + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) - mango_service_v3_client.cancel_all_orders() - for order in mango_service_v3_client.get_orders(): + mango_service_v4_client.cancel_all_orders() + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) diff --git a/py/example1_cancel_all_orders.py b/py/example1_cancel_all_orders.py index d5bd960..e8892cf 100644 --- a/py/example1_cancel_all_orders.py +++ b/py/example1_cancel_all_orders.py @@ -1,15 +1,15 @@ -from mango_service_v3_py.api import MangoServiceV3Client +from mango_service_v4_py.api import MangoServiceV4Client if __name__ == "__main__": - mango_service_v3_client = MangoServiceV3Client() + mango_service_v4_client = MangoServiceV4Client() print("orders before cancelling") - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) print("") - mango_service_v3_client.cancel_all_orders() + mango_service_v4_client.cancel_all_orders() print("orders after cancelling") - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(order.json(indent=4, sort_keys=True)) diff --git a/py/example2_set_stink_bids.py b/py/example2_set_stink_bids.py index 2bde52f..835a281 100644 --- a/py/example2_set_stink_bids.py +++ b/py/example2_set_stink_bids.py @@ -1,7 +1,7 @@ import time -from mango_service_v3_py.api import MangoServiceV3Client -from mango_service_v3_py.dtos import PlaceOrder +from mango_service_v4_py.api import MangoServiceV4Client +from mango_service_v4_py.dtos import PlaceOrder MARKET = "BTC-PERP" @@ -13,17 +13,17 @@ def fibonacci_of(n): if __name__ == "__main__": - mango_service_v3_client = MangoServiceV3Client() + mango_service_v4_client = MangoServiceV4Client() - market = mango_service_v3_client.get_market_by_market_name("BTC-PERP")[0] + market = mango_service_v4_client.get_market_by_market_name("BTC-PERP")[0] print(f"latest btc-perp price is {market.last}") - mango_service_v3_client.cancel_all_orders() + mango_service_v4_client.cancel_all_orders() - balances = mango_service_v3_client.get_balances() + balances = mango_service_v4_client.get_balances() total_usd_balance = sum([balance.usd_value for balance in balances]) - market = mango_service_v3_client.get_market_by_market_name(MARKET)[0] + market = mango_service_v4_client.get_market_by_market_name(MARKET)[0] lowest = 25 fibs = [fib for fib in [fibonacci_of(n) for n in range(10)] if fib < lowest][1:] @@ -31,14 +31,14 @@ if __name__ == "__main__": for i, fib in enumerate(fibs): price = market.last * ((100 - fibs[-1] + fib) / 100) - price = mango_service_v3_client.to_nearest(price, market.price_increment) + price = mango_service_v4_client.to_nearest(price, market.price_increment) size = (total_usd_balance / market.price) * (fibs[len(fibs) - 1 - i] / fibs_sum) - size = mango_service_v3_client.to_nearest(size, market.size_increment) + size = mango_service_v4_client.to_nearest(size, market.size_increment) if size < market.size_increment: continue print(f"setting order, price: {price}, size: {size}, value: {price * size}") - mango_service_v3_client.place_order( + mango_service_v4_client.place_order( PlaceOrder( market=MARKET, side="buy", @@ -51,5 +51,5 @@ if __name__ == "__main__": client_id=int(time.time()), ) ) - for order in mango_service_v3_client.get_orders(): + for order in mango_service_v4_client.get_orders(): print(f"set order at, price: {order.price}, size: {order.size}") diff --git a/py/example3_market_maker.py b/py/example3_market_maker.py index 09ffe67..522a0e1 100644 --- a/py/example3_market_maker.py +++ b/py/example3_market_maker.py @@ -9,8 +9,8 @@ from os.path import getmtime from tenacity import retry, wait_fixed, stop_after_delay, stop_after_attempt -from mango_service_v3_py.api import MangoServiceV3Client -from mango_service_v3_py.dtos import Side, PlaceOrder +from mango_service_v4_py.api import MangoServiceV4Client +from mango_service_v4_py.dtos import Side, PlaceOrder # based on https://github.com/BitMEX/sample-market-maker/blob/master/market_maker/market_maker.py @@ -44,7 +44,7 @@ def toNearest(num, tickDec): class MM: def __init__(self): - self.mango_service_v3_client = MangoServiceV3Client() + self.mango_service_v4_client = MangoServiceV4Client() self.market = None self.start_position_buy = None self.start_position_sell = None @@ -52,11 +52,11 @@ class MM: # todo unused @retry(stop=(stop_after_delay(10) | stop_after_attempt(5)), wait=wait_fixed(5)) - def retry_wrapper(self, mango_service_v3_client_method, *arg): - getattr(self.mango_service_v3_client, mango_service_v3_client_method)(arg) + def retry_wrapper(self, mango_service_v4_client_method, *arg): + getattr(self.mango_service_v4_client, mango_service_v4_client_method)(arg) def log_recent_trades(self) -> None: - trades = self.mango_service_v3_client.get_trades(MARKET) + trades = self.mango_service_v4_client.get_trades(MARKET) recent_trades = [ trade for trade in trades @@ -73,13 +73,13 @@ class MM: logger.info("") def get_ticker(self): - self.market = self.mango_service_v3_client.get_market_by_market_name(MARKET)[0] + self.market = self.mango_service_v4_client.get_market_by_market_name(MARKET)[0] self.start_position_buy = self.market.bid - self.market.price_increment self.start_position_sell = self.market.ask + self.market.price_increment self.positions = [ position - for position in self.mango_service_v3_client.get_open_positions() + for position in self.mango_service_v4_client.get_open_positions() if position.future == MARKET ] @@ -103,7 +103,7 @@ class MM: to_cancel = [] buys_matched = 0 sells_matched = 0 - existing_orders = self.mango_service_v3_client.get_orders() + existing_orders = self.mango_service_v4_client.get_orders() existing_orders = sorted(existing_orders, key=lambda order_: order_.price) buy_orders = sorted(buy_orders, key=lambda order_: order_.price) @@ -145,7 +145,7 @@ class MM: ) for order in to_cancel: try: - self.mango_service_v3_client.cancel_order_by_order_id(order.id) + self.mango_service_v4_client.cancel_order_by_order_id(order.id) except: pass logger.info("") @@ -178,7 +178,7 @@ class MM: f" |_ price {order.price}, side {order.side:4}, size {order.size}, value {order.price * order.size}" ) for order in to_create: - self.mango_service_v3_client.place_order( + self.mango_service_v4_client.place_order( PlaceOrder( market=MARKET, side=order.side, @@ -243,12 +243,11 @@ class MM: if __name__ == "__main__": - mm = MM() logger.info("cancelling all orders...") try: - mm.mango_service_v3_client.cancel_all_orders() + mm.mango_service_v4_client.cancel_all_orders() except Exception as e: logger.error(f"Exception: {e}") diff --git a/py/mango_service_v3_py/api.py b/py/mango_service_v3_py/api.py index 20b91b2..49743c3 100644 --- a/py/mango_service_v3_py/api.py +++ b/py/mango_service_v3_py/api.py @@ -6,7 +6,7 @@ from typing import List import httpx from pydantic import parse_obj_as -from mango_service_v3_py.dtos import ( +from mango_service_4_py.dtos import ( Position, Balance, Market, @@ -32,7 +32,7 @@ def delayed(seconds): return decorator -class MangoServiceV3Client: +class MangoServiceV4Client: def __init__(self, base_url=None, timeout=None): self.timeout = timeout if timeout else 10.0 if base_url: @@ -78,7 +78,9 @@ class MangoServiceV3Client: ) return parse_obj_as(List[Candle], json.loads(response.text)["result"]) - def get_orders(self,) -> List[Order]: + def get_orders( + self, + ) -> List[Order]: response = httpx.get(f"{self.BASE_URL}/orders", timeout=self.timeout) return parse_obj_as(List[Order], json.loads(response.text)["result"]) diff --git a/py/test-devnet.py b/py/test-devnet.py index c5a7d34..deb0552 100644 --- a/py/test-devnet.py +++ b/py/test-devnet.py @@ -1,27 +1,27 @@ import pytest -from mango_service_v3_py.api import MangoServiceV3Client -from mango_service_v3_py.dtos import PlaceOrder +from mango_service_v4_py.api import MangoServiceV4Client +from mango_service_v4_py.dtos import PlaceOrder @pytest.fixture -def mango_service_v3_client(): - return MangoServiceV3Client("http://localhost:3001/api", timeout=60.0) +def mango_service_v4_client(): + return MangoServiceV4Client("http://localhost:3001/api", timeout=60.0) @pytest.fixture(autouse=True) -def run_around_tests(mango_service_v3_client): +def run_around_tests(mango_service_v4_client): # cleanup - mango_service_v3_client.cancel_all_orders() - orders = mango_service_v3_client.get_orders() + mango_service_v4_client.cancel_all_orders() + orders = mango_service_v4_client.get_orders() assert len(orders) == 0 yield # teardown assert True -def place_order(mango_service_v3_client, market): - mango_service_v3_client.place_order( +def place_order(mango_service_v4_client, market): + mango_service_v4_client.place_order( PlaceOrder( market=market, side="buy", @@ -41,22 +41,22 @@ PERP_MARKET = [("BTC-PERP")] @pytest.mark.parametrize("market", PERP_MARKET) -def test_get_positions(mango_service_v3_client, market): - positions = mango_service_v3_client.get_open_positions() +def test_get_positions(mango_service_v4_client, market): + positions = mango_service_v4_client.get_open_positions() assert len(positions) > 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_get_balances(mango_service_v3_client, market): - balances = mango_service_v3_client.get_balances() +def test_get_balances(mango_service_v4_client, market): + balances = mango_service_v4_client.get_balances() assert len(balances) > 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_place_order(mango_service_v3_client, market): - mango_service_v3_client.cancel_all_orders() - place_order(mango_service_v3_client, market) - orders = mango_service_v3_client.get_orders() +def test_place_order(mango_service_v4_client, market): + mango_service_v4_client.cancel_all_orders() + place_order(mango_service_v4_client, market) + orders = mango_service_v4_client.get_orders() assert len(orders) == 1 order = orders[0] @@ -67,24 +67,24 @@ def test_place_order(mango_service_v3_client, market): @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_cancel_order_by_order_id(mango_service_v3_client, market): - place_order(mango_service_v3_client, market) +def test_cancel_order_by_order_id(mango_service_v4_client, market): + place_order(mango_service_v4_client, market) - orders = mango_service_v3_client.get_orders() + orders = mango_service_v4_client.get_orders() order = orders[0] - mango_service_v3_client.cancel_order_by_order_id(order.id) + mango_service_v4_client.cancel_order_by_order_id(order.id) - orders = mango_service_v3_client.get_orders() + orders = mango_service_v4_client.get_orders() assert len(orders) == 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_cancel_order_by_client_id(mango_service_v3_client, market): - place_order(mango_service_v3_client, market) +def test_cancel_order_by_client_id(mango_service_v4_client, market): + place_order(mango_service_v4_client, market) - orders = mango_service_v3_client.get_orders() + orders = mango_service_v4_client.get_orders() order = orders[0] - mango_service_v3_client.cancel_order_by_client_id(order.client_id) + mango_service_v4_client.cancel_order_by_client_id(order.client_id) - orders = mango_service_v3_client.get_orders() + orders = mango_service_v4_client.get_orders() assert len(orders) == 0 diff --git a/py/test-mainnet.py b/py/test-mainnet.py index b243c91..e40d19a 100644 --- a/py/test-mainnet.py +++ b/py/test-mainnet.py @@ -1,30 +1,30 @@ import pytest -from mango_service_v3_py.api import MangoServiceV3Client -from mango_service_v3_py.dtos import PlaceOrder +from mango_service_v4_py.api import MangoServiceV4Client +from mango_service_v4_py.dtos import PlaceOrder # Note: some endpoints only return useful data for mainnet, this is because the REST API service rely on # other off chain REST services which only serve data for mainnet @pytest.fixture -def mango_service_v3_client(): - return MangoServiceV3Client("http://localhost:3000/api", timeout=60.0) +def mango_service_v4_client(): + return MangoServiceV4Client("http://localhost:3000/api", timeout=60.0) @pytest.fixture(autouse=True) -def run_around_tests(mango_service_v3_client): +def run_around_tests(mango_service_v4_client): # cleanup - mango_service_v3_client.cancel_all_orders() - orders = mango_service_v3_client.get_orders() + mango_service_v4_client.cancel_all_orders() + orders = mango_service_v4_client.get_orders() assert len(orders) == 0 yield # teardown assert True -def place_order(mango_service_v3_client, market): - mango_service_v3_client.place_order( +def place_order(mango_service_v4_client, market): + mango_service_v4_client.place_order( PlaceOrder( market=market, side="buy", @@ -44,26 +44,26 @@ PERP_MARKET = [("BTC-PERP")] @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_get_markets(mango_service_v3_client, market): - markets = mango_service_v3_client.get_markets() +def test_get_markets(mango_service_v4_client, market): + markets = mango_service_v4_client.get_markets() assert len(markets) > 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_get_candles(mango_service_v3_client, market): - candles = mango_service_v3_client.get_candles(market, 60, 1625922900, 1631214960) +def test_get_candles(mango_service_v4_client, market): + candles = mango_service_v4_client.get_candles(market, 60, 1625922900, 1631214960) print(candles) assert len(candles) > 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_get_orderbook(mango_service_v3_client, market): - ob = mango_service_v3_client.get_orderbook(market) +def test_get_orderbook(mango_service_v4_client, market): + ob = mango_service_v4_client.get_orderbook(market) assert len(ob.asks) > 0 assert len(ob.bids) > 0 @pytest.mark.parametrize("market", SPOT_AND_PERP_MARKETS) -def test_get_trades(mango_service_v3_client, market): - trades = mango_service_v3_client.get_trades(market) +def test_get_trades(mango_service_v4_client, market): + trades = mango_service_v4_client.get_trades(market) assert len(trades) > 0