Skip to content

Commit

Permalink
feat: python ob docs (#12)
Browse files Browse the repository at this point in the history
* feat: python ob docs

* refactor: change dlob python to sync
  • Loading branch information
soundsonacid authored Jan 9, 2024
1 parent 76b5644 commit a72a731
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion source/includes/_orderbook_blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ const userMap = new UserMap({
await userMap.subscribe();
```


```python
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
from anchorpy import Wallet
from driftpy.drift_client import DriftClient
from driftpy.keypair import load_keypair
from driftpy.user_map.user_map import UserMap
from driftpy.user_map.user_map_config import UserMapConfig, WebsocketConfig

connection = AsyncClient("https://api.mainnet-beta.solana/com")

keypair_file = "~/.config/solana/my-keypair.json"

wallet = Wallet(load_keypair(keypair_file))

drift_client = DriftClient(
connnection,
wallet,
"mainnet"
)

await drift_client.subscribe()

user_map_config = UserMapConfig(drift_client, WebsocketConfig())
user_map = UserMap(user_map_config)

await user_map.subscribe()
```

## Dlob Source - `OrderSubscriber`

`OrderSubscriber` is a more efficient version of `UserMap`, only tracking user accounts that have orders.
Expand Down Expand Up @@ -115,13 +145,34 @@ import {DLOBSubscriber} from "@drift-labs/sdk";
await dlobSubscriber.subscribe();
```

### TypeScript
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| driftClient | DriftClient object | No | |
| dlobSource | Where to build the orderbook from. Can subscribe to user accounts on-chain using UserMap or request DLOB from api using DLOBApiClient | No | |
| slotSource | Where to get slot from | No | |
| updateFrequency | How often to rebuild the orderbook from the dlobSource in milliseconds | No | |


```python
from driftpy.dlob.dlob_client import DLOBClient
from driftpy.dlob.client_types import DLOBClientConfig

config = DLOBClientConfig(drift_client, user_map, slot_subscriber, 1_000)
dlob_client = DLOBClient(config = config)

await dlob_client.subscribe()
```

### Python
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| drift_client | DriftClient object | No | |
| dlob_source | Where to build the orderbook from. Can subscribe to user accounts on-chain using UserMap. | No | |
| slot_source | Where to get slot from | No | |
| update_frequency | How often to rebuild the orderbook from the dlob_source in milliseconds | No | |


## Get L2 Orderbook

```typescript
Expand All @@ -131,6 +182,7 @@ const l2 = dlobSubscriber.getL2({
});
```

### TypeScript
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| marketName | The market name of the orderbook to get. If not set, marketIndex and marketType must be set | Yes | |
Expand All @@ -140,6 +192,21 @@ const l2 = dlobSubscriber.getL2({
| includeVamm | Whether to include vAMM | Yes | false |
| fallbackL2Generators | L2OrderbookGenerators for fallback liquidity e.g. vAmm, openbook, phoenix. Unnecessary if includeVamm is true | Yes | |

```python
l2 = dlob_client.get_l2_orderbook_sync("SOL-PERP")
```

### Python
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| market_name | The market name of the orderbook to get. If not set, market_index and market_type must be set | Yes | |
| market_index | The market index of the orderbook to get. If not set, market_name must be set | Yes | |
| market_type | The market type of the orderbook to get. If not set, market_name must be set | Yes | |
| depth | The depth of the orderbook to get | Yes | 10 |
| include_vamm | Whether to include vAMM | Yes | false |
| num_vamm_orders | Number of orders to include from the vAMM. If not provided, depth is used. | Yes | |
| fallback_l2_generators | L2OrderbookGenerators for fallback liquidity e.g. vAmm, openbook, phoenix. Unnecessary if includeVamm is true | Yes | |

The L2 orderbook is an aggregate of drift dlob orders and, optionally, fallback liquidity.

## Get L3 Orderbook
Expand All @@ -150,10 +217,22 @@ const l3 = dlobSubscriber.getL3({
});
```

### TypeScript
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| marketName | The market name of the orderbook to get. If not set, marketIndex and marketType must be set | Yes | |
| marketIndex | The market index of the orderbook to get. If not set, marketName must be set | Yes | |
| marketType | The market type of the orderbook to get. If not set, marketName must be set | Yes | |

The L3 orderbook contains every maker order on drift dlob, including the address for the user that placed the order.
```python
l3 = dlob_client.get_l3_orderbook_sync("SOL-PERP")
```

### Python
| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| market_name | The market name of the orderbook to get. If not set, market_index and market_type must be set | Yes | |
| market_index | The market index of the orderbook to get. If not set, market_name must be set | Yes | |
| market_type | The market type of the orderbook to get. If not set, market_name must be set | Yes | |

The L3 orderbook contains every maker order on drift dlob, including the address for the user that placed the order.

0 comments on commit a72a731

Please sign in to comment.