@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +14 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +30 -27
- package/lib/accounts/types.d.ts +9 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +15 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +38 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +13 -7
- package/lib/admin.js +111 -44
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +767 -278
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +24 -22
- package/lib/clearingHouseUser.js +273 -177
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/lib/constants/{markets.js → perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +557 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1152 -503
- package/lib/index.d.ts +10 -3
- package/lib/index.js +10 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +11 -10
- package/lib/math/market.js +30 -7
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +6 -6
- package/lib/math/orders.js +31 -16
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +193 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -57
- package/lib/types.js +39 -11
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/package.json +6 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +42 -38
- package/src/accounts/types.ts +12 -9
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +65 -52
- package/src/addresses/pda.ts +49 -44
- package/src/admin.ts +190 -55
- package/src/clearingHouse.ts +1092 -365
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.ts +518 -255
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.ts +17 -15
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/dlob/DLOB.ts +884 -0
- package/src/dlob/DLOBNode.ts +163 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/types.ts +3 -0
- package/src/examples/makeTradeExample.js +152 -75
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/idl/clearing_house.json +1152 -503
- package/src/index.ts +10 -3
- package/src/math/amm.ts +6 -3
- package/src/math/funding.ts +7 -7
- package/src/math/margin.ts +34 -23
- package/src/math/market.ts +72 -20
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.ts +33 -25
- package/src/math/position.ts +31 -31
- package/src/math/spotBalance.ts +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.ts +4 -4
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.ts +2 -1
- package/src/types.ts +135 -56
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/tests/bn/test.ts +2 -3
- package/tests/dlob/helpers.ts +322 -0
- package/tests/dlob/test.ts +2865 -0
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/constants/numericConstants.js +0 -41
- package/src/math/bankBalance.ts +0 -258
- package/src/math/oracles.js +0 -26
- package/src/math/state.js +0 -15
- package/src/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/tokenFaucet.js +0 -189
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BN, MarketTypeStr, Order, PerpMarketAccount, SpotMarketAccount } from '..';
|
|
3
|
+
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { DLOBNode, DLOBNodeMap } from './DLOBNode';
|
|
5
|
+
export declare type SortDirection = 'asc' | 'desc';
|
|
6
|
+
export declare function getOrderSignature(orderId: BN, userAccount: PublicKey): string;
|
|
7
|
+
export interface DLOBNodeGenerator {
|
|
8
|
+
getGenerator(): Generator<DLOBNode>;
|
|
9
|
+
}
|
|
10
|
+
export declare class NodeList<NodeType extends keyof DLOBNodeMap> implements DLOBNodeGenerator {
|
|
11
|
+
private nodeType;
|
|
12
|
+
private sortDirection;
|
|
13
|
+
head?: DLOBNodeMap[NodeType];
|
|
14
|
+
length: number;
|
|
15
|
+
nodeMap: Map<string, DLOBNodeMap[NodeType]>;
|
|
16
|
+
constructor(nodeType: NodeType, sortDirection: SortDirection);
|
|
17
|
+
insert(order: Order, marketType: MarketTypeStr, market: PerpMarketAccount | SpotMarketAccount, userAccount: PublicKey): void;
|
|
18
|
+
prependNode(currentNode: DLOBNodeMap[NodeType], newNode: DLOBNodeMap[NodeType]): boolean;
|
|
19
|
+
update(order: Order, userAccount: PublicKey): void;
|
|
20
|
+
remove(order: Order, userAccount: PublicKey): void;
|
|
21
|
+
getGenerator(): Generator<DLOBNode>;
|
|
22
|
+
has(order: Order, userAccount: PublicKey): boolean;
|
|
23
|
+
print(): void;
|
|
24
|
+
printTop(): void;
|
|
25
|
+
}
|
|
26
|
+
export declare function getVammNodeGenerator(price: BN | undefined): Generator<DLOBNode>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVammNodeGenerator = exports.NodeList = exports.getOrderSignature = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const DLOBNode_1 = require("./DLOBNode");
|
|
6
|
+
function getOrderSignature(orderId, userAccount) {
|
|
7
|
+
return `${userAccount.toString()}-${orderId.toString()}`;
|
|
8
|
+
}
|
|
9
|
+
exports.getOrderSignature = getOrderSignature;
|
|
10
|
+
class NodeList {
|
|
11
|
+
constructor(nodeType, sortDirection) {
|
|
12
|
+
this.nodeType = nodeType;
|
|
13
|
+
this.sortDirection = sortDirection;
|
|
14
|
+
this.length = 0;
|
|
15
|
+
this.nodeMap = new Map();
|
|
16
|
+
}
|
|
17
|
+
insert(order, marketType, market, userAccount) {
|
|
18
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (marketType === 'spot') {
|
|
22
|
+
market = market;
|
|
23
|
+
}
|
|
24
|
+
else if (marketType === 'perp') {
|
|
25
|
+
market = market;
|
|
26
|
+
}
|
|
27
|
+
const newNode = (0, DLOBNode_1.createNode)(this.nodeType, order, market, userAccount);
|
|
28
|
+
const orderId = getOrderSignature(order.orderId, userAccount);
|
|
29
|
+
if (this.nodeMap.has(orderId)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.nodeMap.set(orderId, newNode);
|
|
33
|
+
this.length += 1;
|
|
34
|
+
if (this.head === undefined) {
|
|
35
|
+
this.head = newNode;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (this.prependNode(this.head, newNode)) {
|
|
39
|
+
this.head.previous = newNode;
|
|
40
|
+
newNode.next = this.head;
|
|
41
|
+
this.head = newNode;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let currentNode = this.head;
|
|
45
|
+
while (currentNode.next !== undefined &&
|
|
46
|
+
!this.prependNode(currentNode.next, newNode)) {
|
|
47
|
+
currentNode = currentNode.next;
|
|
48
|
+
}
|
|
49
|
+
newNode.next = currentNode.next;
|
|
50
|
+
if (currentNode.next !== undefined) {
|
|
51
|
+
newNode.next.previous = newNode;
|
|
52
|
+
}
|
|
53
|
+
currentNode.next = newNode;
|
|
54
|
+
newNode.previous = currentNode;
|
|
55
|
+
}
|
|
56
|
+
prependNode(currentNode, newNode) {
|
|
57
|
+
const currentOrder = currentNode.order;
|
|
58
|
+
const newOrder = newNode.order;
|
|
59
|
+
const currentOrderSortPrice = currentNode.sortValue;
|
|
60
|
+
const newOrderSortPrice = newNode.sortValue;
|
|
61
|
+
if (newOrderSortPrice.eq(currentOrderSortPrice)) {
|
|
62
|
+
return newOrder.ts.lt(currentOrder.ts);
|
|
63
|
+
}
|
|
64
|
+
if (this.sortDirection === 'asc') {
|
|
65
|
+
return newOrderSortPrice.lt(currentOrderSortPrice);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return newOrderSortPrice.gt(currentOrderSortPrice);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
update(order, userAccount) {
|
|
72
|
+
const orderId = getOrderSignature(order.orderId, userAccount);
|
|
73
|
+
if (this.nodeMap.has(orderId)) {
|
|
74
|
+
const node = this.nodeMap.get(orderId);
|
|
75
|
+
Object.assign(node.order, order);
|
|
76
|
+
node.haveFilled = false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
remove(order, userAccount) {
|
|
80
|
+
const orderId = getOrderSignature(order.orderId, userAccount);
|
|
81
|
+
if (this.nodeMap.has(orderId)) {
|
|
82
|
+
const node = this.nodeMap.get(orderId);
|
|
83
|
+
if (node.next) {
|
|
84
|
+
node.next.previous = node.previous;
|
|
85
|
+
}
|
|
86
|
+
if (node.previous) {
|
|
87
|
+
node.previous.next = node.next;
|
|
88
|
+
}
|
|
89
|
+
if (this.head && node.order.orderId.eq(this.head.order.orderId)) {
|
|
90
|
+
this.head = node.next;
|
|
91
|
+
}
|
|
92
|
+
node.previous = undefined;
|
|
93
|
+
node.next = undefined;
|
|
94
|
+
this.nodeMap.delete(orderId);
|
|
95
|
+
this.length--;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
*getGenerator() {
|
|
99
|
+
let node = this.head;
|
|
100
|
+
while (node !== undefined) {
|
|
101
|
+
yield node;
|
|
102
|
+
node = node.next;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
has(order, userAccount) {
|
|
106
|
+
return this.nodeMap.has(getOrderSignature(order.orderId, userAccount));
|
|
107
|
+
}
|
|
108
|
+
print() {
|
|
109
|
+
let currentNode = this.head;
|
|
110
|
+
while (currentNode !== undefined) {
|
|
111
|
+
console.log(currentNode.getLabel());
|
|
112
|
+
currentNode = currentNode.next;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
printTop() {
|
|
116
|
+
if (this.head) {
|
|
117
|
+
console.log(this.sortDirection.toUpperCase(), this.head.getLabel());
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
console.log('---');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.NodeList = NodeList;
|
|
125
|
+
function* getVammNodeGenerator(price) {
|
|
126
|
+
if (!price) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
yield {
|
|
130
|
+
getPrice: () => price,
|
|
131
|
+
isVammNode: () => true,
|
|
132
|
+
order: undefined,
|
|
133
|
+
market: undefined,
|
|
134
|
+
userAccount: undefined,
|
|
135
|
+
haveFilled: false,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
exports.getVammNodeGenerator = getVammNodeGenerator;
|
package/lib/events/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Commitment, TransactionSignature } from '@solana/web3.js';
|
|
2
|
-
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord } from '../index';
|
|
2
|
+
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord } from '../index';
|
|
3
3
|
export declare type EventSubscriptionOptions = {
|
|
4
4
|
eventTypes?: EventType[];
|
|
5
5
|
maxEventsPerType?: number;
|
|
@@ -31,6 +31,7 @@ export declare type EventMap = {
|
|
|
31
31
|
SettlePnlRecord: Event<SettlePnlRecord>;
|
|
32
32
|
NewUserRecord: Event<NewUserRecord>;
|
|
33
33
|
LPRecord: Event<LPRecord>;
|
|
34
|
+
InsuranceFundRecord: Event<InsuranceFundRecord>;
|
|
34
35
|
};
|
|
35
36
|
export declare type EventType = keyof EventMap;
|
|
36
37
|
export interface EventSubscriberEvents {
|
package/lib/events/types.js
CHANGED
|
@@ -6,7 +6,7 @@ const __1 = require("..");
|
|
|
6
6
|
const spl_token_1 = require("@solana/spl-token");
|
|
7
7
|
const web3_js_1 = require("@solana/web3.js");
|
|
8
8
|
const __2 = require("..");
|
|
9
|
-
const
|
|
9
|
+
const spotMarkets_1 = require("../constants/spotMarkets");
|
|
10
10
|
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
11
11
|
return spl_token_1.Token.getAssociatedTokenAddress(new web3_js_1.PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`), spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
12
12
|
};
|
|
@@ -36,8 +36,8 @@ const main = async () => {
|
|
|
36
36
|
connection,
|
|
37
37
|
wallet: provider.wallet,
|
|
38
38
|
programID: clearingHousePublicKey,
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
perpMarketIndexes: __2.PerpMarkets[cluster].map((market) => market.marketIndex),
|
|
40
|
+
spotMarketIndexes: spotMarkets_1.SpotMarkets[cluster].map((spotMarket) => spotMarket.marketIndex),
|
|
41
41
|
accountSubscription: {
|
|
42
42
|
type: 'polling',
|
|
43
43
|
accountLoader: bulkAccountLoader,
|
|
@@ -58,16 +58,16 @@ const main = async () => {
|
|
|
58
58
|
if (!userAccountExists) {
|
|
59
59
|
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
60
60
|
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
61
|
-
await clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, await (0, exports.getTokenAddress)(usdcTokenAddress.toString(), wallet.publicKey.toString()),
|
|
61
|
+
await clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, await (0, exports.getTokenAddress)(usdcTokenAddress.toString(), wallet.publicKey.toString()), spotMarkets_1.SpotMarkets['devnet'][0].marketIndex);
|
|
62
62
|
}
|
|
63
63
|
await user.subscribe();
|
|
64
64
|
// Get current price
|
|
65
|
-
const solMarketInfo = sdkConfig.
|
|
66
|
-
const currentMarketPrice = (0, __2.calculateMarkPrice)(clearingHouse.
|
|
65
|
+
const solMarketInfo = sdkConfig.PERP_MARKETS.find((market) => market.baseAssetSymbol === 'SOL');
|
|
66
|
+
const currentMarketPrice = (0, __2.calculateMarkPrice)(clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex), undefined);
|
|
67
67
|
const formattedPrice = (0, __2.convertToNumber)(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
68
68
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
69
69
|
// Estimate the slippage for a $5000 LONG trade
|
|
70
|
-
const solMarketAccount = clearingHouse.
|
|
70
|
+
const solMarketAccount = clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex);
|
|
71
71
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
72
72
|
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', undefined)[0], __2.MARK_PRICE_PRECISION);
|
|
73
73
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|