@sodax/sdk 2.1.0-rc.3 → 2.1.0-rc.4
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/index.cjs +347 -128
- package/dist/index.d.cts +203 -60
- package/dist/index.d.ts +203 -60
- package/dist/index.mjs +347 -128
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -3337,7 +3337,7 @@ var spokeChainConfig = {
|
|
|
3337
3337
|
supportedTokens: suiSupportedTokens,
|
|
3338
3338
|
nativeToken: "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
|
|
3339
3339
|
bnUSD: "0xff4de2b2b57dd7611d2812d231a467d007b702a101fd5c7ad3b278257cddb507::bnusd::BNUSD",
|
|
3340
|
-
rpc_url: "https://
|
|
3340
|
+
rpc_url: "https://sui-rpc.publicnode.com",
|
|
3341
3341
|
chain: baseChainInfo[ChainKeys.SUI_MAINNET],
|
|
3342
3342
|
pollingConfig: {
|
|
3343
3343
|
pollingIntervalMs: 500,
|
|
@@ -4428,7 +4428,13 @@ function isValidWalletProviderForChainKey(chainKey, walletProvider) {
|
|
|
4428
4428
|
}
|
|
4429
4429
|
|
|
4430
4430
|
// ../types/dist/index.js
|
|
4431
|
-
var CONFIG_VERSION =
|
|
4431
|
+
var CONFIG_VERSION = 224;
|
|
4432
|
+
|
|
4433
|
+
// src/shared/utils/resolveTimeoutMs.ts
|
|
4434
|
+
function resolveTimeoutMs(timeout, fallbackMs) {
|
|
4435
|
+
if (timeout === void 0 || !Number.isFinite(timeout)) return fallbackMs;
|
|
4436
|
+
return Math.max(0, timeout);
|
|
4437
|
+
}
|
|
4432
4438
|
function isEvmSpokeChainConfig(value) {
|
|
4433
4439
|
return typeof value === "object" && value !== null && value.chain.type === "EVM" && value.chain.key !== HUB_CHAIN_KEY;
|
|
4434
4440
|
}
|
|
@@ -4855,7 +4861,7 @@ async function pollForExecutedPacket(payload, deadline) {
|
|
|
4855
4861
|
}
|
|
4856
4862
|
async function waitUntilIntentExecuted(payload) {
|
|
4857
4863
|
try {
|
|
4858
|
-
const timeout = payload.timeout
|
|
4864
|
+
const timeout = resolveTimeoutMs(payload.timeout, DEFAULT_RELAY_TX_TIMEOUT);
|
|
4859
4865
|
const startTime = Date.now();
|
|
4860
4866
|
const deadline = startTime + timeout;
|
|
4861
4867
|
let lastPollingError;
|
|
@@ -4888,7 +4894,8 @@ async function waitUntilIntentExecuted(payload) {
|
|
|
4888
4894
|
}
|
|
4889
4895
|
async function relayTxAndWaitPacket(params) {
|
|
4890
4896
|
try {
|
|
4891
|
-
const { srcTxHash, data, chainKey, relayerApiEndpoint,
|
|
4897
|
+
const { srcTxHash, data, chainKey, relayerApiEndpoint, pollTxHash } = params;
|
|
4898
|
+
const timeout = resolveTimeoutMs(params.timeout, DEFAULT_RELAY_TX_TIMEOUT);
|
|
4892
4899
|
const intentRelayChainId = getIntentRelayChainId(chainKey).toString();
|
|
4893
4900
|
const isSplitTxChain = isSolanaChainKeyType(chainKey) || isBitcoinChainKeyType(chainKey);
|
|
4894
4901
|
invariant(!isSplitTxChain || data !== void 0, "Data is required for Solana and Bitcoin chain keys");
|
|
@@ -14296,6 +14303,17 @@ var ConfigService = class {
|
|
|
14296
14303
|
get leverageYieldPartnerFee() {
|
|
14297
14304
|
return this.leverageYield.partnerFee ?? this.fee;
|
|
14298
14305
|
}
|
|
14306
|
+
// Effective backend submit-tx toggle per feature. Read live off the same `swaps` / `bridge` slots as
|
|
14307
|
+
// `partnerFee` so an omitted flag resolves to the ON default here, in one place, instead of leaving
|
|
14308
|
+
// `config.swaps.useBackendSubmitTx === undefined` while the backend path is actually active.
|
|
14309
|
+
// The deprecated `swapsOptions` / `bridgeOptions` keys are still honoured as a second-precedence
|
|
14310
|
+
// fallback, so a pre-existing explicit opt-out keeps the client-side path instead of silently flipping.
|
|
14311
|
+
get swapUseBackendSubmitTx() {
|
|
14312
|
+
return this.swaps.useBackendSubmitTx ?? this.sodax.swapsOptions?.useBackendSubmitTx ?? true;
|
|
14313
|
+
}
|
|
14314
|
+
get bridgeUseBackendSubmitTx() {
|
|
14315
|
+
return this.bridge.useBackendSubmitTx ?? this.sodax.bridgeOptions?.useBackendSubmitTx ?? true;
|
|
14316
|
+
}
|
|
14299
14317
|
get dex() {
|
|
14300
14318
|
return this.sodax.dex;
|
|
14301
14319
|
}
|
|
@@ -26919,19 +26937,20 @@ var SolverApiService = class {
|
|
|
26919
26937
|
|
|
26920
26938
|
// src/backendApi/pollBackendSubmitTx.ts
|
|
26921
26939
|
async function pollBackendSubmitTx({
|
|
26922
|
-
|
|
26940
|
+
attempt,
|
|
26923
26941
|
terminalStatus,
|
|
26924
26942
|
getStatus,
|
|
26925
26943
|
onExecuted,
|
|
26926
26944
|
intervalMs = 1e3,
|
|
26927
26945
|
requestTimeoutMs
|
|
26928
26946
|
}) {
|
|
26929
|
-
|
|
26930
|
-
|
|
26931
|
-
|
|
26932
|
-
|
|
26933
|
-
const statusResult = await getStatus(
|
|
26947
|
+
let polls = 0;
|
|
26948
|
+
let lastStatusError;
|
|
26949
|
+
for (let requestTimeout = attempt.requestTimeout(requestTimeoutMs); requestTimeout !== null; requestTimeout = attempt.requestTimeout(requestTimeoutMs)) {
|
|
26950
|
+
polls += 1;
|
|
26951
|
+
const statusResult = await getStatus({ timeout: requestTimeout });
|
|
26934
26952
|
if (statusResult.ok) {
|
|
26953
|
+
lastStatusError = void 0;
|
|
26935
26954
|
const { status, result, failureReason, abandonedAt } = statusResult.value.data;
|
|
26936
26955
|
if (status === terminalStatus) {
|
|
26937
26956
|
const value = onExecuted(result);
|
|
@@ -26941,11 +26960,70 @@ async function pollBackendSubmitTx({
|
|
|
26941
26960
|
const reason = failureReason ? `: ${failureReason}` : "";
|
|
26942
26961
|
return { ok: false, cause: new Error(`backend submit-tx ${status}${reason}`) };
|
|
26943
26962
|
}
|
|
26963
|
+
} else {
|
|
26964
|
+
lastStatusError = statusResult.error;
|
|
26944
26965
|
}
|
|
26945
|
-
|
|
26966
|
+
if (attempt.remaining() <= intervalMs) break;
|
|
26967
|
+
await sleep(intervalMs);
|
|
26968
|
+
}
|
|
26969
|
+
if (polls === 0) {
|
|
26970
|
+
return { ok: false, cause: new Error("backend submit-tx submitted, but no budget was left to poll for status") };
|
|
26971
|
+
}
|
|
26972
|
+
if (lastStatusError !== void 0) {
|
|
26973
|
+
return {
|
|
26974
|
+
ok: false,
|
|
26975
|
+
cause: new Error(`backend submit-tx status polling failed before reaching ${terminalStatus}`, {
|
|
26976
|
+
cause: lastStatusError
|
|
26977
|
+
})
|
|
26978
|
+
};
|
|
26946
26979
|
}
|
|
26947
26980
|
return { ok: false, cause: new Error(`backend submit-tx polling timed out before reaching ${terminalStatus}`) };
|
|
26948
26981
|
}
|
|
26982
|
+
|
|
26983
|
+
// src/backendApi/submitTxAttempt.ts
|
|
26984
|
+
function noRequestBudgetCause(serviceTimeoutMs) {
|
|
26985
|
+
return serviceTimeoutMs > 0 ? new Error("backend submit-tx skipped: the caller timeout left no budget for the attempt") : new Error(`backend submit-tx skipped: api timeout must be a positive number, got ${serviceTimeoutMs}`);
|
|
26986
|
+
}
|
|
26987
|
+
function createSubmitTxAttempt(callerTimeoutMs) {
|
|
26988
|
+
const deadline = Date.now() + resolveTimeoutMs(callerTimeoutMs, 0);
|
|
26989
|
+
const remaining = () => Math.max(0, deadline - Date.now());
|
|
26990
|
+
return {
|
|
26991
|
+
remaining,
|
|
26992
|
+
requestTimeout: (serviceTimeoutMs) => {
|
|
26993
|
+
const timeout = Math.min(remaining(), serviceTimeoutMs);
|
|
26994
|
+
return timeout > 0 ? timeout : null;
|
|
26995
|
+
}
|
|
26996
|
+
};
|
|
26997
|
+
}
|
|
26998
|
+
|
|
26999
|
+
// src/backendApi/runBackendSubmitTx.ts
|
|
27000
|
+
async function runBackendSubmitTx({
|
|
27001
|
+
attempt,
|
|
27002
|
+
api,
|
|
27003
|
+
body,
|
|
27004
|
+
statusQuery,
|
|
27005
|
+
terminalStatus,
|
|
27006
|
+
onExecuted
|
|
27007
|
+
}) {
|
|
27008
|
+
const serviceTimeoutMs = api.getTimeout();
|
|
27009
|
+
const requestTimeout = attempt.requestTimeout(serviceTimeoutMs);
|
|
27010
|
+
if (requestTimeout === null) return { ok: false, cause: noRequestBudgetCause(serviceTimeoutMs) };
|
|
27011
|
+
const submitted = await api.submitTx(body, { timeout: requestTimeout });
|
|
27012
|
+
if (!submitted.ok) return { ok: false, cause: submitted.error };
|
|
27013
|
+
if (!submitted.value.success) {
|
|
27014
|
+
return { ok: false, cause: new Error(`backend submit-tx not accepted: ${submitted.value.data.message}`) };
|
|
27015
|
+
}
|
|
27016
|
+
const polled = await pollBackendSubmitTx({
|
|
27017
|
+
attempt,
|
|
27018
|
+
terminalStatus,
|
|
27019
|
+
getStatus: (override) => api.getSubmitTxStatus(statusQuery, override),
|
|
27020
|
+
onExecuted,
|
|
27021
|
+
// While this ceiling is the smaller bound the poll retries within the attempt; once the attempt's
|
|
27022
|
+
// remainder falls below it, one stalled request can spend the rest.
|
|
27023
|
+
requestTimeoutMs: serviceTimeoutMs
|
|
27024
|
+
});
|
|
27025
|
+
return polled.ok ? { ok: true, value: polled.value } : { ok: false, cause: polled.cause };
|
|
27026
|
+
}
|
|
26949
27027
|
function decodePacketIntentTarget(payload) {
|
|
26950
27028
|
try {
|
|
26951
27029
|
const hex = payload.startsWith("0x") ? payload.slice(2) : payload;
|
|
@@ -26996,20 +27074,33 @@ var SwapService = class {
|
|
|
26996
27074
|
spoke;
|
|
26997
27075
|
// swap config
|
|
26998
27076
|
solver;
|
|
26999
|
-
partnerFee;
|
|
27000
27077
|
relayerApiEndpoint;
|
|
27001
|
-
// backend swaps-API client
|
|
27078
|
+
// backend swaps-API client
|
|
27002
27079
|
backendApi;
|
|
27003
|
-
|
|
27004
|
-
|
|
27080
|
+
/**
|
|
27081
|
+
* Effective 2-step submit-tx flow (`swaps.useBackendSubmitTx`, default on). Read live off
|
|
27082
|
+
* `ConfigService`, like {@link SwapService.partnerFee}, so the config object and the behavior can
|
|
27083
|
+
* never disagree.
|
|
27084
|
+
*/
|
|
27085
|
+
get useBackendSubmitTx() {
|
|
27086
|
+
return this.config.swapUseBackendSubmitTx;
|
|
27087
|
+
}
|
|
27088
|
+
/**
|
|
27089
|
+
* Effective swap partner fee (`swaps.partnerFee`, else the global `fee`). Read live off
|
|
27090
|
+
* `ConfigService` rather than snapshotted in the constructor, so it cannot diverge from
|
|
27091
|
+
* `config.swapPartnerFee` if the config object is ever replaced (see `ConfigService.initialize`).
|
|
27092
|
+
* `BridgeService` and `LeverageYieldService` resolve their fees the same way.
|
|
27093
|
+
*/
|
|
27094
|
+
get partnerFee() {
|
|
27095
|
+
return this.config.swapPartnerFee;
|
|
27096
|
+
}
|
|
27097
|
+
constructor({ config, hubProvider, spoke, backendApi }) {
|
|
27005
27098
|
this.solver = config.solver;
|
|
27006
|
-
this.partnerFee = config.swapPartnerFee;
|
|
27007
27099
|
this.relayerApiEndpoint = config.relay.relayerApiEndpoint;
|
|
27008
27100
|
this.config = config;
|
|
27009
27101
|
this.hubProvider = hubProvider;
|
|
27010
27102
|
this.spoke = spoke;
|
|
27011
27103
|
this.backendApi = backendApi;
|
|
27012
|
-
this.useBackendSubmitTx = useBackendSubmitTx ?? false;
|
|
27013
27104
|
}
|
|
27014
27105
|
/**
|
|
27015
27106
|
* Estimates the gas cost for a raw (unsigned) transaction on a spoke chain.
|
|
@@ -27154,21 +27245,26 @@ var SwapService = class {
|
|
|
27154
27245
|
* Executes a full end-to-end cross-chain swap.
|
|
27155
27246
|
*
|
|
27156
27247
|
* Orchestrates the complete swap lifecycle. `createIntent` first submits the intent transaction on
|
|
27157
|
-
* the source spoke chain; completion then runs via one of two paths,
|
|
27158
|
-
*
|
|
27159
|
-
*
|
|
27160
|
-
* - **Client-side (
|
|
27161
|
-
* relays it to the hub (Sonic) and waits for the packet — skipped when
|
|
27162
|
-
* where the spoke tx already is the hub tx — then calls `postExecution` to
|
|
27163
|
-
* triggering it to fill the intent.
|
|
27164
|
-
* - **Backend 2-step (
|
|
27248
|
+
* the source spoke chain; completion then runs via one of two paths, each bounded by its own `timeout`
|
|
27249
|
+
* budget:
|
|
27250
|
+
*
|
|
27251
|
+
* - **Client-side (opt-out via `swaps.useBackendSubmitTx: false`), {@link fallbackSwapSteps}:** verifies
|
|
27252
|
+
* the spoke tx landed on-chain, relays it to the hub (Sonic) and waits for the packet — skipped when
|
|
27253
|
+
* `srcChainKey` is the hub, where the spoke tx already is the hub tx — then calls `postExecution` to
|
|
27254
|
+
* notify the solver, triggering it to fill the intent.
|
|
27255
|
+
* - **Backend 2-step (default via `swaps.useBackendSubmitTx`), {@link submitTx}:** hands the
|
|
27165
27256
|
* broadcast tx to the swaps API, which verifies, relays and post-executes server-side, then polls
|
|
27166
27257
|
* for completion. On ANY non-success it transparently falls back to the client-side path above —
|
|
27167
27258
|
* safe because re-relaying / re-posting an already-processed swap is idempotent (no double-fill).
|
|
27168
27259
|
*
|
|
27169
27260
|
* @param _params - Swap action params including intent parameters, wallet provider, and an optional
|
|
27170
|
-
* `timeout` —
|
|
27171
|
-
*
|
|
27261
|
+
* `timeout` — a PER-ATTEMPT budget, not an end-to-end one. The backend attempt (submit POST + status
|
|
27262
|
+
* poll) gets it, and if that attempt does not complete the client-side relay wait gets a fresh one
|
|
27263
|
+
* starting after verification, so neither a stalled backend nor a slow source-chain confirmation can
|
|
27264
|
+
* shorten it. Worst-case wall-clock is `createIntent + timeout + verification +
|
|
27265
|
+
* max(timeout, RELAY_FALLBACK_FLOOR_MS) + postExecution`, where verification is bounded by the source
|
|
27266
|
+
* chain's `pollingConfig.maxTimeoutMs` and the first and last terms are not bounded by `timeout` at
|
|
27267
|
+
* all. See `docs/SWAPS.md` § How `timeout` bounds each attempt.
|
|
27172
27268
|
* @returns A `Result<SwapResponse, SwapError>`. On success:
|
|
27173
27269
|
* - `solverExecutionResponse` — solver acknowledgement (`{ answer: 'OK', intent_hash }`).
|
|
27174
27270
|
* - `intent` — the on-chain intent object that was created.
|
|
@@ -27202,9 +27298,9 @@ var SwapService = class {
|
|
|
27202
27298
|
return { ok: false, error: createIntentResult.error };
|
|
27203
27299
|
}
|
|
27204
27300
|
const created = createIntentResult.value;
|
|
27205
|
-
const
|
|
27301
|
+
const timeoutMs = resolveTimeoutMs(_params.timeout, DEFAULT_RELAY_TX_TIMEOUT);
|
|
27206
27302
|
if (this.useBackendSubmitTx) {
|
|
27207
|
-
const submitted = await this.submitTx(_params, created,
|
|
27303
|
+
const submitted = await this.submitTx(_params, created, createSubmitTxAttempt(timeoutMs));
|
|
27208
27304
|
if (submitted.ok) return submitted;
|
|
27209
27305
|
this.config.logger.warn(
|
|
27210
27306
|
"[swap] backend submit-tx did not complete; falling back to the client-side relay",
|
|
@@ -27213,7 +27309,7 @@ var SwapService = class {
|
|
|
27213
27309
|
}
|
|
27214
27310
|
);
|
|
27215
27311
|
}
|
|
27216
|
-
return this.fallbackSwapSteps(_params, created,
|
|
27312
|
+
return this.fallbackSwapSteps(_params, created, timeoutMs);
|
|
27217
27313
|
} catch (error) {
|
|
27218
27314
|
if (isSwapError(error)) return { ok: false, error };
|
|
27219
27315
|
return {
|
|
@@ -27240,12 +27336,16 @@ var SwapService = class {
|
|
|
27240
27336
|
);
|
|
27241
27337
|
}
|
|
27242
27338
|
/**
|
|
27243
|
-
* Client-side swap completion (
|
|
27244
|
-
* use it directly when the source IS the hub — then notify the
|
|
27245
|
-
* build the {@link SwapResponse}. Extracted verbatim from `swap()` so the
|
|
27246
|
-
* path ({@link submitTx}) can fall back to it on any non-success.
|
|
27339
|
+
* Client-side swap completion (opt-out via `swaps.useBackendSubmitTx: false`): verify the broadcast intent
|
|
27340
|
+
* tx landed, then relay it to the hub — or use it directly when the source IS the hub — then notify the
|
|
27341
|
+
* solver via post-execution and build the {@link SwapResponse}. Extracted verbatim from `swap()` so the
|
|
27342
|
+
* backend 2-step path ({@link submitTx}) can fall back to it on any non-success.
|
|
27343
|
+
*
|
|
27344
|
+
* Verification belongs to THIS path only: the backend runs its own, so verifying before handing over
|
|
27345
|
+
* would delay every backend success by the source chain's confirmation wait and could fail a swap the
|
|
27346
|
+
* backend would have completed.
|
|
27247
27347
|
*/
|
|
27248
|
-
async fallbackSwapSteps(_params, created,
|
|
27348
|
+
async fallbackSwapSteps(_params, created, timeoutMs) {
|
|
27249
27349
|
const { params } = _params;
|
|
27250
27350
|
const srcChainKey = params.srcChainKey;
|
|
27251
27351
|
const baseCtx = { srcChainKey, dstChainKey: params.dstChainKey };
|
|
@@ -27266,9 +27366,12 @@ var SwapService = class {
|
|
|
27266
27366
|
data: relayData,
|
|
27267
27367
|
chainKey: srcChainKey,
|
|
27268
27368
|
relayerApiEndpoint: this.relayerApiEndpoint,
|
|
27269
|
-
//
|
|
27270
|
-
//
|
|
27271
|
-
|
|
27369
|
+
// The caller's full `timeout`, starting HERE — after verification, and whether this runs as the
|
|
27370
|
+
// only path or as the backend's fallback. Neither a stalled backend attempt nor a slow source-chain
|
|
27371
|
+
// confirmation may shorten the relay wait. The floor covers a sub-floor caller `timeout`:
|
|
27372
|
+
// `relayTxAndWaitPacket` SUBMITS before `timeout` bounds anything, so a zero budget would strand an
|
|
27373
|
+
// already-landed tx unrelayed. Re-relay is idempotent, so spending it is safe.
|
|
27374
|
+
timeout: Math.max(timeoutMs, RELAY_FALLBACK_FLOOR_MS)
|
|
27272
27375
|
});
|
|
27273
27376
|
if (!packet.ok) {
|
|
27274
27377
|
return { ok: false, error: mapRelayFailure(packet.error, { feature: "swap", action: "swap", ...baseCtx }) };
|
|
@@ -27298,7 +27401,7 @@ var SwapService = class {
|
|
|
27298
27401
|
};
|
|
27299
27402
|
}
|
|
27300
27403
|
/**
|
|
27301
|
-
* Backend 2-step swap path (
|
|
27404
|
+
* Backend 2-step swap path (default via `swaps.useBackendSubmitTx`): hand the broadcast
|
|
27302
27405
|
* intent tx to the swaps API (`POST /swaps/submit-tx`); the backend relays + post-executes
|
|
27303
27406
|
* server-side. Polls `getSubmitTxStatus` until `solved`, then reconstructs the same
|
|
27304
27407
|
* {@link SwapResponse} the client-side path returns (`result.dstIntentTxHash` → delivery info,
|
|
@@ -27309,32 +27412,36 @@ var SwapService = class {
|
|
|
27309
27412
|
*
|
|
27310
27413
|
* Falling back is safe: re-relaying / re-posting an already-processed swap is idempotent — the
|
|
27311
27414
|
* relay dedups and returns the existing `executed` packet, and the solver re-affirms the intent
|
|
27312
|
-
* (no double-fill). Verified live by `e2e-tests/e2e-relay.test.ts`.
|
|
27313
|
-
*
|
|
27415
|
+
* (no double-fill). Verified live by `e2e-tests/e2e-relay.test.ts`. It is also load-bearing rather
|
|
27416
|
+
* than belt-and-braces: the backend keeps processing after this attempt gives up, so the fallback's
|
|
27417
|
+
* relay can race the backend's own.
|
|
27418
|
+
*
|
|
27419
|
+
* The attempt itself — POST, budget clamps, status poll — is {@link runBackendSubmitTx}, shared with
|
|
27420
|
+
* bridge. What is swap-specific and lives here: the request body, the `solved` terminal status, the
|
|
27421
|
+
* mapping from a terminal result to a {@link SwapResponse}, and the swap error taxonomy.
|
|
27422
|
+
*
|
|
27423
|
+
* `attempt` bounds this attempt alone — the POST and every status request draw on it, and the
|
|
27424
|
+
* client-side fallback holds a separate fresh `timeout`.
|
|
27314
27425
|
*/
|
|
27315
|
-
async submitTx(_params, created,
|
|
27426
|
+
async submitTx(_params, created, attempt) {
|
|
27316
27427
|
const { params } = _params;
|
|
27317
27428
|
const srcChainKey = params.srcChainKey;
|
|
27318
27429
|
const baseCtx = { srcChainKey, dstChainKey: params.dstChainKey };
|
|
27319
27430
|
const { tx: spokeTxHash, intent, relayData } = created;
|
|
27320
|
-
const submitTxFailed = (cause) => ({
|
|
27321
|
-
ok: false,
|
|
27322
|
-
error: executionFailed("swap", cause, { ...baseCtx, action: "swap" })
|
|
27323
|
-
});
|
|
27324
27431
|
try {
|
|
27325
|
-
const
|
|
27326
|
-
|
|
27327
|
-
|
|
27328
|
-
|
|
27329
|
-
|
|
27330
|
-
|
|
27331
|
-
|
|
27332
|
-
|
|
27333
|
-
|
|
27334
|
-
|
|
27432
|
+
const outcome = await runBackendSubmitTx({
|
|
27433
|
+
attempt,
|
|
27434
|
+
api: this.backendApi.swaps,
|
|
27435
|
+
body: {
|
|
27436
|
+
txHash: spokeTxHash,
|
|
27437
|
+
srcChainKey,
|
|
27438
|
+
walletAddress: params.srcAddress,
|
|
27439
|
+
intent,
|
|
27440
|
+
relayData: relayData.payload
|
|
27441
|
+
},
|
|
27442
|
+
statusQuery: { txHash: spokeTxHash, srcChainKey },
|
|
27335
27443
|
// Swaps terminal success is `solved` (solver filled) — not the bridge `executed`.
|
|
27336
27444
|
terminalStatus: "solved",
|
|
27337
|
-
getStatus: () => this.backendApi.swaps.getSubmitTxStatus({ txHash: spokeTxHash, srcChainKey }),
|
|
27338
27445
|
onExecuted: (result) => result?.dstIntentTxHash && result.intent_hash ? {
|
|
27339
27446
|
// Backend serializes the hex intent_hash as a plain string; brand it at the boundary.
|
|
27340
27447
|
solverExecutionResponse: { answer: "OK", intent_hash: result.intent_hash },
|
|
@@ -27349,7 +27456,7 @@ var SwapService = class {
|
|
|
27349
27456
|
}
|
|
27350
27457
|
} : void 0
|
|
27351
27458
|
});
|
|
27352
|
-
return
|
|
27459
|
+
return outcome.ok ? { ok: true, value: outcome.value } : { ok: false, error: executionFailed("swap", outcome.cause, { ...baseCtx, action: "swap" }) };
|
|
27353
27460
|
} catch (error) {
|
|
27354
27461
|
return { ok: false, error: unknownFailed("swap", error, { ...baseCtx, action: "swap" }) };
|
|
27355
27462
|
}
|
|
@@ -30198,7 +30305,6 @@ async function makeRequest(params) {
|
|
|
30198
30305
|
body: config.body,
|
|
30199
30306
|
signal: controller.signal
|
|
30200
30307
|
});
|
|
30201
|
-
clearTimeout(timeoutId);
|
|
30202
30308
|
if (!response.ok) {
|
|
30203
30309
|
const errorText = await response.text();
|
|
30204
30310
|
throw new BackendHttpError(response.status, errorText);
|
|
@@ -30206,7 +30312,6 @@ async function makeRequest(params) {
|
|
|
30206
30312
|
const data = await response.json();
|
|
30207
30313
|
return data;
|
|
30208
30314
|
} catch (error) {
|
|
30209
|
-
clearTimeout(timeoutId);
|
|
30210
30315
|
if (error instanceof Error) {
|
|
30211
30316
|
if (error.name === "AbortError") {
|
|
30212
30317
|
throw new Error("REQUEST_TIMEOUT", { cause: new Error(`Request timeout after ${timeout}ms`) });
|
|
@@ -30216,6 +30321,8 @@ async function makeRequest(params) {
|
|
|
30216
30321
|
}
|
|
30217
30322
|
logger.error(`[${serviceLabel}] Unknown error`, error);
|
|
30218
30323
|
throw new Error("UNKNOWN_REQUEST_ERROR", { cause: error });
|
|
30324
|
+
} finally {
|
|
30325
|
+
clearTimeout(timeoutId);
|
|
30219
30326
|
}
|
|
30220
30327
|
}
|
|
30221
30328
|
function resolveRequestConfig(config, defaults) {
|
|
@@ -30420,7 +30527,9 @@ var SwapsApiService = class _SwapsApiService {
|
|
|
30420
30527
|
// ──────────────────────────────────────────────────────────────────────
|
|
30421
30528
|
// Submit-tx state machine
|
|
30422
30529
|
// ──────────────────────────────────────────────────────────────────────
|
|
30423
|
-
/** Submit a swap transaction to be processed (relay, post-execution, etc.). Idempotent on `(txHash, srcChainKey)`.
|
|
30530
|
+
/** Submit a swap transaction to be processed (relay, post-execution, etc.). Idempotent on `(txHash, srcChainKey)`.
|
|
30531
|
+
* Response is immediate after successful submission. Use getSubmitTxStatus to poll the status of the submission.
|
|
30532
|
+
*/
|
|
30424
30533
|
async submitTx(body, config) {
|
|
30425
30534
|
return this.toResult(
|
|
30426
30535
|
"/swaps/submit-tx",
|
|
@@ -30449,6 +30558,18 @@ var SwapsApiService = class _SwapsApiService {
|
|
|
30449
30558
|
getBaseURL() {
|
|
30450
30559
|
return this.config.baseURL;
|
|
30451
30560
|
}
|
|
30561
|
+
/**
|
|
30562
|
+
* Return the effective per-request timeout (ms) — the same value {@link buildClient} resolves, so
|
|
30563
|
+
* `SwapsApiConfig.timeout` being optional never leaks an `undefined` ceiling.
|
|
30564
|
+
*
|
|
30565
|
+
* Callers bounding a request tighter than this need it as the CEILING, because a
|
|
30566
|
+
* `RequestOverrideConfig.timeout` REPLACES the service value rather than lowering it: an override
|
|
30567
|
+
* derived from a caller budget alone would raise the bound whenever that budget is the larger of the
|
|
30568
|
+
* two. `SubmitTxAttempt.requestTimeout` clamps against both (`min(budget left in the attempt, this)`).
|
|
30569
|
+
*/
|
|
30570
|
+
getTimeout() {
|
|
30571
|
+
return this.config.timeout ?? DEFAULT_BACKEND_API_TIMEOUT;
|
|
30572
|
+
}
|
|
30452
30573
|
};
|
|
30453
30574
|
var StellarSponsorConfigSchema = v7.object({
|
|
30454
30575
|
sponsorAccount: v7.string(),
|
|
@@ -30935,9 +31056,12 @@ var BridgeApiService = class {
|
|
|
30935
31056
|
return this.config.baseURL;
|
|
30936
31057
|
}
|
|
30937
31058
|
/**
|
|
30938
|
-
* Return the effective per-request timeout (ms).
|
|
30939
|
-
*
|
|
30940
|
-
*
|
|
31059
|
+
* Return the effective per-request timeout (ms).
|
|
31060
|
+
*
|
|
31061
|
+
* Callers bounding a request tighter than this need it as the CEILING, because a
|
|
31062
|
+
* `RequestOverrideConfig.timeout` REPLACES the service value rather than lowering it: an override
|
|
31063
|
+
* derived from a caller budget alone would raise the bound whenever that budget is the larger of the
|
|
31064
|
+
* two. `SubmitTxAttempt.requestTimeout` clamps against both (`min(budget left in the attempt, this)`).
|
|
30941
31065
|
*/
|
|
30942
31066
|
getTimeout() {
|
|
30943
31067
|
return this.config.timeout;
|
|
@@ -31549,15 +31673,20 @@ var BridgeService = class {
|
|
|
31549
31673
|
hubProvider;
|
|
31550
31674
|
config;
|
|
31551
31675
|
spoke;
|
|
31552
|
-
// backend bridge-API client
|
|
31676
|
+
// backend bridge-API client
|
|
31553
31677
|
backendApi;
|
|
31554
|
-
|
|
31555
|
-
|
|
31678
|
+
/**
|
|
31679
|
+
* Effective backend submit-tx flow (`bridge.useBackendSubmitTx`, default on). Read live off
|
|
31680
|
+
* `ConfigService` like `bridgePartnerFee`, so the config object and the behavior can never disagree.
|
|
31681
|
+
*/
|
|
31682
|
+
get useBackendSubmitTx() {
|
|
31683
|
+
return this.config.bridgeUseBackendSubmitTx;
|
|
31684
|
+
}
|
|
31685
|
+
constructor({ hubProvider, config, spoke, backendApi }) {
|
|
31556
31686
|
this.config = config;
|
|
31557
31687
|
this.hubProvider = hubProvider;
|
|
31558
31688
|
this.spoke = spoke;
|
|
31559
31689
|
this.backendApi = backendApi;
|
|
31560
|
-
this.useBackendSubmitTx = useBackendSubmitTx ?? false;
|
|
31561
31690
|
}
|
|
31562
31691
|
/**
|
|
31563
31692
|
* Calculates the partner fee deducted from a given bridge input amount.
|
|
@@ -31722,15 +31851,27 @@ var BridgeService = class {
|
|
|
31722
31851
|
/**
|
|
31723
31852
|
* Executes a full end-to-end bridge transfer: spoke deposit → relay → hub settlement.
|
|
31724
31853
|
*
|
|
31725
|
-
* Internally calls `createBridgeIntent()` to submit the spoke-side deposit transaction,
|
|
31726
|
-
*
|
|
31727
|
-
*
|
|
31854
|
+
* Internally calls `createBridgeIntent()` to submit the spoke-side deposit transaction, then
|
|
31855
|
+
* completes the transfer via one of two paths. Use this method for the typical "fire and wait" UX.
|
|
31856
|
+
*
|
|
31857
|
+
* - **Backend submit-tx (default, `bridge.useBackendSubmitTx`), {@link submitTx}:** hands the deposit
|
|
31858
|
+
* to the bridge API, which relays server-side, and polls until `executed`. On ANY non-success it
|
|
31859
|
+
* falls back to the path below — safe because re-relaying is idempotent.
|
|
31860
|
+
* - **Client-side (opt-out with `useBackendSubmitTx: false`), {@link fallbackBridgeSteps}:** verifies
|
|
31861
|
+
* the deposit landed on-chain, then relays it and waits for the settlement packet. `verifyTxHash`
|
|
31862
|
+
* runs on this path only; the backend runs its own.
|
|
31728
31863
|
*
|
|
31729
31864
|
* For manual relay control (e.g. monitoring or batching), use `createBridgeIntent()` directly
|
|
31730
31865
|
* and handle the relay step yourself.
|
|
31731
31866
|
*
|
|
31732
31867
|
* @param _params - Bridge parameters including source/destination chain keys, token addresses,
|
|
31733
|
-
* amount, recipient address, wallet provider, and optional timeout
|
|
31868
|
+
* amount, recipient address, wallet provider, and an optional `timeout` — a PER-ATTEMPT budget,
|
|
31869
|
+
* not an end-to-end one. The backend attempt gets it, and if that attempt does not complete the
|
|
31870
|
+
* client-side relay wait gets a fresh one starting after verification, so neither a stalled backend
|
|
31871
|
+
* nor a slow source-chain confirmation can shorten it. Worst-case wall-clock is
|
|
31872
|
+
* `createBridgeIntent + timeout + verification + max(timeout, RELAY_FALLBACK_FLOOR_MS)`, where
|
|
31873
|
+
* verification is bounded by the source chain's `pollingConfig.maxTimeoutMs` and intent creation is
|
|
31874
|
+
* not bounded by `timeout` at all. See `docs/BRIDGE.md` § Completion paths and `timeout`.
|
|
31734
31875
|
* @returns `Result<TxHashPair>` — `{ srcChainTxHash, dstChainTxHash }` on success,
|
|
31735
31876
|
* where `srcChainTxHash` is the spoke deposit tx and `dstChainTxHash` is the hub settlement tx.
|
|
31736
31877
|
*
|
|
@@ -31764,16 +31905,16 @@ var BridgeService = class {
|
|
|
31764
31905
|
try {
|
|
31765
31906
|
const created = await this.createBridgeIntent(_params);
|
|
31766
31907
|
if (!created.ok) return { ok: false, error: created.error };
|
|
31767
|
-
const
|
|
31908
|
+
const timeoutMs = resolveTimeoutMs(_params.timeout, DEFAULT_RELAY_TX_TIMEOUT);
|
|
31768
31909
|
if (this.useBackendSubmitTx) {
|
|
31769
|
-
const submitted = await this.submitTx(_params, created.value,
|
|
31910
|
+
const submitted = await this.submitTx(_params, created.value, createSubmitTxAttempt(timeoutMs));
|
|
31770
31911
|
if (submitted.ok) return submitted;
|
|
31771
31912
|
this.config.logger.warn(
|
|
31772
31913
|
"[bridge] backend submit-tx did not complete; falling back to the client-side relay",
|
|
31773
31914
|
{ error: submitted.error }
|
|
31774
31915
|
);
|
|
31775
31916
|
}
|
|
31776
|
-
return this.fallbackBridgeSteps(_params, created.value,
|
|
31917
|
+
return this.fallbackBridgeSteps(_params, created.value, timeoutMs);
|
|
31777
31918
|
} catch (error) {
|
|
31778
31919
|
if (isBridgeOrchestrationError(error)) return { ok: false, error };
|
|
31779
31920
|
return {
|
|
@@ -31801,12 +31942,16 @@ var BridgeService = class {
|
|
|
31801
31942
|
);
|
|
31802
31943
|
}
|
|
31803
31944
|
/**
|
|
31804
|
-
* Client-side bridge completion (
|
|
31805
|
-
* relay it to the hub and wait for the settlement packet. Extracted verbatim from
|
|
31806
|
-
*
|
|
31807
|
-
* always relays — there is no hub-source short-circuit.
|
|
31945
|
+
* Client-side bridge completion (opt-out via `bridge.useBackendSubmitTx: false`): verify the broadcast
|
|
31946
|
+
* spoke-deposit tx, then relay it to the hub and wait for the settlement packet. Extracted verbatim from
|
|
31947
|
+
* `bridge()` so the backend submit-tx path ({@link submitTx}) can fall back to it on any non-success.
|
|
31948
|
+
* Bridge always relays — there is no hub-source short-circuit.
|
|
31949
|
+
*
|
|
31950
|
+
* Verification belongs to THIS path only: the backend runs its own, so verifying before handing over
|
|
31951
|
+
* would delay every backend success by the source chain's confirmation wait and could fail a bridge the
|
|
31952
|
+
* backend would have completed.
|
|
31808
31953
|
*/
|
|
31809
|
-
async fallbackBridgeSteps(_params, created,
|
|
31954
|
+
async fallbackBridgeSteps(_params, created, timeoutMs) {
|
|
31810
31955
|
const { params } = _params;
|
|
31811
31956
|
const baseCtx = { srcChainKey: params.srcChainKey, dstChainKey: params.dstChainKey };
|
|
31812
31957
|
const verifyTxHashResult = await this.spoke.verifyTxHash({
|
|
@@ -31821,13 +31966,13 @@ var BridgeService = class {
|
|
|
31821
31966
|
data: created.relayData,
|
|
31822
31967
|
chainKey: params.srcChainKey,
|
|
31823
31968
|
relayerApiEndpoint: this.config.relay.relayerApiEndpoint,
|
|
31824
|
-
//
|
|
31825
|
-
// or the
|
|
31826
|
-
//
|
|
31827
|
-
//
|
|
31828
|
-
//
|
|
31829
|
-
//
|
|
31830
|
-
timeout: Math.max(
|
|
31969
|
+
// The caller's full `timeout`, starting HERE — after verification, and whether this runs as the only
|
|
31970
|
+
// path or as the backend's fallback. Neither a stalled backend attempt nor a slow source-chain
|
|
31971
|
+
// confirmation (Stacks polls for up to its full 120s `maxTimeoutMs`) may shorten the relay wait. The
|
|
31972
|
+
// floor covers a sub-floor caller `timeout`: `relayTxAndWaitPacket` SUBMITS the tx to the relay
|
|
31973
|
+
// before `timeout` bounds anything, so a zero budget would strand an already-landed deposit
|
|
31974
|
+
// unrelayed. Re-relay is idempotent, so always spending the floor is safe.
|
|
31975
|
+
timeout: Math.max(timeoutMs, RELAY_FALLBACK_FLOOR_MS)
|
|
31831
31976
|
});
|
|
31832
31977
|
if (!packetResult.ok) {
|
|
31833
31978
|
return {
|
|
@@ -31846,7 +31991,7 @@ var BridgeService = class {
|
|
|
31846
31991
|
};
|
|
31847
31992
|
}
|
|
31848
31993
|
/**
|
|
31849
|
-
* Backend bridge path (
|
|
31994
|
+
* Backend bridge path (default via `bridge.useBackendSubmitTx`): hand the broadcast
|
|
31850
31995
|
* spoke-deposit tx to the bridge API (`POST /bridge/submit-tx`) with the FULL `relayData`
|
|
31851
31996
|
* envelope; the backend relays server-side. Polls `getSubmitTxStatus` until `executed`, then
|
|
31852
31997
|
* returns the same {@link TxHashPair} the client-side path returns (`result.dstIntentTxHash` →
|
|
@@ -31855,39 +32000,39 @@ var BridgeService = class {
|
|
|
31855
32000
|
* Never throws — returns `{ ok: false }` on any non-success (submit `!ok`, terminal `failed` /
|
|
31856
32001
|
* abandoned, or poll timeout) so `bridge()` falls back to {@link fallbackBridgeSteps}. Falling back
|
|
31857
32002
|
* is safe: re-relaying an already-relayed bridge tx is idempotent — the relay dedups and returns the
|
|
31858
|
-
* existing `executed` packet.
|
|
31859
|
-
*
|
|
32003
|
+
* existing `executed` packet. It is also load-bearing rather than belt-and-braces: the backend keeps
|
|
32004
|
+
* processing after this attempt gives up, so the fallback's relay can race the backend's own.
|
|
32005
|
+
*
|
|
32006
|
+
* The attempt itself — POST, budget clamps, status poll — is {@link runBackendSubmitTx}, shared with
|
|
32007
|
+
* swaps. What is bridge-specific and lives here: the request body (the FULL `relayData` envelope), the
|
|
32008
|
+
* `executed` terminal status, the mapping to a {@link TxHashPair}, and the bridge error taxonomy.
|
|
32009
|
+
*
|
|
32010
|
+
* `attempt` bounds this attempt alone — the POST and every status request draw on it, and the
|
|
32011
|
+
* client-side fallback holds a separate fresh `timeout`.
|
|
31860
32012
|
*/
|
|
31861
|
-
async submitTx(_params, created,
|
|
32013
|
+
async submitTx(_params, created, attempt) {
|
|
31862
32014
|
const { params } = _params;
|
|
31863
32015
|
const srcChainKey = params.srcChainKey;
|
|
31864
32016
|
const baseCtx = { srcChainKey, dstChainKey: params.dstChainKey };
|
|
31865
32017
|
const { tx: spokeTxHash, relayData } = created;
|
|
31866
|
-
const submitTxFailed = (cause) => ({
|
|
31867
|
-
ok: false,
|
|
31868
|
-
error: executionFailed("bridge", cause, { ...baseCtx, action: "bridge" })
|
|
31869
|
-
});
|
|
31870
32018
|
try {
|
|
31871
|
-
const
|
|
31872
|
-
|
|
31873
|
-
|
|
31874
|
-
|
|
31875
|
-
|
|
31876
|
-
|
|
31877
|
-
|
|
31878
|
-
|
|
31879
|
-
|
|
31880
|
-
|
|
31881
|
-
|
|
32019
|
+
const outcome = await runBackendSubmitTx({
|
|
32020
|
+
attempt,
|
|
32021
|
+
api: this.backendApi.bridge,
|
|
32022
|
+
body: {
|
|
32023
|
+
txHash: spokeTxHash,
|
|
32024
|
+
srcChainKey,
|
|
32025
|
+
walletAddress: params.srcAddress,
|
|
32026
|
+
// Send the FULL { address, payload } envelope — bridge has no intent.creator for the backend
|
|
32027
|
+
// to rebuild the relay address, so dropping it would break split-tx-chain relay.
|
|
32028
|
+
relayData
|
|
32029
|
+
},
|
|
32030
|
+
statusQuery: { txHash: spokeTxHash, srcChainKey },
|
|
31882
32031
|
// Bridge terminal success is `executed` (hub-settled) — not the swaps `solved`.
|
|
31883
32032
|
terminalStatus: "executed",
|
|
31884
|
-
|
|
31885
|
-
onExecuted: (result) => result?.dstIntentTxHash ? { srcChainTxHash: spokeTxHash, dstChainTxHash: result.dstIntentTxHash } : void 0,
|
|
31886
|
-
// Bound each status request by the poll cutoff, never above the service default — so a stalled
|
|
31887
|
-
// request can neither eat the fallback's reserve nor consume the window in one attempt.
|
|
31888
|
-
requestTimeoutMs: this.backendApi.bridge.getTimeout()
|
|
32033
|
+
onExecuted: (result) => result?.dstIntentTxHash ? { srcChainTxHash: spokeTxHash, dstChainTxHash: result.dstIntentTxHash } : void 0
|
|
31889
32034
|
});
|
|
31890
|
-
return
|
|
32035
|
+
return outcome.ok ? { ok: true, value: outcome.value } : { ok: false, error: executionFailed("bridge", outcome.cause, { ...baseCtx, action: "bridge" }) };
|
|
31891
32036
|
} catch (error) {
|
|
31892
32037
|
return { ok: false, error: unknownFailed("bridge", error, { ...baseCtx, action: "bridge" }) };
|
|
31893
32038
|
}
|
|
@@ -38061,15 +38206,25 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
38061
38206
|
config;
|
|
38062
38207
|
spoke;
|
|
38063
38208
|
// money market config (hoisted from config for ergonomics, mirrors SwapService)
|
|
38064
|
-
partnerFee;
|
|
38065
38209
|
relayerApiEndpoint;
|
|
38066
38210
|
// sub-service
|
|
38067
38211
|
data;
|
|
38212
|
+
/**
|
|
38213
|
+
* Effective money-market partner fee (`moneyMarket.partnerFee`, else the global `fee`). Read live
|
|
38214
|
+
* off `ConfigService` rather than snapshotted in the constructor, so it cannot diverge from
|
|
38215
|
+
* `config.moneyMarketPartnerFee` if the config object is ever replaced. Mirrors
|
|
38216
|
+
* {@link SwapService.partnerFee}.
|
|
38217
|
+
*
|
|
38218
|
+
* Money market has no per-action override: unlike swap / bridge / leverage-yield, every flow here
|
|
38219
|
+
* charges this configured fee.
|
|
38220
|
+
*/
|
|
38221
|
+
get partnerFee() {
|
|
38222
|
+
return this.config.moneyMarketPartnerFee;
|
|
38223
|
+
}
|
|
38068
38224
|
constructor({ config, hubProvider, spoke }) {
|
|
38069
38225
|
this.config = config;
|
|
38070
38226
|
this.hubProvider = hubProvider;
|
|
38071
38227
|
this.spoke = spoke;
|
|
38072
|
-
this.partnerFee = config.moneyMarketPartnerFee;
|
|
38073
38228
|
this.relayerApiEndpoint = config.relay.relayerApiEndpoint;
|
|
38074
38229
|
this.data = new MoneyMarketDataService({ hubProvider, config });
|
|
38075
38230
|
}
|
|
@@ -40382,6 +40537,68 @@ var LeverageYieldService = class {
|
|
|
40382
40537
|
return { ok: false, error: lookupFailed("leverageYield", "resolveDeadline", error, { srcChainKey }) };
|
|
40383
40538
|
}
|
|
40384
40539
|
}
|
|
40540
|
+
/**
|
|
40541
|
+
* Quotes a vault deposit or withdraw. Vault shares are solver-tradeable, so this is the
|
|
40542
|
+
* generic solver quote with one difference that matters: the fee deducted before quoting is
|
|
40543
|
+
* the effective **leverage-yield** fee, matching what {@link LeverageYieldService.createVaultIntent}
|
|
40544
|
+
* will charge. `sodax.swaps.getQuote` deducts the effective *swap* fee instead, so quoting a
|
|
40545
|
+
* vault flow through it makes the quote and the intent disagree whenever the two feature fees
|
|
40546
|
+
* differ. Quoting a vault flow through the swap service can be made to agree — pass the same fee
|
|
40547
|
+
* explicitly, using a zero fee (`{ address, percentage: 0 }`) where the effective leverage-yield
|
|
40548
|
+
* fee is `undefined`, since an explicit `undefined` there falls back to the swap fee — but this
|
|
40549
|
+
* method resolves it for you and is the canonical way to quote a vault flow.
|
|
40550
|
+
*
|
|
40551
|
+
* Pass the vault address as `token_dst` to quote a deposit, or as `token_src` to quote a
|
|
40552
|
+
* withdraw; subtract your slippage tolerance from `quoted_amount` to get `minOutputAmount`.
|
|
40553
|
+
* When overriding the fee per intent, pass the same `partnerFee` here and to whichever builder
|
|
40554
|
+
* you use ({@link LeverageYieldService.deposit}, {@link LeverageYieldService.withdraw}) or
|
|
40555
|
+
* directly to {@link LeverageYieldService.vaultSwap} — omitting it everywhere is equally safe,
|
|
40556
|
+
* since each side then resolves the same effective leverage-yield fee. Both directions are
|
|
40557
|
+
* charged, so this applies to withdrawals as much as deposits.
|
|
40558
|
+
*
|
|
40559
|
+
* @returns `SolverIntentQuoteResponse` on success. On failure `result.error` is either the
|
|
40560
|
+
* solver's own `SolverErrorResponse` (no path, insufficient liquidity, …) or a SodaxError
|
|
40561
|
+
* with `VALIDATION_FAILED` (bad `amount`, or a partner fee that leaves nothing to quote),
|
|
40562
|
+
* `LOOKUP_FAILED` (unsupported token — the solver payload could not be assembled) or
|
|
40563
|
+
* `UNKNOWN`. Discriminate with `isSodaxError(error)`.
|
|
40564
|
+
*/
|
|
40565
|
+
async getQuote(payload) {
|
|
40566
|
+
const { partnerFee = this.config.leverageYieldPartnerFee, ...request } = payload;
|
|
40567
|
+
const baseCtx = {
|
|
40568
|
+
method: "getQuote",
|
|
40569
|
+
tokenSrcChainKey: request.token_src_blockchain_id,
|
|
40570
|
+
tokenDstChainKey: request.token_dst_blockchain_id
|
|
40571
|
+
};
|
|
40572
|
+
try {
|
|
40573
|
+
leverageYieldInvariant(request.amount > 0n, "amount must be greater than 0", {
|
|
40574
|
+
...baseCtx,
|
|
40575
|
+
field: "amount"
|
|
40576
|
+
});
|
|
40577
|
+
if (isPartnerFeeAmount(partnerFee)) {
|
|
40578
|
+
leverageYieldInvariant(
|
|
40579
|
+
partnerFee.amount < request.amount,
|
|
40580
|
+
`partnerFee amount (${partnerFee.amount}) must be less than the quote amount (${request.amount})`,
|
|
40581
|
+
{ ...baseCtx, field: "partnerFee" }
|
|
40582
|
+
);
|
|
40583
|
+
} else if (isPartnerFeePercentage(partnerFee)) {
|
|
40584
|
+
leverageYieldInvariant(
|
|
40585
|
+
Number.isInteger(partnerFee.percentage) && partnerFee.percentage >= 0 && partnerFee.percentage <= Number(FEE_PERCENTAGE_SCALE),
|
|
40586
|
+
`partnerFee percentage must be a whole number of basis points between 0 and ${FEE_PERCENTAGE_SCALE} (got ${partnerFee.percentage})`,
|
|
40587
|
+
{ ...baseCtx, field: "partnerFee" }
|
|
40588
|
+
);
|
|
40589
|
+
}
|
|
40590
|
+
const netAmount = adjustAmountByFee(request.amount, partnerFee, request.quote_type);
|
|
40591
|
+
leverageYieldInvariant(netAmount > 0n, "amount net of the partner fee must be greater than 0", {
|
|
40592
|
+
...baseCtx,
|
|
40593
|
+
field: "partnerFee"
|
|
40594
|
+
});
|
|
40595
|
+
const adjustedPayload = { ...request, amount: netAmount };
|
|
40596
|
+
return await SolverApiService.getQuote(adjustedPayload, this.config.solver, this.config);
|
|
40597
|
+
} catch (error) {
|
|
40598
|
+
if (isLeverageYieldLookupError(error)) return { ok: false, error };
|
|
40599
|
+
return { ok: false, error: lookupFailed("leverageYield", "getQuote", error, baseCtx) };
|
|
40600
|
+
}
|
|
40601
|
+
}
|
|
40385
40602
|
/**
|
|
40386
40603
|
* Builds the {@link LeverageYieldSwapPayload} for a leverage-yield deposit (any token → lsoda*).
|
|
40387
40604
|
* The lsoda* output is delivered to the user's hub wallet on Sonic so a later
|
|
@@ -40389,9 +40606,8 @@ var LeverageYieldService = class {
|
|
|
40389
40606
|
* {@link LeverageYieldService.vaultSwap}: `vaultSwap({ ...payload, walletProvider })`.
|
|
40390
40607
|
* An optional `partnerFee` is forwarded on the payload as the per-intent fee override.
|
|
40391
40608
|
*
|
|
40392
|
-
* For `minOutputAmount`, quote via
|
|
40393
|
-
* destination token (`token_dst`)
|
|
40394
|
-
* quote applies; then subtract your slippage tolerance.
|
|
40609
|
+
* For `minOutputAmount`, quote via {@link LeverageYieldService.getQuote} with the vault
|
|
40610
|
+
* address as the destination token (`token_dst`), then subtract your slippage tolerance.
|
|
40395
40611
|
*/
|
|
40396
40612
|
async deposit(params) {
|
|
40397
40613
|
const baseCtx = { srcChainKey: params.srcChainKey, action: "deposit" };
|
|
@@ -40444,9 +40660,13 @@ var LeverageYieldService = class {
|
|
|
40444
40660
|
* a call shape uniform with {@link LeverageYieldService.deposit}; async because the default
|
|
40445
40661
|
* `deadline` is read from the hub block timestamp.
|
|
40446
40662
|
*
|
|
40447
|
-
*
|
|
40448
|
-
*
|
|
40449
|
-
*
|
|
40663
|
+
* An optional `partnerFee` is forwarded on the payload as the per-intent fee override; omit it
|
|
40664
|
+
* and the configured leverage-yield fee applies. Withdrawals **are** charged — the fee comes out
|
|
40665
|
+
* of `inputAmount`, which for a withdraw is the vault's own shares, so the receiver accrues
|
|
40666
|
+
* lsoda\* rather than the output token.
|
|
40667
|
+
*
|
|
40668
|
+
* For `minOutputAmount`, quote via {@link LeverageYieldService.getQuote} with the vault
|
|
40669
|
+
* address as the source token (`token_src`), then subtract your slippage tolerance.
|
|
40450
40670
|
*/
|
|
40451
40671
|
async withdraw(params) {
|
|
40452
40672
|
const baseCtx = { srcChainKey: params.srcChainKey, action: "withdraw" };
|
|
@@ -40480,7 +40700,10 @@ var LeverageYieldService = class {
|
|
|
40480
40700
|
solver: params.solver ?? ANY_SOLVER_ADDRESS,
|
|
40481
40701
|
data: "0x"
|
|
40482
40702
|
},
|
|
40483
|
-
hubWalletSwap: true
|
|
40703
|
+
hubWalletSwap: true,
|
|
40704
|
+
// Per-intent fee override — only included when the caller supplies one, so the
|
|
40705
|
+
// payload stays free of undefined-valued keys (mirrors `deposit`).
|
|
40706
|
+
...params.partnerFee !== void 0 && { partnerFee: params.partnerFee }
|
|
40484
40707
|
}
|
|
40485
40708
|
};
|
|
40486
40709
|
} catch (error) {
|
|
@@ -40513,7 +40736,7 @@ var LeverageYieldService = class {
|
|
|
40513
40736
|
* `UNKNOWN` (defensive fallback).
|
|
40514
40737
|
*/
|
|
40515
40738
|
async createVaultIntent(_params) {
|
|
40516
|
-
const { params, skipSimulation, hubWalletSwap, partnerFee = this.config.
|
|
40739
|
+
const { params, skipSimulation, hubWalletSwap, partnerFee = this.config.leverageYieldPartnerFee } = _params;
|
|
40517
40740
|
const baseCtx = { srcChainKey: params.srcChainKey, dstChainKey: params.dstChainKey };
|
|
40518
40741
|
try {
|
|
40519
40742
|
leverageYieldInvariant(
|
|
@@ -41743,8 +41966,6 @@ var Sodax = class {
|
|
|
41743
41966
|
const analytics = resolveAnalytics(options?.analytics);
|
|
41744
41967
|
const fee = options?.fee;
|
|
41745
41968
|
const radfiSigner = options?.radfi?.signRequest;
|
|
41746
|
-
const useBackendSubmitTx = options?.swapsOptions?.useBackendSubmitTx ?? false;
|
|
41747
|
-
const bridgeUseBackendSubmitTx = options?.bridgeOptions?.useBackendSubmitTx ?? false;
|
|
41748
41969
|
this.instanceConfig = options ? mergeSodaxConfig(sodaxConfig, options) : sodaxConfig;
|
|
41749
41970
|
this.backendApi = new BackendApiService(this.instanceConfig.api, logger);
|
|
41750
41971
|
this.api = this.backendApi;
|
|
@@ -41763,8 +41984,7 @@ var Sodax = class {
|
|
|
41763
41984
|
config: this.config,
|
|
41764
41985
|
hubProvider: this.hubProvider,
|
|
41765
41986
|
spoke: this.spoke,
|
|
41766
|
-
backendApi: this.backendApi
|
|
41767
|
-
useBackendSubmitTx
|
|
41987
|
+
backendApi: this.backendApi
|
|
41768
41988
|
});
|
|
41769
41989
|
this.moneyMarket = new MoneyMarketService({
|
|
41770
41990
|
config: this.config,
|
|
@@ -41785,8 +42005,7 @@ var Sodax = class {
|
|
|
41785
42005
|
hubProvider: this.hubProvider,
|
|
41786
42006
|
config: this.config,
|
|
41787
42007
|
spoke: this.spoke,
|
|
41788
|
-
backendApi: this.backendApi
|
|
41789
|
-
useBackendSubmitTx: bridgeUseBackendSubmitTx
|
|
42008
|
+
backendApi: this.backendApi
|
|
41790
42009
|
});
|
|
41791
42010
|
this.staking = new StakingService({ hubProvider: this.hubProvider, config: this.config, spoke: this.spoke });
|
|
41792
42011
|
this.partners = new PartnerService({
|