@drift-labs/sdk 2.25.3 → 2.26.0-beta.0
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/pollingUserAccountSubscriber.d.ts +5 -4
- package/lib/accounts/pollingUserAccountSubscriber.js +35 -48
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +4 -4
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +30 -47
- package/lib/accounts/types.d.ts +2 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +4 -3
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketUserAccountSubscriber.js +9 -0
- package/lib/addresses/pda.d.ts +2 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/adminClient.d.ts +2 -0
- package/lib/adminClient.js +20 -0
- package/lib/constants/numericConstants.d.ts +61 -59
- package/lib/constants/spotMarkets.d.ts +1 -0
- package/lib/dlob/DLOB.d.ts +1 -0
- package/lib/dlob/DLOBApiClient.d.ts +14 -0
- package/lib/dlob/DLOBApiClient.js +34 -0
- package/lib/dlob/DLOBNode.d.ts +1 -0
- package/lib/dlob/DLOBSubscriber.d.ts +19 -0
- package/lib/dlob/DLOBSubscriber.js +42 -0
- package/lib/dlob/NodeList.d.ts +1 -0
- package/lib/dlob/types.d.ts +16 -0
- package/lib/dlob/types.js +2 -0
- package/lib/driftClient.d.ts +14 -10
- package/lib/driftClient.js +124 -28
- package/lib/examples/loadDlob.js +2 -4
- package/lib/factory/bigNum.d.ts +8 -7
- package/lib/idl/drift.json +264 -82
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/math/amm.d.ts +2 -1
- package/lib/math/auction.d.ts +1 -0
- package/lib/math/conversion.d.ts +2 -1
- package/lib/math/funding.d.ts +1 -0
- package/lib/math/insurance.d.ts +1 -0
- package/lib/math/margin.d.ts +1 -0
- package/lib/math/market.d.ts +1 -0
- package/lib/math/oracles.d.ts +1 -0
- package/lib/math/orders.d.ts +1 -0
- package/lib/math/position.d.ts +1 -0
- package/lib/math/repeg.d.ts +1 -0
- package/lib/math/spotBalance.d.ts +1 -0
- package/lib/math/spotMarket.d.ts +1 -0
- package/lib/math/spotPosition.d.ts +1 -0
- package/lib/math/trade.d.ts +1 -0
- package/lib/math/utils.d.ts +1 -0
- package/lib/oracles/pythClient.d.ts +2 -1
- package/lib/oracles/types.d.ts +1 -0
- package/lib/orderParams.d.ts +1 -0
- package/lib/serum/serumSubscriber.d.ts +1 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/tokenFaucet.d.ts +1 -0
- package/lib/tx/retryTxSender.d.ts +2 -1
- package/lib/tx/retryTxSender.js +5 -1
- package/lib/tx/types.d.ts +4 -1
- package/lib/types.d.ts +12 -0
- package/lib/user.d.ts +1 -0
- package/lib/userMap/userMap.d.ts +17 -6
- package/lib/userMap/userMap.js +74 -46
- package/lib/userMap/userStatsMap.d.ts +6 -3
- package/lib/userMap/userStatsMap.js +30 -30
- package/package.json +2 -1
- package/src/accounts/pollingUserAccountSubscriber.ts +51 -63
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +46 -62
- package/src/accounts/types.ts +2 -1
- package/src/accounts/webSocketAccountSubscriber.ts +4 -3
- package/src/accounts/webSocketUserAccountSubscriber.ts +10 -0
- package/src/addresses/pda.ts +13 -0
- package/src/adminClient.ts +34 -0
- package/src/dlob/DLOBApiClient.ts +37 -0
- package/src/dlob/DLOBSubscriber.ts +57 -0
- package/src/dlob/types.ts +20 -0
- package/src/driftClient.ts +216 -59
- package/src/examples/loadDlob.ts +2 -4
- package/src/idl/drift.json +264 -82
- package/src/index.ts +2 -0
- package/src/slot/SlotSubscriber.ts +4 -0
- package/src/tx/retryTxSender.ts +19 -2
- package/src/tx/types.ts +13 -0
- package/src/types.ts +12 -0
- package/src/userMap/userMap.ts +111 -60
- package/src/userMap/userStatsMap.ts +43 -44
- package/tests/dlob/helpers.ts +3 -1
- package/src/assert/assert.js +0 -9
- package/src/token/index.js +0 -38
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DLOBApiClient = void 0;
|
|
7
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
+
const DLOBOrders_1 = require("./DLOBOrders");
|
|
9
|
+
const DLOB_1 = require("./DLOB");
|
|
10
|
+
class DLOBApiClient {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.dlobCoder = DLOBOrders_1.DLOBOrdersCoder.create();
|
|
13
|
+
this.lastSeenSlot = 0;
|
|
14
|
+
this.url = config.url;
|
|
15
|
+
}
|
|
16
|
+
async getDLOB(slot) {
|
|
17
|
+
const r = await (0, node_fetch_1.default)(this.url);
|
|
18
|
+
if (!r.ok) {
|
|
19
|
+
throw new Error(`Failed to fetch DLOB from ${this.url}`);
|
|
20
|
+
}
|
|
21
|
+
const resp = await r.json();
|
|
22
|
+
const responseSlot = resp['slot'];
|
|
23
|
+
if (responseSlot > this.lastSeenSlot) {
|
|
24
|
+
const dlobOrdersBuffer = Buffer.from(resp['data'], 'base64');
|
|
25
|
+
const dlobOrders = this.dlobCoder.decode(Buffer.from(dlobOrdersBuffer));
|
|
26
|
+
const dlob = new DLOB_1.DLOB();
|
|
27
|
+
dlob.initFromOrders(dlobOrders, slot);
|
|
28
|
+
this.lastSeenDLOB = dlob;
|
|
29
|
+
this.lastSeenSlot = responseSlot;
|
|
30
|
+
}
|
|
31
|
+
return this.lastSeenDLOB;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.DLOBApiClient = DLOBApiClient;
|
package/lib/dlob/DLOBNode.d.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { DLOB } from './DLOB';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
|
+
import { DLOBSource, DLOBSubscriberEvents, DLOBSubscriptionConfig, SlotSource } from './types';
|
|
7
|
+
export declare class DLOBSubscriber {
|
|
8
|
+
dlobSource: DLOBSource;
|
|
9
|
+
slotSource: SlotSource;
|
|
10
|
+
updateFrequency: number;
|
|
11
|
+
intervalId?: NodeJS.Timeout;
|
|
12
|
+
dlob: DLOB;
|
|
13
|
+
eventEmitter: StrictEventEmitter<EventEmitter, DLOBSubscriberEvents>;
|
|
14
|
+
constructor(config: DLOBSubscriptionConfig);
|
|
15
|
+
subscribe(): Promise<void>;
|
|
16
|
+
updateDLOB(): Promise<void>;
|
|
17
|
+
getDLOB(): DLOB;
|
|
18
|
+
unsubscribe(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DLOBSubscriber = void 0;
|
|
4
|
+
const DLOB_1 = require("./DLOB");
|
|
5
|
+
const events_1 = require("events");
|
|
6
|
+
class DLOBSubscriber {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.dlob = new DLOB_1.DLOB();
|
|
9
|
+
this.dlobSource = config.dlobSource;
|
|
10
|
+
this.slotSource = config.slotSource;
|
|
11
|
+
this.updateFrequency = config.updateFrequency;
|
|
12
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
13
|
+
}
|
|
14
|
+
async subscribe() {
|
|
15
|
+
if (this.intervalId) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
await this.updateDLOB();
|
|
19
|
+
this.intervalId = setInterval(async () => {
|
|
20
|
+
try {
|
|
21
|
+
await this.updateDLOB();
|
|
22
|
+
this.eventEmitter.emit('update', this.dlob);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
this.eventEmitter.emit('error', e);
|
|
26
|
+
}
|
|
27
|
+
}, this.updateFrequency);
|
|
28
|
+
}
|
|
29
|
+
async updateDLOB() {
|
|
30
|
+
this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot());
|
|
31
|
+
}
|
|
32
|
+
getDLOB() {
|
|
33
|
+
return this.dlob;
|
|
34
|
+
}
|
|
35
|
+
async unsubscribe() {
|
|
36
|
+
if (this.intervalId) {
|
|
37
|
+
clearInterval(this.intervalId);
|
|
38
|
+
this.intervalId = undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.DLOBSubscriber = DLOBSubscriber;
|
package/lib/dlob/NodeList.d.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DLOB } from './DLOB';
|
|
2
|
+
export type DLOBSubscriptionConfig = {
|
|
3
|
+
dlobSource: DLOBSource;
|
|
4
|
+
slotSource: SlotSource;
|
|
5
|
+
updateFrequency: number;
|
|
6
|
+
};
|
|
7
|
+
export interface DLOBSubscriberEvents {
|
|
8
|
+
update: (dlob: DLOB) => void;
|
|
9
|
+
error: (e: Error) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface DLOBSource {
|
|
12
|
+
getDLOB(slot: number): Promise<DLOB>;
|
|
13
|
+
}
|
|
14
|
+
export interface SlotSource {
|
|
15
|
+
getSlot(): number;
|
|
16
|
+
}
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="bn.js" />
|
|
2
3
|
import { AnchorProvider, BN, Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
3
|
-
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount, ReferrerNameAccount, OrderTriggerCondition, PerpMarketExtendedInfo, UserStatsAccount, ModifyOrderParams } from './types';
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount, ReferrerNameAccount, OrderTriggerCondition, PerpMarketExtendedInfo, UserStatsAccount, ModifyOrderParams, PhoenixV1FulfillmentConfigAccount } from './types';
|
|
4
5
|
import * as anchor from '@coral-xyz/anchor';
|
|
5
6
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, Transaction, TransactionInstruction, AccountMeta, Signer, AddressLookupTableAccount } from '@solana/web3.js';
|
|
6
7
|
import { TokenFaucet } from './tokenFaucet';
|
|
@@ -43,6 +44,7 @@ export declare class DriftClient {
|
|
|
43
44
|
spotMarketLastSlotCache: Map<number, number>;
|
|
44
45
|
authority: PublicKey;
|
|
45
46
|
marketLookupTable: PublicKey;
|
|
47
|
+
lookupTableAccount: AddressLookupTableAccount;
|
|
46
48
|
get isSubscribed(): boolean;
|
|
47
49
|
set isSubscribed(val: boolean);
|
|
48
50
|
constructor(config: DriftClientConfig);
|
|
@@ -82,6 +84,7 @@ export declare class DriftClient {
|
|
|
82
84
|
getQuoteSpotMarketAccount(): SpotMarketAccount;
|
|
83
85
|
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
84
86
|
getSerumV3FulfillmentConfig(serumMarket: PublicKey): Promise<SerumV3FulfillmentConfigAccount>;
|
|
87
|
+
getPhoenixV1FulfillmentConfig(phoenixMarket: PublicKey): Promise<PhoenixV1FulfillmentConfigAccount>;
|
|
85
88
|
fetchMarketLookupTableAccount(): Promise<AddressLookupTableAccount>;
|
|
86
89
|
/**
|
|
87
90
|
* Update the wallet to use for drift transactions and linked user account
|
|
@@ -199,10 +202,11 @@ export declare class DriftClient {
|
|
|
199
202
|
getRevertFillIx(): Promise<TransactionInstruction>;
|
|
200
203
|
placeSpotOrder(orderParams: OptionalOrderParams, txParams?: TxParams): Promise<TransactionSignature>;
|
|
201
204
|
getPlaceSpotOrderIx(orderParams: OptionalOrderParams): Promise<TransactionInstruction>;
|
|
202
|
-
fillSpotOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<TransactionSignature>;
|
|
203
|
-
getFillSpotOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
204
|
-
addSpotFulfillmentAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig?: SerumV3FulfillmentConfigAccount): void;
|
|
205
|
+
fillSpotOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<TransactionSignature>;
|
|
206
|
+
getFillSpotOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order?: Order, fulfillmentConfig?: SerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
207
|
+
addSpotFulfillmentAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig?: SerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount): void;
|
|
205
208
|
addSerumRemainingAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig: SerumV3FulfillmentConfigAccount): void;
|
|
209
|
+
addPhoenixRemainingAccounts(marketIndex: number, remainingAccounts: AccountMeta[], fulfillmentConfig: PhoenixV1FulfillmentConfigAccount): void;
|
|
206
210
|
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order, txParams?: TxParams): Promise<TransactionSignature>;
|
|
207
211
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
208
212
|
forceCancelOrders(userAccountPublicKey: PublicKey, user: UserAccount, txParams?: TxParams): Promise<TransactionSignature>;
|
|
@@ -344,7 +348,7 @@ export declare class DriftClient {
|
|
|
344
348
|
/**
|
|
345
349
|
* Add to an insurance fund stake and optionally initialize the account
|
|
346
350
|
*/
|
|
347
|
-
addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, }: {
|
|
351
|
+
addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, fromSubaccount, }: {
|
|
348
352
|
/**
|
|
349
353
|
* Spot market index
|
|
350
354
|
*/
|
|
@@ -358,14 +362,14 @@ export declare class DriftClient {
|
|
|
358
362
|
* Add instructions to initialize the staking account -- required if its the first time the currrent authority has staked in this market
|
|
359
363
|
*/
|
|
360
364
|
initializeStakeAccount?: boolean;
|
|
365
|
+
/**
|
|
366
|
+
* Optional -- withdraw from current subaccount to fund stake amount, instead of wallet balance
|
|
367
|
+
*/
|
|
368
|
+
fromSubaccount?: boolean;
|
|
361
369
|
}): Promise<TransactionSignature>;
|
|
362
370
|
requestRemoveInsuranceFundStake(marketIndex: number, amount: BN): Promise<TransactionSignature>;
|
|
363
371
|
cancelRequestRemoveInsuranceFundStake(marketIndex: number): Promise<TransactionSignature>;
|
|
364
|
-
removeInsuranceFundStake(marketIndex: number, collateralAccountPublicKey: PublicKey
|
|
365
|
-
/**
|
|
366
|
-
* If unstaking SOL, it's required to pass in the amount
|
|
367
|
-
*/
|
|
368
|
-
amount?: BN): Promise<TransactionSignature>;
|
|
372
|
+
removeInsuranceFundStake(marketIndex: number, collateralAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
369
373
|
settleRevenueToInsuranceFund(marketIndex: number): Promise<TransactionSignature>;
|
|
370
374
|
resolvePerpPnlDeficit(spotMarketIndex: number, perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
371
375
|
getResolvePerpPnlDeficitIx(spotMarketIndex: number, perpMarketIndex: number): Promise<TransactionInstruction>;
|
package/lib/driftClient.js
CHANGED
|
@@ -242,12 +242,20 @@ class DriftClient {
|
|
|
242
242
|
const address = await (0, pda_1.getSerumFulfillmentConfigPublicKey)(this.program.programId, serumMarket);
|
|
243
243
|
return (await this.program.account.serumV3FulfillmentConfig.fetch(address));
|
|
244
244
|
}
|
|
245
|
+
async getPhoenixV1FulfillmentConfig(phoenixMarket) {
|
|
246
|
+
const address = await (0, pda_1.getPhoenixFulfillmentConfigPublicKey)(this.program.programId, phoenixMarket);
|
|
247
|
+
return (await this.program.account.phoenixV1FulfillmentConfig.fetch(address));
|
|
248
|
+
}
|
|
245
249
|
async fetchMarketLookupTableAccount() {
|
|
250
|
+
if (this.lookupTableAccount)
|
|
251
|
+
return this.lookupTableAccount;
|
|
246
252
|
if (!this.marketLookupTable) {
|
|
247
|
-
|
|
253
|
+
console.log('Market lookup table address not set');
|
|
254
|
+
return;
|
|
248
255
|
}
|
|
249
|
-
|
|
250
|
-
|
|
256
|
+
const lookupTableAccount = (await this.connection.getAddressLookupTable(this.marketLookupTable)).value;
|
|
257
|
+
this.lookupTableAccount = lookupTableAccount;
|
|
258
|
+
return lookupTableAccount;
|
|
251
259
|
}
|
|
252
260
|
/**
|
|
253
261
|
* Update the wallet to use for drift transactions and linked user account
|
|
@@ -887,7 +895,7 @@ class DriftClient {
|
|
|
887
895
|
return false;
|
|
888
896
|
}
|
|
889
897
|
}
|
|
890
|
-
async getWrappedSolAccountCreationIxs(amount,
|
|
898
|
+
async getWrappedSolAccountCreationIxs(amount, includeRent) {
|
|
891
899
|
const wrappedSolAccount = new web3_js_1.Keypair();
|
|
892
900
|
const result = {
|
|
893
901
|
ixs: [],
|
|
@@ -895,7 +903,7 @@ class DriftClient {
|
|
|
895
903
|
pubkey: wrappedSolAccount.publicKey,
|
|
896
904
|
};
|
|
897
905
|
const rentSpaceLamports = new anchor_1.BN(web3_js_1.LAMPORTS_PER_SOL / 100);
|
|
898
|
-
const lamports =
|
|
906
|
+
const lamports = includeRent
|
|
899
907
|
? amount.add(rentSpaceLamports)
|
|
900
908
|
: rentSpaceLamports;
|
|
901
909
|
const authority = this.wallet.publicKey;
|
|
@@ -1206,26 +1214,54 @@ class DriftClient {
|
|
|
1206
1214
|
* @returns
|
|
1207
1215
|
*/
|
|
1208
1216
|
async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo) {
|
|
1217
|
+
var _a, _b;
|
|
1209
1218
|
const marketIndex = orderParams.marketIndex;
|
|
1210
1219
|
const orderId = userAccount.nextOrderId;
|
|
1211
|
-
const
|
|
1220
|
+
const bracketOrderIxs = [];
|
|
1221
|
+
const placePerpOrderIx = await this.getPlacePerpOrderIx(orderParams);
|
|
1212
1222
|
for (const bracketOrderParams of bracketOrdersParams) {
|
|
1213
|
-
|
|
1223
|
+
const placeBracketOrderIx = await this.getPlacePerpOrderIx(bracketOrderParams);
|
|
1224
|
+
bracketOrderIxs.push(placeBracketOrderIx);
|
|
1214
1225
|
}
|
|
1215
|
-
const
|
|
1226
|
+
const fillPerpOrderIx = await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
|
|
1216
1227
|
orderId,
|
|
1217
1228
|
marketIndex,
|
|
1218
|
-
}, makerInfo, referrerInfo)
|
|
1219
|
-
|
|
1220
|
-
const
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
+
}, makerInfo, referrerInfo);
|
|
1230
|
+
const lookupTableAccount = await this.fetchMarketLookupTableAccount();
|
|
1231
|
+
const walletSupportsVersionedTxns =
|
|
1232
|
+
//@ts-ignore
|
|
1233
|
+
(_b = (_a = this.wallet.supportedTransactionVersions) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : 0 > 1;
|
|
1234
|
+
// use versioned transactions if there is a lookup table account and wallet is compatible
|
|
1235
|
+
if (walletSupportsVersionedTxns && lookupTableAccount) {
|
|
1236
|
+
const versionedMarketOrderTx = await this.txSender.getVersionedTransaction([placePerpOrderIx].concat(bracketOrderIxs), [lookupTableAccount], [], this.opts);
|
|
1237
|
+
const versionedFillTx = await this.txSender.getVersionedTransaction([fillPerpOrderIx], [lookupTableAccount], [], this.opts);
|
|
1238
|
+
const [signedVersionedMarketOrderTx, signedVersionedFillTx] = await this.provider.wallet.signAllTransactions([
|
|
1239
|
+
//@ts-ignore
|
|
1240
|
+
versionedMarketOrderTx,
|
|
1241
|
+
//@ts-ignore
|
|
1242
|
+
versionedFillTx,
|
|
1243
|
+
]);
|
|
1244
|
+
const { txSig, slot } = await this.txSender.sendRawTransaction(signedVersionedMarketOrderTx.serialize(), this.opts);
|
|
1245
|
+
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
1246
|
+
return { txSig, signedFillTx: signedVersionedFillTx };
|
|
1247
|
+
}
|
|
1248
|
+
else {
|
|
1249
|
+
const marketOrderTx = (0, utils_1.wrapInTx)(placePerpOrderIx, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
1250
|
+
if (bracketOrderIxs.length > 0) {
|
|
1251
|
+
marketOrderTx.add(...bracketOrderIxs);
|
|
1252
|
+
}
|
|
1253
|
+
const fillTx = (0, utils_1.wrapInTx)(fillPerpOrderIx, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
1254
|
+
// Apply the latest blockhash to the txs so that we can sign before sending them
|
|
1255
|
+
const currentBlockHash = (await this.connection.getLatestBlockhash('finalized')).blockhash;
|
|
1256
|
+
marketOrderTx.recentBlockhash = currentBlockHash;
|
|
1257
|
+
fillTx.recentBlockhash = currentBlockHash;
|
|
1258
|
+
marketOrderTx.feePayer = userAccount.authority;
|
|
1259
|
+
fillTx.feePayer = userAccount.authority;
|
|
1260
|
+
const [signedMarketOrderTx, signedFillTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx]);
|
|
1261
|
+
const { txSig, slot } = await this.sendTransaction(signedMarketOrderTx, [], this.opts, true);
|
|
1262
|
+
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
1263
|
+
return { txSig, signedFillTx };
|
|
1264
|
+
}
|
|
1229
1265
|
}
|
|
1230
1266
|
async placePerpOrder(orderParams, txParams) {
|
|
1231
1267
|
const { txSig, slot } = await this.sendTransaction((0, utils_1.wrapInTx)(await this.getPlacePerpOrderIx(orderParams), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice), [], this.opts);
|
|
@@ -1574,7 +1610,15 @@ class DriftClient {
|
|
|
1574
1610
|
}
|
|
1575
1611
|
addSpotFulfillmentAccounts(marketIndex, remainingAccounts, fulfillmentConfig) {
|
|
1576
1612
|
if (fulfillmentConfig) {
|
|
1577
|
-
|
|
1613
|
+
if ('serumProgramId' in fulfillmentConfig) {
|
|
1614
|
+
this.addSerumRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1615
|
+
}
|
|
1616
|
+
else if ('phoenixProgramId' in fulfillmentConfig) {
|
|
1617
|
+
this.addPhoenixRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig);
|
|
1618
|
+
}
|
|
1619
|
+
else {
|
|
1620
|
+
throw Error('Invalid fulfillment config type');
|
|
1621
|
+
}
|
|
1578
1622
|
}
|
|
1579
1623
|
else {
|
|
1580
1624
|
remainingAccounts.push({
|
|
@@ -1671,6 +1715,58 @@ class DriftClient {
|
|
|
1671
1715
|
isSigner: false,
|
|
1672
1716
|
});
|
|
1673
1717
|
}
|
|
1718
|
+
addPhoenixRemainingAccounts(marketIndex, remainingAccounts, fulfillmentConfig) {
|
|
1719
|
+
remainingAccounts.push({
|
|
1720
|
+
pubkey: fulfillmentConfig.pubkey,
|
|
1721
|
+
isWritable: false,
|
|
1722
|
+
isSigner: false,
|
|
1723
|
+
});
|
|
1724
|
+
remainingAccounts.push({
|
|
1725
|
+
pubkey: fulfillmentConfig.phoenixProgramId,
|
|
1726
|
+
isWritable: false,
|
|
1727
|
+
isSigner: false,
|
|
1728
|
+
});
|
|
1729
|
+
remainingAccounts.push({
|
|
1730
|
+
pubkey: fulfillmentConfig.phoenixLogAuthority,
|
|
1731
|
+
isWritable: false,
|
|
1732
|
+
isSigner: false,
|
|
1733
|
+
});
|
|
1734
|
+
remainingAccounts.push({
|
|
1735
|
+
pubkey: fulfillmentConfig.phoenixMarket,
|
|
1736
|
+
isWritable: true,
|
|
1737
|
+
isSigner: false,
|
|
1738
|
+
});
|
|
1739
|
+
remainingAccounts.push({
|
|
1740
|
+
pubkey: this.getSignerPublicKey(),
|
|
1741
|
+
isWritable: false,
|
|
1742
|
+
isSigner: false,
|
|
1743
|
+
});
|
|
1744
|
+
remainingAccounts.push({
|
|
1745
|
+
pubkey: fulfillmentConfig.phoenixBaseVault,
|
|
1746
|
+
isWritable: true,
|
|
1747
|
+
isSigner: false,
|
|
1748
|
+
});
|
|
1749
|
+
remainingAccounts.push({
|
|
1750
|
+
pubkey: fulfillmentConfig.phoenixQuoteVault,
|
|
1751
|
+
isWritable: true,
|
|
1752
|
+
isSigner: false,
|
|
1753
|
+
});
|
|
1754
|
+
remainingAccounts.push({
|
|
1755
|
+
pubkey: this.getSpotMarketAccount(marketIndex).vault,
|
|
1756
|
+
isWritable: true,
|
|
1757
|
+
isSigner: false,
|
|
1758
|
+
});
|
|
1759
|
+
remainingAccounts.push({
|
|
1760
|
+
pubkey: this.getQuoteSpotMarketAccount().vault,
|
|
1761
|
+
isWritable: true,
|
|
1762
|
+
isSigner: false,
|
|
1763
|
+
});
|
|
1764
|
+
remainingAccounts.push({
|
|
1765
|
+
pubkey: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1766
|
+
isWritable: false,
|
|
1767
|
+
isSigner: false,
|
|
1768
|
+
});
|
|
1769
|
+
}
|
|
1674
1770
|
async triggerOrder(userAccountPublicKey, user, order, txParams) {
|
|
1675
1771
|
const { txSig } = await this.sendTransaction((0, utils_1.wrapInTx)(await this.getTriggerOrderIx(userAccountPublicKey, user, order), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice), [], this.opts);
|
|
1676
1772
|
return txSig;
|
|
@@ -2400,7 +2496,7 @@ class DriftClient {
|
|
|
2400
2496
|
/**
|
|
2401
2497
|
* Add to an insurance fund stake and optionally initialize the account
|
|
2402
2498
|
*/
|
|
2403
|
-
async addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, }) {
|
|
2499
|
+
async addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, fromSubaccount, }) {
|
|
2404
2500
|
const tx = new web3_js_1.Transaction();
|
|
2405
2501
|
const additionalSigners = [];
|
|
2406
2502
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
@@ -2418,6 +2514,10 @@ class DriftClient {
|
|
|
2418
2514
|
else {
|
|
2419
2515
|
tokenAccount = collateralAccountPublicKey;
|
|
2420
2516
|
}
|
|
2517
|
+
if (fromSubaccount) {
|
|
2518
|
+
const withdrawIx = await this.getWithdrawIx(amount, marketIndex, tokenAccount);
|
|
2519
|
+
tx.add(withdrawIx);
|
|
2520
|
+
}
|
|
2421
2521
|
if (initializeStakeAccount) {
|
|
2422
2522
|
const initializeIx = await this.getInitializeInsuranceFundStakeIx(marketIndex);
|
|
2423
2523
|
tx.add(initializeIx);
|
|
@@ -2474,11 +2574,7 @@ class DriftClient {
|
|
|
2474
2574
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
2475
2575
|
return txSig;
|
|
2476
2576
|
}
|
|
2477
|
-
async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey
|
|
2478
|
-
/**
|
|
2479
|
-
* If unstaking SOL, it's required to pass in the amount
|
|
2480
|
-
*/
|
|
2481
|
-
amount) {
|
|
2577
|
+
async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey) {
|
|
2482
2578
|
const tx = new web3_js_1.Transaction();
|
|
2483
2579
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
2484
2580
|
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
@@ -2487,7 +2583,7 @@ class DriftClient {
|
|
|
2487
2583
|
const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
|
|
2488
2584
|
let tokenAccount;
|
|
2489
2585
|
if (createWSOLTokenAccount) {
|
|
2490
|
-
const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(
|
|
2586
|
+
const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(numericConstants_1.ZERO, true);
|
|
2491
2587
|
tokenAccount = pubkey;
|
|
2492
2588
|
ixs.forEach((ix) => {
|
|
2493
2589
|
tx.add(ix);
|
|
@@ -2521,7 +2617,7 @@ class DriftClient {
|
|
|
2521
2617
|
if (createWSOLTokenAccount) {
|
|
2522
2618
|
tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
|
|
2523
2619
|
}
|
|
2524
|
-
const { txSig } = await this.sendTransaction(tx,
|
|
2620
|
+
const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
2525
2621
|
return txSig;
|
|
2526
2622
|
}
|
|
2527
2623
|
async settleRevenueToInsuranceFund(marketIndex) {
|
package/lib/examples/loadDlob.js
CHANGED
|
@@ -38,16 +38,14 @@ const main = async () => {
|
|
|
38
38
|
accountLoader: bulkAccountLoader,
|
|
39
39
|
});
|
|
40
40
|
// fetches all users and subscribes for updates
|
|
41
|
-
await userMap.
|
|
41
|
+
await userMap.subscribe();
|
|
42
42
|
console.log('Loading dlob from user map...');
|
|
43
43
|
const dlob = new __1.DLOB();
|
|
44
44
|
await dlob.initFromUserMap(userMap, bulkAccountLoader.mostRecentSlot);
|
|
45
45
|
console.log('number of orders', dlob.getDLOBOrders().length);
|
|
46
46
|
dlob.clear();
|
|
47
47
|
console.log('Unsubscribing users...');
|
|
48
|
-
|
|
49
|
-
await user.unsubscribe();
|
|
50
|
-
}
|
|
48
|
+
await userMap.unsubscribe();
|
|
51
49
|
console.log('Unsubscribing drift client...');
|
|
52
50
|
await driftClient.unsubscribe();
|
|
53
51
|
};
|
package/lib/factory/bigNum.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { BN } from '@coral-xyz/anchor';
|
|
2
3
|
export declare class BigNum {
|
|
3
4
|
val: BN;
|
|
@@ -43,11 +44,11 @@ export declare class BigNum {
|
|
|
43
44
|
gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
44
45
|
lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
45
46
|
eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
46
|
-
eqZero():
|
|
47
|
-
gtZero():
|
|
48
|
-
ltZero():
|
|
49
|
-
gteZero():
|
|
50
|
-
lteZero():
|
|
47
|
+
eqZero(): boolean;
|
|
48
|
+
gtZero(): boolean;
|
|
49
|
+
ltZero(): boolean;
|
|
50
|
+
gteZero(): boolean;
|
|
51
|
+
lteZero(): boolean;
|
|
51
52
|
abs(): BigNum;
|
|
52
53
|
neg(): BigNum;
|
|
53
54
|
toString: (base?: number | 'hex', length?: number) => string;
|
|
@@ -87,8 +88,8 @@ export declare class BigNum {
|
|
|
87
88
|
toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
|
|
88
89
|
toMillified(precision?: number, rounded?: boolean): string;
|
|
89
90
|
toJSON(): {
|
|
90
|
-
val:
|
|
91
|
-
precision:
|
|
91
|
+
val: string;
|
|
92
|
+
precision: string;
|
|
92
93
|
};
|
|
93
94
|
isNeg(): boolean;
|
|
94
95
|
isPos(): boolean;
|