@drift-labs/sdk 2.43.0-beta.8 → 2.44.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/VERSION +1 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketAccountSubscriber.js +18 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketProgramAccountSubscriber.js +18 -3
- package/lib/adminClient.d.ts +1 -0
- package/lib/adminClient.js +8 -0
- package/lib/constants/perpMarkets.js +20 -0
- package/lib/dlob/DLOB.d.ts +9 -3
- package/lib/dlob/DLOB.js +77 -14
- package/lib/dlob/orderBookLevels.js +1 -0
- package/lib/driftClient.d.ts +3 -1
- package/lib/driftClient.js +86 -69
- package/lib/idl/drift.json +22 -1
- package/lib/jupiter/jupiterClient.js +10 -2
- package/lib/math/amm.js +2 -1
- package/lib/math/trade.js +18 -24
- package/lib/tx/fastSingleTxSender.js +6 -1
- package/lib/user.js +7 -8
- package/package.json +1 -1
- package/src/accounts/webSocketAccountSubscriber.ts +20 -6
- package/src/accounts/webSocketProgramAccountSubscriber.ts +19 -6
- package/src/adminClient.ts +14 -0
- package/src/constants/perpMarkets.ts +20 -0
- package/src/dlob/DLOB.ts +114 -20
- package/src/dlob/orderBookLevels.ts +1 -0
- package/src/driftClient.ts +129 -82
- package/src/idl/drift.json +22 -1
- package/src/jupiter/jupiterClient.ts +12 -2
- package/src/math/amm.ts +2 -2
- package/src/math/trade.ts +26 -26
- package/src/tx/fastSingleTxSender.ts +7 -3
- package/src/user.ts +12 -8
package/src/driftClient.ts
CHANGED
|
@@ -708,6 +708,8 @@ export class DriftClient {
|
|
|
708
708
|
name = DEFAULT_USER_NAME,
|
|
709
709
|
referrerInfo?: ReferrerInfo
|
|
710
710
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
711
|
+
const initializeIxs = [];
|
|
712
|
+
|
|
711
713
|
const [userAccountPublicKey, initializeUserAccountIx] =
|
|
712
714
|
await this.getInitializeUserInstructions(
|
|
713
715
|
subAccountId,
|
|
@@ -715,15 +717,16 @@ export class DriftClient {
|
|
|
715
717
|
referrerInfo
|
|
716
718
|
);
|
|
717
719
|
|
|
718
|
-
const tx = new Transaction();
|
|
719
720
|
if (subAccountId === 0) {
|
|
720
721
|
if (
|
|
721
722
|
!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
|
|
722
723
|
) {
|
|
723
|
-
|
|
724
|
+
initializeIxs.push(await this.getInitializeUserStatsIx());
|
|
724
725
|
}
|
|
725
726
|
}
|
|
726
|
-
|
|
727
|
+
initializeIxs.push(initializeUserAccountIx);
|
|
728
|
+
const tx = await this.buildTransaction(initializeIxs);
|
|
729
|
+
|
|
727
730
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
728
731
|
|
|
729
732
|
await this.addUser(subAccountId);
|
|
@@ -1800,6 +1803,8 @@ export class DriftClient {
|
|
|
1800
1803
|
referrerInfo?: ReferrerInfo,
|
|
1801
1804
|
txParams?: TxParams
|
|
1802
1805
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
1806
|
+
const ixs = [];
|
|
1807
|
+
|
|
1803
1808
|
const [userAccountPublicKey, initializeUserAccountIx] =
|
|
1804
1809
|
await this.getInitializeUserInstructions(
|
|
1805
1810
|
subAccountId,
|
|
@@ -1813,20 +1818,15 @@ export class DriftClient {
|
|
|
1813
1818
|
|
|
1814
1819
|
const isSolMarket = spotMarket.mint.equals(WRAPPED_SOL_MINT);
|
|
1815
1820
|
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
1820
|
-
units: txParams?.computeUnits ?? 600_000,
|
|
1821
|
-
})
|
|
1822
|
-
);
|
|
1821
|
+
let params: TxParams = {
|
|
1822
|
+
computeUnits: txParams?.computeUnits ?? 600_000,
|
|
1823
|
+
};
|
|
1823
1824
|
|
|
1824
1825
|
if (txParams?.computeUnitsPrice) {
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
);
|
|
1826
|
+
params = {
|
|
1827
|
+
...params,
|
|
1828
|
+
computeUnitsPrice: txParams.computeUnitsPrice,
|
|
1829
|
+
};
|
|
1830
1830
|
}
|
|
1831
1831
|
|
|
1832
1832
|
const authority = this.wallet.publicKey;
|
|
@@ -1845,9 +1845,7 @@ export class DriftClient {
|
|
|
1845
1845
|
|
|
1846
1846
|
userTokenAccount = pubkey;
|
|
1847
1847
|
|
|
1848
|
-
|
|
1849
|
-
tx.add(ix);
|
|
1850
|
-
});
|
|
1848
|
+
ixs.push(...startIxs);
|
|
1851
1849
|
}
|
|
1852
1850
|
|
|
1853
1851
|
const depositCollateralIx = isFromSubaccount
|
|
@@ -1870,14 +1868,14 @@ export class DriftClient {
|
|
|
1870
1868
|
if (
|
|
1871
1869
|
!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
|
|
1872
1870
|
) {
|
|
1873
|
-
|
|
1871
|
+
ixs.push(await this.getInitializeUserStatsIx());
|
|
1874
1872
|
}
|
|
1875
1873
|
}
|
|
1876
|
-
|
|
1874
|
+
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
1877
1875
|
|
|
1878
1876
|
// Close the wrapped sol account at the end of the transaction
|
|
1879
1877
|
if (createWSOLTokenAccount) {
|
|
1880
|
-
|
|
1878
|
+
ixs.push(
|
|
1881
1879
|
createCloseAccountInstruction(
|
|
1882
1880
|
userTokenAccount,
|
|
1883
1881
|
authority,
|
|
@@ -1887,6 +1885,8 @@ export class DriftClient {
|
|
|
1887
1885
|
);
|
|
1888
1886
|
}
|
|
1889
1887
|
|
|
1888
|
+
const tx = await this.buildTransaction(ixs, params);
|
|
1889
|
+
|
|
1890
1890
|
const { txSig, slot } = await this.sendTransaction(
|
|
1891
1891
|
tx,
|
|
1892
1892
|
additionalSigners,
|
|
@@ -1907,6 +1907,8 @@ export class DriftClient {
|
|
|
1907
1907
|
amount: BN,
|
|
1908
1908
|
referrerInfo?: ReferrerInfo
|
|
1909
1909
|
): Promise<[TransactionSignature, PublicKey]> {
|
|
1910
|
+
const ixs = [];
|
|
1911
|
+
|
|
1910
1912
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] =
|
|
1911
1913
|
await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(
|
|
1912
1914
|
this.wallet.publicKey,
|
|
@@ -1929,18 +1931,20 @@ export class DriftClient {
|
|
|
1929
1931
|
false
|
|
1930
1932
|
);
|
|
1931
1933
|
|
|
1932
|
-
|
|
1934
|
+
ixs.push(createAssociatedAccountIx, mintToIx);
|
|
1933
1935
|
|
|
1934
1936
|
if (subAccountId === 0) {
|
|
1935
1937
|
if (
|
|
1936
1938
|
!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
|
|
1937
1939
|
) {
|
|
1938
|
-
|
|
1940
|
+
ixs.push(await this.getInitializeUserStatsIx());
|
|
1939
1941
|
}
|
|
1940
1942
|
}
|
|
1941
|
-
|
|
1943
|
+
ixs.push(initializeUserAccountIx, depositCollateralIx);
|
|
1942
1944
|
|
|
1943
|
-
const
|
|
1945
|
+
const tx = await this.buildTransaction(ixs);
|
|
1946
|
+
|
|
1947
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1944
1948
|
|
|
1945
1949
|
await this.addUser(subAccountId);
|
|
1946
1950
|
|
|
@@ -1960,12 +1964,7 @@ export class DriftClient {
|
|
|
1960
1964
|
associatedTokenAddress: PublicKey,
|
|
1961
1965
|
reduceOnly = false
|
|
1962
1966
|
): Promise<TransactionSignature> {
|
|
1963
|
-
const
|
|
1964
|
-
tx.add(
|
|
1965
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
1966
|
-
units: 600_000,
|
|
1967
|
-
})
|
|
1968
|
-
);
|
|
1967
|
+
const withdrawIxs = [];
|
|
1969
1968
|
|
|
1970
1969
|
const additionalSigners: Array<Signer> = [];
|
|
1971
1970
|
|
|
@@ -1986,9 +1985,7 @@ export class DriftClient {
|
|
|
1986
1985
|
|
|
1987
1986
|
associatedTokenAddress = pubkey;
|
|
1988
1987
|
|
|
1989
|
-
|
|
1990
|
-
tx.add(ix);
|
|
1991
|
-
});
|
|
1988
|
+
withdrawIxs.push(...ixs);
|
|
1992
1989
|
} else {
|
|
1993
1990
|
const accountExists = await this.checkIfAccountExists(
|
|
1994
1991
|
associatedTokenAddress
|
|
@@ -2001,22 +1998,22 @@ export class DriftClient {
|
|
|
2001
1998
|
associatedTokenAddress
|
|
2002
1999
|
);
|
|
2003
2000
|
|
|
2004
|
-
|
|
2001
|
+
withdrawIxs.push(createAssociatedTokenAccountIx);
|
|
2005
2002
|
}
|
|
2006
2003
|
}
|
|
2007
2004
|
|
|
2008
|
-
const
|
|
2005
|
+
const withdrawCollateralIx = await this.getWithdrawIx(
|
|
2009
2006
|
amount,
|
|
2010
2007
|
spotMarketAccount.marketIndex,
|
|
2011
2008
|
associatedTokenAddress,
|
|
2012
2009
|
reduceOnly
|
|
2013
2010
|
);
|
|
2014
2011
|
|
|
2015
|
-
|
|
2012
|
+
withdrawIxs.push(withdrawCollateralIx);
|
|
2016
2013
|
|
|
2017
2014
|
// Close the wrapped sol account at the end of the transaction
|
|
2018
2015
|
if (createWSOLTokenAccount) {
|
|
2019
|
-
|
|
2016
|
+
withdrawIxs.push(
|
|
2020
2017
|
createCloseAccountInstruction(
|
|
2021
2018
|
associatedTokenAddress,
|
|
2022
2019
|
authority,
|
|
@@ -2026,6 +2023,9 @@ export class DriftClient {
|
|
|
2026
2023
|
);
|
|
2027
2024
|
}
|
|
2028
2025
|
|
|
2026
|
+
const tx = await this.buildTransaction(withdrawIxs, {
|
|
2027
|
+
computeUnits: 600_000,
|
|
2028
|
+
});
|
|
2029
2029
|
const { txSig, slot } = await this.sendTransaction(
|
|
2030
2030
|
tx,
|
|
2031
2031
|
additionalSigners,
|
|
@@ -2448,6 +2448,7 @@ export class DriftClient {
|
|
|
2448
2448
|
* @param makerInfo
|
|
2449
2449
|
* @param txParams
|
|
2450
2450
|
* @param bracketOrdersParams
|
|
2451
|
+
* @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same perp market. Intended use is to auto-cancel TP/SL orders when closing a position. Ignored if orderParams.marketType is not MarketType.PERP
|
|
2451
2452
|
* @returns
|
|
2452
2453
|
*/
|
|
2453
2454
|
public async sendMarketOrderAndGetSignedFillTx(
|
|
@@ -2457,8 +2458,13 @@ export class DriftClient {
|
|
|
2457
2458
|
makerInfo?: MakerInfo | MakerInfo[],
|
|
2458
2459
|
txParams?: TxParams,
|
|
2459
2460
|
bracketOrdersParams = new Array<OptionalOrderParams>(),
|
|
2460
|
-
referrerInfo?: ReferrerInfo
|
|
2461
|
-
|
|
2461
|
+
referrerInfo?: ReferrerInfo,
|
|
2462
|
+
cancelExistingOrders?: boolean
|
|
2463
|
+
): Promise<{
|
|
2464
|
+
txSig: TransactionSignature;
|
|
2465
|
+
signedFillTx: Transaction;
|
|
2466
|
+
signedCancelExistingOrdersTx?: Transaction;
|
|
2467
|
+
}> {
|
|
2462
2468
|
const marketIndex = orderParams.marketIndex;
|
|
2463
2469
|
const orderId = userAccount.nextOrderId;
|
|
2464
2470
|
const bracketOrderIxs = [];
|
|
@@ -2483,6 +2489,23 @@ export class DriftClient {
|
|
|
2483
2489
|
referrerInfo
|
|
2484
2490
|
);
|
|
2485
2491
|
|
|
2492
|
+
let cancelOrdersIx: TransactionInstruction;
|
|
2493
|
+
let cancelExistingOrdersTx: Transaction;
|
|
2494
|
+
if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
|
|
2495
|
+
cancelOrdersIx = await this.getCancelOrdersIx(
|
|
2496
|
+
orderParams.marketType,
|
|
2497
|
+
orderParams.marketIndex,
|
|
2498
|
+
null
|
|
2499
|
+
);
|
|
2500
|
+
|
|
2501
|
+
//@ts-ignore
|
|
2502
|
+
cancelExistingOrdersTx = await this.buildTransaction(
|
|
2503
|
+
[cancelOrdersIx],
|
|
2504
|
+
txParams,
|
|
2505
|
+
this.txVersion
|
|
2506
|
+
);
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2486
2509
|
// use versioned transactions if there is a lookup table account and wallet is compatible
|
|
2487
2510
|
if (this.txVersion === 0) {
|
|
2488
2511
|
const versionedMarketOrderTx = await this.buildTransaction(
|
|
@@ -2495,21 +2518,31 @@ export class DriftClient {
|
|
|
2495
2518
|
txParams,
|
|
2496
2519
|
0
|
|
2497
2520
|
);
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2521
|
+
|
|
2522
|
+
const [
|
|
2523
|
+
signedVersionedMarketOrderTx,
|
|
2524
|
+
signedVersionedFillTx,
|
|
2525
|
+
signedCancelExistingOrdersTx,
|
|
2526
|
+
] = await this.provider.wallet.signAllTransactions(
|
|
2527
|
+
[
|
|
2501
2528
|
versionedMarketOrderTx,
|
|
2502
|
-
//@ts-ignore
|
|
2503
2529
|
versionedFillTx,
|
|
2504
|
-
|
|
2530
|
+
cancelExistingOrdersTx,
|
|
2531
|
+
].filter((tx) => tx !== undefined)
|
|
2532
|
+
);
|
|
2505
2533
|
const { txSig, slot } = await this.txSender.sendRawTransaction(
|
|
2506
2534
|
signedVersionedMarketOrderTx.serialize(),
|
|
2507
2535
|
this.opts
|
|
2508
2536
|
);
|
|
2509
2537
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2510
2538
|
|
|
2511
|
-
|
|
2512
|
-
|
|
2539
|
+
return {
|
|
2540
|
+
txSig,
|
|
2541
|
+
// @ts-ignore
|
|
2542
|
+
signedFillTx: signedVersionedFillTx,
|
|
2543
|
+
// @ts-ignore
|
|
2544
|
+
signedCancelExistingOrdersTx,
|
|
2545
|
+
};
|
|
2513
2546
|
} else {
|
|
2514
2547
|
const marketOrderTx = wrapInTx(
|
|
2515
2548
|
placePerpOrderIx,
|
|
@@ -2537,8 +2570,12 @@ export class DriftClient {
|
|
|
2537
2570
|
marketOrderTx.feePayer = userAccount.authority;
|
|
2538
2571
|
fillTx.feePayer = userAccount.authority;
|
|
2539
2572
|
|
|
2540
|
-
const [signedMarketOrderTx, signedFillTx] =
|
|
2541
|
-
await this.provider.wallet.signAllTransactions(
|
|
2573
|
+
const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] =
|
|
2574
|
+
await this.provider.wallet.signAllTransactions(
|
|
2575
|
+
[marketOrderTx, fillTx, cancelExistingOrdersTx].filter(
|
|
2576
|
+
(tx) => tx !== undefined
|
|
2577
|
+
)
|
|
2578
|
+
);
|
|
2542
2579
|
const { txSig, slot } = await this.sendTransaction(
|
|
2543
2580
|
signedMarketOrderTx,
|
|
2544
2581
|
[],
|
|
@@ -2547,7 +2584,7 @@ export class DriftClient {
|
|
|
2547
2584
|
);
|
|
2548
2585
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2549
2586
|
|
|
2550
|
-
return { txSig, signedFillTx };
|
|
2587
|
+
return { txSig, signedFillTx, signedCancelExistingOrdersTx };
|
|
2551
2588
|
}
|
|
2552
2589
|
}
|
|
2553
2590
|
|
|
@@ -2859,7 +2896,8 @@ export class DriftClient {
|
|
|
2859
2896
|
|
|
2860
2897
|
let readablePerpMarketIndex = undefined;
|
|
2861
2898
|
let readableSpotMarketIndexes = undefined;
|
|
2862
|
-
|
|
2899
|
+
|
|
2900
|
+
if (typeof marketIndex === 'number') {
|
|
2863
2901
|
if (marketType && isVariant(marketType, 'perp')) {
|
|
2864
2902
|
readablePerpMarketIndex = marketIndex;
|
|
2865
2903
|
} else if (marketType && isVariant(marketType, 'spot')) {
|
|
@@ -3696,6 +3734,10 @@ export class DriftClient {
|
|
|
3696
3734
|
quote = fetchedQuote;
|
|
3697
3735
|
}
|
|
3698
3736
|
|
|
3737
|
+
if (!quote) {
|
|
3738
|
+
throw new Error("Could not fetch Jupiter's quote. Please try again.");
|
|
3739
|
+
}
|
|
3740
|
+
|
|
3699
3741
|
const transaction = await jupiterClient.getSwap({
|
|
3700
3742
|
quote,
|
|
3701
3743
|
userPublicKey: this.provider.wallet.publicKey,
|
|
@@ -4607,12 +4649,12 @@ export class DriftClient {
|
|
|
4607
4649
|
oraclePriceOffset: newOraclePriceOffset || null,
|
|
4608
4650
|
triggerPrice: newTriggerPrice || null,
|
|
4609
4651
|
triggerCondition: newTriggerCondition || null,
|
|
4610
|
-
auctionDuration: auctionDuration ||
|
|
4611
|
-
auctionStartPrice: auctionStartPrice ||
|
|
4612
|
-
auctionEndPrice: auctionEndPrice ||
|
|
4613
|
-
reduceOnly: reduceOnly ||
|
|
4652
|
+
auctionDuration: auctionDuration || 0,
|
|
4653
|
+
auctionStartPrice: auctionStartPrice || ZERO,
|
|
4654
|
+
auctionEndPrice: auctionEndPrice || ZERO,
|
|
4655
|
+
reduceOnly: reduceOnly || false,
|
|
4614
4656
|
postOnly: postOnly || null,
|
|
4615
|
-
immediateOrCancel: immediateOrCancel ||
|
|
4657
|
+
immediateOrCancel: immediateOrCancel || false,
|
|
4616
4658
|
policy: policy || null,
|
|
4617
4659
|
maxTs: maxTs || null,
|
|
4618
4660
|
};
|
|
@@ -4725,12 +4767,12 @@ export class DriftClient {
|
|
|
4725
4767
|
oraclePriceOffset: newOraclePriceOffset || null,
|
|
4726
4768
|
triggerPrice: newTriggerPrice || null,
|
|
4727
4769
|
triggerCondition: newTriggerCondition || null,
|
|
4728
|
-
auctionDuration: auctionDuration ||
|
|
4729
|
-
auctionStartPrice: auctionStartPrice ||
|
|
4730
|
-
auctionEndPrice: auctionEndPrice ||
|
|
4731
|
-
reduceOnly: reduceOnly ||
|
|
4770
|
+
auctionDuration: auctionDuration || 0,
|
|
4771
|
+
auctionStartPrice: auctionStartPrice || ZERO,
|
|
4772
|
+
auctionEndPrice: auctionEndPrice || ZERO,
|
|
4773
|
+
reduceOnly: reduceOnly || false,
|
|
4732
4774
|
postOnly: postOnly || null,
|
|
4733
|
-
immediateOrCancel: immediateOrCancel ||
|
|
4775
|
+
immediateOrCancel: immediateOrCancel || false,
|
|
4734
4776
|
policy: policy || null,
|
|
4735
4777
|
maxTs: maxTs || null,
|
|
4736
4778
|
};
|
|
@@ -4758,13 +4800,8 @@ export class DriftClient {
|
|
|
4758
4800
|
marketIndexes: number[]
|
|
4759
4801
|
): Promise<TransactionSignature> {
|
|
4760
4802
|
const ixs = await this.getSettlePNLsIxs(users, marketIndexes);
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
4764
|
-
units: 1_000_000,
|
|
4765
|
-
})
|
|
4766
|
-
)
|
|
4767
|
-
.add(...ixs);
|
|
4803
|
+
|
|
4804
|
+
const tx = await this.buildTransaction(ixs, { computeUnits: 1_000_000 });
|
|
4768
4805
|
|
|
4769
4806
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
4770
4807
|
return txSig;
|
|
@@ -5484,7 +5521,7 @@ export class DriftClient {
|
|
|
5484
5521
|
*/
|
|
5485
5522
|
fromSubaccount?: boolean;
|
|
5486
5523
|
}): Promise<TransactionSignature> {
|
|
5487
|
-
const
|
|
5524
|
+
const addIfStakeIxs = [];
|
|
5488
5525
|
|
|
5489
5526
|
const additionalSigners: Array<Signer> = [];
|
|
5490
5527
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
@@ -5501,7 +5538,7 @@ export class DriftClient {
|
|
|
5501
5538
|
);
|
|
5502
5539
|
tokenAccount = pubkey;
|
|
5503
5540
|
ixs.forEach((ix) => {
|
|
5504
|
-
|
|
5541
|
+
addIfStakeIxs.push(ix);
|
|
5505
5542
|
});
|
|
5506
5543
|
} else {
|
|
5507
5544
|
tokenAccount = collateralAccountPublicKey;
|
|
@@ -5513,14 +5550,14 @@ export class DriftClient {
|
|
|
5513
5550
|
marketIndex,
|
|
5514
5551
|
tokenAccount
|
|
5515
5552
|
);
|
|
5516
|
-
|
|
5553
|
+
addIfStakeIxs.push(withdrawIx);
|
|
5517
5554
|
}
|
|
5518
5555
|
|
|
5519
5556
|
if (initializeStakeAccount) {
|
|
5520
5557
|
const initializeIx = await this.getInitializeInsuranceFundStakeIx(
|
|
5521
5558
|
marketIndex
|
|
5522
5559
|
);
|
|
5523
|
-
|
|
5560
|
+
addIfStakeIxs.push(initializeIx);
|
|
5524
5561
|
}
|
|
5525
5562
|
|
|
5526
5563
|
const addFundsIx = await this.getAddInsuranceFundStakeIx(
|
|
@@ -5529,10 +5566,10 @@ export class DriftClient {
|
|
|
5529
5566
|
tokenAccount
|
|
5530
5567
|
);
|
|
5531
5568
|
|
|
5532
|
-
|
|
5569
|
+
addIfStakeIxs.push(addFundsIx);
|
|
5533
5570
|
|
|
5534
5571
|
if (createWSOLTokenAccount) {
|
|
5535
|
-
|
|
5572
|
+
addIfStakeIxs.push(
|
|
5536
5573
|
createCloseAccountInstruction(
|
|
5537
5574
|
tokenAccount,
|
|
5538
5575
|
this.wallet.publicKey,
|
|
@@ -5542,6 +5579,8 @@ export class DriftClient {
|
|
|
5542
5579
|
);
|
|
5543
5580
|
}
|
|
5544
5581
|
|
|
5582
|
+
const tx = await this.buildTransaction(addIfStakeIxs);
|
|
5583
|
+
|
|
5545
5584
|
const { txSig } = await this.sendTransaction(
|
|
5546
5585
|
tx,
|
|
5547
5586
|
additionalSigners,
|
|
@@ -5568,7 +5607,7 @@ export class DriftClient {
|
|
|
5568
5607
|
writableSpotMarketIndexes: [marketIndex],
|
|
5569
5608
|
});
|
|
5570
5609
|
|
|
5571
|
-
const
|
|
5610
|
+
const ix = await this.program.instruction.requestRemoveInsuranceFundStake(
|
|
5572
5611
|
marketIndex,
|
|
5573
5612
|
amount,
|
|
5574
5613
|
{
|
|
@@ -5584,6 +5623,8 @@ export class DriftClient {
|
|
|
5584
5623
|
}
|
|
5585
5624
|
);
|
|
5586
5625
|
|
|
5626
|
+
const tx = await this.buildTransaction(ix);
|
|
5627
|
+
|
|
5587
5628
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
5588
5629
|
return txSig;
|
|
5589
5630
|
}
|
|
@@ -5604,8 +5645,8 @@ export class DriftClient {
|
|
|
5604
5645
|
writableSpotMarketIndexes: [marketIndex],
|
|
5605
5646
|
});
|
|
5606
5647
|
|
|
5607
|
-
const
|
|
5608
|
-
await this.program.
|
|
5648
|
+
const ix =
|
|
5649
|
+
await this.program.instruction.cancelRequestRemoveInsuranceFundStake(
|
|
5609
5650
|
marketIndex,
|
|
5610
5651
|
{
|
|
5611
5652
|
accounts: {
|
|
@@ -5620,6 +5661,8 @@ export class DriftClient {
|
|
|
5620
5661
|
}
|
|
5621
5662
|
);
|
|
5622
5663
|
|
|
5664
|
+
const tx = await this.buildTransaction(ix);
|
|
5665
|
+
|
|
5623
5666
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
5624
5667
|
return txSig;
|
|
5625
5668
|
}
|
|
@@ -5628,7 +5671,7 @@ export class DriftClient {
|
|
|
5628
5671
|
marketIndex: number,
|
|
5629
5672
|
collateralAccountPublicKey: PublicKey
|
|
5630
5673
|
): Promise<TransactionSignature> {
|
|
5631
|
-
const
|
|
5674
|
+
const removeIfStakeIxs = [];
|
|
5632
5675
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
5633
5676
|
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
|
|
5634
5677
|
this.program.programId,
|
|
@@ -5650,7 +5693,7 @@ export class DriftClient {
|
|
|
5650
5693
|
);
|
|
5651
5694
|
tokenAccount = pubkey;
|
|
5652
5695
|
ixs.forEach((ix) => {
|
|
5653
|
-
|
|
5696
|
+
removeIfStakeIxs.push(ix);
|
|
5654
5697
|
});
|
|
5655
5698
|
} else {
|
|
5656
5699
|
tokenAccount = collateralAccountPublicKey;
|
|
@@ -5678,11 +5721,11 @@ export class DriftClient {
|
|
|
5678
5721
|
remainingAccounts,
|
|
5679
5722
|
});
|
|
5680
5723
|
|
|
5681
|
-
|
|
5724
|
+
removeIfStakeIxs.push(removeStakeIx);
|
|
5682
5725
|
|
|
5683
5726
|
// Close the wrapped sol account at the end of the transaction
|
|
5684
5727
|
if (createWSOLTokenAccount) {
|
|
5685
|
-
|
|
5728
|
+
removeIfStakeIxs.push(
|
|
5686
5729
|
createCloseAccountInstruction(
|
|
5687
5730
|
tokenAccount,
|
|
5688
5731
|
this.wallet.publicKey,
|
|
@@ -5692,6 +5735,8 @@ export class DriftClient {
|
|
|
5692
5735
|
);
|
|
5693
5736
|
}
|
|
5694
5737
|
|
|
5738
|
+
const tx = await this.buildTransaction(removeIfStakeIxs);
|
|
5739
|
+
|
|
5695
5740
|
const { txSig } = await this.sendTransaction(
|
|
5696
5741
|
tx,
|
|
5697
5742
|
additionalSigners,
|
|
@@ -5711,7 +5756,7 @@ export class DriftClient {
|
|
|
5711
5756
|
writableSpotMarketIndexes: [marketIndex],
|
|
5712
5757
|
});
|
|
5713
5758
|
|
|
5714
|
-
const
|
|
5759
|
+
const ix = await this.program.instruction.settleRevenueToInsuranceFund(
|
|
5715
5760
|
marketIndex,
|
|
5716
5761
|
{
|
|
5717
5762
|
accounts: {
|
|
@@ -5726,6 +5771,8 @@ export class DriftClient {
|
|
|
5726
5771
|
}
|
|
5727
5772
|
);
|
|
5728
5773
|
|
|
5774
|
+
const tx = await this.buildTransaction(ix);
|
|
5775
|
+
|
|
5729
5776
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
5730
5777
|
return txSig;
|
|
5731
5778
|
}
|
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.43.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -4098,6 +4098,27 @@
|
|
|
4098
4098
|
}
|
|
4099
4099
|
]
|
|
4100
4100
|
},
|
|
4101
|
+
{
|
|
4102
|
+
"name": "updateLiquidationMarginBufferRatio",
|
|
4103
|
+
"accounts": [
|
|
4104
|
+
{
|
|
4105
|
+
"name": "admin",
|
|
4106
|
+
"isMut": false,
|
|
4107
|
+
"isSigner": true
|
|
4108
|
+
},
|
|
4109
|
+
{
|
|
4110
|
+
"name": "state",
|
|
4111
|
+
"isMut": true,
|
|
4112
|
+
"isSigner": false
|
|
4113
|
+
}
|
|
4114
|
+
],
|
|
4115
|
+
"args": [
|
|
4116
|
+
{
|
|
4117
|
+
"name": "liquidationMarginBufferRatio",
|
|
4118
|
+
"type": "u32"
|
|
4119
|
+
}
|
|
4120
|
+
]
|
|
4121
|
+
},
|
|
4101
4122
|
{
|
|
4102
4123
|
"name": "updateOracleGuardRails",
|
|
4103
4124
|
"accounts": [
|
|
@@ -319,6 +319,10 @@ export class JupiterClient {
|
|
|
319
319
|
userPublicKey: PublicKey;
|
|
320
320
|
slippageBps?: number;
|
|
321
321
|
}): Promise<VersionedTransaction> {
|
|
322
|
+
if (!quote) {
|
|
323
|
+
throw new Error('Jupiter swap quote not provided. Please try again.');
|
|
324
|
+
}
|
|
325
|
+
|
|
322
326
|
const resp = await (
|
|
323
327
|
await fetch(`${this.url}/v6/swap`, {
|
|
324
328
|
method: 'POST',
|
|
@@ -334,8 +338,14 @@ export class JupiterClient {
|
|
|
334
338
|
).json();
|
|
335
339
|
const { swapTransaction } = resp;
|
|
336
340
|
|
|
337
|
-
|
|
338
|
-
|
|
341
|
+
try {
|
|
342
|
+
const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
|
|
343
|
+
return VersionedTransaction.deserialize(swapTransactionBuf);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
throw new Error(
|
|
346
|
+
'Something went wrong with creating the Jupiter swap transaction. Please try again.'
|
|
347
|
+
);
|
|
348
|
+
}
|
|
339
349
|
}
|
|
340
350
|
|
|
341
351
|
/**
|
package/src/math/amm.ts
CHANGED
|
@@ -760,9 +760,9 @@ export function calculateSpreadReserves(
|
|
|
760
760
|
quoteAssetReserve: amm.quoteAssetReserve,
|
|
761
761
|
};
|
|
762
762
|
}
|
|
763
|
-
|
|
763
|
+
const spreadFraction = BN.max(new BN(spread / 2), ONE);
|
|
764
764
|
const quoteAssetReserveDelta = amm.quoteAssetReserve.div(
|
|
765
|
-
BID_ASK_SPREAD_PRECISION.div(
|
|
765
|
+
BID_ASK_SPREAD_PRECISION.div(spreadFraction)
|
|
766
766
|
);
|
|
767
767
|
|
|
768
768
|
let quoteAssetReserve;
|
package/src/math/trade.ts
CHANGED
|
@@ -649,15 +649,15 @@ export function calculateEstimatedPerpEntryPrice(
|
|
|
649
649
|
}
|
|
650
650
|
}
|
|
651
651
|
|
|
652
|
-
const entryPrice =
|
|
653
|
-
.
|
|
654
|
-
|
|
652
|
+
const entryPrice =
|
|
653
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
654
|
+
? cumulativeQuoteFilled.mul(BASE_PRECISION).div(cumulativeBaseFilled)
|
|
655
|
+
: ZERO;
|
|
655
656
|
|
|
656
|
-
const priceImpact =
|
|
657
|
-
.
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
.abs();
|
|
657
|
+
const priceImpact =
|
|
658
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
659
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
660
|
+
: ZERO;
|
|
661
661
|
|
|
662
662
|
return {
|
|
663
663
|
entryPrice,
|
|
@@ -858,15 +858,15 @@ export function calculateEstimatedSpotEntryPrice(
|
|
|
858
858
|
}
|
|
859
859
|
}
|
|
860
860
|
|
|
861
|
-
const entryPrice =
|
|
862
|
-
.
|
|
863
|
-
|
|
861
|
+
const entryPrice =
|
|
862
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
863
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
864
|
+
: ZERO;
|
|
864
865
|
|
|
865
|
-
const priceImpact =
|
|
866
|
-
.
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
.abs();
|
|
866
|
+
const priceImpact =
|
|
867
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
868
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
869
|
+
: ZERO;
|
|
870
870
|
|
|
871
871
|
return {
|
|
872
872
|
entryPrice,
|
|
@@ -900,8 +900,8 @@ export function calculateEstimatedEntryPriceWithL2(
|
|
|
900
900
|
const levels = [...(takerIsLong ? l2.asks : l2.bids)];
|
|
901
901
|
let nextLevel = levels.shift();
|
|
902
902
|
|
|
903
|
-
let bestPrice;
|
|
904
|
-
let worstPrice;
|
|
903
|
+
let bestPrice: BN;
|
|
904
|
+
let worstPrice: BN;
|
|
905
905
|
if (nextLevel) {
|
|
906
906
|
bestPrice = nextLevel.price;
|
|
907
907
|
worstPrice = nextLevel.price;
|
|
@@ -945,15 +945,15 @@ export function calculateEstimatedEntryPriceWithL2(
|
|
|
945
945
|
}
|
|
946
946
|
}
|
|
947
947
|
|
|
948
|
-
const entryPrice =
|
|
949
|
-
.
|
|
950
|
-
|
|
948
|
+
const entryPrice =
|
|
949
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
950
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
951
|
+
: ZERO;
|
|
951
952
|
|
|
952
|
-
const priceImpact =
|
|
953
|
-
.
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
.abs();
|
|
953
|
+
const priceImpact =
|
|
954
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
955
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
956
|
+
: ZERO;
|
|
957
957
|
|
|
958
958
|
return {
|
|
959
959
|
entryPrice,
|