|
| 1 | +# Wazirx Python |
| 2 | + |
| 3 | +[](https://docs.wazirx.com) |
| 4 | + |
| 5 | +This is an official Python wrapper for the Wazirx exchange REST and WebSocket APIs. |
| 6 | + |
| 7 | +##### Notice |
| 8 | + |
| 9 | +We are now at 1.0 and there may be things breaking, don't hesitate to raise an issue if you feel so! |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```python |
| 14 | +pip3 install -r requirements.txt |
| 15 | +``` |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +#### Current |
| 20 | + |
| 21 | +* Basic implementation of REST API |
| 22 | + * Easy to use authentication |
| 23 | + * Methods return parsed JSON |
| 24 | + * No need to generate timestamps |
| 25 | + * No need to generate signatures |
| 26 | +* Basic implementation of WebSocket API |
| 27 | + * Pass procs or lambdas to event handlers |
| 28 | + * Single and multiple streams supported |
| 29 | + * Runs on EventMachine |
| 30 | + |
| 31 | +#### Planned |
| 32 | + |
| 33 | +* Exception handling with responses |
| 34 | +* High level abstraction |
| 35 | + |
| 36 | +## Getting Started |
| 37 | + |
| 38 | +#### REST Client |
| 39 | + |
| 40 | +Import Wazirx Client for Rest: |
| 41 | + |
| 42 | +```python |
| 43 | +from wazirx_sapi_client.rest import Client |
| 44 | +``` |
| 45 | + |
| 46 | +Create a new instance of the REST Client: |
| 47 | + |
| 48 | +```python |
| 49 | +# If you only plan on touching public API endpoints, you can forgo any arguments |
| 50 | +client = Client() |
| 51 | +# Otherwise provide an api_key and secret_key as keyword arguments |
| 52 | +client = Client(api_key='x', secret_key='y') |
| 53 | +``` |
| 54 | + |
| 55 | +Create various requests: |
| 56 | + |
| 57 | +## General Endpoints |
| 58 | + |
| 59 | +#### Ping |
| 60 | + |
| 61 | +```python |
| 62 | +client.send("ping") |
| 63 | +``` |
| 64 | +Response: |
| 65 | +```json-doc |
| 66 | +{} |
| 67 | +``` |
| 68 | +#### Server time |
| 69 | + |
| 70 | +```python |
| 71 | +client.send("time") |
| 72 | +``` |
| 73 | +Response: |
| 74 | +```json-doc |
| 75 | +{ |
| 76 | + "serverTime": 1632375945160 |
| 77 | +} |
| 78 | +``` |
| 79 | +#### System status |
| 80 | + |
| 81 | +```python |
| 82 | +client.send("system_status") |
| 83 | +``` |
| 84 | +Response: |
| 85 | +```json-doc |
| 86 | +{ |
| 87 | + "status": "normal", |
| 88 | + "message": "System is running normally." |
| 89 | +} |
| 90 | +``` |
| 91 | +#### Exchange info |
| 92 | + |
| 93 | +```python |
| 94 | +client.send("exchange_info") |
| 95 | +``` |
| 96 | +Response: |
| 97 | +```json-doc |
| 98 | +{ |
| 99 | + "timezone": "UTC", |
| 100 | + "serverTime": 1632376074413, |
| 101 | + "symbols": [ |
| 102 | + { |
| 103 | + "symbol": "wrxinr", |
| 104 | + "status": "trading", |
| 105 | + "baseAsset": "wrx", |
| 106 | + "quoteAsset": "inr", |
| 107 | + "baseAssetPrecision": 5, |
| 108 | + "quoteAssetPrecision": 0, |
| 109 | + "orderTypes": [ |
| 110 | + "limit", |
| 111 | + "stop_limit" |
| 112 | + ], |
| 113 | + "isSpotTradingAllowed": true, |
| 114 | + "filters": [ |
| 115 | + { |
| 116 | + "filterType": "PRICE_FILTER", |
| 117 | + "minPrice": "1", |
| 118 | + "tickSize": "1" |
| 119 | + } |
| 120 | + ] |
| 121 | + } |
| 122 | + ] |
| 123 | +} |
| 124 | +``` |
| 125 | +#### Create an order |
| 126 | +```python |
| 127 | +client.send("create_order", {"symbol": 'btcinr', "side": 'buy', "type": 'limit', |
| 128 | + "quantity": 100, "price": 0.00055, "recvWindow": 1000}) |
| 129 | +``` |
| 130 | +Response: |
| 131 | +```json-doc |
| 132 | +{"id"=>27007862, "symbol"=>"btcinr", "type"=>"limit", "side"=>"buy", |
| 133 | +"status"=>"wait", "price"=>"210.0", "origQty"=>"2.0", "executedQty"=>"0.0", |
| 134 | +"createdTime"=>1632310960000, "updatedTime"=>1632310960000} |
| 135 | +``` |
| 136 | +##### For other methods follow [this](https://github.com/WazirX/wazirx-connector-python/blob/master/wazirx_sapi_client/rest/api_mapper.json). |
| 137 | + |
| 138 | +Required and optional parameters, as well as enum values, can be found on the [Wazirx Documentation](https://docs.wazirx.com). Parameters should always be passed to client methods as keyword arguments in snake_case form. |
| 139 | + |
| 140 | +#### WebSocket Client |
| 141 | + |
| 142 | +Import Wazirx Client for WebSocket and [AsyncIO](https://docs.python.org/3/library/asyncio.html) |
| 143 | + |
| 144 | +```python |
| 145 | +import asyncio |
| 146 | +from wazirx_sapi_client.websocket import WebsocketClient |
| 147 | +``` |
| 148 | + |
| 149 | +Create a new instance of the REST Client: |
| 150 | + |
| 151 | +```python |
| 152 | +# If you only plan on touching public API endpoints, you can forgo any arguments |
| 153 | +ws = WebsocketClient() |
| 154 | +# Otherwise provide an api_key and secret_key as keyword arguments |
| 155 | +ws = WebsocketClient(api_key='x', secret_key='y') |
| 156 | +``` |
| 157 | + |
| 158 | +Create a connection with the websocket using: |
| 159 | + |
| 160 | +```python |
| 161 | +asyncio.create_task( |
| 162 | + ws.connect() |
| 163 | +) |
| 164 | +``` |
| 165 | + |
| 166 | +Create various WebSocket streams with `await` |
| 167 | + |
| 168 | +```python |
| 169 | +# Pass the symbol/symbols to subscribe to trades |
| 170 | +await ws.trades(symbol=['btcinr','wrxinr'], id=0, action='subscribe') |
| 171 | + |
| 172 | +# Pass the symbol/symbols to subscribe to depth |
| 173 | +await ws.depth(symbol=['btcinr','wrxinr'], id=0, action='subscribe') |
| 174 | + |
| 175 | +# For all market tickers |
| 176 | +await ws.all_market_ticker(id=0, action='subscribe') |
| 177 | +``` |
| 178 | + |
| 179 | +##### Note: |
| 180 | +* `symbol` can be `Array` for multiple symbols. |
| 181 | +* `id` by default is `0`, for unique identification any positive integer can be used. |
| 182 | +* `action` only needs to pass in case of `unsubscribe`, default is `subscribe` if no data passed. |
| 183 | +#### User Data Stream |
| 184 | + |
| 185 | +User data streams utilize both the REST and WebSocket APIs. |
| 186 | + |
| 187 | +Request a listen key from the REST API, and then create a WebSocket stream using it. |
| 188 | + |
| 189 | +```python |
| 190 | +await ws.user_stream(streams=['orderUpdate', 'ownTrade', 'outboundAccountPosition'], id=0, action='subscribe') |
| 191 | +``` |
| 192 | + |
| 193 | +To make sure that websocket stays live add the given below code for your `main`, for understanding better refer [here](https://github.com/WazirX/wazirx-connector-python/blob/master/wazirx_sapi_client/websocket/test.py) |
| 194 | + |
| 195 | +```python |
| 196 | +loop = asyncio.get_event_loop() |
| 197 | +loop.create_task(main()) |
| 198 | +loop.run_forever() |
| 199 | +``` |
| 200 | + |
| 201 | +## Development |
| 202 | + |
| 203 | +After checking out the repo, run `python3 wazirx_sapi_client/rest/test.py` or `python3 wazirx_sapi_client/websocket/test.py` to run the rest apis or websocket tests or experiments respectively. |
| 204 | + |
| 205 | +## Contributing |
| 206 | + |
| 207 | +Bug reports and pull requests are welcome on GitHub at [Issues](https://github.com/WazirX/wazirx-connector-python/issues). |
| 208 | + |
| 209 | +## License |
| 210 | + |
| 211 | +The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
0 commit comments