@audren-xyz/sdk 1.3.5 → 1.3.7
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 +20 -0
- package/dist/aggregators.js +250 -78
- package/dist/relayer.js +1 -0
- 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;
|
|
@@ -206,6 +210,16 @@ export interface TitanAdapterConfig {
|
|
|
206
210
|
export interface BedrockAdapterConfig {
|
|
207
211
|
apiUrl?: string;
|
|
208
212
|
apiKey?: string;
|
|
213
|
+
/**
|
|
214
|
+
* Optional Ed25519 private signing key for Order OS `EdDSA` edge JWT authentication
|
|
215
|
+
* (32-byte seed hex/base64/base64url, 64-byte Ed25519 keypair, 48-byte PKCS#8 DER, or JWK JSON).
|
|
216
|
+
* Takes precedence over static `apiKey` when configured.
|
|
217
|
+
*/
|
|
218
|
+
signingKey?: string;
|
|
219
|
+
jwtKeyId?: string;
|
|
220
|
+
jwtIssuer?: string;
|
|
221
|
+
jwtAudience?: string;
|
|
222
|
+
jwtSubject?: string;
|
|
209
223
|
fetchImpl?: typeof fetch;
|
|
210
224
|
}
|
|
211
225
|
export interface AudrenSwapRouterConfig {
|
|
@@ -256,6 +270,12 @@ export interface SwapRouterEnvLike {
|
|
|
256
270
|
TITAN_API_URL?: string;
|
|
257
271
|
BEDROCK_API_URL?: string;
|
|
258
272
|
BEDROCK_API_KEY?: string;
|
|
273
|
+
BEDROCK_SIGNING_KEY?: string;
|
|
274
|
+
ORDER_OS_SIGNING_KEY?: string;
|
|
275
|
+
BEDROCK_JWT_KID?: string;
|
|
276
|
+
BEDROCK_JWT_ISS?: string;
|
|
277
|
+
BEDROCK_JWT_AUD?: string;
|
|
278
|
+
BEDROCK_JWT_SUB?: string;
|
|
259
279
|
ENABLED_AGGREGATORS?: string;
|
|
260
280
|
DEFAULT_AGGREGATOR?: string;
|
|
261
281
|
/** Optional comma-separated list of approved swap router program IDs (defaults to `AUDREN_AUDITED_SWAP_ROUTERS`). */
|
package/dist/aggregators.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ed25519 } from '@noble/curves/ed25519.js';
|
|
1
2
|
import { AUDREN_PROGRAM_ID, CLASSIC_TOKEN_PROGRAM_ID, CU_LIMIT_EXEC_VIA_ESCROW_BASE, TOKEN_2022_PROGRAM_ID, } from './constants.js';
|
|
2
3
|
import { deriveEscrowPda } from './pda.js';
|
|
3
4
|
import { AUDREN_AUDITED_SWAP_ROUTERS } from './relayer.js';
|
|
@@ -124,6 +125,84 @@ function bytesToBase64(bytes) {
|
|
|
124
125
|
}
|
|
125
126
|
return btoa(bin);
|
|
126
127
|
}
|
|
128
|
+
function bytesToBase64Url(bytes) {
|
|
129
|
+
return bytesToBase64(bytes).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
130
|
+
}
|
|
131
|
+
function decodeBase64Any(str) {
|
|
132
|
+
const norm = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
133
|
+
const padded = norm.length % 4 === 0 ? norm : norm + '='.repeat(4 - (norm.length % 4));
|
|
134
|
+
const bin = atob(padded);
|
|
135
|
+
const out = new Uint8Array(bin.length);
|
|
136
|
+
for (let i = 0; i < bin.length; i++)
|
|
137
|
+
out[i] = bin.charCodeAt(i);
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
const ED25519_PKCS8_PREFIX = new Uint8Array([
|
|
141
|
+
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
|
|
142
|
+
]);
|
|
143
|
+
function parseEd25519Seed32(raw) {
|
|
144
|
+
const trimmed = raw.trim();
|
|
145
|
+
if (!trimmed)
|
|
146
|
+
throw new Error('Bedrock signingKey must not be empty');
|
|
147
|
+
let bytes;
|
|
148
|
+
if (trimmed.startsWith('{')) {
|
|
149
|
+
const jwk = JSON.parse(trimmed);
|
|
150
|
+
if (typeof jwk.d !== 'string' || !jwk.d.trim()) {
|
|
151
|
+
throw new Error('Bedrock signingKey JWK missing "d" private key field');
|
|
152
|
+
}
|
|
153
|
+
bytes = decodeBase64Any(jwk.d.trim());
|
|
154
|
+
}
|
|
155
|
+
else if (/^[0-9a-fA-F]{64}$/.test(trimmed)) {
|
|
156
|
+
bytes = new Uint8Array(32);
|
|
157
|
+
for (let i = 0; i < 32; i++) {
|
|
158
|
+
bytes[i] = parseInt(trimmed.slice(i * 2, i * 2 + 2), 16);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const body = trimmed.startsWith('-----BEGIN')
|
|
163
|
+
? trimmed.replace(/-----[^-]+-----/g, '').replace(/\s+/g, '')
|
|
164
|
+
: trimmed;
|
|
165
|
+
bytes = decodeBase64Any(body);
|
|
166
|
+
}
|
|
167
|
+
let seed;
|
|
168
|
+
if (bytes.byteLength === 32) {
|
|
169
|
+
seed = bytes;
|
|
170
|
+
}
|
|
171
|
+
else if (bytes.byteLength === 64) {
|
|
172
|
+
seed = bytes.subarray(0, 32);
|
|
173
|
+
}
|
|
174
|
+
else if (bytes.byteLength === 48 &&
|
|
175
|
+
ED25519_PKCS8_PREFIX.every((b, idx) => bytes[idx] === b)) {
|
|
176
|
+
seed = bytes.subarray(16, 48);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
throw new Error(`Bedrock signingKey has unsupported byte length (${bytes.byteLength})`);
|
|
180
|
+
}
|
|
181
|
+
if (seed.every((b) => b === 0)) {
|
|
182
|
+
throw new Error('Bedrock signingKey must not be all-zero');
|
|
183
|
+
}
|
|
184
|
+
return seed;
|
|
185
|
+
}
|
|
186
|
+
const JWT_TEXT_ENCODER = new TextEncoder();
|
|
187
|
+
function signBedrockEdDsaJwt(seed32, opts) {
|
|
188
|
+
const now = Math.floor(Date.now() / 1000);
|
|
189
|
+
const reqBytes = new Uint8Array(8);
|
|
190
|
+
if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
|
|
191
|
+
crypto.getRandomValues(reqBytes);
|
|
192
|
+
}
|
|
193
|
+
const reqId = `audren-${bytesToBase64Url(reqBytes)}`;
|
|
194
|
+
const hdr = bytesToBase64Url(JWT_TEXT_ENCODER.encode(JSON.stringify({ alg: 'EdDSA', typ: 'JWT', kid: opts.kid })));
|
|
195
|
+
const pay = bytesToBase64Url(JWT_TEXT_ENCODER.encode(JSON.stringify({
|
|
196
|
+
iss: opts.iss,
|
|
197
|
+
aud: opts.aud,
|
|
198
|
+
sub: opts.sub,
|
|
199
|
+
iat: now,
|
|
200
|
+
exp: now + 120,
|
|
201
|
+
request: reqId,
|
|
202
|
+
})));
|
|
203
|
+
const sig = ed25519.sign(JWT_TEXT_ENCODER.encode(`${hdr}.${pay}`), seed32);
|
|
204
|
+
return `${hdr}.${pay}.${bytesToBase64Url(sig)}`;
|
|
205
|
+
}
|
|
127
206
|
function parsePositiveUintString(v) {
|
|
128
207
|
if (typeof v === 'bigint') {
|
|
129
208
|
return v > 0n && v <= U64_MAX ? v : null;
|
|
@@ -177,21 +256,35 @@ function isValidRouterInstruction(v) {
|
|
|
177
256
|
typeof a.isSigner === 'boolean' &&
|
|
178
257
|
typeof a.isWritable === 'boolean');
|
|
179
258
|
}
|
|
259
|
+
class PrebuiltRouteBoundsError extends Error {
|
|
260
|
+
constructor(message) {
|
|
261
|
+
super(message);
|
|
262
|
+
this.name = 'PrebuiltRouteBoundsError';
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async function cancelBodyQuietly(res) {
|
|
266
|
+
try {
|
|
267
|
+
await res.body?.cancel?.();
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// ignore already-locked or closed stream
|
|
271
|
+
}
|
|
272
|
+
}
|
|
180
273
|
function validatePrebuiltEscrowInstructionBounds(ixs, escrowPubkey, providerLabel) {
|
|
181
274
|
const nonWrapper = ixs.filter((ix) => !WRAPPER_PROGRAMS.has(ix.programId));
|
|
182
275
|
if (nonWrapper.length !== 1) {
|
|
183
|
-
throw new
|
|
276
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route must expose exactly 1 non-wrapper swap instruction (got ${nonWrapper.length})`);
|
|
184
277
|
}
|
|
185
278
|
const swapIx = nonWrapper[0];
|
|
186
279
|
if (!swapIx.accounts.some((a) => a.pubkey === escrowPubkey)) {
|
|
187
280
|
throw new Error(`${providerLabel} did not include an escrow-signed swap instruction`);
|
|
188
281
|
}
|
|
189
282
|
if (swapIx.accounts.length > MAX_EXEC_ROUTER_CPI_ACCOUNTS) {
|
|
190
|
-
throw new
|
|
283
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route requires ${swapIx.accounts.length} router accounts (exceeds ExecViaEscrow max ${MAX_EXEC_ROUTER_CPI_ACCOUNTS})`);
|
|
191
284
|
}
|
|
192
285
|
const uniquePubkeys = new Set(swapIx.accounts.map((a) => a.pubkey)).size;
|
|
193
286
|
if (uniquePubkeys > MAX_TX_V1_UNIQUE_ROUTER_ACCOUNTS) {
|
|
194
|
-
throw new
|
|
287
|
+
throw new PrebuiltRouteBoundsError(`${providerLabel} route requires ${uniquePubkeys} unique router accounts (exceeds Tx v1 safe budget ${MAX_TX_V1_UNIQUE_ROUTER_ACCOUNTS})`);
|
|
195
288
|
}
|
|
196
289
|
return swapIx;
|
|
197
290
|
}
|
|
@@ -357,6 +450,9 @@ export function createJupiterSwapAdapter(config = {}) {
|
|
|
357
450
|
};
|
|
358
451
|
},
|
|
359
452
|
async swapInstructions(req) {
|
|
453
|
+
if (!this.isConfigured()) {
|
|
454
|
+
throw new Error('Jupiter adapter not configured (set apiKey or apiUrl)');
|
|
455
|
+
}
|
|
360
456
|
const body = {
|
|
361
457
|
quoteResponse: req.quoteResponse,
|
|
362
458
|
userPublicKey: req.escrowPubkey,
|
|
@@ -373,8 +469,10 @@ export function createJupiterSwapAdapter(config = {}) {
|
|
|
373
469
|
signal: req.signal,
|
|
374
470
|
timeoutMs: req.timeoutMs,
|
|
375
471
|
});
|
|
376
|
-
if (!res.ok)
|
|
472
|
+
if (!res.ok) {
|
|
473
|
+
await cancelBodyQuietly(res);
|
|
377
474
|
throw new Error(`Jupiter swap-instructions HTTP ${res.status}`);
|
|
475
|
+
}
|
|
378
476
|
const payloadRaw = await res.json();
|
|
379
477
|
if (!isPlainObject(payloadRaw))
|
|
380
478
|
throw new Error('Jupiter returned non-object swap-instructions JSON');
|
|
@@ -446,8 +544,10 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
446
544
|
headers: makeHeaders(),
|
|
447
545
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
448
546
|
}, 'DFlow');
|
|
449
|
-
if (!res.ok)
|
|
547
|
+
if (!res.ok) {
|
|
548
|
+
await cancelBodyQuietly(res);
|
|
450
549
|
throw new Error(`DFlow quote HTTP ${res.status}`);
|
|
550
|
+
}
|
|
451
551
|
const rawJson = await res.json();
|
|
452
552
|
if (!isPlainObject(rawJson))
|
|
453
553
|
throw new Error('DFlow returned non-object quote JSON');
|
|
@@ -502,6 +602,9 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
502
602
|
};
|
|
503
603
|
},
|
|
504
604
|
async swapInstructions(req) {
|
|
605
|
+
if (!this.isConfigured()) {
|
|
606
|
+
throw new Error('DFlow adapter not configured (set apiKey or apiUrl)');
|
|
607
|
+
}
|
|
505
608
|
const base = resolveBase();
|
|
506
609
|
const res = await safeAggregatorFetch(fetchImpl, `${base}/swap-instructions`, {
|
|
507
610
|
method: 'POST',
|
|
@@ -515,8 +618,10 @@ export function createDFlowSwapAdapter(config = {}) {
|
|
|
515
618
|
}),
|
|
516
619
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
517
620
|
}, 'DFlow');
|
|
518
|
-
if (!res.ok)
|
|
621
|
+
if (!res.ok) {
|
|
622
|
+
await cancelBodyQuietly(res);
|
|
519
623
|
throw new Error(`DFlow swap-instructions HTTP ${res.status}`);
|
|
624
|
+
}
|
|
520
625
|
const payloadRaw = await res.json();
|
|
521
626
|
if (!isPlainObject(payloadRaw))
|
|
522
627
|
throw new Error('DFlow returned non-object swap-instructions JSON');
|
|
@@ -614,8 +719,10 @@ export function createOkxSwapAdapter(config = {}) {
|
|
|
614
719
|
headers,
|
|
615
720
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
616
721
|
}, 'OKX');
|
|
617
|
-
if (!res.ok)
|
|
722
|
+
if (!res.ok) {
|
|
723
|
+
await cancelBodyQuietly(res);
|
|
618
724
|
throw new Error(`OKX swap-instruction HTTP ${res.status}`);
|
|
725
|
+
}
|
|
619
726
|
const bodyRaw = await res.json();
|
|
620
727
|
if (!isPlainObject(bodyRaw))
|
|
621
728
|
throw new Error('OKX returned non-object response JSON');
|
|
@@ -637,9 +744,7 @@ export function createOkxSwapAdapter(config = {}) {
|
|
|
637
744
|
candidate = await fetchOkxCandidate(Boolean(req.onlyDirectRoutes));
|
|
638
745
|
}
|
|
639
746
|
catch (err) {
|
|
640
|
-
|
|
641
|
-
const isBoundsRejection = /exceeds ExecViaEscrow max|exceeds Solana Tx v1|non-wrapper swap instruction/i.test(msg);
|
|
642
|
-
if (!req.onlyDirectRoutes && isBoundsRejection && !req.signal?.aborted) {
|
|
747
|
+
if (!req.onlyDirectRoutes && err instanceof PrebuiltRouteBoundsError && !req.signal?.aborted) {
|
|
643
748
|
candidate = await fetchOkxCandidate(true);
|
|
644
749
|
}
|
|
645
750
|
else {
|
|
@@ -824,67 +929,96 @@ export function createTitanSwapAdapter(config = {}) {
|
|
|
824
929
|
}
|
|
825
930
|
const base = resolveBase();
|
|
826
931
|
const t0 = Date.now();
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
:
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
: {};
|
|
854
|
-
const winnerId = decoded.metadata?.ExpectedWinner;
|
|
855
|
-
let winnerKey = '';
|
|
856
|
-
let route;
|
|
857
|
-
let routeOut = null;
|
|
858
|
-
if (typeof winnerId === 'string' && Object.prototype.hasOwnProperty.call(quotesMap, winnerId)) {
|
|
859
|
-
const candidate = quotesMap[winnerId];
|
|
860
|
-
const candidateOut = parsePositiveUintString(candidate?.outAmount);
|
|
861
|
-
if (candidate && candidateOut !== null) {
|
|
862
|
-
winnerKey = winnerId;
|
|
863
|
-
route = candidate;
|
|
864
|
-
routeOut = candidateOut;
|
|
932
|
+
const escrowPubkey = req.escrowPubkey;
|
|
933
|
+
const defaultAccountsLimit = Math.min(Math.max(req.maxAccounts ?? 36, 16), 40);
|
|
934
|
+
const fetchTitanCandidate = async (onlyDirect, accountsLimit) => {
|
|
935
|
+
const qs = new URLSearchParams({
|
|
936
|
+
inputMint: req.inputMint,
|
|
937
|
+
outputMint: req.outputMint,
|
|
938
|
+
amount: req.amount,
|
|
939
|
+
slippageBps: String(req.slippageBps),
|
|
940
|
+
userPublicKey: escrowPubkey,
|
|
941
|
+
outputWsol: 'true',
|
|
942
|
+
accountsLimitTotal: String(accountsLimit),
|
|
943
|
+
...(onlyDirect ? { onlyDirectRoutes: 'true' } : {}),
|
|
944
|
+
...(req.platformFeeBps && req.platformFeeBps > 0 && req.feeAccountAta
|
|
945
|
+
? { feeBps: String(req.platformFeeBps), feeAccount: req.feeAccountAta }
|
|
946
|
+
: {}),
|
|
947
|
+
});
|
|
948
|
+
const res = await safeAggregatorFetch(fetchImpl, `${base}/quote/swap?${qs.toString()}`, {
|
|
949
|
+
headers: {
|
|
950
|
+
accept: 'application/json',
|
|
951
|
+
...(apiKey ? { authorization: `Bearer ${apiKey}`, 'x-api-key': apiKey } : {}),
|
|
952
|
+
},
|
|
953
|
+
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
954
|
+
}, 'Titan');
|
|
955
|
+
if (!res.ok) {
|
|
956
|
+
await cancelBodyQuietly(res);
|
|
957
|
+
throw new Error(`Titan quote HTTP ${res.status}`);
|
|
865
958
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
959
|
+
const decodedRaw = await res.json();
|
|
960
|
+
if (!isPlainObject(decodedRaw))
|
|
961
|
+
throw new Error('Titan returned non-object quote JSON');
|
|
962
|
+
const decoded = decodedRaw;
|
|
963
|
+
const quotesMap = decoded.quotes && typeof decoded.quotes === 'object' && !Array.isArray(decoded.quotes)
|
|
964
|
+
? decoded.quotes
|
|
965
|
+
: {};
|
|
966
|
+
// Rank sub-providers: ExpectedWinner first (when valid), then remaining sub-providers by highest outAmount.
|
|
967
|
+
const ranked = [];
|
|
968
|
+
const winnerId = decoded.metadata?.ExpectedWinner;
|
|
869
969
|
for (const [k, r] of Object.entries(quotesMap)) {
|
|
870
970
|
const out = parsePositiveUintString(r?.outAmount);
|
|
871
|
-
if (
|
|
872
|
-
|
|
873
|
-
winnerKey = k;
|
|
874
|
-
route = r;
|
|
875
|
-
routeOut = out;
|
|
971
|
+
if (r && out !== null) {
|
|
972
|
+
ranked.push({ key: k, route: r, out });
|
|
876
973
|
}
|
|
877
974
|
}
|
|
975
|
+
if (ranked.length === 0) {
|
|
976
|
+
throw new Error('Titan returned no valid quotes');
|
|
977
|
+
}
|
|
978
|
+
ranked.sort((a, b) => {
|
|
979
|
+
if (a.key === winnerId && b.key !== winnerId)
|
|
980
|
+
return -1;
|
|
981
|
+
if (b.key === winnerId && a.key !== winnerId)
|
|
982
|
+
return 1;
|
|
983
|
+
return b.out > a.out ? 1 : b.out < a.out ? -1 : 0;
|
|
984
|
+
});
|
|
985
|
+
let firstErr = null;
|
|
986
|
+
for (const cand of ranked) {
|
|
987
|
+
const prebuiltIxs = Array.isArray(cand.route.instructions)
|
|
988
|
+
? cand.route.instructions
|
|
989
|
+
.map(normalizeTitanInstruction)
|
|
990
|
+
.filter((x) => x !== null)
|
|
991
|
+
: [];
|
|
992
|
+
try {
|
|
993
|
+
const swapIx = validatePrebuiltEscrowInstructionBounds(prebuiltIxs, escrowPubkey, 'Titan');
|
|
994
|
+
return {
|
|
995
|
+
decoded,
|
|
996
|
+
winnerKey: cand.key,
|
|
997
|
+
route: cand.route,
|
|
998
|
+
routeOut: cand.out,
|
|
999
|
+
swapIx,
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
catch (err) {
|
|
1003
|
+
if (firstErr === null)
|
|
1004
|
+
firstErr = err;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
throw firstErr ?? new Error('Titan returned no bounded escrow-compatible quotes');
|
|
1008
|
+
};
|
|
1009
|
+
let chosen;
|
|
1010
|
+
try {
|
|
1011
|
+
chosen = await fetchTitanCandidate(Boolean(req.onlyDirectRoutes), defaultAccountsLimit);
|
|
878
1012
|
}
|
|
879
|
-
|
|
880
|
-
|
|
1013
|
+
catch (err) {
|
|
1014
|
+
if (!req.onlyDirectRoutes && err instanceof PrebuiltRouteBoundsError && !req.signal?.aborted) {
|
|
1015
|
+
chosen = await fetchTitanCandidate(true, Math.min(defaultAccountsLimit, 28));
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
throw err;
|
|
1019
|
+
}
|
|
881
1020
|
}
|
|
882
|
-
const
|
|
883
|
-
? route.instructions
|
|
884
|
-
.map(normalizeTitanInstruction)
|
|
885
|
-
.filter((x) => x !== null)
|
|
886
|
-
: [];
|
|
887
|
-
const swapIx = validatePrebuiltEscrowInstructionBounds(prebuiltIxs, req.escrowPubkey, 'Titan');
|
|
1021
|
+
const { decoded, winnerKey, route, routeOut, swapIx } = chosen;
|
|
888
1022
|
const outAmount = routeOut.toString();
|
|
889
1023
|
const minOut = ((routeOut * BigInt(10_000 - req.slippageBps)) / 10000n).toString();
|
|
890
1024
|
const steps = Array.isArray(route.steps)
|
|
@@ -967,7 +1101,7 @@ export function createTitanSwapAdapter(config = {}) {
|
|
|
967
1101
|
? raw.route.addressLookupTables.filter((x) => typeof x === 'string')
|
|
968
1102
|
: [],
|
|
969
1103
|
computeUnitLimit: computeAggregatorExecCuLimit({
|
|
970
|
-
reportedRouterCu: raw.route.computeUnits,
|
|
1104
|
+
reportedRouterCu: raw.route.computeUnitsSafe ?? raw.route.computeUnits,
|
|
971
1105
|
routeHopCount,
|
|
972
1106
|
}),
|
|
973
1107
|
feeAccount: req.feeAccountAta ?? null,
|
|
@@ -978,17 +1112,36 @@ export function createTitanSwapAdapter(config = {}) {
|
|
|
978
1112
|
export function createBedrockSwapAdapter(config = {}) {
|
|
979
1113
|
const fetchImpl = config.fetchImpl ?? defaultFetchImpl;
|
|
980
1114
|
const apiKey = config.apiKey?.trim() ?? '';
|
|
1115
|
+
const signingKeyRaw = config.signingKey?.trim() ?? '';
|
|
981
1116
|
const rawUrl = config.apiUrl?.trim() ?? '';
|
|
1117
|
+
const jwtOpts = {
|
|
1118
|
+
kid: config.jwtKeyId?.trim() || 'k256-app-order-os-1',
|
|
1119
|
+
iss: config.jwtIssuer?.trim() || 'k256-gateway',
|
|
1120
|
+
aud: config.jwtAudience?.trim() || 'k256-order-os',
|
|
1121
|
+
sub: config.jwtSubject?.trim() || 'gateway',
|
|
1122
|
+
};
|
|
1123
|
+
let cachedSeed32 = null;
|
|
982
1124
|
const resolveBase = () => {
|
|
983
1125
|
if (!rawUrl)
|
|
984
1126
|
throw new Error('Bedrock adapter not configured (set apiUrl)');
|
|
985
1127
|
return validateAggregatorBaseUrl(rawUrl, 'Bedrock');
|
|
986
1128
|
};
|
|
987
|
-
const makeHeaders = () =>
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1129
|
+
const makeHeaders = () => {
|
|
1130
|
+
const baseHeaders = {
|
|
1131
|
+
accept: 'application/json',
|
|
1132
|
+
'content-type': 'application/json',
|
|
1133
|
+
};
|
|
1134
|
+
if (signingKeyRaw) {
|
|
1135
|
+
if (!cachedSeed32)
|
|
1136
|
+
cachedSeed32 = parseEd25519Seed32(signingKeyRaw);
|
|
1137
|
+
baseHeaders.authorization = `Bearer ${signBedrockEdDsaJwt(cachedSeed32, jwtOpts)}`;
|
|
1138
|
+
}
|
|
1139
|
+
else if (apiKey) {
|
|
1140
|
+
baseHeaders.authorization = `Bearer ${apiKey}`;
|
|
1141
|
+
baseHeaders['x-api-key'] = apiKey;
|
|
1142
|
+
}
|
|
1143
|
+
return baseHeaders;
|
|
1144
|
+
};
|
|
992
1145
|
return {
|
|
993
1146
|
id: 'bedrock',
|
|
994
1147
|
name: 'Bedrock',
|
|
@@ -1024,8 +1177,10 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1024
1177
|
headers: makeHeaders(),
|
|
1025
1178
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
1026
1179
|
}, 'Bedrock');
|
|
1027
|
-
if (!res.ok)
|
|
1180
|
+
if (!res.ok) {
|
|
1181
|
+
await cancelBodyQuietly(res);
|
|
1028
1182
|
throw new Error(`Bedrock quote HTTP ${res.status}`);
|
|
1183
|
+
}
|
|
1029
1184
|
const rawJson = await res.json();
|
|
1030
1185
|
if (!isPlainObject(rawJson))
|
|
1031
1186
|
throw new Error('Bedrock returned non-object quote JSON');
|
|
@@ -1075,6 +1230,9 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1075
1230
|
};
|
|
1076
1231
|
},
|
|
1077
1232
|
async swapInstructions(req) {
|
|
1233
|
+
if (!this.isConfigured()) {
|
|
1234
|
+
throw new Error('Bedrock adapter not configured (set apiUrl)');
|
|
1235
|
+
}
|
|
1078
1236
|
const base = resolveBase();
|
|
1079
1237
|
const res = await safeAggregatorFetch(fetchImpl, `${base}/swap-instructions`, {
|
|
1080
1238
|
method: 'POST',
|
|
@@ -1088,8 +1246,10 @@ export function createBedrockSwapAdapter(config = {}) {
|
|
|
1088
1246
|
}),
|
|
1089
1247
|
signal: buildFetchSignal(req.signal, req.timeoutMs ?? 10_000),
|
|
1090
1248
|
}, 'Bedrock');
|
|
1091
|
-
if (!res.ok)
|
|
1249
|
+
if (!res.ok) {
|
|
1250
|
+
await cancelBodyQuietly(res);
|
|
1092
1251
|
throw new Error(`Bedrock swap-instructions HTTP ${res.status}`);
|
|
1252
|
+
}
|
|
1093
1253
|
const payloadRaw = await res.json();
|
|
1094
1254
|
if (!isPlainObject(payloadRaw))
|
|
1095
1255
|
throw new Error('Bedrock returned non-object swap-instructions JSON');
|
|
@@ -1171,7 +1331,15 @@ export class AudrenSwapRouter {
|
|
|
1171
1331
|
apiUrl: env.OKX_API_URL,
|
|
1172
1332
|
},
|
|
1173
1333
|
titan: { apiKey: env.TITAN_API_KEY, apiUrl: env.TITAN_API_URL },
|
|
1174
|
-
bedrock: {
|
|
1334
|
+
bedrock: {
|
|
1335
|
+
apiUrl: env.BEDROCK_API_URL,
|
|
1336
|
+
apiKey: env.BEDROCK_API_KEY,
|
|
1337
|
+
signingKey: env.BEDROCK_SIGNING_KEY ?? env.ORDER_OS_SIGNING_KEY,
|
|
1338
|
+
jwtKeyId: env.BEDROCK_JWT_KID,
|
|
1339
|
+
jwtIssuer: env.BEDROCK_JWT_ISS,
|
|
1340
|
+
jwtAudience: env.BEDROCK_JWT_AUD,
|
|
1341
|
+
jwtSubject: env.BEDROCK_JWT_SUB,
|
|
1342
|
+
},
|
|
1175
1343
|
...(enabledProviders && enabledProviders.length > 0 ? { enabledProviders } : {}),
|
|
1176
1344
|
...(defaultRaw ? { defaultProvider: defaultRaw } : {}),
|
|
1177
1345
|
...(allowedRouterPrograms && allowedRouterPrograms.length > 0 ? { allowedRouterPrograms } : {}),
|
|
@@ -1225,11 +1393,13 @@ export class AudrenSwapRouter {
|
|
|
1225
1393
|
}
|
|
1226
1394
|
return null;
|
|
1227
1395
|
}
|
|
1228
|
-
checkPrebuiltRouterAllowlist(quote) {
|
|
1229
|
-
|
|
1396
|
+
checkPrebuiltRouterAllowlist(quote, req) {
|
|
1397
|
+
const allowUnverified = req.allowUnverifiedRouter ?? this.allowUnverifiedRouter;
|
|
1398
|
+
if (allowUnverified)
|
|
1230
1399
|
return null;
|
|
1400
|
+
const allowed = req.allowedRouterPrograms ?? this.allowedRouterPrograms;
|
|
1231
1401
|
const prebuiltRouter = extractPrebuiltRouterProgramId(quote.rawQuote);
|
|
1232
|
-
if (prebuiltRouter && !
|
|
1402
|
+
if (prebuiltRouter && !allowed.includes(prebuiltRouter)) {
|
|
1233
1403
|
return `Aggregator "${quote.provider}" returned unapproved swap router program ${prebuiltRouter}`;
|
|
1234
1404
|
}
|
|
1235
1405
|
return null;
|
|
@@ -1264,7 +1434,7 @@ export class AudrenSwapRouter {
|
|
|
1264
1434
|
if (badVenue) {
|
|
1265
1435
|
throw new Error(`Aggregator "${chosen}" routed via PDA-CPI-excluded venue "${badVenue}"`);
|
|
1266
1436
|
}
|
|
1267
|
-
const routerErr = this.checkPrebuiltRouterAllowlist(quote);
|
|
1437
|
+
const routerErr = this.checkPrebuiltRouterAllowlist(quote, req);
|
|
1268
1438
|
if (routerErr)
|
|
1269
1439
|
throw new Error(routerErr);
|
|
1270
1440
|
return {
|
|
@@ -1302,7 +1472,7 @@ export class AudrenSwapRouter {
|
|
|
1302
1472
|
});
|
|
1303
1473
|
return;
|
|
1304
1474
|
}
|
|
1305
|
-
const routerErr = this.checkPrebuiltRouterAllowlist(res.value);
|
|
1475
|
+
const routerErr = this.checkPrebuiltRouterAllowlist(res.value, req);
|
|
1306
1476
|
if (routerErr) {
|
|
1307
1477
|
candidates.push({
|
|
1308
1478
|
provider: id,
|
|
@@ -1400,6 +1570,8 @@ export class AudrenSwapRouter {
|
|
|
1400
1570
|
platformFeeBps: req.platformFeeBps,
|
|
1401
1571
|
feeAccountAta: req.feeAccountAta,
|
|
1402
1572
|
feeOwnerWallet: req.feeOwnerWallet,
|
|
1573
|
+
allowedRouterPrograms: req.allowedRouterPrograms,
|
|
1574
|
+
allowUnverifiedRouter: req.allowUnverifiedRouter,
|
|
1403
1575
|
timeoutMs: req.timeoutMs,
|
|
1404
1576
|
signal: req.signal,
|
|
1405
1577
|
});
|
package/dist/relayer.js
CHANGED
|
@@ -18,6 +18,7 @@ export const AUDREN_AUDITED_SWAP_ROUTERS = [
|
|
|
18
18
|
'6m2CDdhRgxpH4WjvdzxAYbGxwdGUz5MziiL5jek2kBma', // OKX DEX Router (v1)
|
|
19
19
|
'T1TANpTeScyeqVzzgNViGDNrkQ6qHz9KrSBS4aNXvGT', // Titan Meta-Aggregator
|
|
20
20
|
'proVF4pMXVaYqmy4NjniPh4pqKNfMmsihgd4wdkCX3u', // OKX DEX Aggregation Router V2/V6
|
|
21
|
+
'BrqCvvZwUaJY2fxkByyahaDuvdbmTXiAghQnfQsHvJoc', // Bedrock (Order OS)
|
|
21
22
|
];
|
|
22
23
|
export class AudrenRelayError extends Error {
|
|
23
24
|
status;
|
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.7",
|
|
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)",
|