@drift-labs/sdk 2.30.0-beta.1 → 2.31.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/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +5 -2
- package/lib/accounts/types.d.ts +3 -3
- package/lib/adminClient.d.ts +1 -0
- package/lib/adminClient.js +9 -0
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/dlob/DLOB.d.ts +20 -1
- package/lib/dlob/DLOB.js +39 -0
- package/lib/driftClient.d.ts +13 -8
- package/lib/driftClient.js +32 -27
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/idl/drift.json +106 -15
- package/lib/jupiter/jupiterClient.d.ts +1 -1
- package/lib/jupiter/jupiterClient.js +7 -2
- package/lib/math/spotBalance.d.ts +41 -0
- package/lib/math/spotBalance.js +41 -0
- package/lib/token/index.d.ts +3 -2
- package/lib/token/index.js +9 -32
- package/lib/tokenFaucet.d.ts +3 -3
- package/lib/tokenFaucet.js +4 -9
- package/lib/tx/retryTxSender.js +3 -0
- package/lib/types.d.ts +22 -2
- package/lib/types.js +12 -1
- package/lib/wallet.d.ts +5 -3
- package/lib/wallet.js +19 -7
- package/package.json +2 -2
- package/src/accounts/pollingTokenAccountSubscriber.ts +8 -5
- package/src/accounts/types.ts +3 -3
- package/src/adminClient.ts +19 -0
- package/src/constants/spotMarkets.ts +11 -0
- package/src/dlob/DLOB.ts +75 -0
- package/src/driftClient.ts +57 -53
- package/src/examples/makeTradeExample.ts +2 -4
- package/src/idl/drift.json +106 -15
- package/src/jupiter/jupiterClient.ts +10 -2
- package/src/math/spotBalance.ts +41 -0
- package/src/token/index.ts +12 -36
- package/src/tokenFaucet.ts +15 -34
- package/src/tx/retryTxSender.ts +4 -0
- package/src/types.ts +23 -2
- package/src/wallet.ts +34 -12
- package/tests/dlob/helpers.ts +1 -0
- package/tests/dlob/test.ts +218 -40
- package/lib/util/getTokenAddress.d.ts +0 -2
- package/lib/util/getTokenAddress.js +0 -9
- package/src/util/getTokenAddress.ts +0 -18
|
@@ -5,7 +5,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
8
|
-
import {
|
|
8
|
+
import { Account } from '@solana/spl-token';
|
|
9
9
|
export declare class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
|
|
10
10
|
isSubscribed: boolean;
|
|
11
11
|
program: Program;
|
|
@@ -14,13 +14,13 @@ export declare class PollingTokenAccountSubscriber implements TokenAccountSubscr
|
|
|
14
14
|
accountLoader: BulkAccountLoader;
|
|
15
15
|
callbackId?: string;
|
|
16
16
|
errorCallbackId?: string;
|
|
17
|
-
tokenAccountAndSlot?: DataAndSlot<
|
|
17
|
+
tokenAccountAndSlot?: DataAndSlot<Account>;
|
|
18
18
|
constructor(publicKey: PublicKey, accountLoader: BulkAccountLoader);
|
|
19
19
|
subscribe(): Promise<boolean>;
|
|
20
20
|
addToAccountLoader(): Promise<void>;
|
|
21
21
|
fetch(): Promise<void>;
|
|
22
22
|
unsubscribe(): Promise<void>;
|
|
23
23
|
assertIsSubscribed(): void;
|
|
24
|
-
getTokenAccountAndSlot(): DataAndSlot<
|
|
24
|
+
getTokenAccountAndSlot(): DataAndSlot<Account>;
|
|
25
25
|
didSubscriptionSucceed(): boolean;
|
|
26
26
|
}
|
|
@@ -34,7 +34,7 @@ class PollingTokenAccountSubscriber {
|
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
this.callbackId = await this.accountLoader.addAccount(this.publicKey, (buffer, slot) => {
|
|
37
|
-
const tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
37
|
+
const tokenAccount = (0, token_1.parseTokenAccount)(buffer, this.publicKey);
|
|
38
38
|
this.tokenAccountAndSlot = { data: tokenAccount, slot };
|
|
39
39
|
// @ts-ignore
|
|
40
40
|
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
@@ -47,7 +47,10 @@ class PollingTokenAccountSubscriber {
|
|
|
47
47
|
async fetch() {
|
|
48
48
|
await this.accountLoader.load();
|
|
49
49
|
const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
|
|
50
|
-
this.tokenAccountAndSlot = {
|
|
50
|
+
this.tokenAccountAndSlot = {
|
|
51
|
+
data: (0, token_1.parseTokenAccount)(buffer, this.publicKey),
|
|
52
|
+
slot,
|
|
53
|
+
};
|
|
51
54
|
}
|
|
52
55
|
async unsubscribe() {
|
|
53
56
|
if (!this.isSubscribed) {
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserA
|
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
|
-
import {
|
|
7
|
+
import { Account } from '@solana/spl-token';
|
|
8
8
|
import { OracleInfo, OraclePriceData } from '..';
|
|
9
9
|
export interface AccountSubscriber<T> {
|
|
10
10
|
dataAndSlot?: DataAndSlot<T>;
|
|
@@ -57,7 +57,7 @@ export interface UserAccountSubscriber {
|
|
|
57
57
|
getUserAccountAndSlot(): DataAndSlot<UserAccount>;
|
|
58
58
|
}
|
|
59
59
|
export interface TokenAccountEvents {
|
|
60
|
-
tokenAccountUpdate: (payload:
|
|
60
|
+
tokenAccountUpdate: (payload: Account) => void;
|
|
61
61
|
update: void;
|
|
62
62
|
error: (e: Error) => void;
|
|
63
63
|
}
|
|
@@ -67,7 +67,7 @@ export interface TokenAccountSubscriber {
|
|
|
67
67
|
subscribe(): Promise<boolean>;
|
|
68
68
|
fetch(): Promise<void>;
|
|
69
69
|
unsubscribe(): Promise<void>;
|
|
70
|
-
getTokenAccountAndSlot(): DataAndSlot<
|
|
70
|
+
getTokenAccountAndSlot(): DataAndSlot<Account>;
|
|
71
71
|
}
|
|
72
72
|
export interface OracleEvents {
|
|
73
73
|
oracleUpdate: (payload: OraclePriceData) => void;
|
package/lib/adminClient.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare class AdminClient extends DriftClient {
|
|
|
19
19
|
depositIntoPerpMarketFeePool(perpMarketIndex: number, amount: BN, sourceVault: PublicKey): Promise<TransactionSignature>;
|
|
20
20
|
updateAdmin(admin: PublicKey): Promise<TransactionSignature>;
|
|
21
21
|
updatePerpMarketCurveUpdateIntensity(perpMarketIndex: number, curveUpdateIntensity: number): Promise<TransactionSignature>;
|
|
22
|
+
updatePerpMarketTargetBaseAssetAmountPerLp(perpMarketIndex: number, targetBaseAssetAmountPerLP: number): Promise<TransactionSignature>;
|
|
22
23
|
updatePerpMarketMarginRatio(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
|
|
23
24
|
updatePerpMarketImfFactor(perpMarketIndex: number, imfFactor: number, unrealizedPnlImfFactor: number): Promise<TransactionSignature>;
|
|
24
25
|
updatePerpMarketBaseSpread(perpMarketIndex: number, baseSpread: number): Promise<TransactionSignature>;
|
package/lib/adminClient.js
CHANGED
|
@@ -292,6 +292,15 @@ class AdminClient extends driftClient_1.DriftClient {
|
|
|
292
292
|
},
|
|
293
293
|
});
|
|
294
294
|
}
|
|
295
|
+
async updatePerpMarketTargetBaseAssetAmountPerLp(perpMarketIndex, targetBaseAssetAmountPerLP) {
|
|
296
|
+
return await this.program.rpc.updatePerpMarketTargetBaseAssetAmountPerLp(targetBaseAssetAmountPerLP, {
|
|
297
|
+
accounts: {
|
|
298
|
+
admin: this.wallet.publicKey,
|
|
299
|
+
state: await this.getStatePublicKey(),
|
|
300
|
+
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
}
|
|
295
304
|
async updatePerpMarketMarginRatio(perpMarketIndex, marginRatioInitial, marginRatioMaintenance) {
|
|
296
305
|
const tx = await this.program.transaction.updatePerpMarketMarginRatio(marginRatioInitial, marginRatioMaintenance, {
|
|
297
306
|
accounts: {
|
|
@@ -58,6 +58,16 @@ exports.MainnetSpotMarkets = [
|
|
|
58
58
|
serumMarket: new web3_js_1.PublicKey('8BnEgHoWFysVcuFFX7QztDmzuH8r5ZFvyP3sYwn1XTh6'),
|
|
59
59
|
phoenixMarket: new web3_js_1.PublicKey('4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg'),
|
|
60
60
|
},
|
|
61
|
+
{
|
|
62
|
+
symbol: 'mSOL',
|
|
63
|
+
marketIndex: 2,
|
|
64
|
+
oracle: new web3_js_1.PublicKey('E4v1BBgoso9s64TQvmyownAVJbhbEPGyzA3qn4n46qj9'),
|
|
65
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
66
|
+
mint: new web3_js_1.PublicKey('mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So'),
|
|
67
|
+
precision: new __1.BN(10).pow(numericConstants_1.NINE),
|
|
68
|
+
precisionExp: numericConstants_1.NINE,
|
|
69
|
+
serumMarket: new web3_js_1.PublicKey('9Lyhks5bQQxb9EyyX55NtgKQzpM4WK7JCmeaWuQ5MoXD'),
|
|
70
|
+
},
|
|
61
71
|
];
|
|
62
72
|
exports.SpotMarkets = {
|
|
63
73
|
devnet: exports.DevnetSpotMarkets,
|
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NodeList } from './NodeList';
|
|
2
|
-
import { BN, DLOBNode, DLOBNodeType, DriftClient, MarketType, MarketTypeStr, OraclePriceData, Order, OrderActionRecord, OrderRecord, PerpMarketAccount, SlotSubscriber, SpotMarketAccount, StateAccount, TriggerOrderNode, UserMap } from '..';
|
|
2
|
+
import { BN, DLOBNode, DLOBNodeType, DriftClient, MarketType, MarketTypeStr, OraclePriceData, Order, OrderActionRecord, OrderRecord, PerpMarketAccount, PositionDirection, SlotSubscriber, SpotMarketAccount, StateAccount, TriggerOrderNode, UserMap } from '..';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
4
|
import { DLOBOrders } from './DLOBOrders';
|
|
5
5
|
import { L2OrderBook, L2OrderBookGenerator, L3OrderBook } from './orderBookLevels';
|
|
@@ -135,5 +135,24 @@ export declare class DLOB {
|
|
|
135
135
|
slot: number;
|
|
136
136
|
oraclePriceData: OraclePriceData;
|
|
137
137
|
}): L3OrderBook;
|
|
138
|
+
private estimateFillExactBaseAmountInForSide;
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param param.marketIndex the index of the market
|
|
142
|
+
* @param param.marketType the type of the market
|
|
143
|
+
* @param param.baseAmount the base amount in to estimate
|
|
144
|
+
* @param param.orderDirection the direction of the trade
|
|
145
|
+
* @param param.slot current slot for estimating dlob node price
|
|
146
|
+
* @param param.oraclePriceData the oracle price data
|
|
147
|
+
* @returns the estimated quote amount filled: QUOTE_PRECISION
|
|
148
|
+
*/
|
|
149
|
+
estimateFillWithExactBaseAmount({ marketIndex, marketType, baseAmount, orderDirection, slot, oraclePriceData, }: {
|
|
150
|
+
marketIndex: number;
|
|
151
|
+
marketType: MarketType;
|
|
152
|
+
baseAmount: BN;
|
|
153
|
+
orderDirection: PositionDirection;
|
|
154
|
+
slot: number;
|
|
155
|
+
oraclePriceData: OraclePriceData;
|
|
156
|
+
}): BN;
|
|
138
157
|
}
|
|
139
158
|
export {};
|
package/lib/dlob/DLOB.js
CHANGED
|
@@ -1023,5 +1023,44 @@ class DLOB {
|
|
|
1023
1023
|
asks,
|
|
1024
1024
|
};
|
|
1025
1025
|
}
|
|
1026
|
+
estimateFillExactBaseAmountInForSide(baseAmountIn, oraclePriceData, slot, dlobSide) {
|
|
1027
|
+
let runningSumQuote = __1.ZERO;
|
|
1028
|
+
let runningSumBase = __1.ZERO;
|
|
1029
|
+
for (const side of dlobSide) {
|
|
1030
|
+
const price = side.getPrice(oraclePriceData, slot); //side.order.quoteAssetAmount.div(side.order.baseAssetAmount);
|
|
1031
|
+
const baseAmountRemaining = side.order.baseAssetAmount.sub(side.order.baseAssetAmountFilled);
|
|
1032
|
+
if (runningSumBase.add(baseAmountRemaining).gt(baseAmountIn)) {
|
|
1033
|
+
const remainingBase = baseAmountIn.sub(runningSumBase);
|
|
1034
|
+
runningSumBase = runningSumBase.add(remainingBase);
|
|
1035
|
+
runningSumQuote = runningSumQuote.add(remainingBase.mul(price));
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
else {
|
|
1039
|
+
runningSumBase = runningSumBase.add(baseAmountRemaining);
|
|
1040
|
+
runningSumQuote = runningSumQuote.add(baseAmountRemaining.mul(price));
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
return runningSumQuote
|
|
1044
|
+
.mul(__1.QUOTE_PRECISION)
|
|
1045
|
+
.div(__1.BASE_PRECISION.mul(__1.PRICE_PRECISION));
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
*
|
|
1049
|
+
* @param param.marketIndex the index of the market
|
|
1050
|
+
* @param param.marketType the type of the market
|
|
1051
|
+
* @param param.baseAmount the base amount in to estimate
|
|
1052
|
+
* @param param.orderDirection the direction of the trade
|
|
1053
|
+
* @param param.slot current slot for estimating dlob node price
|
|
1054
|
+
* @param param.oraclePriceData the oracle price data
|
|
1055
|
+
* @returns the estimated quote amount filled: QUOTE_PRECISION
|
|
1056
|
+
*/
|
|
1057
|
+
estimateFillWithExactBaseAmount({ marketIndex, marketType, baseAmount, orderDirection, slot, oraclePriceData, }) {
|
|
1058
|
+
if ((0, __1.isVariant)(orderDirection, 'long')) {
|
|
1059
|
+
return this.estimateFillExactBaseAmountInForSide(baseAmount, oraclePriceData, slot, this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData));
|
|
1060
|
+
}
|
|
1061
|
+
else if ((0, __1.isVariant)(orderDirection, 'short')) {
|
|
1062
|
+
return this.estimateFillExactBaseAmountInForSide(baseAmount, oraclePriceData, slot, this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData));
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1026
1065
|
}
|
|
1027
1066
|
exports.DLOB = DLOB;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
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, PhoenixV1FulfillmentConfigAccount, ModifyOrderPolicy } from './types';
|
|
3
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount, ReferrerNameAccount, OrderTriggerCondition, PerpMarketExtendedInfo, UserStatsAccount, PhoenixV1FulfillmentConfigAccount, ModifyOrderPolicy, SwapReduceOnly } from './types';
|
|
4
4
|
import * as anchor from '@coral-xyz/anchor';
|
|
5
5
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, Transaction, TransactionInstruction, AccountMeta, Signer, AddressLookupTableAccount, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
|
|
6
6
|
import { TokenFaucet } from './tokenFaucet';
|
|
@@ -13,7 +13,7 @@ import { DriftClientConfig } from './driftClientConfig';
|
|
|
13
13
|
import { User } from './user';
|
|
14
14
|
import { UserSubscriptionConfig } from './userConfig';
|
|
15
15
|
import { UserStats } from './userStats';
|
|
16
|
-
import { JupiterClient } from './jupiter/jupiterClient';
|
|
16
|
+
import { JupiterClient, Route, SwapMode } from './jupiter/jupiterClient';
|
|
17
17
|
type RemainingAccountParams = {
|
|
18
18
|
userAccounts: UserAccount[];
|
|
19
19
|
writablePerpMarketIndexes?: number[];
|
|
@@ -172,7 +172,7 @@ export declare class DriftClient {
|
|
|
172
172
|
* @param useNative
|
|
173
173
|
*/
|
|
174
174
|
getAssociatedTokenAccount(marketIndex: number, useNative?: boolean): Promise<PublicKey>;
|
|
175
|
-
createAssociatedTokenAccountIdempotentInstruction(account: PublicKey, payer: PublicKey, owner: PublicKey, mint: PublicKey):
|
|
175
|
+
createAssociatedTokenAccountIdempotentInstruction(account: PublicKey, payer: PublicKey, owner: PublicKey, mint: PublicKey): TransactionInstruction;
|
|
176
176
|
/**
|
|
177
177
|
* Deposit funds into the given spot market
|
|
178
178
|
*
|
|
@@ -285,16 +285,20 @@ export declare class DriftClient {
|
|
|
285
285
|
* @param inAssociatedTokenAccount the token account to
|
|
286
286
|
* @param amount the amount of the token to sell
|
|
287
287
|
* @param slippageBps the max slippage passed to jupiter api
|
|
288
|
+
* @param route the jupiter route to use for the swap
|
|
288
289
|
* @param txParams
|
|
289
290
|
*/
|
|
290
|
-
swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, txParams, }: {
|
|
291
|
+
swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, }: {
|
|
291
292
|
jupiterClient: JupiterClient;
|
|
292
293
|
outMarketIndex: number;
|
|
293
294
|
inMarketIndex: number;
|
|
294
|
-
outAssociatedTokenAccount
|
|
295
|
-
inAssociatedTokenAccount
|
|
295
|
+
outAssociatedTokenAccount?: PublicKey;
|
|
296
|
+
inAssociatedTokenAccount?: PublicKey;
|
|
296
297
|
amount: BN;
|
|
297
|
-
slippageBps
|
|
298
|
+
slippageBps?: number;
|
|
299
|
+
swapMode?: SwapMode;
|
|
300
|
+
route?: Route;
|
|
301
|
+
reduceOnly?: SwapReduceOnly;
|
|
298
302
|
txParams?: TxParams;
|
|
299
303
|
}): Promise<TransactionSignature>;
|
|
300
304
|
/**
|
|
@@ -307,13 +311,14 @@ export declare class DriftClient {
|
|
|
307
311
|
* @param outTokenAccount the token account to receive the tokens being bought
|
|
308
312
|
* @param limitPrice the limit price of the swap
|
|
309
313
|
*/
|
|
310
|
-
getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, }: {
|
|
314
|
+
getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, }: {
|
|
311
315
|
outMarketIndex: number;
|
|
312
316
|
inMarketIndex: number;
|
|
313
317
|
amountIn: BN;
|
|
314
318
|
inTokenAccount: PublicKey;
|
|
315
319
|
outTokenAccount: PublicKey;
|
|
316
320
|
limitPrice?: BN;
|
|
321
|
+
reduceOnly?: SwapReduceOnly;
|
|
317
322
|
}): Promise<{
|
|
318
323
|
beginSwapIx: TransactionInstruction;
|
|
319
324
|
endSwapIx: TransactionInstruction;
|
package/lib/driftClient.js
CHANGED
|
@@ -411,7 +411,7 @@ class DriftClient {
|
|
|
411
411
|
}
|
|
412
412
|
const state = this.getStateAccount();
|
|
413
413
|
if (!state.whitelistMint.equals(web3_js_1.PublicKey.default)) {
|
|
414
|
-
const associatedTokenPublicKey = await
|
|
414
|
+
const associatedTokenPublicKey = await (0, spl_token_1.getAssociatedTokenAddress)(state.whitelistMint, this.wallet.publicKey);
|
|
415
415
|
remainingAccounts.push({
|
|
416
416
|
pubkey: associatedTokenPublicKey,
|
|
417
417
|
isWritable: false,
|
|
@@ -955,9 +955,9 @@ class DriftClient {
|
|
|
955
955
|
return this.wallet.publicKey;
|
|
956
956
|
}
|
|
957
957
|
const mint = spotMarket.mint;
|
|
958
|
-
return await
|
|
958
|
+
return await (0, spl_token_1.getAssociatedTokenAddress)(mint, this.wallet.publicKey);
|
|
959
959
|
}
|
|
960
|
-
|
|
960
|
+
createAssociatedTokenAccountIdempotentInstruction(account, payer, owner, mint) {
|
|
961
961
|
return new web3_js_1.TransactionInstruction({
|
|
962
962
|
keys: [
|
|
963
963
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
@@ -965,7 +965,7 @@ class DriftClient {
|
|
|
965
965
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
966
966
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
967
967
|
{
|
|
968
|
-
pubkey:
|
|
968
|
+
pubkey: anchor.web3.SystemProgram.programId,
|
|
969
969
|
isSigner: false,
|
|
970
970
|
isWritable: false,
|
|
971
971
|
},
|
|
@@ -1006,7 +1006,7 @@ class DriftClient {
|
|
|
1006
1006
|
tx.add(depositCollateralIx);
|
|
1007
1007
|
// Close the wrapped sol account at the end of the transaction
|
|
1008
1008
|
if (createWSOLTokenAccount) {
|
|
1009
|
-
tx.add(spl_token_1.
|
|
1009
|
+
tx.add((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAccount, signerAuthority, signerAuthority, []));
|
|
1010
1010
|
}
|
|
1011
1011
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
1012
1012
|
this.spotMarketLastSlotCache.set(marketIndex, slot);
|
|
@@ -1072,13 +1072,12 @@ class DriftClient {
|
|
|
1072
1072
|
space: 165,
|
|
1073
1073
|
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1074
1074
|
}));
|
|
1075
|
-
result.ixs.push(spl_token_1.
|
|
1075
|
+
result.ixs.push((0, spl_token_1.createInitializeAccountInstruction)(wrappedSolAccount.publicKey, spotMarkets_1.WRAPPED_SOL_MINT, authority));
|
|
1076
1076
|
result.signers.push(wrappedSolAccount);
|
|
1077
1077
|
return result;
|
|
1078
1078
|
}
|
|
1079
1079
|
getAssociatedTokenAccountCreationIx(tokenMintAddress, associatedTokenAddress) {
|
|
1080
|
-
|
|
1081
|
-
return createAssociatedAccountIx;
|
|
1080
|
+
return (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.wallet.publicKey, associatedTokenAddress, this.wallet.publicKey, tokenMintAddress);
|
|
1082
1081
|
}
|
|
1083
1082
|
/**
|
|
1084
1083
|
* Creates the Clearing House User account for a user, and deposits some initial collateral
|
|
@@ -1126,7 +1125,7 @@ class DriftClient {
|
|
|
1126
1125
|
tx.add(initializeUserAccountIx).add(depositCollateralIx);
|
|
1127
1126
|
// Close the wrapped sol account at the end of the transaction
|
|
1128
1127
|
if (createWSOLTokenAccount) {
|
|
1129
|
-
tx.add(spl_token_1.
|
|
1128
|
+
tx.add((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, authority, authority, []));
|
|
1130
1129
|
}
|
|
1131
1130
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
1132
1131
|
this.spotMarketLastSlotCache.set(marketIndex, slot);
|
|
@@ -1184,7 +1183,7 @@ class DriftClient {
|
|
|
1184
1183
|
tx.add(withdrawCollateral);
|
|
1185
1184
|
// Close the wrapped sol account at the end of the transaction
|
|
1186
1185
|
if (createWSOLTokenAccount) {
|
|
1187
|
-
tx.add(spl_token_1.
|
|
1186
|
+
tx.add((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAddress, authority, authority, []));
|
|
1188
1187
|
}
|
|
1189
1188
|
const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
1190
1189
|
this.spotMarketLastSlotCache.set(marketIndex, slot);
|
|
@@ -1974,21 +1973,25 @@ class DriftClient {
|
|
|
1974
1973
|
* @param inAssociatedTokenAccount the token account to
|
|
1975
1974
|
* @param amount the amount of the token to sell
|
|
1976
1975
|
* @param slippageBps the max slippage passed to jupiter api
|
|
1976
|
+
* @param route the jupiter route to use for the swap
|
|
1977
1977
|
* @param txParams
|
|
1978
1978
|
*/
|
|
1979
|
-
async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, txParams, }) {
|
|
1979
|
+
async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, }) {
|
|
1980
1980
|
const outMarket = this.getSpotMarketAccount(outMarketIndex);
|
|
1981
1981
|
const inMarket = this.getSpotMarketAccount(inMarketIndex);
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1982
|
+
if (!route) {
|
|
1983
|
+
const routes = await jupiterClient.getRoutes({
|
|
1984
|
+
inputMint: inMarket.mint,
|
|
1985
|
+
outputMint: outMarket.mint,
|
|
1986
|
+
amount,
|
|
1987
|
+
slippageBps,
|
|
1988
|
+
swapMode,
|
|
1989
|
+
});
|
|
1990
|
+
if (!routes || routes.length === 0) {
|
|
1991
|
+
throw new Error('No jupiter routes found');
|
|
1992
|
+
}
|
|
1993
|
+
route = routes[0];
|
|
1990
1994
|
}
|
|
1991
|
-
const route = routes[0];
|
|
1992
1995
|
const transaction = await jupiterClient.getSwapTransaction({
|
|
1993
1996
|
route,
|
|
1994
1997
|
userPublicKey: this.provider.wallet.publicKey,
|
|
@@ -2004,14 +2007,14 @@ class DriftClient {
|
|
|
2004
2007
|
});
|
|
2005
2008
|
const preInstructions = [];
|
|
2006
2009
|
if (!outAssociatedTokenAccount) {
|
|
2007
|
-
outAssociatedTokenAccount = await this.getAssociatedTokenAccount(outMarket.marketIndex);
|
|
2010
|
+
outAssociatedTokenAccount = await this.getAssociatedTokenAccount(outMarket.marketIndex, false);
|
|
2008
2011
|
const accountInfo = await this.connection.getAccountInfo(outAssociatedTokenAccount);
|
|
2009
2012
|
if (!accountInfo) {
|
|
2010
2013
|
preInstructions.push(this.createAssociatedTokenAccountIdempotentInstruction(outAssociatedTokenAccount, this.provider.wallet.publicKey, this.provider.wallet.publicKey, outMarket.mint));
|
|
2011
2014
|
}
|
|
2012
2015
|
}
|
|
2013
2016
|
if (!inAssociatedTokenAccount) {
|
|
2014
|
-
inAssociatedTokenAccount = await this.getAssociatedTokenAccount(inMarket.marketIndex);
|
|
2017
|
+
inAssociatedTokenAccount = await this.getAssociatedTokenAccount(inMarket.marketIndex, false);
|
|
2015
2018
|
const accountInfo = await this.connection.getAccountInfo(inAssociatedTokenAccount);
|
|
2016
2019
|
if (!accountInfo) {
|
|
2017
2020
|
preInstructions.push(this.createAssociatedTokenAccountIdempotentInstruction(inAssociatedTokenAccount, this.provider.wallet.publicKey, this.provider.wallet.publicKey, inMarket.mint));
|
|
@@ -2023,6 +2026,7 @@ class DriftClient {
|
|
|
2023
2026
|
amountIn: amount,
|
|
2024
2027
|
inTokenAccount: inAssociatedTokenAccount,
|
|
2025
2028
|
outTokenAccount: outAssociatedTokenAccount,
|
|
2029
|
+
reduceOnly,
|
|
2026
2030
|
});
|
|
2027
2031
|
const instructions = [
|
|
2028
2032
|
...preInstructions,
|
|
@@ -2046,7 +2050,7 @@ class DriftClient {
|
|
|
2046
2050
|
* @param outTokenAccount the token account to receive the tokens being bought
|
|
2047
2051
|
* @param limitPrice the limit price of the swap
|
|
2048
2052
|
*/
|
|
2049
|
-
async getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, }) {
|
|
2053
|
+
async getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, }) {
|
|
2050
2054
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
2051
2055
|
const remainingAccounts = this.getRemainingAccounts({
|
|
2052
2056
|
userAccounts: [this.getUserAccount()],
|
|
@@ -2070,7 +2074,7 @@ class DriftClient {
|
|
|
2070
2074
|
},
|
|
2071
2075
|
remainingAccounts,
|
|
2072
2076
|
});
|
|
2073
|
-
const endSwapIx = await this.program.instruction.endSwap(inMarketIndex, outMarketIndex, limitPrice !== null && limitPrice !== void 0 ? limitPrice : null, {
|
|
2077
|
+
const endSwapIx = await this.program.instruction.endSwap(inMarketIndex, outMarketIndex, limitPrice !== null && limitPrice !== void 0 ? limitPrice : null, reduceOnly !== null && reduceOnly !== void 0 ? reduceOnly : null, {
|
|
2074
2078
|
accounts: {
|
|
2075
2079
|
state: await this.getStatePublicKey(),
|
|
2076
2080
|
user: userAccountPublicKey,
|
|
@@ -2848,7 +2852,7 @@ class DriftClient {
|
|
|
2848
2852
|
const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount);
|
|
2849
2853
|
tx.add(addFundsIx);
|
|
2850
2854
|
if (createWSOLTokenAccount) {
|
|
2851
|
-
tx.add(spl_token_1.
|
|
2855
|
+
tx.add((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
|
|
2852
2856
|
}
|
|
2853
2857
|
const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
2854
2858
|
return txSig;
|
|
@@ -2938,7 +2942,7 @@ class DriftClient {
|
|
|
2938
2942
|
tx.add(removeStakeIx);
|
|
2939
2943
|
// Close the wrapped sol account at the end of the transaction
|
|
2940
2944
|
if (createWSOLTokenAccount) {
|
|
2941
|
-
tx.add(spl_token_1.
|
|
2945
|
+
tx.add((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
|
|
2942
2946
|
}
|
|
2943
2947
|
const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
2944
2948
|
return txSig;
|
|
@@ -3057,7 +3061,8 @@ class DriftClient {
|
|
|
3057
3061
|
else {
|
|
3058
3062
|
allIx.push(instructions);
|
|
3059
3063
|
}
|
|
3060
|
-
|
|
3064
|
+
txVersion = txVersion !== null && txVersion !== void 0 ? txVersion : this.txVersion;
|
|
3065
|
+
if (txVersion === 'legacy') {
|
|
3061
3066
|
return new web3_js_1.Transaction().add(...allIx);
|
|
3062
3067
|
}
|
|
3063
3068
|
else {
|
|
@@ -8,7 +8,7 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
8
8
|
const __2 = require("..");
|
|
9
9
|
const spotMarkets_1 = require("../constants/spotMarkets");
|
|
10
10
|
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
11
|
-
return
|
|
11
|
+
return (0, spl_token_1.getAssociatedTokenAddress)(new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
12
12
|
};
|
|
13
13
|
exports.getTokenAddress = getTokenAddress;
|
|
14
14
|
const env = 'devnet';
|