@drift-labs/sdk 2.96.0-beta.9 → 2.97.0-beta.1
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 +3 -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 -0
- 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/webSocketDriftClientAccountSubscriber.d.ts +5 -4
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
- package/lib/config.d.ts +6 -1
- package/lib/config.js +10 -1
- package/lib/constants/perpMarkets.js +33 -1
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/constants/txConstants.d.ts +1 -0
- package/lib/constants/txConstants.js +4 -0
- package/lib/driftClient.d.ts +36 -8
- package/lib/driftClient.js +166 -43
- package/lib/driftClientConfig.d.ts +3 -0
- 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/orderParams.js +8 -8
- package/lib/orderSubscriber/OrderSubscriber.js +1 -6
- package/lib/tokenFaucet.js +2 -1
- package/lib/tx/baseTxSender.d.ts +0 -1
- package/lib/tx/baseTxSender.js +8 -26
- package/lib/tx/fastSingleTxSender.js +2 -2
- package/lib/tx/forwardOnlyTxSender.js +2 -2
- package/lib/tx/reportTransactionError.d.ts +20 -0
- package/lib/tx/reportTransactionError.js +103 -0
- package/lib/tx/retryTxSender.js +2 -2
- package/lib/tx/txHandler.js +10 -7
- package/lib/tx/whileValidTxSender.d.ts +4 -5
- package/lib/tx/whileValidTxSender.js +16 -17
- package/lib/types.d.ts +22 -1
- package/lib/types.js +6 -1
- package/lib/util/TransactionConfirmationManager.d.ts +16 -0
- package/lib/util/TransactionConfirmationManager.js +174 -0
- package/package.json +4 -3
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
- package/src/accounts/types.ts +6 -0
- package/src/accounts/utils.ts +42 -0
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +40 -5
- package/src/config.ts +17 -1
- package/src/constants/perpMarkets.ts +35 -1
- package/src/constants/spotMarkets.ts +11 -0
- package/src/constants/txConstants.ts +1 -0
- package/src/driftClient.ts +346 -53
- package/src/driftClientConfig.ts +3 -0
- package/src/events/types.ts +1 -5
- package/src/idl/drift.json +169 -1
- package/src/index.ts +1 -0
- package/src/orderParams.ts +20 -12
- package/src/orderSubscriber/OrderSubscriber.ts +2 -5
- package/src/tokenFaucet.ts +2 -2
- package/src/tx/baseTxSender.ts +10 -32
- package/src/tx/fastSingleTxSender.ts +2 -2
- package/src/tx/forwardOnlyTxSender.ts +2 -2
- package/src/tx/reportTransactionError.ts +159 -0
- package/src/tx/retryTxSender.ts +2 -2
- package/src/tx/txHandler.ts +8 -2
- package/src/tx/whileValidTxSender.ts +18 -27
- package/src/types.ts +31 -1
- package/src/util/TransactionConfirmationManager.ts +292 -0
- package/tests/ci/verifyConstants.ts +13 -0
- package/tests/tx/TransactionConfirmationManager.test.ts +305 -0
package/lib/driftClient.js
CHANGED
|
@@ -27,16 +27,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.DriftClient = void 0;
|
|
30
|
+
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
30
31
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
31
32
|
const anchor_30_1 = require("@coral-xyz/anchor-30");
|
|
32
33
|
const bs58_1 = __importDefault(require("bs58"));
|
|
33
34
|
const spl_token_1 = require("@solana/spl-token");
|
|
34
35
|
const types_1 = require("./types");
|
|
35
|
-
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
36
36
|
const drift_json_1 = __importDefault(require("./idl/drift.json"));
|
|
37
37
|
const web3_js_1 = require("@solana/web3.js");
|
|
38
38
|
const events_1 = require("events");
|
|
39
39
|
const pda_1 = require("./addresses/pda");
|
|
40
|
+
const types_2 = require("./accounts/types");
|
|
40
41
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
41
42
|
const position_1 = require("./math/position");
|
|
42
43
|
const spotBalance_1 = require("./math/spotBalance");
|
|
@@ -62,12 +63,11 @@ const txHandler_1 = require("./tx/txHandler");
|
|
|
62
63
|
const pyth_solana_receiver_1 = require("@pythnetwork/pyth-solana-receiver");
|
|
63
64
|
const price_service_sdk_1 = require("@pythnetwork/price-service-sdk");
|
|
64
65
|
const address_1 = require("@pythnetwork/pyth-solana-receiver/lib/address");
|
|
65
|
-
const config_2 = require("./config");
|
|
66
66
|
const pythPullOracleUtils_1 = require("./util/pythPullOracleUtils");
|
|
67
67
|
const utils_2 = require("./tx/utils");
|
|
68
68
|
const pyth_solana_receiver_json_1 = __importDefault(require("./idl/pyth_solana_receiver.json"));
|
|
69
69
|
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
|
70
|
-
const
|
|
70
|
+
const ed = __importStar(require("@noble/ed25519"));
|
|
71
71
|
/**
|
|
72
72
|
* # DriftClient
|
|
73
73
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -80,7 +80,7 @@ class DriftClient {
|
|
|
80
80
|
this._isSubscribed = val;
|
|
81
81
|
}
|
|
82
82
|
constructor(config) {
|
|
83
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
|
|
83
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8;
|
|
84
84
|
this.users = new Map();
|
|
85
85
|
this._isSubscribed = false;
|
|
86
86
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -90,7 +90,7 @@ class DriftClient {
|
|
|
90
90
|
this.connection = config.connection;
|
|
91
91
|
this.wallet = config.wallet;
|
|
92
92
|
this.opts = config.opts || {
|
|
93
|
-
...
|
|
93
|
+
...config_1.DEFAULT_CONFIRMATION_OPTS,
|
|
94
94
|
commitment: (_a = config === null || config === void 0 ? void 0 : config.connection) === null || _a === void 0 ? void 0 : _a.commitment,
|
|
95
95
|
preflightCommitment: (_b = config === null || config === void 0 ? void 0 : config.connection) === null || _b === void 0 ? void 0 : _b.commitment, // At the moment this ensures that our transaction simulations (which use Connection object) will use the same commitment level as our Transaction blockhashes (which use these opts)
|
|
96
96
|
};
|
|
@@ -98,17 +98,19 @@ class DriftClient {
|
|
|
98
98
|
// @ts-ignore
|
|
99
99
|
config.wallet, this.opts);
|
|
100
100
|
this.program = new anchor_1.Program(drift_json_1.default, (_c = config.programID) !== null && _c !== void 0 ? _c : new web3_js_1.PublicKey(config_1.DRIFT_PROGRAM_ID), this.provider);
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
101
|
+
this.swiftID = (_d = config.swiftID) !== null && _d !== void 0 ? _d : new web3_js_1.PublicKey(config_1.SWIFT_ID);
|
|
102
|
+
this.authority = (_e = config.authority) !== null && _e !== void 0 ? _e : this.wallet.publicKey;
|
|
103
|
+
this.activeSubAccountId = (_f = config.activeSubAccountId) !== null && _f !== void 0 ? _f : 0;
|
|
104
|
+
this.skipLoadUsers = (_g = config.skipLoadUsers) !== null && _g !== void 0 ? _g : false;
|
|
105
|
+
this.txVersion = (_h = config.txVersion) !== null && _h !== void 0 ? _h : 'legacy';
|
|
105
106
|
this.txParams = {
|
|
106
|
-
computeUnits: (
|
|
107
|
-
computeUnitsPrice: (
|
|
107
|
+
computeUnits: (_k = (_j = config.txParams) === null || _j === void 0 ? void 0 : _j.computeUnits) !== null && _k !== void 0 ? _k : 600000,
|
|
108
|
+
computeUnitsPrice: (_m = (_l = config.txParams) === null || _l === void 0 ? void 0 : _l.computeUnitsPrice) !== null && _m !== void 0 ? _m : 0,
|
|
108
109
|
};
|
|
109
110
|
this.txHandler =
|
|
110
|
-
(
|
|
111
|
+
(_o = config === null || config === void 0 ? void 0 : config.txHandler) !== null && _o !== void 0 ? _o : new txHandler_1.TxHandler({
|
|
111
112
|
connection: this.connection,
|
|
113
|
+
// @ts-ignore
|
|
112
114
|
wallet: this.provider.wallet,
|
|
113
115
|
confirmationOptions: this.opts,
|
|
114
116
|
opts: {
|
|
@@ -132,8 +134,8 @@ class DriftClient {
|
|
|
132
134
|
: config.subAccountIds
|
|
133
135
|
? new Map([[this.authority.toString(), config.subAccountIds]])
|
|
134
136
|
: new Map();
|
|
135
|
-
this.includeDelegates = (
|
|
136
|
-
if (((
|
|
137
|
+
this.includeDelegates = (_p = config.includeDelegates) !== null && _p !== void 0 ? _p : false;
|
|
138
|
+
if (((_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.type) === 'polling') {
|
|
137
139
|
this.userAccountSubscriptionConfig = {
|
|
138
140
|
type: 'polling',
|
|
139
141
|
accountLoader: config.accountSubscription.accountLoader,
|
|
@@ -146,15 +148,15 @@ class DriftClient {
|
|
|
146
148
|
else {
|
|
147
149
|
this.userAccountSubscriptionConfig = {
|
|
148
150
|
type: 'websocket',
|
|
149
|
-
resubTimeoutMs: (
|
|
150
|
-
logResubMessages: (
|
|
151
|
-
commitment: (
|
|
151
|
+
resubTimeoutMs: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.resubTimeoutMs,
|
|
152
|
+
logResubMessages: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.logResubMessages,
|
|
153
|
+
commitment: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.commitment,
|
|
152
154
|
};
|
|
153
155
|
this.userStatsAccountSubscriptionConfig = {
|
|
154
156
|
type: 'websocket',
|
|
155
|
-
resubTimeoutMs: (
|
|
156
|
-
logResubMessages: (
|
|
157
|
-
commitment: (
|
|
157
|
+
resubTimeoutMs: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.resubTimeoutMs,
|
|
158
|
+
logResubMessages: (_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.logResubMessages,
|
|
159
|
+
commitment: (_w = config.accountSubscription) === null || _w === void 0 ? void 0 : _w.commitment,
|
|
158
160
|
};
|
|
159
161
|
}
|
|
160
162
|
if (config.userStats) {
|
|
@@ -168,17 +170,18 @@ class DriftClient {
|
|
|
168
170
|
if (config.env && !this.marketLookupTable) {
|
|
169
171
|
this.marketLookupTable = new web3_js_1.PublicKey(config_1.configs[config.env].MARKET_LOOKUP_TABLE);
|
|
170
172
|
}
|
|
173
|
+
const delistedMarketSetting = config.delistedMarketSetting || types_2.DelistedMarketSetting.Subscribe;
|
|
171
174
|
const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
|
|
172
175
|
config.spotMarketIndexes === undefined &&
|
|
173
176
|
config.oracleInfos === undefined;
|
|
174
|
-
if (((
|
|
175
|
-
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (
|
|
177
|
+
if (((_x = config.accountSubscription) === null || _x === void 0 ? void 0 : _x.type) === 'polling') {
|
|
178
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_y = config.perpMarketIndexes) !== null && _y !== void 0 ? _y : [], (_z = config.spotMarketIndexes) !== null && _z !== void 0 ? _z : [], (_0 = config.oracleInfos) !== null && _0 !== void 0 ? _0 : [], noMarketsAndOraclesSpecified, delistedMarketSetting);
|
|
176
179
|
}
|
|
177
180
|
else {
|
|
178
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (
|
|
179
|
-
resubTimeoutMs: (
|
|
180
|
-
logResubMessages: (
|
|
181
|
-
}, (
|
|
181
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_1 = config.perpMarketIndexes) !== null && _1 !== void 0 ? _1 : [], (_2 = config.spotMarketIndexes) !== null && _2 !== void 0 ? _2 : [], (_3 = config.oracleInfos) !== null && _3 !== void 0 ? _3 : [], noMarketsAndOraclesSpecified, delistedMarketSetting, {
|
|
182
|
+
resubTimeoutMs: (_4 = config.accountSubscription) === null || _4 === void 0 ? void 0 : _4.resubTimeoutMs,
|
|
183
|
+
logResubMessages: (_5 = config.accountSubscription) === null || _5 === void 0 ? void 0 : _5.logResubMessages,
|
|
184
|
+
}, (_6 = config.accountSubscription) === null || _6 === void 0 ? void 0 : _6.commitment);
|
|
182
185
|
}
|
|
183
186
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
184
187
|
this.metricsEventEmitter = new events_1.EventEmitter();
|
|
@@ -186,12 +189,14 @@ class DriftClient {
|
|
|
186
189
|
this.enableMetricsEvents = true;
|
|
187
190
|
}
|
|
188
191
|
this.txSender =
|
|
189
|
-
(
|
|
192
|
+
(_7 = config.txSender) !== null && _7 !== void 0 ? _7 : new retryTxSender_1.RetryTxSender({
|
|
190
193
|
connection: this.connection,
|
|
191
194
|
wallet: this.wallet,
|
|
192
195
|
opts: this.opts,
|
|
193
196
|
txHandler: this.txHandler,
|
|
194
197
|
});
|
|
198
|
+
this.sbOnDemandProgramdId =
|
|
199
|
+
config_1.configs[(_8 = config.env) !== null && _8 !== void 0 ? _8 : 'mainnet-beta'].SB_ON_DEMAND_PID;
|
|
195
200
|
}
|
|
196
201
|
getUserMapKey(subAccountId, authority) {
|
|
197
202
|
return `${subAccountId}_${authority.toString()}`;
|
|
@@ -1606,7 +1611,7 @@ class DriftClient {
|
|
|
1606
1611
|
direction,
|
|
1607
1612
|
baseAssetAmount: amount,
|
|
1608
1613
|
price: limitPrice,
|
|
1609
|
-
}, undefined, undefined, undefined, subAccountId);
|
|
1614
|
+
}, undefined, undefined, undefined, undefined, subAccountId);
|
|
1610
1615
|
}
|
|
1611
1616
|
async sendSignedTx(tx, opts) {
|
|
1612
1617
|
const { txSig } = await this.sendTransaction(tx, undefined, opts !== null && opts !== void 0 ? opts : this.opts, true);
|
|
@@ -2724,8 +2729,8 @@ class DriftClient {
|
|
|
2724
2729
|
remainingAccounts,
|
|
2725
2730
|
});
|
|
2726
2731
|
}
|
|
2727
|
-
async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, txParams, subAccountId) {
|
|
2728
|
-
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId), txParams), [], this.opts);
|
|
2732
|
+
async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, successCondition, txParams, subAccountId) {
|
|
2733
|
+
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, successCondition, subAccountId), txParams), [], this.opts);
|
|
2729
2734
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2730
2735
|
return txSig;
|
|
2731
2736
|
}
|
|
@@ -2741,7 +2746,7 @@ class DriftClient {
|
|
|
2741
2746
|
let earlyExitFailedPlaceAndTakeSim = false;
|
|
2742
2747
|
const prepPlaceAndTakeTx = async () => {
|
|
2743
2748
|
var _a;
|
|
2744
|
-
const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId);
|
|
2749
|
+
const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, undefined, subAccountId);
|
|
2745
2750
|
placeAndTakeIxs.push(placeAndTakeIx);
|
|
2746
2751
|
if (bracketOrdersParams.length > 0) {
|
|
2747
2752
|
const bracketOrdersIx = await this.getPlaceOrdersIx(bracketOrdersParams, subAccountId);
|
|
@@ -2800,7 +2805,9 @@ class DriftClient {
|
|
|
2800
2805
|
if (!txsToSign) {
|
|
2801
2806
|
return null;
|
|
2802
2807
|
}
|
|
2803
|
-
const signedTxs = (await this.txHandler.getSignedTransactionMap(txsToSign,
|
|
2808
|
+
const signedTxs = (await this.txHandler.getSignedTransactionMap(txsToSign,
|
|
2809
|
+
// @ts-ignore
|
|
2810
|
+
this.provider.wallet)).signedTxMap;
|
|
2804
2811
|
const { txSig, slot } = await this.sendTransaction(signedTxs.placeAndTakeTx, [], this.opts, true);
|
|
2805
2812
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2806
2813
|
return {
|
|
@@ -2809,7 +2816,7 @@ class DriftClient {
|
|
|
2809
2816
|
signedSettlePnlTx: signedTxs.settlePnlTx,
|
|
2810
2817
|
};
|
|
2811
2818
|
}
|
|
2812
|
-
async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId) {
|
|
2819
|
+
async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, successCondition, subAccountId) {
|
|
2813
2820
|
orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
|
|
2814
2821
|
const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
|
|
2815
2822
|
const user = await this.getUserAccountPublicKey(subAccountId);
|
|
@@ -2855,7 +2862,7 @@ class DriftClient {
|
|
|
2855
2862
|
});
|
|
2856
2863
|
}
|
|
2857
2864
|
}
|
|
2858
|
-
return await this.program.instruction.placeAndTakePerpOrder(orderParams, null, {
|
|
2865
|
+
return await this.program.instruction.placeAndTakePerpOrder(orderParams, successCondition !== null && successCondition !== void 0 ? successCondition : null, {
|
|
2859
2866
|
accounts: {
|
|
2860
2867
|
state: await this.getStatePublicKey(),
|
|
2861
2868
|
user,
|
|
@@ -2907,6 +2914,123 @@ class DriftClient {
|
|
|
2907
2914
|
remainingAccounts,
|
|
2908
2915
|
});
|
|
2909
2916
|
}
|
|
2917
|
+
encodeSwiftServerMessage(message) {
|
|
2918
|
+
const messageWithBuffer = {
|
|
2919
|
+
slot: message.slot,
|
|
2920
|
+
swiftOrderSignature: message.swiftOrderSignature,
|
|
2921
|
+
};
|
|
2922
|
+
return this.program.coder.types.encode('SwiftServerMessage', messageWithBuffer);
|
|
2923
|
+
}
|
|
2924
|
+
decodeSwiftServerMessage(encodedMessage) {
|
|
2925
|
+
const decodedSwiftMessage = this.program.coder.types.decode('SwiftServerMessage', encodedMessage);
|
|
2926
|
+
return {
|
|
2927
|
+
slot: decodedSwiftMessage.slot,
|
|
2928
|
+
swiftOrderSignature: decodedSwiftMessage.swiftSignature,
|
|
2929
|
+
};
|
|
2930
|
+
}
|
|
2931
|
+
async signSwiftServerMessage(message) {
|
|
2932
|
+
const swiftServerMessage = Uint8Array.from(this.encodeSwiftServerMessage(message));
|
|
2933
|
+
return await this.signMessage(swiftServerMessage);
|
|
2934
|
+
}
|
|
2935
|
+
async signSwiftOrderParamsMessage(orderParamsMessage) {
|
|
2936
|
+
const takerOrderParamsMessage = Uint8Array.from(this.encodeSwiftOrderParamsMessage(orderParamsMessage));
|
|
2937
|
+
return await this.signMessage(takerOrderParamsMessage);
|
|
2938
|
+
}
|
|
2939
|
+
encodeSwiftOrderParamsMessage(orderParamsMessage) {
|
|
2940
|
+
return this.program.coder.types.encode('SwiftOrderParamsMessage', orderParamsMessage);
|
|
2941
|
+
}
|
|
2942
|
+
decodeSwiftOrderParamsMessage(encodedMessage) {
|
|
2943
|
+
return this.program.coder.types.decode('SwiftOrderParamsMessage', encodedMessage);
|
|
2944
|
+
}
|
|
2945
|
+
async signMessage(message, keypair = this.wallet.payer) {
|
|
2946
|
+
return Buffer.from(await ed.sign(message, keypair.secretKey.slice(0, 32)));
|
|
2947
|
+
}
|
|
2948
|
+
async placeSwiftTakerOrder(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo, txParams) {
|
|
2949
|
+
const ixs = await this.getPlaceSwiftTakerPerpOrderIxs(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo);
|
|
2950
|
+
const { txSig } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
|
|
2951
|
+
return txSig;
|
|
2952
|
+
}
|
|
2953
|
+
async getPlaceSwiftTakerPerpOrderIxs(encodedSwiftServerMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo) {
|
|
2954
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
2955
|
+
userAccounts: [takerInfo.takerUserAccount],
|
|
2956
|
+
useMarketLastSlotCache: true,
|
|
2957
|
+
readablePerpMarketIndex: marketIndex,
|
|
2958
|
+
});
|
|
2959
|
+
const swiftServerSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
|
|
2960
|
+
publicKey: new web3_js_1.PublicKey(this.swiftID).toBytes(),
|
|
2961
|
+
signature: Uint8Array.from(swiftSignature),
|
|
2962
|
+
message: Uint8Array.from(encodedSwiftServerMessage),
|
|
2963
|
+
});
|
|
2964
|
+
const swiftOrderParamsSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
|
|
2965
|
+
publicKey: takerInfo.takerUserAccount.authority.toBytes(),
|
|
2966
|
+
signature: Uint8Array.from(swiftOrderParamsSignature),
|
|
2967
|
+
message: Uint8Array.from(encodedSwiftOrderParamsMessage),
|
|
2968
|
+
});
|
|
2969
|
+
const placeTakerSwiftPerpOrderIx = await this.program.instruction.placeSwiftTakerOrder(encodedSwiftServerMessage, encodedSwiftOrderParamsMessage, swiftSignature, {
|
|
2970
|
+
accounts: {
|
|
2971
|
+
state: await this.getStatePublicKey(),
|
|
2972
|
+
user: takerInfo.taker,
|
|
2973
|
+
userStats: takerInfo.takerStats,
|
|
2974
|
+
authority: this.wallet.publicKey,
|
|
2975
|
+
ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
2976
|
+
},
|
|
2977
|
+
remainingAccounts,
|
|
2978
|
+
});
|
|
2979
|
+
return [
|
|
2980
|
+
swiftServerSignatureIx,
|
|
2981
|
+
swiftOrderParamsSignatureIx,
|
|
2982
|
+
placeTakerSwiftPerpOrderIx,
|
|
2983
|
+
];
|
|
2984
|
+
}
|
|
2985
|
+
async placeAndMakeSwiftPerpOrder(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, txParams, subAccountId) {
|
|
2986
|
+
const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId);
|
|
2987
|
+
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
|
|
2988
|
+
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2989
|
+
return txSig;
|
|
2990
|
+
}
|
|
2991
|
+
async getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId) {
|
|
2992
|
+
const [swiftServerSignatureIx, swiftOrderSignatureIx, placeTakerSwiftPerpOrderIx,] = await this.getPlaceSwiftTakerPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, orderParams.marketIndex, takerInfo);
|
|
2993
|
+
orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
|
|
2994
|
+
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
2995
|
+
const user = await this.getUserAccountPublicKey(subAccountId);
|
|
2996
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
2997
|
+
userAccounts: [
|
|
2998
|
+
this.getUserAccount(subAccountId),
|
|
2999
|
+
takerInfo.takerUserAccount,
|
|
3000
|
+
],
|
|
3001
|
+
useMarketLastSlotCache: true,
|
|
3002
|
+
writablePerpMarketIndexes: [orderParams.marketIndex],
|
|
3003
|
+
});
|
|
3004
|
+
if (referrerInfo) {
|
|
3005
|
+
remainingAccounts.push({
|
|
3006
|
+
pubkey: referrerInfo.referrer,
|
|
3007
|
+
isWritable: true,
|
|
3008
|
+
isSigner: false,
|
|
3009
|
+
});
|
|
3010
|
+
remainingAccounts.push({
|
|
3011
|
+
pubkey: referrerInfo.referrerStats,
|
|
3012
|
+
isWritable: true,
|
|
3013
|
+
isSigner: false,
|
|
3014
|
+
});
|
|
3015
|
+
}
|
|
3016
|
+
const placeAndMakeIx = await this.program.instruction.placeAndMakePerpOrder(orderParams, takerExpectedOrderId, {
|
|
3017
|
+
accounts: {
|
|
3018
|
+
state: await this.getStatePublicKey(),
|
|
3019
|
+
user,
|
|
3020
|
+
userStats: userStatsPublicKey,
|
|
3021
|
+
taker: takerInfo.taker,
|
|
3022
|
+
takerStats: takerInfo.takerStats,
|
|
3023
|
+
authority: this.wallet.publicKey,
|
|
3024
|
+
},
|
|
3025
|
+
remainingAccounts,
|
|
3026
|
+
});
|
|
3027
|
+
return [
|
|
3028
|
+
swiftServerSignatureIx,
|
|
3029
|
+
swiftOrderSignatureIx,
|
|
3030
|
+
placeTakerSwiftPerpOrderIx,
|
|
3031
|
+
placeAndMakeIx,
|
|
3032
|
+
];
|
|
3033
|
+
}
|
|
2910
3034
|
async preparePlaceAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo, txParams, subAccountId) {
|
|
2911
3035
|
const tx = await this.buildTransaction(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo, subAccountId), txParams);
|
|
2912
3036
|
return {
|
|
@@ -3034,7 +3158,7 @@ class DriftClient {
|
|
|
3034
3158
|
baseAssetAmount: userPosition.baseAssetAmount.abs(),
|
|
3035
3159
|
reduceOnly: true,
|
|
3036
3160
|
price: limitPrice,
|
|
3037
|
-
}, undefined, undefined, undefined, subAccountId);
|
|
3161
|
+
}, undefined, undefined, undefined, undefined, subAccountId);
|
|
3038
3162
|
}
|
|
3039
3163
|
/**
|
|
3040
3164
|
* Modifies an open order by closing it and replacing it with a new order.
|
|
@@ -3928,9 +4052,10 @@ class DriftClient {
|
|
|
3928
4052
|
}
|
|
3929
4053
|
return this.receiverProgram;
|
|
3930
4054
|
}
|
|
3931
|
-
getSwitchboardOnDemandProgram() {
|
|
4055
|
+
async getSwitchboardOnDemandProgram() {
|
|
4056
|
+
const idl = (await anchor_30_1.Program.fetchIdl(this.sbOnDemandProgramdId, this.provider));
|
|
3932
4057
|
if (this.sbOnDemandProgram === undefined) {
|
|
3933
|
-
this.sbOnDemandProgram = new anchor_30_1.Program(
|
|
4058
|
+
this.sbOnDemandProgram = new anchor_30_1.Program(idl, this.provider);
|
|
3934
4059
|
}
|
|
3935
4060
|
return this.sbOnDemandProgram;
|
|
3936
4061
|
}
|
|
@@ -3971,7 +4096,7 @@ class DriftClient {
|
|
|
3971
4096
|
postIxs.push(this.program.instruction.postMultiPythPullOracleUpdatesAtomic(encodedParams, {
|
|
3972
4097
|
accounts: {
|
|
3973
4098
|
keeper: this.wallet.publicKey,
|
|
3974
|
-
pythSolanaReceiver:
|
|
4099
|
+
pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
|
|
3975
4100
|
guardianSet,
|
|
3976
4101
|
},
|
|
3977
4102
|
remainingAccounts,
|
|
@@ -3994,7 +4119,7 @@ class DriftClient {
|
|
|
3994
4119
|
return this.program.instruction.postPythPullOracleUpdateAtomic(feedIdBuffer, encodedParams, {
|
|
3995
4120
|
accounts: {
|
|
3996
4121
|
keeper: this.wallet.publicKey,
|
|
3997
|
-
pythSolanaReceiver:
|
|
4122
|
+
pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
|
|
3998
4123
|
guardianSet,
|
|
3999
4124
|
priceFeed: (0, pda_1.getPythPullOraclePublicKey)(this.program.programId, feedIdBuffer),
|
|
4000
4125
|
},
|
|
@@ -4022,14 +4147,14 @@ class DriftClient {
|
|
|
4022
4147
|
return this.program.instruction.updatePythPullOracle(feedIdBuffer, encodedParams, {
|
|
4023
4148
|
accounts: {
|
|
4024
4149
|
keeper: this.wallet.publicKey,
|
|
4025
|
-
pythSolanaReceiver:
|
|
4150
|
+
pythSolanaReceiver: config_1.DRIFT_ORACLE_RECEIVER_ID,
|
|
4026
4151
|
encodedVaa: encodedVaaAddress,
|
|
4027
4152
|
priceFeed: (0, pda_1.getPythPullOraclePublicKey)(this.program.programId, feedIdBuffer),
|
|
4028
4153
|
},
|
|
4029
4154
|
});
|
|
4030
4155
|
}
|
|
4031
4156
|
async getPostSwitchboardOnDemandUpdateAtomicIx(feed, numSignatures = 3) {
|
|
4032
|
-
const program = this.getSwitchboardOnDemandProgram();
|
|
4157
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
4033
4158
|
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
4034
4159
|
if (!this.sbProgramFeedConfigs) {
|
|
4035
4160
|
this.sbProgramFeedConfigs = new Map();
|
|
@@ -4040,8 +4165,6 @@ class DriftClient {
|
|
|
4040
4165
|
}
|
|
4041
4166
|
const [pullIx, _responses, success] = await feedAccount.fetchUpdateIx({
|
|
4042
4167
|
numSignatures,
|
|
4043
|
-
// @ts-ignore :: TODO someone needs to look at this
|
|
4044
|
-
feedConfigs: this.sbProgramFeedConfigs.get(feed.toString()),
|
|
4045
4168
|
});
|
|
4046
4169
|
if (!success) {
|
|
4047
4170
|
return undefined;
|
|
@@ -5,11 +5,13 @@ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
|
5
5
|
import { DriftEnv } from './config';
|
|
6
6
|
import { TxSender } from './tx/types';
|
|
7
7
|
import { TxHandler, TxHandlerConfig } from './tx/txHandler';
|
|
8
|
+
import { DelistedMarketSetting } from './accounts/types';
|
|
8
9
|
export type DriftClientConfig = {
|
|
9
10
|
connection: Connection;
|
|
10
11
|
wallet: IWallet;
|
|
11
12
|
env?: DriftEnv;
|
|
12
13
|
programID?: PublicKey;
|
|
14
|
+
swiftID?: PublicKey;
|
|
13
15
|
accountSubscription?: DriftClientSubscriptionConfig;
|
|
14
16
|
opts?: ConfirmOptions;
|
|
15
17
|
txSender?: TxSender;
|
|
@@ -29,6 +31,7 @@ export type DriftClientConfig = {
|
|
|
29
31
|
txParams?: TxParams;
|
|
30
32
|
enableMetricsEvents?: boolean;
|
|
31
33
|
txHandlerConfig?: TxHandlerConfig;
|
|
34
|
+
delistedMarketSetting?: DelistedMarketSetting;
|
|
32
35
|
};
|
|
33
36
|
export type DriftClientSubscriptionConfig = {
|
|
34
37
|
type: 'websocket';
|
package/lib/events/types.js
CHANGED
|
@@ -25,10 +25,6 @@ exports.DefaultEventSubscriptionOptions = {
|
|
|
25
25
|
commitment: 'confirmed',
|
|
26
26
|
maxTx: 4096,
|
|
27
27
|
logProviderConfig: {
|
|
28
|
-
type: '
|
|
29
|
-
url: 'wss://events.drift.trade/ws',
|
|
30
|
-
maxReconnectAttempts: 5,
|
|
31
|
-
fallbackFrequency: 1000,
|
|
32
|
-
fallbackBatchSize: 100,
|
|
28
|
+
type: 'websocket',
|
|
33
29
|
},
|
|
34
30
|
};
|
package/lib/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/lib/index.d.ts
CHANGED
|
@@ -107,5 +107,6 @@ export * from './memcmp';
|
|
|
107
107
|
export * from './decode/user';
|
|
108
108
|
export * from './blockhashSubscriber';
|
|
109
109
|
export * from './util/chainClock';
|
|
110
|
+
export * from './util/TransactionConfirmationManager';
|
|
110
111
|
export * from './clock/clockSubscriber';
|
|
111
112
|
export { BN, PublicKey, pyth };
|
package/lib/index.js
CHANGED
|
@@ -130,4 +130,5 @@ __exportStar(require("./memcmp"), exports);
|
|
|
130
130
|
__exportStar(require("./decode/user"), exports);
|
|
131
131
|
__exportStar(require("./blockhashSubscriber"), exports);
|
|
132
132
|
__exportStar(require("./util/chainClock"), exports);
|
|
133
|
+
__exportStar(require("./util/TransactionConfirmationManager"), exports);
|
|
133
134
|
__exportStar(require("./clock/clockSubscriber"), exports);
|