Skip to content

Commit

Permalink
Merge pull request #7 from jordy25519/init-gateway
Browse files Browse the repository at this point in the history
Initial http gateway docs
  • Loading branch information
wphan authored Feb 9, 2024
2 parents ec01f00 + 4f5c404 commit c04d4a9
Showing 1 changed file with 162 additions and 4 deletions.
166 changes: 162 additions & 4 deletions source/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: protocol-v2 API
language_tabs: # must be one of https://github.com/rouge-ruby/rouge/wiki/List-of-supported-languages-and-lexers
- typescript
- python
- shell
- shell: http

toc_footers:
- <a href='https://github.com/drift-labs/protocol-v2/releases/'> Release History </a>
Expand Down Expand Up @@ -94,6 +94,9 @@ Install driftpy from PyPI using pip:

auto-generated documentation here: [https://drift-labs.github.io/driftpy/]

## HTTP
Use the self-hosted HTTP [gateway](https://github.com/drift-labs/gateway)

## Connection

```typescript
Expand All @@ -110,8 +113,13 @@ url = 'https://api.mainnet-beta.solana.com' # replace w/ any rpc
connection = AsyncClient(url)
```

```shell
drift-gateway https://api.mainnet-beta.solana.com --port 8080
```

The connection object is used to send transactions to the Solana blockchain. It is used by the DriftClient to send transactions to the blockchain.


## Wallet

```typescript
Expand All @@ -129,6 +137,11 @@ keypair_file = os.path.expanduser('~/.config/solana/my-keypair.json')
keypair = load_keypair(keypair_file)
wallet = Wallet(kp)
```
```shell
# use `DRIFT_GATEWAY_KEY` environment variable to configure the gateway wallet
# either path to .json keypair or base58 seed string
export DRIFT_GATEWAY_KEY="</path/to/my-keypair.json|<KEYPAIR_BASE58_SEED>"
```

The wallet used to sign solana transactions. The wallet can be created from a private key or from a keypair file.

Expand Down Expand Up @@ -246,6 +259,10 @@ driftClient.switchActiveUser(
drift_client.switch_active_user(sub_account_id=1)
```

```shell
curl http://localhost:8080/v2/resource?subAccountId=1
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| subAccountId | The sub account to switch to | No | 0 |
Expand Down Expand Up @@ -339,7 +356,7 @@ driftClient.transferDeposit(

```python
market_index = 0
amount = drift_client.convert_to_spot_precision(market_ndex, 100)
amount = drift_client.convert_to_spot_precision(market_index, 100)
from_sub_account_id = 0
to_sub_account_id = 0

Expand Down Expand Up @@ -464,6 +481,23 @@ order_params = OrderParams(
await drift_client.get_place_perp_order(order_params)
```

```shell
curl -X POST -H 'content-type: application/json' \
-d '{
"orders": [{
"marketIndex": 0,
"marketType": "perp",
"amount": 1.23,
"price": 80.0,
"postOnly": true,
"orderType": "limit",
"immediateOrCancel": false,
"reduceOnly": false
}]
}' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| orderParams | The order params | No | |
Expand Down Expand Up @@ -500,6 +534,23 @@ order_params = OrderParams(
await driftClient.place_spot_order(order_params);
```

```shell
curl -X POST -H 'content-type: application/json' \
-d '{
"orders": [{
"marketIndex": 0,
"marketType": "spot",
"amount": -1.23,
"price": 80.0,
"postOnly": true,
"orderType": "limit",
"immediateOrCancel": false,
"reduceOnly": false
}]
}' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| orderParams | The order params | No | |
Expand Down Expand Up @@ -556,6 +607,30 @@ place_order_params = [
await drift_client.place_orders(place_order_params);
```

```shell
curl -X POST -H 'content-type: application/json' \
-d '{
"orders": [{
"marketIndex": 0,
"marketType": "spot",
"amount": 1.23,
"price": 80.0,
"postOnly": true,
"orderType": "limit",
"immediateOrCancel": false,
"reduceOnly": false
},
{
"marketIndex": 2,
"marketType": "perp",
"amount": 0.1,
"price": 10000.0,
"orderType": "market",
}]
}' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| placeOrderParams | Parameters for place order instructions | | |
Expand Down Expand Up @@ -622,6 +697,13 @@ await drift_client.cancel_order(order_id);
| ----------- | ----------- | -------- | ------- |
| orderId | The order being canceled | No | |


```shell
curl -X DELETE -H 'content-type: application/json' \
-d '{ "ids": [1] }' \
localhost:8080/v2/orders
```

## Canceling Order By User Order Id

```typescript
Expand All @@ -636,6 +718,12 @@ const user_order_id = 1;
await drift_client.cancel_order_by_user_order_id(user_order_id);
```

```shell
curl -X DELETE -H 'content-type: application/json' \
-d '{ "userIds": [1] }' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| userOrderId | Unique order id specified by user when order was placed | No | |
Expand All @@ -659,6 +747,12 @@ await drift_client.cancel_orders(market_type, market_index, direction) # cancel
await drift_client.cancel_orders() # cancels all orders
```

```shell
curl -X DELETE -H 'content-type: application/json' \
-d '{ "marketIndex": 1, "marketType": "spot" }' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| marketType | The market type of orders to cancel. Must be set if marketIndex set | Yes | |
Expand Down Expand Up @@ -721,6 +815,29 @@ place_order_params = [
await drift_client.cancel_and_place_orders(canel_order_params, place_order_params);
```

```shell
curl -X POST -H 'content-type: application/json' \
-d '{
"cancel": {
"marketIndex": 0,
"marketType": "perp"
},
"place": {
"orders": [{
"marketIndex": 0,
"marketType": "perp",
"amount": -1.23,
"price": 80.0,
"postOnly": true,
"orderType": "limit",
"immediateOrCancel": false,
"reduceOnly": false
}]
}
}' \
localhost:8080/v2/orders/cancelAndPlace
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| cancelOrderParams | Parameters for cancel orders instruction | | |
Expand Down Expand Up @@ -762,14 +879,26 @@ await driftClient.modifyOrder(orderParams);

order_id = 1

modfiy_order_params = ModifyOrderParams(
modify_order_params = ModifyOrderParams(
base_asset_amount=drift_client.convert_to_perp_precision(1),
price=drift_client.convert_to_price_precision(20),
)

await drift_client.modify_order(order_id, modify_order_params);
```

```shell
curl -X PATCH -H 'content-type: application/json' \
-d '{
"orders": [{
"orderId": 32,
"amount": 1.05,
"price": 61.0
}]
}' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| orderId | The order id of order to modify | No | |
Expand All @@ -795,14 +924,26 @@ await driftClient.modifyOrderByUserOrderId(orderParams);

user_order_id = 1

modfiy_order_params = ModifyOrderParams(
modify_order_params = ModifyOrderParams(
base_asset_amount=drift_client.convert_to_perp_precision(1),
price=drift_client.convert_to_price_precision(20),
)

await drift_client.modify_order_by_user_id(user_order_id, modify_order_params);
```

```shell
curl -X PATCH -H 'content-type: application/json' \
-d '{
"orders": [{
"userOrderId": 69,
"amount": 1.05,
"price": 61.0
}]
}' \
localhost:8080/v2/orders
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| userOrderId | The user order id of order to modify | No | |
Expand Down Expand Up @@ -913,6 +1054,13 @@ is_deposit = token_amount > 0
is_borrow = token_amount < 0
```

```shell
curl -X GET \
-H 'content-type: application/json' \
-d '{"marketIndex":0,"marketType":"spot"}' \
localhost:8080/v2/positions
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
| marketIndex | Market index for the spot market | No | |
Expand Down Expand Up @@ -942,6 +1090,12 @@ is_long = base_asset_amount > 0
is_short = base_asset_amount < 0
```

```shell
curl -X GET \
-H 'content-type: application/json' \
-d '{"marketIndex":0,"marketType":"perp"}' \
localhost:8080/v2/positions
```

| Parameter | Description | Optional | Default |
| ----------- | ----------- | -------- | ------- |
Expand Down Expand Up @@ -997,6 +1151,10 @@ const orders = user.getOpenOrders();
orders = user.get_open_orders()
```

```shell
curl localhost:8080/v2/orders
```

## Get Unrealized Perp Pnl

```typescript
Expand Down

0 comments on commit c04d4a9

Please sign in to comment.