@drift-labs/sdk 0.2.0-master.22 → 0.2.0-master.25
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/addresses/pda.d.ts +4 -0
- package/lib/addresses/pda.js +27 -1
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +36 -10
- package/lib/clearingHouse.d.ts +11 -2
- package/lib/clearingHouse.js +140 -5
- package/lib/clearingHouseUser.d.ts +3 -1
- package/lib/clearingHouseUser.js +11 -9
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -0
- package/lib/constants/banks.js +9 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +4 -1
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +7 -10
- package/lib/events/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +13 -1
- package/lib/idl/clearing_house.json +1081 -173
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/math/amm.d.ts +3 -1
- package/lib/math/amm.js +41 -3
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +1 -1
- package/lib/math/margin.js +5 -7
- package/lib/math/position.js +1 -1
- package/lib/types.d.ts +79 -24
- package/lib/types.js +7 -4
- package/package.json +1 -1
- package/src/addresses/pda.ts +56 -0
- package/src/admin.ts +64 -18
- package/src/clearingHouse.ts +230 -10
- package/src/clearingHouseUser.ts +16 -11
- package/src/config.ts +1 -1
- package/src/constants/banks.ts +16 -0
- package/src/constants/numericConstants.ts +4 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +10 -14
- package/src/events/types.ts +7 -1
- package/src/examples/makeTradeExample.ts +20 -1
- package/src/idl/clearing_house.json +1081 -173
- package/src/index.ts +2 -0
- package/src/math/amm.ts +67 -2
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +3 -10
- package/src/math/position.ts +2 -2
- package/src/types.ts +83 -24
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/sort.js +0 -44
package/src/events/sort.ts
CHANGED
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
SortFn,
|
|
7
7
|
Event,
|
|
8
8
|
} from './types';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { OrderActionRecord } from '../types';
|
|
10
|
+
import { ZERO } from '../index';
|
|
11
11
|
|
|
12
12
|
function clientSortAscFn(): 'less than' {
|
|
13
13
|
return 'less than';
|
|
@@ -24,21 +24,17 @@ function defaultBlockchainSortFn(
|
|
|
24
24
|
return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
function
|
|
28
|
-
currentEvent: Event<
|
|
29
|
-
newEvent: Event<
|
|
27
|
+
function orderActionRecordSortFn(
|
|
28
|
+
currentEvent: Event<OrderActionRecord>,
|
|
29
|
+
newEvent: Event<OrderActionRecord>
|
|
30
30
|
): 'less than' | 'greater than' {
|
|
31
|
-
const currentEventMarketIndex =
|
|
32
|
-
|
|
33
|
-
: currentEvent.takerOrder.marketIndex;
|
|
34
|
-
const newEventMarketIndex = !newEvent.maker.equals(PublicKey.default)
|
|
35
|
-
? newEvent.makerOrder.marketIndex
|
|
36
|
-
: newEvent.takerOrder.marketIndex;
|
|
31
|
+
const currentEventMarketIndex = currentEvent.marketIndex;
|
|
32
|
+
const newEventMarketIndex = newEvent.marketIndex;
|
|
37
33
|
if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
|
|
38
34
|
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
39
35
|
}
|
|
40
36
|
|
|
41
|
-
if (currentEvent.fillRecordId
|
|
37
|
+
if (currentEvent.fillRecordId?.gt(ZERO) && newEvent.fillRecordId?.gt(ZERO)) {
|
|
42
38
|
return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
|
|
43
39
|
? 'less than'
|
|
44
40
|
: 'greater than';
|
|
@@ -57,8 +53,8 @@ export function getSortFn(
|
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
switch (eventType) {
|
|
60
|
-
case '
|
|
61
|
-
return
|
|
56
|
+
case 'OrderActionRecord':
|
|
57
|
+
return orderActionRecordSortFn;
|
|
62
58
|
default:
|
|
63
59
|
return defaultBlockchainSortFn;
|
|
64
60
|
}
|
package/src/events/types.ts
CHANGED
|
@@ -5,8 +5,10 @@ import {
|
|
|
5
5
|
FundingRateRecord,
|
|
6
6
|
LiquidationRecord,
|
|
7
7
|
NewUserRecord,
|
|
8
|
+
OrderActionRecord,
|
|
8
9
|
OrderRecord,
|
|
9
10
|
SettlePnlRecord,
|
|
11
|
+
LPRecord,
|
|
10
12
|
} from '../index';
|
|
11
13
|
|
|
12
14
|
export type EventSubscriptionOptions = {
|
|
@@ -28,9 +30,11 @@ export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
|
|
|
28
30
|
'FundingPaymentRecord',
|
|
29
31
|
'LiquidationRecord',
|
|
30
32
|
'OrderRecord',
|
|
33
|
+
'OrderActionRecord',
|
|
31
34
|
'FundingRateRecord',
|
|
32
35
|
'NewUserRecord',
|
|
33
36
|
'SettlePnlRecord',
|
|
37
|
+
'LPRecord',
|
|
34
38
|
],
|
|
35
39
|
maxEventsPerType: 4096,
|
|
36
40
|
orderBy: 'blockchain',
|
|
@@ -63,8 +67,10 @@ export type EventMap = {
|
|
|
63
67
|
LiquidationRecord: Event<LiquidationRecord>;
|
|
64
68
|
FundingRateRecord: Event<FundingRateRecord>;
|
|
65
69
|
OrderRecord: Event<OrderRecord>;
|
|
70
|
+
OrderActionRecord: Event<OrderActionRecord>;
|
|
66
71
|
SettlePnlRecord: Event<SettlePnlRecord>;
|
|
67
72
|
NewUserRecord: Event<NewUserRecord>;
|
|
73
|
+
LPRecord: Event<LPRecord>;
|
|
68
74
|
};
|
|
69
75
|
|
|
70
76
|
export type EventType = keyof EventMap;
|
|
@@ -86,7 +92,7 @@ export type logProviderCallback = (
|
|
|
86
92
|
|
|
87
93
|
export interface LogProvider {
|
|
88
94
|
isSubscribed(): boolean;
|
|
89
|
-
subscribe(callback: logProviderCallback): boolean;
|
|
95
|
+
subscribe(callback: logProviderCallback, skipHistory?: boolean): boolean;
|
|
90
96
|
unsubscribe(): Promise<boolean>;
|
|
91
97
|
}
|
|
92
98
|
|
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
PositionDirection,
|
|
11
11
|
convertToNumber,
|
|
12
12
|
calculateTradeSlippage,
|
|
13
|
+
BulkAccountLoader,
|
|
14
|
+
Markets,
|
|
13
15
|
MARK_PRICE_PRECISION,
|
|
14
16
|
QUOTE_PRECISION,
|
|
15
17
|
} from '..';
|
|
@@ -27,9 +29,11 @@ export const getTokenAddress = (
|
|
|
27
29
|
);
|
|
28
30
|
};
|
|
29
31
|
|
|
32
|
+
const cluster = 'devnet';
|
|
33
|
+
|
|
30
34
|
const main = async () => {
|
|
31
35
|
// Initialize Drift SDK
|
|
32
|
-
const sdkConfig = initialize({ env:
|
|
36
|
+
const sdkConfig = initialize({ env: cluster });
|
|
33
37
|
|
|
34
38
|
// Set up the Wallet and Provider
|
|
35
39
|
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
@@ -63,10 +67,21 @@ const main = async () => {
|
|
|
63
67
|
const clearingHousePublicKey = new PublicKey(
|
|
64
68
|
sdkConfig.CLEARING_HOUSE_PROGRAM_ID
|
|
65
69
|
);
|
|
70
|
+
const bulkAccountLoader = new BulkAccountLoader(
|
|
71
|
+
connection,
|
|
72
|
+
'confirmed',
|
|
73
|
+
1000
|
|
74
|
+
);
|
|
66
75
|
const clearingHouse = new ClearingHouse({
|
|
67
76
|
connection,
|
|
68
77
|
wallet: provider.wallet,
|
|
69
78
|
programID: clearingHousePublicKey,
|
|
79
|
+
marketIndexes: Markets[cluster].map((market) => market.marketIndex),
|
|
80
|
+
bankIndexes: Banks[cluster].map((bank) => bank.bankIndex),
|
|
81
|
+
accountSubscription: {
|
|
82
|
+
type: 'polling',
|
|
83
|
+
accountLoader: bulkAccountLoader,
|
|
84
|
+
},
|
|
70
85
|
});
|
|
71
86
|
await clearingHouse.subscribe();
|
|
72
87
|
|
|
@@ -74,6 +89,10 @@ const main = async () => {
|
|
|
74
89
|
const user = new ClearingHouseUser({
|
|
75
90
|
clearingHouse,
|
|
76
91
|
userAccountPublicKey: await clearingHouse.getUserAccountPublicKey(),
|
|
92
|
+
accountSubscription: {
|
|
93
|
+
type: 'polling',
|
|
94
|
+
accountLoader: bulkAccountLoader,
|
|
95
|
+
},
|
|
77
96
|
});
|
|
78
97
|
|
|
79
98
|
//// Check if clearing house account exists for the current wallet
|