@audren-xyz/sdk 1.3.4 → 1.3.6
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/dist/aggregators.d.ts +4 -0
- package/dist/aggregators.js +111 -51
- package/dist/swap.js +2 -0
- package/package.json +1 -1
package/dist/aggregators.d.ts
CHANGED
|
@@ -127,6 +127,10 @@ export interface AudrenSwapQuoteRequest extends SwapAdapterQuoteRequest {
|
|
|
127
127
|
poolId?: number;
|
|
128
128
|
shardId?: number;
|
|
129
129
|
programId?: string;
|
|
130
|
+
/** Optional per-request router allowlist override (checked for prebuilt adapters like OKX/Titan). */
|
|
131
|
+
allowedRouterPrograms?: readonly string[];
|
|
132
|
+
/** Optional per-request opt-out of prebuilt router allowlist checks. */
|
|
133
|
+
allowUnverifiedRouter?: boolean;
|
|
130
134
|
}
|
|
131
135
|
export interface AudrenSwapQuoteResult {
|
|
132
136
|
winner: NormalizedSwapQuote;
|
package/dist/aggregators.js
CHANGED
|
@@ -177,21 +177,35 @@ function isValidRouterInstruction(v) {
|
|
|
177
177
|
typeof a.isSigner === 'boolean' &&
|
|
178
178
|
typeof a.isWritable === 'boolean');
|
|
179
179
|
}
|
|
180
|
+
class PrebuiltRouteBoundsError extends Error {
|
|
181
|
+
constructor(message) {
|
|
182
|
+
super(message);
|
|
183
|
+
this.name = 'PrebuiltRouteBoundsError';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function cancelBodyQuietly(res) {
|
|
187
|
+
try {
|
|
188
|
+
await res.body?.cancel?.();
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// ignore already-locked or closed stream
|
|
192
|
+
}
|
|
193
|
+
}
|
|
180
194
|
function validatePrebuiltEscrowInstructionBounds(ixs, escrowPubkey, providerLabel) {
|
|
181
195
|
const nonWrapper = ixs.filter((ix) => !WRAPPER_PROGRAMS.has(ix.programId));
|
|
182
196
|
if (nonWrapper.length !== 1) {
|
|
183
|
-
throw new
|
|
197
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route must expose exactly 1 non-wrapper swap instruction (got ${nonWrapper.length})`);
|
|
184
198
|
}
|
|
185
199
|
const swapIx = nonWrapper[0];
|
|
186
200
|
if (!swapIx.accounts.some((a) => a.pubkey === escrowPubkey)) {
|
|
187
201
|
throw new Error(`${providerLabel} did not include an escrow-signed swap instruction`);
|
|
188
202
|
}
|
|
189
203
|
if (swapIx.accounts.length > MAX_EXEC_ROUTER_CPI_ACCOUNTS) {
|
|
190
|
-
throw new
|
|
204
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route requires ${swapIx.accounts.length} router accounts (exceeds ExecViaEscrow max ${MAX_EXEC_ROUTER_CPI_ACCOUNTS})`);
|
|
191
205
|
}
|
|
192
206
|
const uniquePubkeys = new Set(swapIx.accounts.map((a) => a.pubkey)).size;
|
|
193
207
|
if (uniquePubkeys > MAX_TX_V1_UNIQUE_ROUTER_ACCOUNTS) {
|
|
194
|
-
throw new
|
|
208
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route requires ${uniquePubkeys} unique router accounts (exceeds Tx v1 safe budget ${MAX_TX_V1_UNIQUE_ROUTER_ACCOUNTS})`);
|
|
195
209
|
}
|
|
196
210
|
return swapIx;
|
|
197
211
|
}
|
|
@@ -357,6 +371,9 @@ export function createJupiterSwapAdapter(config = {}) {
|
|
|
357
371
|
};
|
|
358
372
|
},
|
|
359
373
|
async swapInstructions(req) {
|
|
374
|
+
if (!this.isConfigured()) {
|
|
375
|
+
throw new Error('Jupiter adapter not configured (set apiKey or apiUrl)');
|
|
376
|
+
}
|
|
360
377
|
const body = {
|
|
361
378
|
quoteResponse: req.quoteResponse,
|
|
362
379
|
userPublicKey: req.escrowPubkey,
|
|
@@ -373,8 +390,10 @@ export function createJupiterSwapAdapter(config = {}) {
|
|
|
373
390
|
signal: req.signal,
|
|
374
391
|
timeoutMs: req.timeoutMs,
|
|
375
392
|
});
|
|
376
|
-
if (!res.ok)
|
|
393
|
+
if (!res.ok) {
|
|
394
|
+
await cancelBodyQuietly(res);
|
|
377
395
|
throw new Error(`Jupiter swap-instructions HTTP ${res.status}`);
|
|
396
|
+
}
|
|
378
397
|
const payloadRaw = await res.json();
|
|
379
398
|
if (!isPlainObject(payloadRaw))
|
|
380
399
|
throw new Error('Jupiter returned non-object swap-instructions JSON');
|
|
@@ -429,12 +448,14 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
429
448
|
}
|
|
430
449
|
const base = resolveBase();
|
|
431
450
|
const t0 = Date.now();
|
|
451
|
+
const excluded = req.excludeDexes ?? JUPITER_CPI_EXCLUDED_DEXES;
|
|
432
452
|
const qs = new URLSearchParams({
|
|
433
453
|
inputMint: req.inputMint,
|
|
434
454
|
outputMint: req.outputMint,
|
|
435
455
|
amount: req.amount,
|
|
436
456
|
slippageBps: String(req.slippageBps),
|
|
437
457
|
transactionVersion: 'v1',
|
|
458
|
+
...(excluded.length > 0 ? { excludeDexes: excluded.join(',') } : {}),
|
|
438
459
|
...(req.onlyDirectRoutes ? { onlyDirectRoutes: 'true' } : {}),
|
|
439
460
|
...(req.platformFeeBps && req.platformFeeBps > 0
|
|
440
461
|
? { platformFeeBps: String(req.platformFeeBps) }
|
|
@@ -444,8 +465,10 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
444
465
|
headers: makeHeaders(),
|
|
445
466
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
446
467
|
}, 'DFlow');
|
|
447
|
-
if (!res.ok)
|
|
468
|
+
if (!res.ok) {
|
|
469
|
+
await cancelBodyQuietly(res);
|
|
448
470
|
throw new Error(`DFlow quote HTTP ${res.status}`);
|
|
471
|
+
}
|
|
449
472
|
const rawJson = await res.json();
|
|
450
473
|
if (!isPlainObject(rawJson))
|
|
451
474
|
throw new Error('DFlow returned non-object quote JSON');
|
|
@@ -500,6 +523,9 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
500
523
|
};
|
|
501
524
|
},
|
|
502
525
|
async swapInstructions(req) {
|
|
526
|
+
if (!this.isConfigured()) {
|
|
527
|
+
throw new Error('DFlow adapter not configured (set apiKey or apiUrl)');
|
|
528
|
+
}
|
|
503
529
|
const base = resolveBase();
|
|
504
530
|
const res = await safeAggregatorFetch(fetchImpl, `${base}/swap-instructions`, {
|
|
505
531
|
method: 'POST',
|
|
@@ -513,8 +539,10 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
513
539
|
}),
|
|
514
540
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
515
541
|
}, 'DFlow');
|
|
516
|
-
if (!res.ok)
|
|
542
|
+
if (!res.ok) {
|
|
543
|
+
await cancelBodyQuietly(res);
|
|
517
544
|
throw new Error(`DFlow swap-instructions HTTP ${res.status}`);
|
|
545
|
+
}
|
|
518
546
|
const payloadRaw = await res.json();
|
|
519
547
|
if (!isPlainObject(payloadRaw))
|
|
520
548
|
throw new Error('DFlow returned non-object swap-instructions JSON');
|
|
@@ -588,44 +616,63 @@ export function createOkxSwapAdapter(config = {}) {
|
|
|
588
616
|
}
|
|
589
617
|
const base = resolveBase();
|
|
590
618
|
const t0 = Date.now();
|
|
591
|
-
const
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
619
|
+
const escrowPubkey = req.escrowPubkey;
|
|
620
|
+
const fetchOkxCandidate = async (directRoute) => {
|
|
621
|
+
const qs = new URLSearchParams({
|
|
622
|
+
chainIndex: OKX_CHAIN_INDEX,
|
|
623
|
+
amount: req.amount,
|
|
624
|
+
fromTokenAddress: req.inputMint,
|
|
625
|
+
toTokenAddress: req.outputMint,
|
|
626
|
+
slippagePercent: (req.slippageBps / 100).toString(),
|
|
627
|
+
userWalletAddress: escrowPubkey,
|
|
628
|
+
swapMode: 'exactIn',
|
|
629
|
+
...(directRoute ? { directRoute: 'true' } : {}),
|
|
630
|
+
...(req.platformFeeBps && req.platformFeeBps > 0 && req.feeOwnerWallet
|
|
631
|
+
? {
|
|
632
|
+
feePercent: (req.platformFeeBps / 100).toFixed(4).replace(/\.?0+$/, ''),
|
|
633
|
+
toTokenReferrerWalletAddress: req.feeOwnerWallet,
|
|
634
|
+
}
|
|
635
|
+
: {}),
|
|
636
|
+
});
|
|
637
|
+
const queryString = `?${qs.toString()}`;
|
|
638
|
+
const headers = await signHeaders('GET', OKX_SWAP_IX_PATH, queryString);
|
|
639
|
+
const res = await safeAggregatorFetch(fetchImpl, `${base}${OKX_SWAP_IX_PATH}${queryString}`, {
|
|
640
|
+
headers,
|
|
641
|
+
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
642
|
+
}, 'OKX');
|
|
643
|
+
if (!res.ok) {
|
|
644
|
+
await cancelBodyQuietly(res);
|
|
645
|
+
throw new Error(`OKX swap-instruction HTTP ${res.status}`);
|
|
646
|
+
}
|
|
647
|
+
const bodyRaw = await res.json();
|
|
648
|
+
if (!isPlainObject(bodyRaw))
|
|
649
|
+
throw new Error('OKX returned non-object response JSON');
|
|
650
|
+
const body = bodyRaw;
|
|
651
|
+
const entry = Array.isArray(body.data) ? body.data[0] : body.data;
|
|
652
|
+
const outBi = parsePositiveUintString(entry?.routerResult?.toTokenAmount);
|
|
653
|
+
if (body.code !== '0' || !entry?.routerResult || outBi === null) {
|
|
654
|
+
const safeCode = typeof body.code === 'string' && body.code.trim() ? body.code.trim().slice(0, 16) : 'unknown';
|
|
655
|
+
throw new Error(`OKX API returned no valid route (code ${safeCode})`);
|
|
656
|
+
}
|
|
657
|
+
const prebuiltIxs = Array.isArray(entry.instructionLists)
|
|
658
|
+
? entry.instructionLists.filter(isValidRouterInstruction)
|
|
659
|
+
: [];
|
|
660
|
+
const swapIx = validatePrebuiltEscrowInstructionBounds(prebuiltIxs, escrowPubkey, 'OKX');
|
|
661
|
+
return { entry, rr: entry.routerResult, outBi, swapIx };
|
|
662
|
+
};
|
|
663
|
+
let candidate;
|
|
664
|
+
try {
|
|
665
|
+
candidate = await fetchOkxCandidate(Boolean(req.onlyDirectRoutes));
|
|
623
666
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
667
|
+
catch (err) {
|
|
668
|
+
if (!req.onlyDirectRoutes && err instanceof PrebuiltRouteBoundsError && !req.signal?.aborted) {
|
|
669
|
+
candidate = await fetchOkxCandidate(true);
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
throw err;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
const { entry, rr, outBi, swapIx } = candidate;
|
|
629
676
|
const outAmount = outBi.toString();
|
|
630
677
|
const minOut = ((outBi * BigInt(10_000 - req.slippageBps)) / 10000n).toString();
|
|
631
678
|
const routePlan = [];
|
|
@@ -821,8 +868,10 @@ export function createTitanSwapAdapter(config = {}) {
|
|
|
821
868
|
},
|
|
822
869
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
823
870
|
}, 'Titan');
|
|
824
|
-
if (!res.ok)
|
|
871
|
+
if (!res.ok) {
|
|
872
|
+
await cancelBodyQuietly(res);
|
|
825
873
|
throw new Error(`Titan quote HTTP ${res.status}`);
|
|
874
|
+
}
|
|
826
875
|
const decodedRaw = await res.json();
|
|
827
876
|
if (!isPlainObject(decodedRaw))
|
|
828
877
|
throw new Error('Titan returned non-object quote JSON');
|
|
@@ -1003,8 +1052,10 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1003
1052
|
headers: makeHeaders(),
|
|
1004
1053
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
1005
1054
|
}, 'Bedrock');
|
|
1006
|
-
if (!res.ok)
|
|
1055
|
+
if (!res.ok) {
|
|
1056
|
+
await cancelBodyQuietly(res);
|
|
1007
1057
|
throw new Error(`Bedrock quote HTTP ${res.status}`);
|
|
1058
|
+
}
|
|
1008
1059
|
const rawJson = await res.json();
|
|
1009
1060
|
if (!isPlainObject(rawJson))
|
|
1010
1061
|
throw new Error('Bedrock returned non-object quote JSON');
|
|
@@ -1054,6 +1105,9 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1054
1105
|
};
|
|
1055
1106
|
},
|
|
1056
1107
|
async swapInstructions(req) {
|
|
1108
|
+
if (!this.isConfigured()) {
|
|
1109
|
+
throw new Error('Bedrock adapter not configured (set apiUrl)');
|
|
1110
|
+
}
|
|
1057
1111
|
const base = resolveBase();
|
|
1058
1112
|
const res = await safeAggregatorFetch(fetchImpl, `${base}/swap-instructions`, {
|
|
1059
1113
|
method: 'POST',
|
|
@@ -1067,8 +1121,10 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1067
1121
|
}),
|
|
1068
1122
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
1069
1123
|
}, 'Bedrock');
|
|
1070
|
-
if (!res.ok)
|
|
1124
|
+
if (!res.ok) {
|
|
1125
|
+
await cancelBodyQuietly(res);
|
|
1071
1126
|
throw new Error(`Bedrock swap-instructions HTTP ${res.status}`);
|
|
1127
|
+
}
|
|
1072
1128
|
const payloadRaw = await res.json();
|
|
1073
1129
|
if (!isPlainObject(payloadRaw))
|
|
1074
1130
|
throw new Error('Bedrock returned non-object swap-instructions JSON');
|
|
@@ -1204,11 +1260,13 @@ export class AudrenSwapRouter {
|
|
|
1204
1260
|
}
|
|
1205
1261
|
return null;
|
|
1206
1262
|
}
|
|
1207
|
-
checkPrebuiltRouterAllowlist(quote) {
|
|
1208
|
-
|
|
1263
|
+
checkPrebuiltRouterAllowlist(quote, req) {
|
|
1264
|
+
const allowUnverified = req.allowUnverifiedRouter ?? this.allowUnverifiedRouter;
|
|
1265
|
+
if (allowUnverified)
|
|
1209
1266
|
return null;
|
|
1267
|
+
const allowed = req.allowedRouterPrograms ?? this.allowedRouterPrograms;
|
|
1210
1268
|
const prebuiltRouter = extractPrebuiltRouterProgramId(quote.rawQuote);
|
|
1211
|
-
if (prebuiltRouter && !
|
|
1269
|
+
if (prebuiltRouter && !allowed.includes(prebuiltRouter)) {
|
|
1212
1270
|
return `Aggregator "${quote.provider}" returned unapproved swap router program ${prebuiltRouter}`;
|
|
1213
1271
|
}
|
|
1214
1272
|
return null;
|
|
@@ -1243,7 +1301,7 @@ export class AudrenSwapRouter {
|
|
|
1243
1301
|
if (badVenue) {
|
|
1244
1302
|
throw new Error(`Aggregator "${chosen}" routed via PDA-CPI-excluded venue "${badVenue}"`);
|
|
1245
1303
|
}
|
|
1246
|
-
const routerErr = this.checkPrebuiltRouterAllowlist(quote);
|
|
1304
|
+
const routerErr = this.checkPrebuiltRouterAllowlist(quote, req);
|
|
1247
1305
|
if (routerErr)
|
|
1248
1306
|
throw new Error(routerErr);
|
|
1249
1307
|
return {
|
|
@@ -1281,7 +1339,7 @@ export class AudrenSwapRouter {
|
|
|
1281
1339
|
});
|
|
1282
1340
|
return;
|
|
1283
1341
|
}
|
|
1284
|
-
const routerErr = this.checkPrebuiltRouterAllowlist(res.value);
|
|
1342
|
+
const routerErr = this.checkPrebuiltRouterAllowlist(res.value, req);
|
|
1285
1343
|
if (routerErr) {
|
|
1286
1344
|
candidates.push({
|
|
1287
1345
|
provider: id,
|
|
@@ -1379,6 +1437,8 @@ export class AudrenSwapRouter {
|
|
|
1379
1437
|
platformFeeBps: req.platformFeeBps,
|
|
1380
1438
|
feeAccountAta: req.feeAccountAta,
|
|
1381
1439
|
feeOwnerWallet: req.feeOwnerWallet,
|
|
1440
|
+
allowedRouterPrograms: req.allowedRouterPrograms,
|
|
1441
|
+
allowUnverifiedRouter: req.allowUnverifiedRouter,
|
|
1382
1442
|
timeoutMs: req.timeoutMs,
|
|
1383
1443
|
signal: req.signal,
|
|
1384
1444
|
});
|
package/dist/swap.js
CHANGED
|
@@ -78,6 +78,7 @@ export async function fetchAggregatorPrivateSwapRoute(aggregatorBaseUrl, quotePa
|
|
|
78
78
|
throw new Error(`Aggregator /quote returned unexpected HTTP ${quoteRes.status} redirect`);
|
|
79
79
|
}
|
|
80
80
|
if (!quoteRes.ok) {
|
|
81
|
+
await quoteRes.body?.cancel?.().catch(() => { });
|
|
81
82
|
throw new Error(`Aggregator /quote failed: HTTP ${quoteRes.status}`);
|
|
82
83
|
}
|
|
83
84
|
const quote = (await quoteRes.json());
|
|
@@ -100,6 +101,7 @@ export async function fetchAggregatorPrivateSwapRoute(aggregatorBaseUrl, quotePa
|
|
|
100
101
|
throw new Error(`Aggregator /swap-instructions returned unexpected HTTP ${swapRes.status} redirect`);
|
|
101
102
|
}
|
|
102
103
|
if (!swapRes.ok) {
|
|
104
|
+
await swapRes.body?.cancel?.().catch(() => { });
|
|
103
105
|
throw new Error(`Aggregator /swap-instructions failed: HTTP ${swapRes.status}`);
|
|
104
106
|
}
|
|
105
107
|
const swap = (await swapRes.json());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audren-xyz/sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "TypeScript SDK for Audren — zero-indexer BN254 Groth16 global MASP core (PDAs, Poseidon, layouts, CompactWalletStore, gateway/relayer client, tx v1) plus WASM Groth16 prover (@audren-xyz/sdk/prover) and IDLs (@audren-xyz/sdk/idl)",
|