@velocity-exchange/sdk 0.8.0 → 0.10.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/browser/adminClient.d.ts +13 -10
- package/lib/browser/adminClient.js +16 -21
- package/lib/browser/decode/user.js +3 -1
- package/lib/browser/equityFloorManager.d.ts +154 -0
- package/lib/browser/equityFloorManager.js +283 -0
- package/lib/browser/idl/velocity.d.ts +85 -10
- package/lib/browser/idl/velocity.json +85 -10
- package/lib/browser/index.d.ts +4 -0
- package/lib/browser/index.js +5 -0
- package/lib/browser/jupiter/jupiterClient.d.ts +31 -27
- package/lib/browser/jupiter/jupiterClient.js +84 -37
- package/lib/browser/math/margin.d.ts +28 -0
- package/lib/browser/math/margin.js +43 -1
- package/lib/browser/math/superStake.d.ts +9 -9
- package/lib/browser/math/superStake.js +8 -8
- package/lib/browser/swap/UnifiedSwapClient.d.ts +38 -76
- package/lib/browser/swap/UnifiedSwapClient.js +53 -110
- package/lib/browser/swap/routeInstructions.d.ts +19 -0
- package/lib/browser/swap/routeInstructions.js +43 -0
- package/lib/browser/swap/types.d.ts +196 -0
- package/lib/browser/swap/types.js +88 -0
- package/lib/browser/titan/titanClient.d.ts +37 -67
- package/lib/browser/titan/titanClient.js +160 -103
- package/lib/browser/types.d.ts +6 -2
- package/lib/browser/types.js +2 -0
- package/lib/browser/user.d.ts +43 -18
- package/lib/browser/user.js +94 -45
- package/lib/browser/velocityClient.d.ts +86 -103
- package/lib/browser/velocityClient.js +157 -298
- package/lib/node/adminClient.d.ts +13 -10
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +16 -21
- package/lib/node/decode/user.d.ts.map +1 -1
- package/lib/node/decode/user.js +3 -1
- package/lib/node/equityFloorManager.d.ts +155 -0
- package/lib/node/equityFloorManager.d.ts.map +1 -0
- package/lib/node/equityFloorManager.js +283 -0
- package/lib/node/idl/velocity.d.ts +85 -10
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +85 -10
- package/lib/node/index.d.ts +4 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +5 -0
- package/lib/node/jupiter/jupiterClient.d.ts +31 -27
- package/lib/node/jupiter/jupiterClient.d.ts.map +1 -1
- package/lib/node/jupiter/jupiterClient.js +84 -37
- package/lib/node/math/margin.d.ts +28 -0
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +43 -1
- package/lib/node/math/superStake.d.ts +9 -9
- package/lib/node/math/superStake.d.ts.map +1 -1
- package/lib/node/math/superStake.js +8 -8
- package/lib/node/swap/UnifiedSwapClient.d.ts +38 -76
- package/lib/node/swap/UnifiedSwapClient.d.ts.map +1 -1
- package/lib/node/swap/UnifiedSwapClient.js +53 -110
- package/lib/node/swap/routeInstructions.d.ts +20 -0
- package/lib/node/swap/routeInstructions.d.ts.map +1 -0
- package/lib/node/swap/routeInstructions.js +43 -0
- package/lib/node/swap/types.d.ts +197 -0
- package/lib/node/swap/types.d.ts.map +1 -0
- package/lib/node/swap/types.js +88 -0
- package/lib/node/titan/titanClient.d.ts +37 -67
- package/lib/node/titan/titanClient.d.ts.map +1 -1
- package/lib/node/titan/titanClient.js +160 -103
- package/lib/node/types.d.ts +6 -2
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/types.js +2 -0
- package/lib/node/user.d.ts +43 -18
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +94 -45
- package/lib/node/velocityClient.d.ts +86 -103
- package/lib/node/velocityClient.d.ts.map +1 -1
- package/lib/node/velocityClient.js +157 -298
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
3
|
+
exports.getEquityFloorLevel = exports.calculateEquityFloorAutoDelta = exports.calculateCollateralDepositRequiredForTrade = exports.calculateMarginUSDCRequiredForTrade = exports.calculatePerpLiabilityValue = exports.calculateWorstCasePerpLiabilityValue = exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Margin calculation helpers — TypeScript mirror of `programs/velocity/src/math/margin.rs`.
|
|
6
6
|
* Computes initial/maintenance margin requirements, free collateral, and account health.
|
|
@@ -210,3 +210,45 @@ function calculateCollateralDepositRequiredForTrade(velocityClient, targetMarket
|
|
|
210
210
|
return baseAmountRequired;
|
|
211
211
|
}
|
|
212
212
|
exports.calculateCollateralDepositRequiredForTrade = calculateCollateralDepositRequiredForTrade;
|
|
213
|
+
/**
|
|
214
|
+
* Minimal equity floor to carry along with a quote transfer of `amount` out
|
|
215
|
+
* of a subaccount so the debited side ends at/above its buffered floor
|
|
216
|
+
* (`equityFloor + equityFloorBuffer`): the first
|
|
217
|
+
* `netEquity - (floor + buffer)` of the transfer carries no floor, the
|
|
218
|
+
* remainder carries floor one-for-one, capped at the floor the subaccount
|
|
219
|
+
* actually holds. `netEquity` is `User.getNetUsdValue()`, the metric the
|
|
220
|
+
* onchain floor checks use. Returns zero when no floor is set. The result
|
|
221
|
+
* never exceeds `amount`, so a credited side that met its own buffered floor
|
|
222
|
+
* before the transfer still meets it after. All values QUOTE_PRECISION.
|
|
223
|
+
*/
|
|
224
|
+
function calculateEquityFloorAutoDelta(amount, netEquity, equityFloor, equityFloorBuffer) {
|
|
225
|
+
if (equityFloor.lte(numericConstants_1.ZERO)) {
|
|
226
|
+
return numericConstants_1.ZERO;
|
|
227
|
+
}
|
|
228
|
+
const excess = anchor_1.BN.max(netEquity.sub(equityFloor.add(equityFloorBuffer)), numericConstants_1.ZERO);
|
|
229
|
+
return anchor_1.BN.min(anchor_1.BN.max(amount.sub(excess), numericConstants_1.ZERO), equityFloor);
|
|
230
|
+
}
|
|
231
|
+
exports.calculateEquityFloorAutoDelta = calculateEquityFloorAutoDelta;
|
|
232
|
+
/**
|
|
233
|
+
* Classifies `netEquity` (`User.getNetUsdValue()`) against the floor
|
|
234
|
+
* thresholds. Used by the `EquityFloorManager` and the equity-floor guard bot
|
|
235
|
+
* so both report the same levels. `warningBufferMultiple` scales the warning
|
|
236
|
+
* threshold above the floor (default 2: warn inside `floor + 2 * buffer`).
|
|
237
|
+
* All QUOTE_PRECISION.
|
|
238
|
+
*/
|
|
239
|
+
function getEquityFloorLevel(netEquity, equityFloor, equityFloorBuffer, warningBufferMultiple = 2) {
|
|
240
|
+
if (equityFloor.lte(numericConstants_1.ZERO)) {
|
|
241
|
+
return 'disabled';
|
|
242
|
+
}
|
|
243
|
+
if (netEquity.lt(equityFloor)) {
|
|
244
|
+
return 'breached';
|
|
245
|
+
}
|
|
246
|
+
if (netEquity.lt(equityFloor.add(equityFloorBuffer))) {
|
|
247
|
+
return 'critical';
|
|
248
|
+
}
|
|
249
|
+
if (netEquity.lt(equityFloor.add(equityFloorBuffer.muln(warningBufferMultiple)))) {
|
|
250
|
+
return 'warning';
|
|
251
|
+
}
|
|
252
|
+
return 'healthy';
|
|
253
|
+
}
|
|
254
|
+
exports.getEquityFloorLevel = getEquityFloorLevel;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
-
import { JupiterClient,
|
|
2
|
+
import { JupiterClient, JupiterSwapQuote } from '../jupiter/jupiterClient';
|
|
3
3
|
import { VelocityClient } from '../velocityClient';
|
|
4
4
|
import { BN } from '../isomorphic/anchor';
|
|
5
5
|
import { User } from '../user';
|
|
@@ -61,7 +61,7 @@ export declare function fetchBSolVelocityEmissions(): Promise<fetch.Response>;
|
|
|
61
61
|
* @param {boolean} [params.forceMarinade] - Force the direct Marinade stake path over a Jupiter
|
|
62
62
|
* swap even if Jupiter would be cheaper (market index 2 only)
|
|
63
63
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
64
|
-
* @param {
|
|
64
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse instead of
|
|
65
65
|
* fetching a fresh one
|
|
66
66
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
67
67
|
* method: 'jupiter' | 'marinade'; price?: number }>} The instructions to submit, any address
|
|
@@ -78,7 +78,7 @@ export declare function findBestSuperStakeIxs({ marketIndex, amount, jupiterClie
|
|
|
78
78
|
userAccountPublicKey?: PublicKey;
|
|
79
79
|
forceMarinade?: boolean;
|
|
80
80
|
onlyDirectRoutes?: boolean;
|
|
81
|
-
jupiterQuote?:
|
|
81
|
+
jupiterQuote?: JupiterSwapQuote;
|
|
82
82
|
}): Promise<{
|
|
83
83
|
ixs: TransactionInstruction[];
|
|
84
84
|
lookupTables: AddressLookupTableAccount[];
|
|
@@ -100,7 +100,7 @@ export declare function findBestSuperStakeIxs({ marketIndex, amount, jupiterClie
|
|
|
100
100
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
101
101
|
* @param {boolean} [params.forceMarinade] - Force the Marinade path regardless of Jupiter pricing
|
|
102
102
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
103
|
-
* @param {
|
|
103
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
104
104
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
105
105
|
* method: 'jupiter' | 'marinade'; price: number }>} The chosen route's instructions, required
|
|
106
106
|
* lookup tables, the method used, and the mSOL/SOL price used for the decision
|
|
@@ -113,7 +113,7 @@ export declare function findBestMSolSuperStakeIxs({ amount, jupiterClient, veloc
|
|
|
113
113
|
userAccountPublicKey?: PublicKey;
|
|
114
114
|
forceMarinade?: boolean;
|
|
115
115
|
onlyDirectRoutes?: boolean;
|
|
116
|
-
jupiterQuote?:
|
|
116
|
+
jupiterQuote?: JupiterSwapQuote;
|
|
117
117
|
}): Promise<{
|
|
118
118
|
ixs: TransactionInstruction[];
|
|
119
119
|
lookupTables: AddressLookupTableAccount[];
|
|
@@ -130,7 +130,7 @@ export declare function findBestMSolSuperStakeIxs({ amount, jupiterClient, veloc
|
|
|
130
130
|
* @param {VelocityClient} params.velocityClient - Velocity client
|
|
131
131
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
132
132
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
133
|
-
* @param {
|
|
133
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
134
134
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
135
135
|
* method: 'jupiter' | 'marinade'; price?: number }>} Always resolves with `method: 'jupiter'`
|
|
136
136
|
*/
|
|
@@ -140,7 +140,7 @@ export declare function findBestJitoSolSuperStakeIxs({ amount, jupiterClient, ve
|
|
|
140
140
|
velocityClient: VelocityClient;
|
|
141
141
|
userAccountPublicKey?: PublicKey;
|
|
142
142
|
onlyDirectRoutes?: boolean;
|
|
143
|
-
jupiterQuote?:
|
|
143
|
+
jupiterQuote?: JupiterSwapQuote;
|
|
144
144
|
}): Promise<{
|
|
145
145
|
ixs: TransactionInstruction[];
|
|
146
146
|
lookupTables: AddressLookupTableAccount[];
|
|
@@ -161,7 +161,7 @@ export declare function findBestJitoSolSuperStakeIxs({ amount, jupiterClient, ve
|
|
|
161
161
|
* @param {VelocityClient} params.velocityClient - Velocity client
|
|
162
162
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
163
163
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
164
|
-
* @param {
|
|
164
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
165
165
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
166
166
|
* method: 'jupiter' | 'marinade' }>} Always resolves with `method: 'jupiter'`
|
|
167
167
|
*/
|
|
@@ -173,7 +173,7 @@ export declare function findBestLstSuperStakeIxs({ amount, jupiterClient, veloci
|
|
|
173
173
|
velocityClient: VelocityClient;
|
|
174
174
|
userAccountPublicKey?: PublicKey;
|
|
175
175
|
onlyDirectRoutes?: boolean;
|
|
176
|
-
jupiterQuote?:
|
|
176
|
+
jupiterQuote?: JupiterSwapQuote;
|
|
177
177
|
}): Promise<{
|
|
178
178
|
ixs: TransactionInstruction[];
|
|
179
179
|
lookupTables: AddressLookupTableAccount[];
|
|
@@ -49,7 +49,7 @@ exports.fetchBSolVelocityEmissions = fetchBSolVelocityEmissions;
|
|
|
49
49
|
* @param {boolean} [params.forceMarinade] - Force the direct Marinade stake path over a Jupiter
|
|
50
50
|
* swap even if Jupiter would be cheaper (market index 2 only)
|
|
51
51
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse instead of
|
|
53
53
|
* fetching a fresh one
|
|
54
54
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
55
55
|
* method: 'jupiter' | 'marinade'; price?: number }>} The instructions to submit, any address
|
|
@@ -112,7 +112,7 @@ exports.findBestSuperStakeIxs = findBestSuperStakeIxs;
|
|
|
112
112
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
113
113
|
* @param {boolean} [params.forceMarinade] - Force the Marinade path regardless of Jupiter pricing
|
|
114
114
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
115
|
-
* @param {
|
|
115
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
116
116
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
117
117
|
* method: 'jupiter' | 'marinade'; price: number }>} The chosen route's instructions, required
|
|
118
118
|
* lookup tables, the method used, and the mSOL/SOL price used for the decision
|
|
@@ -155,10 +155,10 @@ async function findBestMSolSuperStakeIxs({ amount, jupiterClient, velocityClient
|
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
158
|
-
const { ixs, lookupTables } = await velocityClient.
|
|
158
|
+
const { ixs, lookupTables } = await velocityClient.getProviderSwapIx({
|
|
159
159
|
inMarketIndex: 1,
|
|
160
160
|
outMarketIndex: 2,
|
|
161
|
-
jupiterClient,
|
|
161
|
+
swapProvider: jupiterClient,
|
|
162
162
|
amount,
|
|
163
163
|
userAccountPublicKey,
|
|
164
164
|
onlyDirectRoutes,
|
|
@@ -183,7 +183,7 @@ exports.findBestMSolSuperStakeIxs = findBestMSolSuperStakeIxs;
|
|
|
183
183
|
* @param {VelocityClient} params.velocityClient - Velocity client
|
|
184
184
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
185
185
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
186
|
-
* @param {
|
|
186
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
187
187
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
188
188
|
* method: 'jupiter' | 'marinade'; price?: number }>} Always resolves with `method: 'jupiter'`
|
|
189
189
|
*/
|
|
@@ -214,15 +214,15 @@ exports.findBestJitoSolSuperStakeIxs = findBestJitoSolSuperStakeIxs;
|
|
|
214
214
|
* @param {VelocityClient} params.velocityClient - Velocity client
|
|
215
215
|
* @param {PublicKey} [params.userAccountPublicKey] - The target sub-account
|
|
216
216
|
* @param {boolean} [params.onlyDirectRoutes] - Restrict Jupiter routing to direct swaps only
|
|
217
|
-
* @param {
|
|
217
|
+
* @param {JupiterSwapQuote} [params.jupiterQuote] - A pre-fetched Jupiter quote to reuse
|
|
218
218
|
* @return {Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[];
|
|
219
219
|
* method: 'jupiter' | 'marinade' }>} Always resolves with `method: 'jupiter'`
|
|
220
220
|
*/
|
|
221
221
|
async function findBestLstSuperStakeIxs({ amount, jupiterClient, velocityClient, userAccountPublicKey, onlyDirectRoutes, lstMarketIndex, jupiterQuote, }) {
|
|
222
|
-
const { ixs, lookupTables } = await velocityClient.
|
|
222
|
+
const { ixs, lookupTables } = await velocityClient.getProviderSwapIx({
|
|
223
223
|
inMarketIndex: 1,
|
|
224
224
|
outMarketIndex: lstMarketIndex,
|
|
225
|
-
jupiterClient,
|
|
225
|
+
swapProvider: jupiterClient,
|
|
226
226
|
amount,
|
|
227
227
|
userAccountPublicKey,
|
|
228
228
|
onlyDirectRoutes,
|
|
@@ -1,66 +1,23 @@
|
|
|
1
|
-
import { Connection,
|
|
2
|
-
import { BN } from '../isomorphic/anchor';
|
|
1
|
+
import { Connection, VersionedTransaction } from '@solana/web3.js';
|
|
3
2
|
import { JupiterClient } from '../jupiter/jupiterClient';
|
|
4
3
|
import { TitanClient } from '../titan/titanClient';
|
|
5
|
-
|
|
6
|
-
export type SwapClientType
|
|
4
|
+
import { GetRouteInstructionsParams, SwapClientType, SwapProvider, SwapQuote, SwapQuoteParams, SwapRouteInstructions } from './types';
|
|
5
|
+
export type { GetRouteInstructionsParams, SwapProviderRoute, SwapClientType, SwapMode, SwapProvider, SwapQuote, SwapQuoteParams, SwapRouteInstructions, UnifiedQuoteResponse, } from './types';
|
|
6
|
+
/** Byte budget handed to a swap provider for the route portion of the tx. */
|
|
7
|
+
export declare const DEFAULT_ROUTE_SIZE_CONSTRAINT: number;
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
9
|
+
* Routes swap calls to the configured provider.
|
|
10
|
+
*
|
|
11
|
+
* Intentionally thin: it picks a provider and forwards. Anything that varies
|
|
12
|
+
* between Jupiter and Titan belongs in the provider, behind
|
|
13
|
+
* {@link SwapProvider} — branching on the provider here is how the two paths
|
|
14
|
+
* drifted apart previously, since nothing forced them to keep the same
|
|
15
|
+
* semantics.
|
|
10
16
|
*/
|
|
11
|
-
export
|
|
12
|
-
inputMint: string;
|
|
13
|
-
inAmount: string;
|
|
14
|
-
outputMint: string;
|
|
15
|
-
outAmount: string;
|
|
16
|
-
swapMode: SwapMode;
|
|
17
|
-
slippageBps: number;
|
|
18
|
-
routePlan: Array<{
|
|
19
|
-
swapInfo: any;
|
|
20
|
-
percent: number;
|
|
21
|
-
}>;
|
|
22
|
-
otherAmountThreshold?: string;
|
|
23
|
-
priceImpactPct?: string;
|
|
24
|
-
platformFee?: {
|
|
25
|
-
amount?: string;
|
|
26
|
-
feeBps?: number;
|
|
27
|
-
};
|
|
28
|
-
contextSlot?: number;
|
|
29
|
-
timeTaken?: number;
|
|
30
|
-
error?: string;
|
|
31
|
-
errorCode?: string;
|
|
32
|
-
}
|
|
33
|
-
export interface SwapQuoteParams {
|
|
34
|
-
inputMint: PublicKey;
|
|
35
|
-
outputMint: PublicKey;
|
|
36
|
-
amount: BN;
|
|
37
|
-
userPublicKey?: PublicKey;
|
|
38
|
-
maxAccounts?: number;
|
|
39
|
-
slippageBps?: number;
|
|
40
|
-
swapMode?: SwapMode;
|
|
41
|
-
onlyDirectRoutes?: boolean;
|
|
42
|
-
excludeDexes?: string[];
|
|
43
|
-
sizeConstraint?: number;
|
|
44
|
-
accountsLimitWritable?: number;
|
|
45
|
-
autoSlippage?: boolean;
|
|
46
|
-
maxAutoSlippageBps?: number;
|
|
47
|
-
usdEstimate?: number;
|
|
48
|
-
}
|
|
49
|
-
export interface SwapTransactionParams {
|
|
50
|
-
quote: UnifiedQuoteResponse;
|
|
51
|
-
userPublicKey: PublicKey;
|
|
52
|
-
slippageBps?: number;
|
|
53
|
-
}
|
|
54
|
-
export interface SwapTransactionResult {
|
|
55
|
-
transaction?: VersionedTransaction;
|
|
56
|
-
transactionMessage?: TransactionMessage;
|
|
57
|
-
lookupTables?: AddressLookupTableAccount[];
|
|
58
|
-
}
|
|
59
|
-
export declare class UnifiedSwapClient {
|
|
17
|
+
export declare class UnifiedSwapClient implements SwapProvider {
|
|
60
18
|
private client;
|
|
61
19
|
private clientType;
|
|
62
20
|
/**
|
|
63
|
-
* Create a unified swap client
|
|
64
21
|
* @param clientType - 'jupiter' or 'titan'
|
|
65
22
|
* @param connection - Solana connection
|
|
66
23
|
* @param authToken - For Titan: auth token (required when not using proxy). For Jupiter: API key (required for api.jup.ag, get free key at https://portal.jup.ag)
|
|
@@ -74,32 +31,37 @@ export declare class UnifiedSwapClient {
|
|
|
74
31
|
url?: string;
|
|
75
32
|
proxyUrl?: string;
|
|
76
33
|
});
|
|
34
|
+
get providerName(): SwapClientType;
|
|
35
|
+
/**
|
|
36
|
+
* The configured client, seen only as {@link SwapProvider}.
|
|
37
|
+
*
|
|
38
|
+
* Forwarding through the interface rather than the concrete union is what
|
|
39
|
+
* makes goal of this class enforceable: a provider added to the union that
|
|
40
|
+
* implements only part of the contract fails to compile here, instead of
|
|
41
|
+
* resolving against whichever call signatures the union happens to share.
|
|
42
|
+
*/
|
|
43
|
+
private get provider();
|
|
77
44
|
/**
|
|
78
|
-
* Get a swap quote from the
|
|
45
|
+
* Get a swap quote from the configured provider.
|
|
46
|
+
*
|
|
47
|
+
* Provider-specific fields on {@link SwapQuoteParams} are mapped by the
|
|
48
|
+
* provider, so the ones it doesn't recognise are simply ignored.
|
|
79
49
|
*/
|
|
80
|
-
getQuote(params: SwapQuoteParams): Promise<
|
|
50
|
+
getQuote(params: SwapQuoteParams): Promise<SwapQuote>;
|
|
81
51
|
/**
|
|
82
|
-
*
|
|
52
|
+
* Builds the route instructions for a quote from {@link getQuote}, at the
|
|
53
|
+
* slippage that quote was priced at.
|
|
54
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
83
55
|
*/
|
|
84
|
-
|
|
56
|
+
getRouteInstructions(params: GetRouteInstructionsParams): Promise<SwapRouteInstructions>;
|
|
85
57
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
58
|
+
* Builds a standalone swap transaction for a quote from {@link getQuote},
|
|
59
|
+
* keeping the provider's own compute budget, token account creation and SOL
|
|
60
|
+
* wrapping. Use {@link getRouteInstructions} for a swap that runs inside
|
|
61
|
+
* velocity's `beginSwap`/`endSwap` bracket.
|
|
62
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
88
63
|
*/
|
|
89
|
-
|
|
90
|
-
inputMint: PublicKey;
|
|
91
|
-
outputMint: PublicKey;
|
|
92
|
-
amount: BN;
|
|
93
|
-
userPublicKey: PublicKey;
|
|
94
|
-
slippageBps?: number;
|
|
95
|
-
swapMode?: SwapMode;
|
|
96
|
-
onlyDirectRoutes?: boolean;
|
|
97
|
-
quote?: UnifiedQuoteResponse;
|
|
98
|
-
sizeConstraint?: number;
|
|
99
|
-
}): Promise<{
|
|
100
|
-
instructions: TransactionInstruction[];
|
|
101
|
-
lookupTables: AddressLookupTableAccount[];
|
|
102
|
-
}>;
|
|
64
|
+
getSwapTransaction(params: GetRouteInstructionsParams): Promise<VersionedTransaction>;
|
|
103
65
|
/**
|
|
104
66
|
* Get the underlying client instance
|
|
105
67
|
*/
|
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnifiedSwapClient = void 0;
|
|
3
|
+
exports.UnifiedSwapClient = exports.DEFAULT_ROUTE_SIZE_CONSTRAINT = void 0;
|
|
4
4
|
const jupiterClient_1 = require("../jupiter/jupiterClient");
|
|
5
5
|
const titanClient_1 = require("../titan/titanClient");
|
|
6
|
+
const utils_1 = require("../tx/utils");
|
|
7
|
+
/**
|
|
8
|
+
* Bytes reserved for the velocity begin/end swap instructions that wrap the
|
|
9
|
+
* route, so the provider only gets the budget actually left for the route.
|
|
10
|
+
*/
|
|
11
|
+
const VELOCITY_SWAP_IX_SIZE_BUFFER = 375;
|
|
12
|
+
/** Byte budget handed to a swap provider for the route portion of the tx. */
|
|
13
|
+
exports.DEFAULT_ROUTE_SIZE_CONSTRAINT = utils_1.MAX_TX_BYTE_SIZE - VELOCITY_SWAP_IX_SIZE_BUFFER;
|
|
14
|
+
/**
|
|
15
|
+
* Routes swap calls to the configured provider.
|
|
16
|
+
*
|
|
17
|
+
* Intentionally thin: it picks a provider and forwards. Anything that varies
|
|
18
|
+
* between Jupiter and Titan belongs in the provider, behind
|
|
19
|
+
* {@link SwapProvider} — branching on the provider here is how the two paths
|
|
20
|
+
* drifted apart previously, since nothing forced them to keep the same
|
|
21
|
+
* semantics.
|
|
22
|
+
*/
|
|
6
23
|
class UnifiedSwapClient {
|
|
7
24
|
/**
|
|
8
|
-
* Create a unified swap client
|
|
9
25
|
* @param clientType - 'jupiter' or 'titan'
|
|
10
26
|
* @param connection - Solana connection
|
|
11
27
|
* @param authToken - For Titan: auth token (required when not using proxy). For Jupiter: API key (required for api.jup.ag, get free key at https://portal.jup.ag)
|
|
@@ -33,123 +49,50 @@ class UnifiedSwapClient {
|
|
|
33
49
|
throw new Error(`Unsupported client type: ${clientType}`);
|
|
34
50
|
}
|
|
35
51
|
}
|
|
52
|
+
get providerName() {
|
|
53
|
+
return this.clientType;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The configured client, seen only as {@link SwapProvider}.
|
|
57
|
+
*
|
|
58
|
+
* Forwarding through the interface rather than the concrete union is what
|
|
59
|
+
* makes goal of this class enforceable: a provider added to the union that
|
|
60
|
+
* implements only part of the contract fails to compile here, instead of
|
|
61
|
+
* resolving against whichever call signatures the union happens to share.
|
|
62
|
+
*/
|
|
63
|
+
get provider() {
|
|
64
|
+
return this.client;
|
|
65
|
+
}
|
|
36
66
|
/**
|
|
37
|
-
* Get a swap quote from the
|
|
67
|
+
* Get a swap quote from the configured provider.
|
|
68
|
+
*
|
|
69
|
+
* Provider-specific fields on {@link SwapQuoteParams} are mapped by the
|
|
70
|
+
* provider, so the ones it doesn't recognise are simply ignored.
|
|
38
71
|
*/
|
|
39
72
|
async getQuote(params) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
sizeConstraint:
|
|
44
|
-
|
|
45
|
-
return await jupiterClient.getQuote(jupiterParams);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
const titanClient = this.client;
|
|
49
|
-
const { autoSlippage: _autoSlippage, // Titan-specific params to exclude
|
|
50
|
-
maxAutoSlippageBps: _maxAutoSlippageBps, usdEstimate: _usdEstimate, ...titanParams } = params;
|
|
51
|
-
if (!titanParams.userPublicKey) {
|
|
52
|
-
throw new Error('userPublicKey is required for Titan quotes');
|
|
53
|
-
}
|
|
54
|
-
// Cast to ensure TypeScript knows userPublicKey is defined
|
|
55
|
-
const titanParamsWithUser = {
|
|
56
|
-
...titanParams,
|
|
57
|
-
userPublicKey: titanParams.userPublicKey,
|
|
58
|
-
swapMode: titanParams.swapMode, // Titan expects string
|
|
59
|
-
sizeConstraint: titanParams.sizeConstraint || 1280 - 375, // Use same default as getSwapInstructions
|
|
60
|
-
};
|
|
61
|
-
return await titanClient.getQuote(titanParamsWithUser);
|
|
62
|
-
}
|
|
73
|
+
var _a;
|
|
74
|
+
return this.provider.getQuote({
|
|
75
|
+
...params,
|
|
76
|
+
sizeConstraint: (_a = params.sizeConstraint) !== null && _a !== void 0 ? _a : exports.DEFAULT_ROUTE_SIZE_CONSTRAINT,
|
|
77
|
+
});
|
|
63
78
|
}
|
|
64
79
|
/**
|
|
65
|
-
*
|
|
80
|
+
* Builds the route instructions for a quote from {@link getQuote}, at the
|
|
81
|
+
* slippage that quote was priced at.
|
|
82
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
66
83
|
*/
|
|
67
|
-
async
|
|
68
|
-
|
|
69
|
-
const jupiterClient = this.client;
|
|
70
|
-
// Cast the quote to Jupiter's specific QuoteResponse type
|
|
71
|
-
const jupiterParams = {
|
|
72
|
-
...params,
|
|
73
|
-
quote: params.quote,
|
|
74
|
-
};
|
|
75
|
-
const transaction = await jupiterClient.getSwap(jupiterParams);
|
|
76
|
-
return { transaction };
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
const titanClient = this.client;
|
|
80
|
-
const { userPublicKey } = params;
|
|
81
|
-
// For Titan, we need to reconstruct the parameters from the quote
|
|
82
|
-
const result = await titanClient.getSwap({
|
|
83
|
-
userPublicKey,
|
|
84
|
-
});
|
|
85
|
-
return {
|
|
86
|
-
transactionMessage: result.transactionMessage,
|
|
87
|
-
lookupTables: result.lookupTables,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
84
|
+
async getRouteInstructions(params) {
|
|
85
|
+
return this.provider.getRouteInstructions(params);
|
|
90
86
|
}
|
|
91
87
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
88
|
+
* Builds a standalone swap transaction for a quote from {@link getQuote},
|
|
89
|
+
* keeping the provider's own compute budget, token account creation and SOL
|
|
90
|
+
* wrapping. Use {@link getRouteInstructions} for a swap that runs inside
|
|
91
|
+
* velocity's `beginSwap`/`endSwap` bracket.
|
|
92
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
94
93
|
*/
|
|
95
|
-
async
|
|
96
|
-
|
|
97
|
-
let swapInstructions;
|
|
98
|
-
let lookupTables;
|
|
99
|
-
if (this.clientType === 'jupiter') {
|
|
100
|
-
const jupiterClient = this.client;
|
|
101
|
-
// Get quote if not provided
|
|
102
|
-
let finalQuote = quote;
|
|
103
|
-
if (!finalQuote) {
|
|
104
|
-
finalQuote = await jupiterClient.getQuote({
|
|
105
|
-
inputMint,
|
|
106
|
-
outputMint,
|
|
107
|
-
amount,
|
|
108
|
-
slippageBps,
|
|
109
|
-
swapMode,
|
|
110
|
-
onlyDirectRoutes,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
if (!finalQuote) {
|
|
114
|
-
throw new Error('Could not fetch swap quote. Please try again.');
|
|
115
|
-
}
|
|
116
|
-
// Get swap transaction and extract instructions
|
|
117
|
-
const transaction = await jupiterClient.getSwap({
|
|
118
|
-
quote: finalQuote,
|
|
119
|
-
userPublicKey,
|
|
120
|
-
slippageBps,
|
|
121
|
-
});
|
|
122
|
-
const { transactionMessage, lookupTables: jupiterLookupTables } = await jupiterClient.getTransactionMessageAndLookupTables({
|
|
123
|
-
transaction,
|
|
124
|
-
});
|
|
125
|
-
swapInstructions = jupiterClient.getJupiterInstructions({
|
|
126
|
-
transactionMessage,
|
|
127
|
-
inputMint,
|
|
128
|
-
outputMint,
|
|
129
|
-
});
|
|
130
|
-
lookupTables = jupiterLookupTables;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
const titanClient = this.client;
|
|
134
|
-
// For Titan, get swap directly (it handles quote internally)
|
|
135
|
-
const { transactionMessage, lookupTables: titanLookupTables } = await titanClient.getSwap({
|
|
136
|
-
inputMint,
|
|
137
|
-
outputMint,
|
|
138
|
-
amount,
|
|
139
|
-
userPublicKey,
|
|
140
|
-
slippageBps,
|
|
141
|
-
swapMode: isExactOut ? titanClient_1.SwapMode.ExactOut : titanClient_1.SwapMode.ExactIn,
|
|
142
|
-
onlyDirectRoutes,
|
|
143
|
-
sizeConstraint: sizeConstraint || 1280 - 375, // MAX_TX_BYTE_SIZE - buffer for velocity instructions
|
|
144
|
-
});
|
|
145
|
-
swapInstructions = titanClient.getTitanInstructions({
|
|
146
|
-
transactionMessage,
|
|
147
|
-
inputMint,
|
|
148
|
-
outputMint,
|
|
149
|
-
});
|
|
150
|
-
lookupTables = titanLookupTables;
|
|
151
|
-
}
|
|
152
|
-
return { instructions: swapInstructions, lookupTables };
|
|
94
|
+
async getSwapTransaction(params) {
|
|
95
|
+
return this.provider.getSwapTransaction(params);
|
|
153
96
|
}
|
|
154
97
|
/**
|
|
155
98
|
* Get the underlying client instance
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
/**
|
|
3
|
+
* Strips the setup and teardown a provider wraps around its route, leaving the
|
|
4
|
+
* instructions that go between `beginSwap` and `endSwap`.
|
|
5
|
+
*
|
|
6
|
+
* Velocity supplies its own compute budget, funds the input token account from
|
|
7
|
+
* the spot market vault, and sweeps the output back, so the provider's versions
|
|
8
|
+
* of all three are redundant. Its ATA creation for the input or output mint is
|
|
9
|
+
* dropped too, since velocity creates those itself; ATAs for any other mint
|
|
10
|
+
* (intermediate hops, fee accounts) are kept because nothing else creates them.
|
|
11
|
+
*
|
|
12
|
+
* Shared by both providers on purpose — when this lived in each client the two
|
|
13
|
+
* copies drifted, and the difference only showed up as a malformed transaction.
|
|
14
|
+
*/
|
|
15
|
+
export declare function filterRouteInstructions({ instructions, inputMint, outputMint, }: {
|
|
16
|
+
instructions: TransactionInstruction[];
|
|
17
|
+
inputMint: PublicKey;
|
|
18
|
+
outputMint: PublicKey;
|
|
19
|
+
}): TransactionInstruction[];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.filterRouteInstructions = void 0;
|
|
4
|
+
const COMPUTE_BUDGET_PROGRAM_ID = 'ComputeBudget111111111111111111111111111111';
|
|
5
|
+
const TOKEN_PROGRAM_ID = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
|
|
6
|
+
const SYSTEM_PROGRAM_ID = '11111111111111111111111111111111';
|
|
7
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
|
|
8
|
+
/** Index of the mint in an associated-token-account instruction's key list. */
|
|
9
|
+
const ATA_IX_MINT_KEY_INDEX = 3;
|
|
10
|
+
/**
|
|
11
|
+
* Strips the setup and teardown a provider wraps around its route, leaving the
|
|
12
|
+
* instructions that go between `beginSwap` and `endSwap`.
|
|
13
|
+
*
|
|
14
|
+
* Velocity supplies its own compute budget, funds the input token account from
|
|
15
|
+
* the spot market vault, and sweeps the output back, so the provider's versions
|
|
16
|
+
* of all three are redundant. Its ATA creation for the input or output mint is
|
|
17
|
+
* dropped too, since velocity creates those itself; ATAs for any other mint
|
|
18
|
+
* (intermediate hops, fee accounts) are kept because nothing else creates them.
|
|
19
|
+
*
|
|
20
|
+
* Shared by both providers on purpose — when this lived in each client the two
|
|
21
|
+
* copies drifted, and the difference only showed up as a malformed transaction.
|
|
22
|
+
*/
|
|
23
|
+
function filterRouteInstructions({ instructions, inputMint, outputMint, }) {
|
|
24
|
+
return instructions.filter((instruction) => {
|
|
25
|
+
var _a;
|
|
26
|
+
const programId = instruction.programId.toString();
|
|
27
|
+
if (programId === COMPUTE_BUDGET_PROGRAM_ID ||
|
|
28
|
+
programId === TOKEN_PROGRAM_ID ||
|
|
29
|
+
programId === SYSTEM_PROGRAM_ID) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (programId === ASSOCIATED_TOKEN_PROGRAM_ID) {
|
|
33
|
+
// Guarded: a malformed ATA instruction with a short key list would
|
|
34
|
+
// otherwise throw here rather than simply being kept.
|
|
35
|
+
const mint = (_a = instruction.keys[ATA_IX_MINT_KEY_INDEX]) === null || _a === void 0 ? void 0 : _a.pubkey;
|
|
36
|
+
if (mint && (mint.equals(inputMint) || mint.equals(outputMint))) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
exports.filterRouteInstructions = filterRouteInstructions;
|