@velocity-exchange/sdk 0.9.0 → 0.11.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 +4 -3
- package/lib/browser/adminClient.js +5 -13
- package/lib/browser/equityFloorManager.d.ts +9 -5
- package/lib/browser/equityFloorManager.js +9 -5
- package/lib/browser/idl/velocity.d.ts +119 -3
- package/lib/browser/idl/velocity.json +119 -3
- package/lib/browser/index.d.ts +2 -0
- package/lib/browser/index.js +3 -0
- package/lib/browser/jupiter/jupiterClient.d.ts +31 -27
- package/lib/browser/jupiter/jupiterClient.js +61 -35
- package/lib/browser/math/amm.d.ts +6 -5
- package/lib/browser/math/amm.js +39 -19
- package/lib/browser/math/margin.d.ts +12 -10
- package/lib/browser/math/margin.js +16 -14
- 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 +46 -117
- 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 +32 -68
- package/lib/browser/titan/titanClient.js +128 -102
- package/lib/browser/types.d.ts +2 -0
- package/lib/browser/user.d.ts +16 -18
- package/lib/browser/user.js +24 -26
- package/lib/browser/velocityClient.d.ts +122 -101
- package/lib/browser/velocityClient.js +221 -289
- package/lib/node/adminClient.d.ts +4 -3
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +5 -13
- package/lib/node/equityFloorManager.d.ts +9 -5
- package/lib/node/equityFloorManager.d.ts.map +1 -1
- package/lib/node/equityFloorManager.js +9 -5
- package/lib/node/idl/velocity.d.ts +119 -3
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +119 -3
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +3 -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 +61 -35
- package/lib/node/math/amm.d.ts +6 -5
- package/lib/node/math/amm.d.ts.map +1 -1
- package/lib/node/math/amm.js +39 -19
- package/lib/node/math/margin.d.ts +12 -10
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +16 -14
- 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 +46 -117
- 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 +32 -68
- package/lib/node/titan/titanClient.d.ts.map +1 -1
- package/lib/node/titan/titanClient.js +128 -102
- package/lib/node/types.d.ts +2 -0
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/user.d.ts +16 -18
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +24 -26
- package/lib/node/velocityClient.d.ts +122 -101
- package/lib/node/velocityClient.d.ts.map +1 -1
- package/lib/node/velocityClient.js +221 -289
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
const utils_1 = require("../tx/utils");
|
|
@@ -10,10 +10,18 @@ const utils_1 = require("../tx/utils");
|
|
|
10
10
|
*/
|
|
11
11
|
const VELOCITY_SWAP_IX_SIZE_BUFFER = 375;
|
|
12
12
|
/** Byte budget handed to a swap provider for the route portion of the tx. */
|
|
13
|
-
|
|
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
|
+
*/
|
|
14
23
|
class UnifiedSwapClient {
|
|
15
24
|
/**
|
|
16
|
-
* Create a unified swap client
|
|
17
25
|
* @param clientType - 'jupiter' or 'titan'
|
|
18
26
|
* @param connection - Solana connection
|
|
19
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)
|
|
@@ -41,129 +49,50 @@ class UnifiedSwapClient {
|
|
|
41
49
|
throw new Error(`Unsupported client type: ${clientType}`);
|
|
42
50
|
}
|
|
43
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
|
+
}
|
|
44
66
|
/**
|
|
45
|
-
* 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.
|
|
46
71
|
*/
|
|
47
72
|
async getQuote(params) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
sizeConstraint:
|
|
52
|
-
|
|
53
|
-
return await jupiterClient.getQuote(jupiterParams);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
const titanClient = this.client;
|
|
57
|
-
const { autoSlippage: _autoSlippage, // Titan-specific params to exclude
|
|
58
|
-
maxAutoSlippageBps: _maxAutoSlippageBps, usdEstimate: _usdEstimate, ...titanParams } = params;
|
|
59
|
-
if (!titanParams.userPublicKey) {
|
|
60
|
-
throw new Error('userPublicKey is required for Titan quotes');
|
|
61
|
-
}
|
|
62
|
-
// Cast to ensure TypeScript knows userPublicKey is defined
|
|
63
|
-
const titanParamsWithUser = {
|
|
64
|
-
...titanParams,
|
|
65
|
-
userPublicKey: titanParams.userPublicKey,
|
|
66
|
-
swapMode: titanParams.swapMode, // Titan expects string
|
|
67
|
-
sizeConstraint: titanParams.sizeConstraint || DEFAULT_ROUTE_SIZE_CONSTRAINT,
|
|
68
|
-
};
|
|
69
|
-
return await titanClient.getQuote(titanParamsWithUser);
|
|
70
|
-
}
|
|
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
|
+
});
|
|
71
78
|
}
|
|
72
79
|
/**
|
|
73
|
-
*
|
|
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.
|
|
74
83
|
*/
|
|
75
|
-
async
|
|
76
|
-
|
|
77
|
-
const jupiterClient = this.client;
|
|
78
|
-
// Cast the quote to Jupiter's specific QuoteResponse type
|
|
79
|
-
const jupiterParams = {
|
|
80
|
-
...params,
|
|
81
|
-
quote: params.quote,
|
|
82
|
-
};
|
|
83
|
-
const transaction = await jupiterClient.getSwap(jupiterParams);
|
|
84
|
-
return { transaction };
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
const titanClient = this.client;
|
|
88
|
-
const { userPublicKey } = params;
|
|
89
|
-
// For Titan, we need to reconstruct the parameters from the quote
|
|
90
|
-
const result = await titanClient.getSwap({
|
|
91
|
-
userPublicKey,
|
|
92
|
-
});
|
|
93
|
-
return {
|
|
94
|
-
transactionMessage: result.transactionMessage,
|
|
95
|
-
lookupTables: result.lookupTables,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
84
|
+
async getRouteInstructions(params) {
|
|
85
|
+
return this.provider.getRouteInstructions(params);
|
|
98
86
|
}
|
|
99
87
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
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.
|
|
102
93
|
*/
|
|
103
|
-
async
|
|
104
|
-
|
|
105
|
-
let swapInstructions;
|
|
106
|
-
let lookupTables;
|
|
107
|
-
if (this.clientType === 'jupiter') {
|
|
108
|
-
const jupiterClient = this.client;
|
|
109
|
-
// Get quote if not provided
|
|
110
|
-
let finalQuote = quote;
|
|
111
|
-
if (!finalQuote) {
|
|
112
|
-
finalQuote = await jupiterClient.getQuote({
|
|
113
|
-
inputMint,
|
|
114
|
-
outputMint,
|
|
115
|
-
amount,
|
|
116
|
-
slippageBps,
|
|
117
|
-
swapMode,
|
|
118
|
-
onlyDirectRoutes,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
if (!finalQuote) {
|
|
122
|
-
throw new Error('Could not fetch swap quote. Please try again.');
|
|
123
|
-
}
|
|
124
|
-
// Get swap transaction and extract instructions
|
|
125
|
-
const transaction = await jupiterClient.getSwap({
|
|
126
|
-
quote: finalQuote,
|
|
127
|
-
userPublicKey,
|
|
128
|
-
slippageBps,
|
|
129
|
-
});
|
|
130
|
-
const { transactionMessage, lookupTables: jupiterLookupTables } = await jupiterClient.getTransactionMessageAndLookupTables({
|
|
131
|
-
transaction,
|
|
132
|
-
});
|
|
133
|
-
swapInstructions = jupiterClient.getJupiterInstructions({
|
|
134
|
-
transactionMessage,
|
|
135
|
-
inputMint,
|
|
136
|
-
outputMint,
|
|
137
|
-
});
|
|
138
|
-
lookupTables = jupiterLookupTables;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
const titanClient = this.client;
|
|
142
|
-
// For Titan, get swap directly (it handles quote internally).
|
|
143
|
-
//
|
|
144
|
-
// NOTE: `getSwap` reads only `userPublicKey` — it replays the route
|
|
145
|
-
// cached by the preceding `getQuote`, so every other argument here is
|
|
146
|
-
// inert. The size constraint is therefore enforced at quote time (see
|
|
147
|
-
// `getQuote` above), which means an explicit `sizeConstraint` passed
|
|
148
|
-
// only to this method does not affect route selection.
|
|
149
|
-
const { transactionMessage, lookupTables: titanLookupTables } = await titanClient.getSwap({
|
|
150
|
-
inputMint,
|
|
151
|
-
outputMint,
|
|
152
|
-
amount,
|
|
153
|
-
userPublicKey,
|
|
154
|
-
slippageBps,
|
|
155
|
-
swapMode: isExactOut ? titanClient_1.SwapMode.ExactOut : titanClient_1.SwapMode.ExactIn,
|
|
156
|
-
onlyDirectRoutes,
|
|
157
|
-
sizeConstraint: sizeConstraint || DEFAULT_ROUTE_SIZE_CONSTRAINT,
|
|
158
|
-
});
|
|
159
|
-
swapInstructions = titanClient.getTitanInstructions({
|
|
160
|
-
transactionMessage,
|
|
161
|
-
inputMint,
|
|
162
|
-
outputMint,
|
|
163
|
-
});
|
|
164
|
-
lookupTables = titanLookupTables;
|
|
165
|
-
}
|
|
166
|
-
return { instructions: swapInstructions, lookupTables };
|
|
94
|
+
async getSwapTransaction(params) {
|
|
95
|
+
return this.provider.getSwapTransaction(params);
|
|
167
96
|
}
|
|
168
97
|
/**
|
|
169
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;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, PublicKey, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import { BN } from '../isomorphic/anchor';
|
|
3
|
+
export type SwapMode = 'ExactIn' | 'ExactOut';
|
|
4
|
+
export type SwapClientType = 'jupiter' | 'titan';
|
|
5
|
+
/** Account budget assumed when a caller doesn't specify one. */
|
|
6
|
+
export declare const DEFAULT_SWAP_MAX_ACCOUNTS = 50;
|
|
7
|
+
/**
|
|
8
|
+
* Quote fields shared by every provider. Provider-specific extras are optional
|
|
9
|
+
* and must never be required to build a swap — see {@link SwapQuote}.
|
|
10
|
+
*/
|
|
11
|
+
export interface UnifiedQuoteResponse {
|
|
12
|
+
inputMint: string;
|
|
13
|
+
inAmount: string;
|
|
14
|
+
outputMint: string;
|
|
15
|
+
outAmount: string;
|
|
16
|
+
swapMode: SwapMode;
|
|
17
|
+
/**
|
|
18
|
+
* The slippage the route was quoted at. Authoritative — this is what the
|
|
19
|
+
* swap executes with, since {@link SwapProvider.getRouteInstructions} takes
|
|
20
|
+
* no slippage override. To swap at different slippage, quote again.
|
|
21
|
+
*/
|
|
22
|
+
slippageBps: number;
|
|
23
|
+
routePlan: Array<{
|
|
24
|
+
swapInfo: any;
|
|
25
|
+
percent: number;
|
|
26
|
+
}>;
|
|
27
|
+
/** Jupiter only. */
|
|
28
|
+
otherAmountThreshold?: string;
|
|
29
|
+
/** Jupiter provides this; Titan doesn't, so callers derive it. */
|
|
30
|
+
priceImpactPct?: string;
|
|
31
|
+
platformFee?: {
|
|
32
|
+
amount?: string;
|
|
33
|
+
feeBps?: number;
|
|
34
|
+
};
|
|
35
|
+
contextSlot?: number;
|
|
36
|
+
timeTaken?: number;
|
|
37
|
+
error?: string;
|
|
38
|
+
errorCode?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What the provider says its route does, recorded when the quote was produced.
|
|
42
|
+
*
|
|
43
|
+
* An implementation detail of the tamper check described on
|
|
44
|
+
* {@link expectProviderRoute} — you never write one of these. {@link
|
|
45
|
+
* buildSwapQuote} records it from the quote a provider is already returning.
|
|
46
|
+
*/
|
|
47
|
+
export interface SwapRouteFields {
|
|
48
|
+
readonly inputMint: string;
|
|
49
|
+
readonly outputMint: string;
|
|
50
|
+
readonly inAmount: string;
|
|
51
|
+
readonly outAmount: string;
|
|
52
|
+
readonly swapMode: SwapMode;
|
|
53
|
+
readonly slippageBps: number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Everything a provider needs to turn its own quote back into instructions.
|
|
57
|
+
*
|
|
58
|
+
* Attached to the quote rather than held on the client so that building a swap
|
|
59
|
+
* is a pure function of the quote you were given. A provider that kept this
|
|
60
|
+
* internally would silently build against whatever route it happened to have
|
|
61
|
+
* cached, which is invisible at the call site and wrong whenever more than one
|
|
62
|
+
* quote is in flight.
|
|
63
|
+
*
|
|
64
|
+
* Opaque — read the normalized fields on {@link SwapQuote} instead.
|
|
65
|
+
*/
|
|
66
|
+
export type SwapProviderRoute = {
|
|
67
|
+
readonly provider: 'jupiter';
|
|
68
|
+
readonly quote: unknown;
|
|
69
|
+
readonly routed: SwapRouteFields;
|
|
70
|
+
readonly quotedFor?: string;
|
|
71
|
+
} | {
|
|
72
|
+
readonly provider: 'titan';
|
|
73
|
+
readonly route: unknown;
|
|
74
|
+
readonly routed: SwapRouteFields;
|
|
75
|
+
readonly quotedFor?: string;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* A {@link SwapProviderRoute} as a provider hands it to {@link buildSwapQuote} —
|
|
79
|
+
* the payload without the `routed` fields, which `buildSwapQuote` records itself.
|
|
80
|
+
*/
|
|
81
|
+
export type SwapProviderRoutePayload = OmitRouted<SwapProviderRoute>;
|
|
82
|
+
/** Distributes over the union, so each member keeps its own payload field. */
|
|
83
|
+
type OmitRouted<R> = R extends unknown ? Omit<R, 'routed'> : never;
|
|
84
|
+
/**
|
|
85
|
+
* A quote plus the provider payload needed to execute it. Always pass the quote
|
|
86
|
+
* you intend to swap on; providers will not fall back to a previous one.
|
|
87
|
+
*
|
|
88
|
+
* Treat it as immutable. The normalized fields are checked against the payload
|
|
89
|
+
* before a swap is built, so an edited copy is rejected rather than silently
|
|
90
|
+
* executed as the swap it was originally quoted for — re-quote instead.
|
|
91
|
+
*/
|
|
92
|
+
export type SwapQuote = UnifiedQuoteResponse & {
|
|
93
|
+
readonly providerRoute: SwapProviderRoute;
|
|
94
|
+
};
|
|
95
|
+
export interface SwapQuoteParams {
|
|
96
|
+
inputMint: PublicKey;
|
|
97
|
+
outputMint: PublicKey;
|
|
98
|
+
amount: BN;
|
|
99
|
+
/** Required by Titan, which bakes the user's token accounts into the route. */
|
|
100
|
+
userPublicKey?: PublicKey;
|
|
101
|
+
maxAccounts?: number;
|
|
102
|
+
slippageBps?: number;
|
|
103
|
+
swapMode?: SwapMode;
|
|
104
|
+
onlyDirectRoutes?: boolean;
|
|
105
|
+
excludeDexes?: string[];
|
|
106
|
+
/** Titan only. */
|
|
107
|
+
sizeConstraint?: number;
|
|
108
|
+
/** Titan only. */
|
|
109
|
+
accountsLimitWritable?: number;
|
|
110
|
+
/** Jupiter only. */
|
|
111
|
+
autoSlippage?: boolean;
|
|
112
|
+
/** Jupiter only. */
|
|
113
|
+
maxAutoSlippageBps?: number;
|
|
114
|
+
/** Jupiter only. */
|
|
115
|
+
usdEstimate?: number;
|
|
116
|
+
}
|
|
117
|
+
export interface SwapRouteInstructions {
|
|
118
|
+
instructions: TransactionInstruction[];
|
|
119
|
+
lookupTables: AddressLookupTableAccount[];
|
|
120
|
+
}
|
|
121
|
+
export interface GetRouteInstructionsParams {
|
|
122
|
+
quote: SwapQuote;
|
|
123
|
+
/**
|
|
124
|
+
* Wallet the swap executes as. Must be the wallet the quote was requested
|
|
125
|
+
* for when the provider binds routes to a wallet — see
|
|
126
|
+
* {@link expectProviderRoute}.
|
|
127
|
+
*
|
|
128
|
+
* Don't be confused by the Velocity user account public key, which is different.
|
|
129
|
+
* This is usually the Velocity authority.
|
|
130
|
+
*/
|
|
131
|
+
userPublicKey: PublicKey;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The contract every swap provider implements.
|
|
135
|
+
*
|
|
136
|
+
* Quote, then build — either into velocity's swap bracket, or into a standalone
|
|
137
|
+
* transaction. Both clients satisfying the same interface is what stops one
|
|
138
|
+
* provider growing behaviour the other doesn't have; callers get identical
|
|
139
|
+
* semantics regardless of which is configured.
|
|
140
|
+
*
|
|
141
|
+
* Provider-specific request fields live on {@link SwapQuoteParams} and are
|
|
142
|
+
* mapped by the provider itself, so adding one never means editing the unified
|
|
143
|
+
* client. Nothing that only one provider can honour belongs on
|
|
144
|
+
* {@link GetRouteInstructionsParams} — a build-time parameter the other silently
|
|
145
|
+
* ignores is indistinguishable from it being applied.
|
|
146
|
+
*/
|
|
147
|
+
export interface SwapProvider {
|
|
148
|
+
readonly providerName: SwapClientType;
|
|
149
|
+
getQuote(params: SwapQuoteParams): Promise<SwapQuote>;
|
|
150
|
+
/**
|
|
151
|
+
* Builds the route instructions for a quote returned by this provider's
|
|
152
|
+
* `getQuote`, at the slippage that quote was priced at.
|
|
153
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
154
|
+
*/
|
|
155
|
+
getRouteInstructions(params: GetRouteInstructionsParams): Promise<SwapRouteInstructions>;
|
|
156
|
+
/**
|
|
157
|
+
* Builds a complete, self-contained swap transaction — the setup and
|
|
158
|
+
* teardown `getRouteInstructions` strips are still attached: compute budget,
|
|
159
|
+
* token account creation, and SOL wrapping.
|
|
160
|
+
*
|
|
161
|
+
* For swaps the caller signs and sends on its own. A swap running inside
|
|
162
|
+
* velocity's `beginSwap`/`endSwap` bracket wants `getRouteInstructions`
|
|
163
|
+
* instead — velocity supplies all three itself, and a second copy wraps SOL
|
|
164
|
+
* that nothing then unwraps.
|
|
165
|
+
*
|
|
166
|
+
* @throws If the quote came from a different provider or a different wallet.
|
|
167
|
+
*/
|
|
168
|
+
getSwapTransaction(params: GetRouteInstructionsParams): Promise<VersionedTransaction>;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Assembles the {@link SwapQuote} a provider's `getQuote` returns, from the
|
|
172
|
+
* normalized quote and the route payload that will execute it.
|
|
173
|
+
*
|
|
174
|
+
* Providers call this rather than building the object themselves. It records
|
|
175
|
+
* what the route was quoted with next to the payload, which is what lets
|
|
176
|
+
* {@link expectProviderRoute} reject a quote edited after it was returned — so
|
|
177
|
+
* that check needs no cooperation from the provider beyond calling this.
|
|
178
|
+
*/
|
|
179
|
+
export declare function buildSwapQuote<Q extends UnifiedQuoteResponse>(quote: Q, providerRoute: SwapProviderRoutePayload): Q & SwapQuote;
|
|
180
|
+
/**
|
|
181
|
+
* Narrows a quote's payload to `provider`, checks it can be executed by
|
|
182
|
+
* `userPublicKey`, and checks it still matches the quote it travels on.
|
|
183
|
+
*
|
|
184
|
+
* A route is wallet-bound when the provider resolves the user's token accounts
|
|
185
|
+
* at quote time (Titan does; Jupiter builds per-wallet at swap time). Those
|
|
186
|
+
* providers record the wallet on `quotedFor`, and executing such a route as
|
|
187
|
+
* anyone else moves funds through accounts the signer doesn't own.
|
|
188
|
+
*
|
|
189
|
+
* @throws If the quote was produced by a different provider, for a wallet other
|
|
190
|
+
* than `userPublicKey`, or for a different pair, size, mode or slippage than the
|
|
191
|
+
* quote now claims.
|
|
192
|
+
*/
|
|
193
|
+
export declare function expectProviderRoute<T extends SwapClientType>(quote: SwapQuote, provider: T, userPublicKey: PublicKey): Extract<SwapProviderRoute, {
|
|
194
|
+
provider: T;
|
|
195
|
+
}>;
|
|
196
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expectProviderRoute = exports.buildSwapQuote = exports.DEFAULT_SWAP_MAX_ACCOUNTS = void 0;
|
|
4
|
+
/** Account budget assumed when a caller doesn't specify one. */
|
|
5
|
+
exports.DEFAULT_SWAP_MAX_ACCOUNTS = 50;
|
|
6
|
+
/**
|
|
7
|
+
* Assembles the {@link SwapQuote} a provider's `getQuote` returns, from the
|
|
8
|
+
* normalized quote and the route payload that will execute it.
|
|
9
|
+
*
|
|
10
|
+
* Providers call this rather than building the object themselves. It records
|
|
11
|
+
* what the route was quoted with next to the payload, which is what lets
|
|
12
|
+
* {@link expectProviderRoute} reject a quote edited after it was returned — so
|
|
13
|
+
* that check needs no cooperation from the provider beyond calling this.
|
|
14
|
+
*/
|
|
15
|
+
function buildSwapQuote(quote, providerRoute) {
|
|
16
|
+
const { inputMint, outputMint, inAmount, outAmount, swapMode, slippageBps } = quote;
|
|
17
|
+
const routed = {
|
|
18
|
+
inputMint,
|
|
19
|
+
outputMint,
|
|
20
|
+
inAmount,
|
|
21
|
+
outAmount,
|
|
22
|
+
swapMode,
|
|
23
|
+
slippageBps,
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
...quote,
|
|
27
|
+
providerRoute: { ...providerRoute, routed },
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.buildSwapQuote = buildSwapQuote;
|
|
31
|
+
/** Fields that must describe the same swap on the quote and on its route. */
|
|
32
|
+
const ROUTED_FIELDS = [
|
|
33
|
+
'inputMint',
|
|
34
|
+
'outputMint',
|
|
35
|
+
'inAmount',
|
|
36
|
+
'outAmount',
|
|
37
|
+
'swapMode',
|
|
38
|
+
'slippageBps',
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* Throws unless the quote's normalized fields still describe the route it
|
|
42
|
+
* carries.
|
|
43
|
+
*
|
|
44
|
+
* Callers and velocity's swap guards read the normalized fields; the provider
|
|
45
|
+
* executes the opaque payload. Nothing but this ties the two together, so a
|
|
46
|
+
* quote that was copied and edited — `{ ...quote, inAmount }`, a mint rewritten
|
|
47
|
+
* in place — would pass every pair, size and slippage check and then execute the
|
|
48
|
+
* route it was originally quoted for.
|
|
49
|
+
*/
|
|
50
|
+
function assertQuoteMatchesRoute(quote, provider, routed) {
|
|
51
|
+
if (!routed) {
|
|
52
|
+
throw new Error(`Quote is missing the route fields recorded at quote time. It must come from ${provider}'s getQuote, which builds it with buildSwapQuote.`);
|
|
53
|
+
}
|
|
54
|
+
for (const field of ROUTED_FIELDS) {
|
|
55
|
+
// Stringified: a provider may normalize a numeric field it decoded.
|
|
56
|
+
if (String(quote[field]) !== String(routed[field])) {
|
|
57
|
+
throw new Error(`Quote reports ${field} ${String(quote[field])} but the route it carries was quoted with ${String(routed[field])}. The quote was modified after it was returned; re-quote instead.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Narrows a quote's payload to `provider`, checks it can be executed by
|
|
63
|
+
* `userPublicKey`, and checks it still matches the quote it travels on.
|
|
64
|
+
*
|
|
65
|
+
* A route is wallet-bound when the provider resolves the user's token accounts
|
|
66
|
+
* at quote time (Titan does; Jupiter builds per-wallet at swap time). Those
|
|
67
|
+
* providers record the wallet on `quotedFor`, and executing such a route as
|
|
68
|
+
* anyone else moves funds through accounts the signer doesn't own.
|
|
69
|
+
*
|
|
70
|
+
* @throws If the quote was produced by a different provider, for a wallet other
|
|
71
|
+
* than `userPublicKey`, or for a different pair, size, mode or slippage than the
|
|
72
|
+
* quote now claims.
|
|
73
|
+
*/
|
|
74
|
+
function expectProviderRoute(quote, provider, userPublicKey) {
|
|
75
|
+
const route = quote === null || quote === void 0 ? void 0 : quote.providerRoute;
|
|
76
|
+
if (!route) {
|
|
77
|
+
throw new Error(`Quote is missing its provider route. It must come from ${provider}'s getQuote.`);
|
|
78
|
+
}
|
|
79
|
+
if (route.provider !== provider) {
|
|
80
|
+
throw new Error(`Quote came from ${route.provider} but is being swapped on ${provider}.`);
|
|
81
|
+
}
|
|
82
|
+
if (route.quotedFor && route.quotedFor !== userPublicKey.toString()) {
|
|
83
|
+
throw new Error(`Quote was requested for ${route.quotedFor} but is being swapped by ${userPublicKey.toString()}.`);
|
|
84
|
+
}
|
|
85
|
+
assertQuoteMatchesRoute(quote, provider, route.routed);
|
|
86
|
+
return route;
|
|
87
|
+
}
|
|
88
|
+
exports.expectProviderRoute = expectProviderRoute;
|
|
@@ -1,36 +1,16 @@
|
|
|
1
|
-
import { Connection,
|
|
2
|
-
import {
|
|
1
|
+
import { Connection, AddressLookupTableAccount, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import { GetRouteInstructionsParams, SwapProvider, SwapQuote, SwapQuoteParams, SwapRouteInstructions } from '../swap/types';
|
|
3
3
|
export declare enum SwapMode {
|
|
4
4
|
ExactIn = "ExactIn",
|
|
5
5
|
ExactOut = "ExactOut"
|
|
6
6
|
}
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
inAmount: string;
|
|
10
|
-
outputMint: string;
|
|
11
|
-
outAmount: string;
|
|
12
|
-
swapMode: SwapMode;
|
|
13
|
-
slippageBps: number;
|
|
14
|
-
platformFee?: {
|
|
15
|
-
amount?: string;
|
|
16
|
-
feeBps?: number;
|
|
17
|
-
};
|
|
18
|
-
routePlan: Array<{
|
|
19
|
-
swapInfo: any;
|
|
20
|
-
percent: number;
|
|
21
|
-
}>;
|
|
22
|
-
contextSlot?: number;
|
|
23
|
-
timeTaken?: number;
|
|
24
|
-
error?: string;
|
|
25
|
-
errorCode?: string;
|
|
26
|
-
}
|
|
27
|
-
export declare class TitanClient {
|
|
7
|
+
export declare class TitanClient implements SwapProvider {
|
|
8
|
+
readonly providerName = "titan";
|
|
28
9
|
authToken: string;
|
|
29
10
|
url: string;
|
|
30
11
|
connection: Connection;
|
|
31
12
|
proxyUrl?: string;
|
|
32
|
-
|
|
33
|
-
private lastQuoteParams?;
|
|
13
|
+
lookupTableCache: Map<string, AddressLookupTableAccount>;
|
|
34
14
|
constructor({ connection, authToken, url, proxyUrl, }: {
|
|
35
15
|
connection: Connection;
|
|
36
16
|
authToken: string;
|
|
@@ -39,57 +19,41 @@ export declare class TitanClient {
|
|
|
39
19
|
});
|
|
40
20
|
private buildParams;
|
|
41
21
|
/**
|
|
42
|
-
* Get
|
|
22
|
+
* Get the best available route for a swap.
|
|
23
|
+
*
|
|
24
|
+
* The route is returned on the quote's `providerRoute`, so
|
|
25
|
+
* {@link getRouteInstructions} builds exactly what was quoted here, at the
|
|
26
|
+
* slippage quoted here.
|
|
27
|
+
* @throws If `userPublicKey` is missing — Titan bakes the user's token
|
|
28
|
+
* accounts into the route, so a route quoted for one wallet cannot be
|
|
29
|
+
* executed by another. The wallet is recorded on the route and enforced
|
|
30
|
+
* when the route is built.
|
|
43
31
|
*/
|
|
44
|
-
getQuote({ inputMint, outputMint, amount, userPublicKey, maxAccounts,
|
|
45
|
-
slippageBps, swapMode, onlyDirectRoutes, excludeDexes, sizeConstraint, accountsLimitWritable, }: {
|
|
46
|
-
inputMint: PublicKey;
|
|
47
|
-
outputMint: PublicKey;
|
|
48
|
-
amount: BN;
|
|
49
|
-
userPublicKey: PublicKey;
|
|
50
|
-
maxAccounts?: number;
|
|
51
|
-
slippageBps?: number;
|
|
52
|
-
swapMode?: string;
|
|
53
|
-
onlyDirectRoutes?: boolean;
|
|
54
|
-
excludeDexes?: string[];
|
|
55
|
-
sizeConstraint?: number;
|
|
56
|
-
accountsLimitWritable?: number;
|
|
57
|
-
}): Promise<QuoteResponse>;
|
|
32
|
+
getQuote({ inputMint, outputMint, amount, userPublicKey, maxAccounts, slippageBps, swapMode, onlyDirectRoutes, excludeDexes, sizeConstraint, accountsLimitWritable, }: SwapQuoteParams): Promise<SwapQuote>;
|
|
58
33
|
/**
|
|
59
|
-
*
|
|
34
|
+
* The route as Titan built it, compiled into a signable transaction. Titan
|
|
35
|
+
* returns instructions rather than a transaction, so unlike Jupiter there is
|
|
36
|
+
* nothing to strip — the route already includes its own setup and teardown.
|
|
37
|
+
* @throws If the quote came from a different provider or a different wallet,
|
|
38
|
+
* or if a lookup table the route depends on can't be loaded.
|
|
60
39
|
*/
|
|
61
|
-
|
|
62
|
-
inputMint?: PublicKey;
|
|
63
|
-
outputMint?: PublicKey;
|
|
64
|
-
amount?: BN;
|
|
65
|
-
userPublicKey: PublicKey;
|
|
66
|
-
maxAccounts?: number;
|
|
67
|
-
slippageBps?: number;
|
|
68
|
-
swapMode?: SwapMode;
|
|
69
|
-
onlyDirectRoutes?: boolean;
|
|
70
|
-
excludeDexes?: string[];
|
|
71
|
-
sizeConstraint?: number;
|
|
72
|
-
accountsLimitWritable?: number;
|
|
73
|
-
}): Promise<{
|
|
74
|
-
transactionMessage: TransactionMessage;
|
|
75
|
-
lookupTables: AddressLookupTableAccount[];
|
|
76
|
-
}>;
|
|
40
|
+
getSwapTransaction({ quote, userPublicKey, }: GetRouteInstructionsParams): Promise<VersionedTransaction>;
|
|
77
41
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
42
|
+
* Builds the route instructions for a quote returned by {@link getQuote}.
|
|
43
|
+
*
|
|
44
|
+
* The route travels on the quote, so this reads no client state and two
|
|
45
|
+
* quotes in flight can never be confused for one another. Slippage was
|
|
46
|
+
* fixed when Titan built the route, so there is nothing to apply here.
|
|
47
|
+
* @throws If the quote came from a different provider or a different wallet,
|
|
48
|
+
* or if a lookup table the route depends on can't be loaded.
|
|
82
49
|
*/
|
|
83
|
-
|
|
84
|
-
transactionMessage: TransactionMessage;
|
|
85
|
-
inputMint: PublicKey;
|
|
86
|
-
outputMint: PublicKey;
|
|
87
|
-
}): TransactionInstruction[];
|
|
50
|
+
getRouteInstructions({ quote, userPublicKey, }: GetRouteInstructionsParams): Promise<SwapRouteInstructions>;
|
|
88
51
|
/**
|
|
89
52
|
* Fetches a lookup table required by a route, retrying transient RPC
|
|
90
|
-
* failures (rate limiting in particular) before giving up.
|
|
53
|
+
* failures (rate limiting in particular) before giving up. Checks the
|
|
54
|
+
* instance cache first and populates it on a fresh fetch.
|
|
91
55
|
* @throws If the table still can't be loaded, or doesn't exist on-chain.
|
|
92
56
|
*/
|
|
93
57
|
private fetchLookupTable;
|
|
94
|
-
private
|
|
58
|
+
private getInstructionsAndLookupTables;
|
|
95
59
|
}
|