@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.20
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/README.md +1 -0
- package/VERSION +1 -1
- package/bun.lockb +0 -0
- package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
- package/lib/accounts/types.d.ts +5 -8
- package/lib/accounts/types.js +7 -1
- package/lib/accounts/utils.d.ts +7 -0
- package/lib/accounts/utils.js +33 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +8 -7
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
- package/lib/config.d.ts +5 -1
- package/lib/config.js +9 -1
- package/lib/constants/perpMarkets.js +21 -0
- package/lib/constants/spotMarkets.js +12 -1
- package/lib/driftClient.d.ts +44 -9
- package/lib/driftClient.js +181 -61
- package/lib/driftClientConfig.d.ts +2 -6
- package/lib/events/eventSubscriber.js +9 -8
- package/lib/events/types.js +1 -5
- package/lib/idl/drift.json +169 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/margin.d.ts +16 -1
- package/lib/math/margin.js +67 -1
- package/lib/orderParams.js +8 -8
- package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
- package/lib/orderSubscriber/OrderSubscriber.js +4 -19
- package/lib/orderSubscriber/types.d.ts +0 -9
- package/lib/tokenFaucet.js +2 -1
- package/lib/tx/baseTxSender.js +2 -2
- package/lib/tx/fastSingleTxSender.js +2 -2
- package/lib/tx/forwardOnlyTxSender.js +2 -2
- package/lib/tx/retryTxSender.js +2 -2
- package/lib/tx/txHandler.js +10 -7
- package/lib/tx/whileValidTxSender.d.ts +2 -4
- package/lib/tx/whileValidTxSender.js +16 -17
- package/lib/types.d.ts +21 -1
- package/lib/types.js +6 -1
- package/lib/user.d.ts +4 -1
- package/lib/user.js +13 -13
- package/lib/userConfig.d.ts +1 -6
- package/lib/userMap/userMap.js +0 -14
- package/lib/userMap/userMapConfig.d.ts +0 -7
- package/lib/userStatsConfig.d.ts +0 -6
- package/lib/util/TransactionConfirmationManager.d.ts +14 -0
- package/lib/util/TransactionConfirmationManager.js +96 -0
- package/package.json +4 -5
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
- package/src/accounts/types.ts +6 -9
- package/src/accounts/utils.ts +42 -0
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +43 -8
- package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
- package/src/config.ts +15 -1
- package/src/constants/perpMarkets.ts +22 -0
- package/src/constants/spotMarkets.ts +14 -1
- package/src/driftClient.ts +423 -91
- package/src/driftClientConfig.ts +2 -7
- package/src/events/eventSubscriber.ts +18 -11
- package/src/events/types.ts +1 -5
- package/src/idl/drift.json +169 -1
- package/src/index.ts +1 -0
- package/src/math/margin.ts +137 -1
- package/src/orderParams.ts +20 -12
- package/src/orderSubscriber/OrderSubscriber.ts +1 -15
- package/src/orderSubscriber/types.ts +0 -10
- package/src/tokenFaucet.ts +2 -2
- package/src/tx/baseTxSender.ts +2 -2
- package/src/tx/fastSingleTxSender.ts +2 -2
- package/src/tx/forwardOnlyTxSender.ts +2 -2
- package/src/tx/retryTxSender.ts +2 -2
- package/src/tx/txHandler.ts +8 -2
- package/src/tx/whileValidTxSender.ts +23 -26
- package/src/types.ts +30 -1
- package/src/user.ts +35 -13
- package/src/userConfig.ts +1 -7
- package/src/userMap/userMap.ts +1 -17
- package/src/userMap/userMapConfig.ts +0 -8
- package/src/userStatsConfig.ts +0 -7
- package/src/util/TransactionConfirmationManager.ts +155 -0
- package/tests/ci/idl.ts +12 -3
- package/tests/ci/verifyConstants.ts +13 -0
- package/tests/tx/TransactionConfirmationManager.test.ts +286 -0
- package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
- package/lib/accounts/grpcAccountSubscriber.js +0 -155
- package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
- package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
- package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
- package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
- package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
- package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
- package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
- package/lib/orderSubscriber/grpcSubscription.js +0 -68
- package/lib/userMap/grpcSubscription.d.ts +0 -26
- package/lib/userMap/grpcSubscription.js +0 -42
- package/src/accounts/grpcAccountSubscriber.ts +0 -158
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
- package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
- package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
- package/src/orderSubscriber/grpcSubscription.ts +0 -126
- package/src/userMap/grpcSubscription.ts +0 -83
package/src/driftClientConfig.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
|
11
11
|
import { DriftEnv } from './config';
|
|
12
12
|
import { TxSender } from './tx/types';
|
|
13
13
|
import { TxHandler, TxHandlerConfig } from './tx/txHandler';
|
|
14
|
-
import {
|
|
14
|
+
import { DelistedMarketSetting } from './accounts/types';
|
|
15
15
|
|
|
16
16
|
export type DriftClientConfig = {
|
|
17
17
|
connection: Connection;
|
|
@@ -37,6 +37,7 @@ export type DriftClientConfig = {
|
|
|
37
37
|
txParams?: TxParams; // default tx params to use
|
|
38
38
|
enableMetricsEvents?: boolean;
|
|
39
39
|
txHandlerConfig?: TxHandlerConfig;
|
|
40
|
+
delistedMarketSetting?: DelistedMarketSetting;
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
export type DriftClientSubscriptionConfig =
|
|
@@ -49,10 +50,4 @@ export type DriftClientSubscriptionConfig =
|
|
|
49
50
|
| {
|
|
50
51
|
type: 'polling';
|
|
51
52
|
accountLoader: BulkAccountLoader;
|
|
52
|
-
}
|
|
53
|
-
| {
|
|
54
|
-
type: 'grpc';
|
|
55
|
-
configs: GrpcConfigs;
|
|
56
|
-
resubTimeoutMs?: number;
|
|
57
|
-
logResubMessages?: boolean;
|
|
58
53
|
};
|
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
LogProvider,
|
|
10
10
|
EventSubscriberEvents,
|
|
11
11
|
WebSocketLogProviderConfig,
|
|
12
|
-
PollingLogProviderConfig,
|
|
13
12
|
EventsServerLogProviderConfig,
|
|
14
13
|
LogProviderType,
|
|
15
14
|
StreamingLogProviderConfig,
|
|
15
|
+
PollingLogProviderConfig,
|
|
16
16
|
} from './types';
|
|
17
17
|
import { TxEventCache } from './txEventCache';
|
|
18
18
|
import { EventList } from './eventList';
|
|
@@ -54,32 +54,39 @@ export class EventSubscriber {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
private initializeLogProvider(subscribe = false) {
|
|
57
|
+
const logProviderConfig = this.options.logProviderConfig;
|
|
58
|
+
|
|
57
59
|
if (this.currentProviderType === 'websocket') {
|
|
58
|
-
const logProviderConfig = this.options
|
|
59
|
-
.logProviderConfig as WebSocketLogProviderConfig;
|
|
60
60
|
this.logProvider = new WebSocketLogProvider(
|
|
61
61
|
// @ts-ignore
|
|
62
62
|
this.connection,
|
|
63
63
|
this.address,
|
|
64
64
|
this.options.commitment,
|
|
65
|
-
|
|
65
|
+
(
|
|
66
|
+
this.options.logProviderConfig as WebSocketLogProviderConfig
|
|
67
|
+
).resubTimeoutMs
|
|
66
68
|
);
|
|
67
69
|
} else if (this.currentProviderType === 'polling') {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
+
const frequency =
|
|
71
|
+
'frequency' in logProviderConfig
|
|
72
|
+
? (logProviderConfig as PollingLogProviderConfig).frequency
|
|
73
|
+
: (logProviderConfig as StreamingLogProviderConfig).fallbackFrequency;
|
|
74
|
+
const batchSize =
|
|
75
|
+
'batchSize' in logProviderConfig
|
|
76
|
+
? (logProviderConfig as PollingLogProviderConfig).batchSize
|
|
77
|
+
: (logProviderConfig as StreamingLogProviderConfig).fallbackBatchSize;
|
|
78
|
+
|
|
70
79
|
this.logProvider = new PollingLogProvider(
|
|
71
80
|
// @ts-ignore
|
|
72
81
|
this.connection,
|
|
73
82
|
this.address,
|
|
74
83
|
this.options.commitment,
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
frequency,
|
|
85
|
+
batchSize
|
|
77
86
|
);
|
|
78
87
|
} else if (this.currentProviderType === 'events-server') {
|
|
79
|
-
const logProviderConfig = this.options
|
|
80
|
-
.logProviderConfig as EventsServerLogProviderConfig;
|
|
81
88
|
this.logProvider = new EventsServerLogProvider(
|
|
82
|
-
logProviderConfig.url,
|
|
89
|
+
(logProviderConfig as EventsServerLogProviderConfig).url,
|
|
83
90
|
this.options.eventTypes,
|
|
84
91
|
this.options.address ? this.options.address.toString() : undefined
|
|
85
92
|
);
|
package/src/events/types.ts
CHANGED
|
@@ -56,11 +56,7 @@ export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
|
|
|
56
56
|
commitment: 'confirmed',
|
|
57
57
|
maxTx: 4096,
|
|
58
58
|
logProviderConfig: {
|
|
59
|
-
type: '
|
|
60
|
-
url: 'wss://events.drift.trade/ws',
|
|
61
|
-
maxReconnectAttempts: 5,
|
|
62
|
-
fallbackFrequency: 1000,
|
|
63
|
-
fallbackBatchSize: 100,
|
|
59
|
+
type: 'websocket',
|
|
64
60
|
},
|
|
65
61
|
};
|
|
66
62
|
|
package/src/idl/drift.json
CHANGED
|
@@ -554,7 +554,7 @@
|
|
|
554
554
|
}
|
|
555
555
|
},
|
|
556
556
|
{
|
|
557
|
-
"name": "
|
|
557
|
+
"name": "successCondition",
|
|
558
558
|
"type": {
|
|
559
559
|
"option": "u32"
|
|
560
560
|
}
|
|
@@ -608,6 +608,60 @@
|
|
|
608
608
|
}
|
|
609
609
|
]
|
|
610
610
|
},
|
|
611
|
+
{
|
|
612
|
+
"name": "placeSwiftTakerOrder",
|
|
613
|
+
"accounts": [
|
|
614
|
+
{
|
|
615
|
+
"name": "state",
|
|
616
|
+
"isMut": false,
|
|
617
|
+
"isSigner": false
|
|
618
|
+
},
|
|
619
|
+
{
|
|
620
|
+
"name": "user",
|
|
621
|
+
"isMut": true,
|
|
622
|
+
"isSigner": false
|
|
623
|
+
},
|
|
624
|
+
{
|
|
625
|
+
"name": "userStats",
|
|
626
|
+
"isMut": true,
|
|
627
|
+
"isSigner": false
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
"name": "authority",
|
|
631
|
+
"isMut": false,
|
|
632
|
+
"isSigner": true
|
|
633
|
+
},
|
|
634
|
+
{
|
|
635
|
+
"name": "ixSysvar",
|
|
636
|
+
"isMut": false,
|
|
637
|
+
"isSigner": false,
|
|
638
|
+
"docs": [
|
|
639
|
+
"the supplied Sysvar could be anything else.",
|
|
640
|
+
"The Instruction Sysvar has not been implemented",
|
|
641
|
+
"in the Anchor framework yet, so this is the safe approach."
|
|
642
|
+
]
|
|
643
|
+
}
|
|
644
|
+
],
|
|
645
|
+
"args": [
|
|
646
|
+
{
|
|
647
|
+
"name": "swiftMessageBytes",
|
|
648
|
+
"type": "bytes"
|
|
649
|
+
},
|
|
650
|
+
{
|
|
651
|
+
"name": "swiftOrderParamsMessageBytes",
|
|
652
|
+
"type": "bytes"
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"name": "swiftMessageSignature",
|
|
656
|
+
"type": {
|
|
657
|
+
"array": [
|
|
658
|
+
"u8",
|
|
659
|
+
64
|
|
660
|
+
]
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
]
|
|
664
|
+
},
|
|
611
665
|
{
|
|
612
666
|
"name": "placeSpotOrder",
|
|
613
667
|
"accounts": [
|
|
@@ -8050,6 +8104,81 @@
|
|
|
8050
8104
|
]
|
|
8051
8105
|
}
|
|
8052
8106
|
},
|
|
8107
|
+
{
|
|
8108
|
+
"name": "SwiftServerMessage",
|
|
8109
|
+
"type": {
|
|
8110
|
+
"kind": "struct",
|
|
8111
|
+
"fields": [
|
|
8112
|
+
{
|
|
8113
|
+
"name": "swiftOrderSignature",
|
|
8114
|
+
"type": {
|
|
8115
|
+
"array": [
|
|
8116
|
+
"u8",
|
|
8117
|
+
64
|
|
8118
|
+
]
|
|
8119
|
+
}
|
|
8120
|
+
},
|
|
8121
|
+
{
|
|
8122
|
+
"name": "slot",
|
|
8123
|
+
"type": "u64"
|
|
8124
|
+
}
|
|
8125
|
+
]
|
|
8126
|
+
}
|
|
8127
|
+
},
|
|
8128
|
+
{
|
|
8129
|
+
"name": "SwiftOrderParamsMessage",
|
|
8130
|
+
"type": {
|
|
8131
|
+
"kind": "struct",
|
|
8132
|
+
"fields": [
|
|
8133
|
+
{
|
|
8134
|
+
"name": "swiftOrderParams",
|
|
8135
|
+
"type": {
|
|
8136
|
+
"defined": "OrderParams"
|
|
8137
|
+
}
|
|
8138
|
+
},
|
|
8139
|
+
{
|
|
8140
|
+
"name": "expectedOrderId",
|
|
8141
|
+
"type": "i32"
|
|
8142
|
+
},
|
|
8143
|
+
{
|
|
8144
|
+
"name": "subAccountId",
|
|
8145
|
+
"type": "u16"
|
|
8146
|
+
},
|
|
8147
|
+
{
|
|
8148
|
+
"name": "takeProfitOrderParams",
|
|
8149
|
+
"type": {
|
|
8150
|
+
"option": {
|
|
8151
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8152
|
+
}
|
|
8153
|
+
}
|
|
8154
|
+
},
|
|
8155
|
+
{
|
|
8156
|
+
"name": "stopLossOrderParams",
|
|
8157
|
+
"type": {
|
|
8158
|
+
"option": {
|
|
8159
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8160
|
+
}
|
|
8161
|
+
}
|
|
8162
|
+
}
|
|
8163
|
+
]
|
|
8164
|
+
}
|
|
8165
|
+
},
|
|
8166
|
+
{
|
|
8167
|
+
"name": "SwiftTriggerOrderParams",
|
|
8168
|
+
"type": {
|
|
8169
|
+
"kind": "struct",
|
|
8170
|
+
"fields": [
|
|
8171
|
+
{
|
|
8172
|
+
"name": "triggerPrice",
|
|
8173
|
+
"type": "u64"
|
|
8174
|
+
},
|
|
8175
|
+
{
|
|
8176
|
+
"name": "baseAssetAmount",
|
|
8177
|
+
"type": "u64"
|
|
8178
|
+
}
|
|
8179
|
+
]
|
|
8180
|
+
}
|
|
8181
|
+
},
|
|
8053
8182
|
{
|
|
8054
8183
|
"name": "ModifyOrderParams",
|
|
8055
8184
|
"type": {
|
|
@@ -10122,6 +10251,20 @@
|
|
|
10122
10251
|
]
|
|
10123
10252
|
}
|
|
10124
10253
|
},
|
|
10254
|
+
{
|
|
10255
|
+
"name": "PlaceAndTakeOrderSuccessCondition",
|
|
10256
|
+
"type": {
|
|
10257
|
+
"kind": "enum",
|
|
10258
|
+
"variants": [
|
|
10259
|
+
{
|
|
10260
|
+
"name": "PartialFill"
|
|
10261
|
+
},
|
|
10262
|
+
{
|
|
10263
|
+
"name": "FullFill"
|
|
10264
|
+
}
|
|
10265
|
+
]
|
|
10266
|
+
}
|
|
10267
|
+
},
|
|
10125
10268
|
{
|
|
10126
10269
|
"name": "PerpOperation",
|
|
10127
10270
|
"type": {
|
|
@@ -12958,6 +13101,31 @@
|
|
|
12958
13101
|
"code": 6284,
|
|
12959
13102
|
"name": "InvalidPredictionMarketOrder",
|
|
12960
13103
|
"msg": "Invalid prediction market order"
|
|
13104
|
+
},
|
|
13105
|
+
{
|
|
13106
|
+
"code": 6285,
|
|
13107
|
+
"name": "InvalidVerificationIxIndex",
|
|
13108
|
+
"msg": "Ed25519 Ix must be before place and make swift order ix"
|
|
13109
|
+
},
|
|
13110
|
+
{
|
|
13111
|
+
"code": 6286,
|
|
13112
|
+
"name": "SigVerificationFailed",
|
|
13113
|
+
"msg": "Swift message verificaiton failed"
|
|
13114
|
+
},
|
|
13115
|
+
{
|
|
13116
|
+
"code": 6287,
|
|
13117
|
+
"name": "MismatchedSwiftOrderParamsMarketIndex",
|
|
13118
|
+
"msg": "Market index mismatched b/w taker and maker swift order params"
|
|
13119
|
+
},
|
|
13120
|
+
{
|
|
13121
|
+
"code": 6288,
|
|
13122
|
+
"name": "InvalidSwiftOrderParam",
|
|
13123
|
+
"msg": "Swift only available for market/oracle perp orders"
|
|
13124
|
+
},
|
|
13125
|
+
{
|
|
13126
|
+
"code": 6289,
|
|
13127
|
+
"name": "PlaceAndTakeOrderSuccessConditionFailed",
|
|
13128
|
+
"msg": "Place and take order success condition failed"
|
|
12961
13129
|
}
|
|
12962
13130
|
],
|
|
12963
13131
|
"metadata": {
|
package/src/index.ts
CHANGED
|
@@ -108,6 +108,7 @@ export * from './memcmp';
|
|
|
108
108
|
export * from './decode/user';
|
|
109
109
|
export * from './blockhashSubscriber';
|
|
110
110
|
export * from './util/chainClock';
|
|
111
|
+
export * from './util/TransactionConfirmationManager';
|
|
111
112
|
export * from './clock/clockSubscriber';
|
|
112
113
|
|
|
113
114
|
export { BN, PublicKey, pyth };
|
package/src/math/margin.ts
CHANGED
|
@@ -7,10 +7,19 @@ import {
|
|
|
7
7
|
AMM_RESERVE_PRECISION,
|
|
8
8
|
MAX_PREDICTION_PRICE,
|
|
9
9
|
BASE_PRECISION,
|
|
10
|
+
MARGIN_PRECISION,
|
|
11
|
+
PRICE_PRECISION,
|
|
12
|
+
QUOTE_PRECISION,
|
|
10
13
|
} from '../constants/numericConstants';
|
|
11
14
|
import { BN } from '@coral-xyz/anchor';
|
|
12
15
|
import { OraclePriceData } from '../oracles/types';
|
|
13
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
calculateMarketMarginRatio,
|
|
18
|
+
calculateScaledInitialAssetWeight,
|
|
19
|
+
DriftClient,
|
|
20
|
+
PerpMarketAccount,
|
|
21
|
+
PerpPosition,
|
|
22
|
+
} from '..';
|
|
14
23
|
import { isVariant } from '../types';
|
|
15
24
|
import { assert } from '../assert/assert';
|
|
16
25
|
|
|
@@ -194,3 +203,130 @@ export function calculatePerpLiabilityValue(
|
|
|
194
203
|
return baseAssetAmount.abs().mul(oraclePrice).div(BASE_PRECISION);
|
|
195
204
|
}
|
|
196
205
|
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Calculates the margin required to open a trade, in quote amount. Only accounts for the trade size as a scalar value, does not account for the trade direction or current open positions and whether the trade would _actually_ be risk-increasing and use any extra collateral.
|
|
209
|
+
* @param targetMarketIndex
|
|
210
|
+
* @param baseSize
|
|
211
|
+
* @returns
|
|
212
|
+
*/
|
|
213
|
+
export function calculateMarginUSDCRequiredForTrade(
|
|
214
|
+
driftClient: DriftClient,
|
|
215
|
+
targetMarketIndex: number,
|
|
216
|
+
baseSize: BN,
|
|
217
|
+
userMaxMarginRatio?: number
|
|
218
|
+
): BN {
|
|
219
|
+
const targetMarket = driftClient.getPerpMarketAccount(targetMarketIndex);
|
|
220
|
+
const oracleData = driftClient.getOracleDataForPerpMarket(
|
|
221
|
+
targetMarket.marketIndex
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const perpLiabilityValue = calculatePerpLiabilityValue(
|
|
225
|
+
baseSize,
|
|
226
|
+
oracleData.price,
|
|
227
|
+
isVariant(targetMarket.contractType, 'prediction')
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
const marginRequired = new BN(
|
|
231
|
+
calculateMarketMarginRatio(
|
|
232
|
+
targetMarket,
|
|
233
|
+
baseSize.abs(),
|
|
234
|
+
'Initial',
|
|
235
|
+
userMaxMarginRatio
|
|
236
|
+
)
|
|
237
|
+
)
|
|
238
|
+
.mul(perpLiabilityValue)
|
|
239
|
+
.div(MARGIN_PRECISION);
|
|
240
|
+
|
|
241
|
+
return marginRequired;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Similar to calculatetMarginUSDCRequiredForTrade, but calculates how much of a given collateral is required to cover the margin requirements for a given trade. Basically does the same thing as getMarginUSDCRequiredForTrade but also accounts for asset weight of the selected collateral.
|
|
246
|
+
*
|
|
247
|
+
* Returns collateral required in the precision of the target collateral market.
|
|
248
|
+
*/
|
|
249
|
+
export function calculateCollateralDepositRequiredForTrade(
|
|
250
|
+
driftClient: DriftClient,
|
|
251
|
+
targetMarketIndex: number,
|
|
252
|
+
baseSize: BN,
|
|
253
|
+
collateralIndex: number,
|
|
254
|
+
userMaxMarginRatio?: number
|
|
255
|
+
): BN {
|
|
256
|
+
const marginRequiredUsdc = calculateMarginUSDCRequiredForTrade(
|
|
257
|
+
driftClient,
|
|
258
|
+
targetMarketIndex,
|
|
259
|
+
baseSize,
|
|
260
|
+
userMaxMarginRatio
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
|
264
|
+
|
|
265
|
+
const collateralOracleData =
|
|
266
|
+
driftClient.getOracleDataForSpotMarket(collateralIndex);
|
|
267
|
+
|
|
268
|
+
const scaledAssetWeight = calculateScaledInitialAssetWeight(
|
|
269
|
+
collateralMarket,
|
|
270
|
+
collateralOracleData.price
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
// Base amount required to deposit = (marginRequiredUsdc / priceOfAsset) / assetWeight .. (E.g. $100 required / $10000 price / 0.5 weight)
|
|
274
|
+
const baseAmountRequired = driftClient
|
|
275
|
+
.convertToSpotPrecision(collateralIndex, marginRequiredUsdc)
|
|
276
|
+
.mul(PRICE_PRECISION) // adjust for division by oracle price
|
|
277
|
+
.mul(SPOT_MARKET_WEIGHT_PRECISION) // adjust for division by scaled asset weight
|
|
278
|
+
.div(collateralOracleData.price)
|
|
279
|
+
.div(scaledAssetWeight)
|
|
280
|
+
.div(QUOTE_PRECISION); // adjust for marginRequiredUsdc value's QUOTE_PRECISION
|
|
281
|
+
|
|
282
|
+
// TODO : Round by step size?
|
|
283
|
+
|
|
284
|
+
return baseAmountRequired;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function calculateCollateralValueOfDeposit(
|
|
288
|
+
driftClient: DriftClient,
|
|
289
|
+
collateralIndex: number,
|
|
290
|
+
baseSize: BN
|
|
291
|
+
): BN {
|
|
292
|
+
const collateralMarket = driftClient.getSpotMarketAccount(collateralIndex);
|
|
293
|
+
|
|
294
|
+
const collateralOracleData =
|
|
295
|
+
driftClient.getOracleDataForSpotMarket(collateralIndex);
|
|
296
|
+
|
|
297
|
+
const scaledAssetWeight = calculateScaledInitialAssetWeight(
|
|
298
|
+
collateralMarket,
|
|
299
|
+
collateralOracleData.price
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
// CollateralBaseValue = oracle price * collateral base amount (and shift to QUOTE_PRECISION)
|
|
303
|
+
const collateralBaseValue = collateralOracleData.price
|
|
304
|
+
.mul(baseSize)
|
|
305
|
+
.mul(QUOTE_PRECISION)
|
|
306
|
+
.div(PRICE_PRECISION)
|
|
307
|
+
.div(new BN(10).pow(new BN(collateralMarket.decimals)));
|
|
308
|
+
|
|
309
|
+
const depositCollateralValue = collateralBaseValue
|
|
310
|
+
.mul(scaledAssetWeight)
|
|
311
|
+
.div(SPOT_MARKET_WEIGHT_PRECISION);
|
|
312
|
+
|
|
313
|
+
return depositCollateralValue;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function calculateLiquidationPrice(
|
|
317
|
+
freeCollateral: BN,
|
|
318
|
+
freeCollateralDelta: BN,
|
|
319
|
+
oraclePrice: BN
|
|
320
|
+
): BN {
|
|
321
|
+
const liqPriceDelta = freeCollateral
|
|
322
|
+
.mul(QUOTE_PRECISION)
|
|
323
|
+
.div(freeCollateralDelta);
|
|
324
|
+
|
|
325
|
+
const liqPrice = oraclePrice.sub(liqPriceDelta);
|
|
326
|
+
|
|
327
|
+
if (liqPrice.lt(ZERO)) {
|
|
328
|
+
return new BN(-1);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return liqPrice;
|
|
332
|
+
}
|
package/src/orderParams.ts
CHANGED
|
@@ -10,9 +10,11 @@ import { BN } from '@coral-xyz/anchor';
|
|
|
10
10
|
export function getLimitOrderParams(
|
|
11
11
|
params: Omit<OptionalOrderParams, 'orderType'> & { price: BN }
|
|
12
12
|
): OptionalOrderParams {
|
|
13
|
-
return
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
return getOrderParams(
|
|
14
|
+
Object.assign({}, params, {
|
|
15
|
+
orderType: OrderType.LIMIT,
|
|
16
|
+
})
|
|
17
|
+
);
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export function getTriggerMarketOrderParams(
|
|
@@ -21,9 +23,11 @@ export function getTriggerMarketOrderParams(
|
|
|
21
23
|
triggerPrice: BN;
|
|
22
24
|
}
|
|
23
25
|
): OptionalOrderParams {
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
return getOrderParams(
|
|
27
|
+
Object.assign({}, params, {
|
|
28
|
+
orderType: OrderType.TRIGGER_MARKET,
|
|
29
|
+
})
|
|
30
|
+
);
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export function getTriggerLimitOrderParams(
|
|
@@ -33,17 +37,21 @@ export function getTriggerLimitOrderParams(
|
|
|
33
37
|
price: BN;
|
|
34
38
|
}
|
|
35
39
|
): OptionalOrderParams {
|
|
36
|
-
return
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
return getOrderParams(
|
|
41
|
+
Object.assign({}, params, {
|
|
42
|
+
orderType: OrderType.TRIGGER_LIMIT,
|
|
43
|
+
})
|
|
44
|
+
);
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
export function getMarketOrderParams(
|
|
42
48
|
params: Omit<OptionalOrderParams, 'orderType'>
|
|
43
49
|
): OptionalOrderParams {
|
|
44
|
-
return
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
return getOrderParams(
|
|
51
|
+
Object.assign({}, params, {
|
|
52
|
+
orderType: OrderType.MARKET,
|
|
53
|
+
})
|
|
54
|
+
);
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
/**
|
|
@@ -11,12 +11,11 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
11
11
|
import { EventEmitter } from 'events';
|
|
12
12
|
import { BN } from '../index';
|
|
13
13
|
import { decodeUser } from '../decode/user';
|
|
14
|
-
import { grpcSubscription } from './grpcSubscription';
|
|
15
14
|
|
|
16
15
|
export class OrderSubscriber {
|
|
17
16
|
driftClient: DriftClient;
|
|
18
17
|
usersAccounts = new Map<string, { slot: number; userAccount: UserAccount }>();
|
|
19
|
-
subscription: PollingSubscription | WebsocketSubscription
|
|
18
|
+
subscription: PollingSubscription | WebsocketSubscription;
|
|
20
19
|
commitment: Commitment;
|
|
21
20
|
eventEmitter: StrictEventEmitter<EventEmitter, OrderSubscriberEvents>;
|
|
22
21
|
|
|
@@ -35,19 +34,6 @@ export class OrderSubscriber {
|
|
|
35
34
|
orderSubscriber: this,
|
|
36
35
|
frequency: config.subscriptionConfig.frequency,
|
|
37
36
|
});
|
|
38
|
-
} else if (config.subscriptionConfig.type === 'grpc') {
|
|
39
|
-
this.subscription = new grpcSubscription({
|
|
40
|
-
grpcConfigs: config.subscriptionConfig.configs,
|
|
41
|
-
orderSubscriber: this,
|
|
42
|
-
commitment: this.commitment,
|
|
43
|
-
skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
|
|
44
|
-
resubOpts: {
|
|
45
|
-
resubTimeoutMs: config.subscriptionConfig?.resubTimeoutMs,
|
|
46
|
-
logResubMessages: config.subscriptionConfig?.logResubMessages,
|
|
47
|
-
},
|
|
48
|
-
resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
|
|
49
|
-
decoded: config.decodeData,
|
|
50
|
-
});
|
|
51
37
|
} else {
|
|
52
38
|
this.subscription = new WebsocketSubscription({
|
|
53
39
|
orderSubscriber: this,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import { Order, UserAccount } from '../types';
|
|
3
3
|
import { DriftClient } from '../driftClient';
|
|
4
|
-
import { GrpcConfigs } from '../accounts/types';
|
|
5
4
|
|
|
6
5
|
export type OrderSubscriberConfig = {
|
|
7
6
|
driftClient: DriftClient;
|
|
@@ -11,15 +10,6 @@ export type OrderSubscriberConfig = {
|
|
|
11
10
|
frequency: number;
|
|
12
11
|
commitment?: Commitment;
|
|
13
12
|
}
|
|
14
|
-
| {
|
|
15
|
-
type: 'grpc';
|
|
16
|
-
skipInitialLoad?: boolean;
|
|
17
|
-
resubTimeoutMs?: number;
|
|
18
|
-
logResubMessages?: boolean;
|
|
19
|
-
resyncIntervalMs?: number;
|
|
20
|
-
configs: GrpcConfigs;
|
|
21
|
-
commitment?: Commitment;
|
|
22
|
-
}
|
|
23
13
|
| {
|
|
24
14
|
type: 'websocket';
|
|
25
15
|
skipInitialLoad?: boolean;
|
package/src/tokenFaucet.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
TransactionInstruction,
|
|
17
17
|
TransactionSignature,
|
|
18
18
|
} from '@solana/web3.js';
|
|
19
|
-
import { BN } from '.';
|
|
19
|
+
import { BN, DEFAULT_CONFIRMATION_OPTS } from '.';
|
|
20
20
|
import tokenFaucet from './idl/token_faucet.json';
|
|
21
21
|
import { IWallet } from './types';
|
|
22
22
|
import { BankrunContextWrapper } from './bankrun/bankrunConnection';
|
|
@@ -41,7 +41,7 @@ export class TokenFaucet {
|
|
|
41
41
|
this.connection = connection;
|
|
42
42
|
this.context = context;
|
|
43
43
|
this.wallet = wallet;
|
|
44
|
-
this.opts = opts ||
|
|
44
|
+
this.opts = opts || DEFAULT_CONFIRMATION_OPTS;
|
|
45
45
|
// @ts-ignore
|
|
46
46
|
const provider = new AnchorProvider(
|
|
47
47
|
context ? context.connection.toConnection() : this.connection,
|
package/src/tx/baseTxSender.ts
CHANGED
|
@@ -20,12 +20,12 @@ import {
|
|
|
20
20
|
AddressLookupTableAccount,
|
|
21
21
|
BlockhashWithExpiryBlockHeight,
|
|
22
22
|
} from '@solana/web3.js';
|
|
23
|
-
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
24
23
|
import assert from 'assert';
|
|
25
24
|
import bs58 from 'bs58';
|
|
26
25
|
import { TxHandler } from './txHandler';
|
|
27
26
|
import { IWallet } from '../types';
|
|
28
27
|
import NodeCache from 'node-cache';
|
|
28
|
+
import { DEFAULT_CONFIRMATION_OPTS } from '../config';
|
|
29
29
|
|
|
30
30
|
const BASELINE_TX_LAND_RATE = 0.9;
|
|
31
31
|
const DEFAULT_TIMEOUT = 35000;
|
|
@@ -54,7 +54,7 @@ export abstract class BaseTxSender implements TxSender {
|
|
|
54
54
|
public constructor({
|
|
55
55
|
connection,
|
|
56
56
|
wallet,
|
|
57
|
-
opts =
|
|
57
|
+
opts = DEFAULT_CONFIRMATION_OPTS,
|
|
58
58
|
timeout = DEFAULT_TIMEOUT,
|
|
59
59
|
additionalConnections = new Array<Connection>(),
|
|
60
60
|
confirmationStrategy = ConfirmationStrategy.Combo,
|
|
@@ -6,10 +6,10 @@ import {
|
|
|
6
6
|
Commitment,
|
|
7
7
|
BlockhashWithExpiryBlockHeight,
|
|
8
8
|
} from '@solana/web3.js';
|
|
9
|
-
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
10
9
|
import { BaseTxSender } from './baseTxSender';
|
|
11
10
|
import { TxHandler } from './txHandler';
|
|
12
11
|
import { IWallet } from '../types';
|
|
12
|
+
import { DEFAULT_CONFIRMATION_OPTS } from '../config';
|
|
13
13
|
|
|
14
14
|
const DEFAULT_TIMEOUT = 35000;
|
|
15
15
|
const DEFAULT_BLOCKHASH_REFRESH = 10000;
|
|
@@ -31,7 +31,7 @@ export class FastSingleTxSender extends BaseTxSender {
|
|
|
31
31
|
public constructor({
|
|
32
32
|
connection,
|
|
33
33
|
wallet,
|
|
34
|
-
opts = { ...
|
|
34
|
+
opts = { ...DEFAULT_CONFIRMATION_OPTS, maxRetries: 0 },
|
|
35
35
|
timeout = DEFAULT_TIMEOUT,
|
|
36
36
|
blockhashRefreshInterval = DEFAULT_BLOCKHASH_REFRESH,
|
|
37
37
|
additionalConnections = new Array<Connection>(),
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
2
1
|
import {
|
|
3
2
|
ConfirmOptions,
|
|
4
3
|
Connection,
|
|
@@ -9,6 +8,7 @@ import { BaseTxSender } from './baseTxSender';
|
|
|
9
8
|
import { ConfirmationStrategy, TxSigAndSlot } from './types';
|
|
10
9
|
import { TxHandler } from './txHandler';
|
|
11
10
|
import { IWallet } from '../types';
|
|
11
|
+
import { DEFAULT_CONFIRMATION_OPTS } from '../config';
|
|
12
12
|
|
|
13
13
|
const DEFAULT_TIMEOUT = 35000;
|
|
14
14
|
const DEFAULT_RETRY = 5000;
|
|
@@ -29,7 +29,7 @@ export class ForwardOnlyTxSender extends BaseTxSender {
|
|
|
29
29
|
public constructor({
|
|
30
30
|
connection,
|
|
31
31
|
wallet,
|
|
32
|
-
opts = { ...
|
|
32
|
+
opts = { ...DEFAULT_CONFIRMATION_OPTS, maxRetries: 0 },
|
|
33
33
|
timeout = DEFAULT_TIMEOUT,
|
|
34
34
|
retrySleep = DEFAULT_RETRY,
|
|
35
35
|
confirmationStrategy = ConfirmationStrategy.Combo,
|
package/src/tx/retryTxSender.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ConfirmationStrategy, TxSigAndSlot } from './types';
|
|
2
2
|
import { ConfirmOptions, Connection } from '@solana/web3.js';
|
|
3
|
-
import { AnchorProvider } from '@coral-xyz/anchor';
|
|
4
3
|
import { BaseTxSender } from './baseTxSender';
|
|
5
4
|
import { TxHandler } from './txHandler';
|
|
6
5
|
import { IWallet } from '../types';
|
|
6
|
+
import { DEFAULT_CONFIRMATION_OPTS } from '../config';
|
|
7
7
|
|
|
8
8
|
const DEFAULT_TIMEOUT = 35000;
|
|
9
9
|
const DEFAULT_RETRY = 2000;
|
|
@@ -24,7 +24,7 @@ export class RetryTxSender extends BaseTxSender {
|
|
|
24
24
|
public constructor({
|
|
25
25
|
connection,
|
|
26
26
|
wallet,
|
|
27
|
-
opts = { ...
|
|
27
|
+
opts = { ...DEFAULT_CONFIRMATION_OPTS, maxRetries: 0 },
|
|
28
28
|
timeout = DEFAULT_TIMEOUT,
|
|
29
29
|
retrySleep = DEFAULT_RETRY,
|
|
30
30
|
additionalConnections = new Array<Connection>(),
|