Skip to content

Commit

Permalink
Merge branch 'main' into new-s3-partitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi authored Jan 29, 2024
2 parents 84ab5be + 474540e commit fd8e1f2
Show file tree
Hide file tree
Showing 8 changed files with 1,074 additions and 292 deletions.
56 changes: 28 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ name: Build

on:
push:
branches: [ '*' ]
branches: ["*"]
pull_request:
branches: [ '*' ]
branches: ["*"]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
ruby-version: [2.6, 2.7, '3.0', 3.1]
ruby-version: [3.2]

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}

- uses: actions/cache@v2
with:
path: vendor/bundle
key: gems-${{ runner.os }}-${{ matrix.ruby-version }}-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
gems-${{ runner.os }}-${{ matrix.ruby-version }}-
gems-${{ runner.os }}-
# necessary to get ruby 2.3 to work nicely with bundler vendor/bundle cache
# can remove once ruby 2.3 is no longer supported
- run: gem update --system

- run: bundle config set deployment 'true'
- name: bundle install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- run: bundle exec middleman build
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}

- uses: actions/cache@v2
with:
path: vendor/bundle
key: gems-${{ runner.os }}-${{ matrix.ruby-version }}-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
gems-${{ runner.os }}-${{ matrix.ruby-version }}-
gems-${{ runner.os }}-
# necessary to get ruby 2.3 to work nicely with bundler vendor/bundle cache
# can remove once ruby 2.3 is no longer supported
- run: gem update --system

- run: bundle config set deployment 'true'
- name: bundle install
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- run: bundle exec middleman build
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

runs-on: ubuntu-latest
env:
ruby-version: 2.6
ruby-version: 3.2

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:

runs-on: ubuntu-latest
env:
ruby-version: 2.6
ruby-version: 3.2

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Make sure latest ruby version is installed:
brew update
brew install ruby
```
(mac users will need to make sure their [`PATH` is set properly](https://mac.install.guide/ruby/13.html) to not use the default system ruby)

Update gem (no `sudo`, if it fails running it again seems to work...):
```
Expand All @@ -51,9 +52,7 @@ bundle config set force_ruby_platform true
Build the docs
```
bundle install
```

# Build and run docs locally
``
After doing the above, you can work on docs locally via:
```
Expand Down
34 changes: 32 additions & 2 deletions source/includes/_historicaldata.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,42 @@ mainnet-beta: `https://drift-historical-data.s3-v2.eu-west-1.amazonaws.com/prog
| accountKey | user sub account public key (not authority) | |
| marketSymbol | market name | SOL-PERP |
| year | | 2023 |
| month | | 02 |
| day | utc time | 01 |
| month | | 4 |
| day | utc time | 25 |
| candleResolution | | 1M |


## Examples

Get historical trades on `SOL-PERP` for August 5, 2023:
```
https://drift-historical-data.s3.eu-west-1.amazonaws.com/program/dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH/market/SOL-PERP/trades/2023/8/5
```

## Records Columns

Below are definitions of the columns in each record type.

### trades

| variable | description | example |
| --- | --- | --- |
| accountKey | user sub account public key (not authority) | |

### market-trades

### funding-rates

### funding-payments

### deposits

### liquidations

### candles

### settle-pnl-records


```python
import requests
Expand Down
241 changes: 241 additions & 0 deletions source/includes/_orderbook_blockchain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# Orderbook (Blockchain)

The drift orderbook is a collection of all open orders on the Drift protocol. There is no single source of truth for the orderbook. The most up to date view of the orderbook is one where you track user orders and maintain the orderbook structure yourself. This section shows how to do that with the various SDKs.

## Dlob Source

This is the main source of orders for maintaing the orderbook.

## Dlob Source - `UserMap`

`UserMap` stores a complete map of all user accounts (idle users are commonly filtered ou).

```typescript
import {Connection} from "@solana/web3.js";
import {DriftClient, UserMap, Wallet, loadKeypair} from "@drift-labs/sdk";

const connection = new Connection("https://api.mainnet-beta.solana.com");

const keyPairFile = '~/.config/solana/my-keypair.json';
const wallet = new Wallet(loadKeypair(privateKeyFile))

const driftClient = new DriftClient({
connection,
wallet,
env: 'mainnet-beta',
});
await driftClient.subscribe();

// polling keep users updated with periodic calls to getProgramAccounts
// websocket keep users updated via programSubscribe
const subscriptionConfig:
| {
type: 'polling';
frequency: number;
commitment?: Commitment;
} | {
type: 'websocket';
resubTimeoutMs?: number;
commitment?: Commitment;
} = {
type: 'websocket',
resubTimeoutMs: 30_000,
commitment: stateCommitment,
};

const userMap = new UserMap({
driftClient,
connection,
subscriptionConfig,
skipInitialLoad: false, // skips initial load of user accounts
includeIdle: false, // filters out idle users
});

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") # switch for your own rpc

keypair_file = "" # path to keypair file

wallet = Wallet(load_keypair(keypair_file))

drift_client = DriftClient(
connection,
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.

```typescript
import {Connection} from "@solana/web3.js";
import {DriftClient, OrderSubscriber, Wallet, loadKeypair} from "@drift-labs/sdk";

const connection = new Connection("https://api.mainnet-beta.solana.com");

const keyPairFile = '~/.config/solana/my-keypair.json';
const wallet = new Wallet(loadKeypair(privateKeyFile))

const driftClient = new DriftClient({
connection,
wallet,
env: 'mainnet-beta',
});
await driftClient.subscribe();

const subscriptionConfig:
| {
type: 'polling',
frequency: ORDERBOOK_UPDATE_INTERVAL,
commitment: stateCommitment,
}
| {
type: 'websocket',
commitment: stateCommitment,
resyncIntervalMs: WS_FALLBACK_FETCH_INTERVAL,
} = {
type: 'websocket',
commitment: stateCommitment,
resyncIntervalMs: WS_FALLBACK_FETCH_INTERVAL, // periodically resyncs the orders in case of missed websocket messages
};

const orderSubscriber = new OrderSubscriber({
driftClient,
subscriptionConfig,
});

await orderSubscriber.subscribe();
```

## Orderbook Subscription

With a `DlobSource` you can then subscribe to the orderbook.

```typescript
import {DLOBSubscriber} from "@drift-labs/sdk";

const dlobSubscriber = new DLOBSubscriber({
driftClient,
dlobSource: orderSubscriber, // or UserMap
slotSource: orderSubscriber, // or UserMap
updateFrequency: 1000,
});

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_subscriber import DLOBSubscriber
from driftpy.dlob.client_types import DLOBClientConfig
from driftpy.slot.slot_subscriber import SlotSubscriber

slot_subscriber = SlotSubscriber(drift_client)

config = DLOBClientConfig(drift_client, user_map, slot_subscriber, 1)
dlob_subscriber = DLOBSubscriber(config = config)

await dlob_subscriber.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
const l2 = dlobSubscriber.getL2({
marketName: 'SOL-PERP',
depth: 50,
});
```

### 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 | |
| depth | The depth of the orderbook to get | Yes | 10 |
| 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_subscriber.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

```typescript
const l3 = dlobSubscriber.getL3({
marketName: 'SOL-PERP',
});
```

### 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 | |

```python
l3 = dlob_subscriber.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.
Loading

0 comments on commit fd8e1f2

Please sign in to comment.