@silentswap/sdk 0.0.89 → 0.0.91
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/bridge.js +65 -36
- package/package.json +1 -1
package/dist/bridge.js
CHANGED
|
@@ -410,47 +410,58 @@ const UINT256_MAX = 2n ** 256n - 1n;
|
|
|
410
410
|
*/
|
|
411
411
|
async function solveRelayUsdcAmount(srcChainId, srcToken, srcAmount, userAddress, depositCalldata, maxImpactPercent, depositorAddress, recipientAddress // Optional recipient address (EVM address for Solana swaps)
|
|
412
412
|
) {
|
|
413
|
-
// Get relay origin asset parameters (matches relay_origin_asset from Svelte app)
|
|
414
|
-
// This ensures we use the same logic as relay_origin_asset() for determining originChainId and originCurrency
|
|
415
413
|
const { originChainId, originCurrency } = getRelayOriginAsset(srcChainId, srcToken);
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
414
|
+
console.log('[SilentSwap:SolveRelay] Starting EXACT_INPUT quote', {
|
|
415
|
+
srcChainId,
|
|
416
|
+
srcToken,
|
|
417
|
+
srcAmount,
|
|
418
|
+
userAddress,
|
|
419
|
+
recipientAddress,
|
|
420
|
+
originChainId,
|
|
421
|
+
originCurrency,
|
|
422
|
+
});
|
|
420
423
|
const initialQuote = await fetchRelayQuote({
|
|
421
424
|
user: userAddress,
|
|
422
|
-
recipient: recipientAddress || userAddress,
|
|
425
|
+
recipient: recipientAddress || userAddress,
|
|
423
426
|
referrer: 'silentswap',
|
|
424
|
-
originChainId,
|
|
427
|
+
originChainId,
|
|
425
428
|
destinationChainId: NI_CHAIN_ID_AVALANCHE,
|
|
426
|
-
originCurrency,
|
|
429
|
+
originCurrency,
|
|
427
430
|
destinationCurrency: S0X_ADDR_USDC_AVALANCHE,
|
|
428
431
|
amount: srcAmount,
|
|
429
432
|
tradeType: 'EXACT_INPUT',
|
|
430
433
|
});
|
|
431
434
|
const baseUsdcOut = BigInt(initialQuote.details.currencyOut.amount);
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
BigNumber.min(BigNumber(baseUsdcOut.toString()).plus(HUNDRED_USDC_MICRO),
|
|
441
|
-
|
|
435
|
+
console.log('[SilentSwap:SolveRelay] EXACT_INPUT result', {
|
|
436
|
+
baseUsdcOut: baseUsdcOut.toString(),
|
|
437
|
+
currencyInAmount: initialQuote.details.currencyIn.amount,
|
|
438
|
+
currencyInAmountUsd: initialQuote.details.currencyIn.amountUsd,
|
|
439
|
+
currencyOutAmountUsd: initialQuote.details.currencyOut.amountUsd,
|
|
440
|
+
});
|
|
441
|
+
const TEN_USDC_MICRO = '10000000';
|
|
442
|
+
const HUNDRED_USDC_MICRO = '100000000';
|
|
443
|
+
let targetUsdcOut = BigInt(BigNumber.max(BigNumber(baseUsdcOut.toString()).plus(TEN_USDC_MICRO), BigNumber.min(BigNumber(baseUsdcOut.toString()).plus(HUNDRED_USDC_MICRO), BigNumber(baseUsdcOut.toString()).times(1.05))).toFixed(0));
|
|
444
|
+
console.log('[SilentSwap:SolveRelay] Overshoot target', {
|
|
445
|
+
baseUsdcOut: baseUsdcOut.toString(),
|
|
446
|
+
targetUsdcOut: targetUsdcOut.toString(),
|
|
447
|
+
overshootDelta: (targetUsdcOut - baseUsdcOut).toString(),
|
|
448
|
+
});
|
|
442
449
|
// Try up to 4 times to find optimal amount
|
|
443
450
|
for (let attempt = 0; attempt < 4; attempt++) {
|
|
451
|
+
console.log(`[SilentSwap:SolveRelay] Attempt ${attempt + 1}/4 EXACT_OUTPUT`, {
|
|
452
|
+
targetUsdcOut: targetUsdcOut.toString(),
|
|
453
|
+
srcAmountBudget: srcAmount,
|
|
454
|
+
});
|
|
444
455
|
const quote = await fetchRelayQuote({
|
|
445
456
|
user: userAddress,
|
|
446
457
|
referrer: 'silentswap',
|
|
447
|
-
originChainId,
|
|
458
|
+
originChainId,
|
|
448
459
|
destinationChainId: NI_CHAIN_ID_AVALANCHE,
|
|
449
|
-
originCurrency,
|
|
460
|
+
originCurrency,
|
|
450
461
|
destinationCurrency: S0X_ADDR_USDC_AVALANCHE,
|
|
451
462
|
amount: targetUsdcOut.toString(),
|
|
452
463
|
tradeType: 'EXACT_OUTPUT',
|
|
453
|
-
recipient: recipientAddress || depositorAddress,
|
|
464
|
+
recipient: recipientAddress || depositorAddress,
|
|
454
465
|
txsGasLimit: 500_000,
|
|
455
466
|
txs: [
|
|
456
467
|
{
|
|
@@ -470,16 +481,29 @@ async function solveRelayUsdcAmount(srcChainId, srcToken, srcAmount, userAddress
|
|
|
470
481
|
],
|
|
471
482
|
});
|
|
472
483
|
const impactPercent = Number(quote.details.totalImpact.percent);
|
|
484
|
+
const amountIn = BigInt(quote.details.currencyIn.amount);
|
|
485
|
+
const amountOut = BigInt(quote.details.currencyOut.amount);
|
|
486
|
+
console.log(`[SilentSwap:SolveRelay] Attempt ${attempt + 1} result`, {
|
|
487
|
+
amountIn: amountIn.toString(),
|
|
488
|
+
amountOut: amountOut.toString(),
|
|
489
|
+
srcAmountBudget: srcAmount,
|
|
490
|
+
withinBudget: amountIn <= BigInt(srcAmount),
|
|
491
|
+
delta: (amountIn - BigInt(srcAmount)).toString(),
|
|
492
|
+
impactPercent: impactPercent.toFixed(4),
|
|
493
|
+
});
|
|
473
494
|
if (impactPercent > maxImpactPercent) {
|
|
474
495
|
throw new Error(`Price impact too high: ${impactPercent.toFixed(2)}%`);
|
|
475
496
|
}
|
|
476
|
-
const amountIn = BigInt(quote.details.currencyIn.amount);
|
|
477
497
|
// Amount in is within our budget
|
|
478
498
|
if (amountIn <= BigInt(srcAmount)) {
|
|
479
|
-
|
|
499
|
+
console.log('[SilentSwap:SolveRelay] Found optimal amount', {
|
|
500
|
+
usdcOut: amountOut.toString(),
|
|
501
|
+
amountIn: amountIn.toString(),
|
|
502
|
+
attempt: attempt + 1,
|
|
503
|
+
});
|
|
504
|
+
return [amountOut, amountIn, ''];
|
|
480
505
|
}
|
|
481
506
|
// Calculate new target based on overshoot
|
|
482
|
-
// Use BigNumber to ensure integer values (matching Svelte implementation)
|
|
483
507
|
const yg_uusdc_price = BigNumber(quote.details.currencyOut.amountUsd).div(quote.details.currencyOut.amount);
|
|
484
508
|
const yg_utoken_price = BigNumber(quote.details.currencyIn.amountUsd).div(quote.details.currencyIn.amount);
|
|
485
509
|
const yg_uusdc_over = BigNumber(quote.details.currencyIn.amount)
|
|
@@ -489,6 +513,11 @@ async function solveRelayUsdcAmount(srcChainId, srcToken, srcAmount, userAddress
|
|
|
489
513
|
const sg_uusdc_target = BigNumber(quote.details.currencyOut.amount)
|
|
490
514
|
.minus(yg_uusdc_over)
|
|
491
515
|
.toFixed(0);
|
|
516
|
+
console.log(`[SilentSwap:SolveRelay] Reducing target`, {
|
|
517
|
+
previousTarget: targetUsdcOut.toString(),
|
|
518
|
+
newTarget: sg_uusdc_target,
|
|
519
|
+
usdcOver: yg_uusdc_over.toFixed(0),
|
|
520
|
+
});
|
|
492
521
|
if (BigInt(sg_uusdc_target) >= targetUsdcOut) {
|
|
493
522
|
throw new Error('Failed to find optimal bridge price');
|
|
494
523
|
}
|
|
@@ -687,23 +716,23 @@ async function solveDebridgeSingleChainUsdcAmount(chainId, srcToken, srcAmount,
|
|
|
687
716
|
* @param maxImpactPercent - Maximum price impact percentage allowed (default from constants)
|
|
688
717
|
* @returns Promise resolving to solve result with optimal amounts
|
|
689
718
|
*/
|
|
690
|
-
export async function solveOptimalUsdcAmount(srcChainId, srcToken, srcAmount, userAddress, depositCalldata, maxImpactPercent, depositorAddress, evmSignerAddress // Optional EVM address for deposit calldata (required for Solana
|
|
719
|
+
export async function solveOptimalUsdcAmount(srcChainId, srcToken, srcAmount, userAddress, depositCalldata, maxImpactPercent, depositorAddress, evmSignerAddress // Optional EVM address for deposit calldata (required for non-EVM swaps: Solana, Bitcoin)
|
|
691
720
|
) {
|
|
692
|
-
// Check if this is a Solana
|
|
693
|
-
const
|
|
694
|
-
// For
|
|
695
|
-
// userAddress is the
|
|
721
|
+
// Check if this is a non-EVM chain (Solana, Bitcoin, etc.)
|
|
722
|
+
const isNonEvmChain = isNonEvmRelayChainId(srcChainId);
|
|
723
|
+
// For non-EVM chains (Solana, Bitcoin), we need an EVM address for deposit calldata
|
|
724
|
+
// userAddress is the source chain address (Solana/Bitcoin) for relay quote user parameter
|
|
696
725
|
// evmSignerAddress is the EVM address (for deposit calldata and relay quote recipient)
|
|
697
|
-
const evmAddressForCalldata = evmSignerAddress || (!
|
|
726
|
+
const evmAddressForCalldata = evmSignerAddress || (!isNonEvmChain && userAddress.startsWith('0x') ? userAddress : undefined);
|
|
698
727
|
if (!depositCalldata && !evmAddressForCalldata) {
|
|
699
|
-
throw new Error('EVM address required for deposit calldata. Provide evmSignerAddress parameter for
|
|
728
|
+
throw new Error('EVM address required for deposit calldata. Provide evmSignerAddress parameter for non-EVM swaps.');
|
|
700
729
|
}
|
|
701
730
|
// Create phony deposit calldata if not provided
|
|
702
731
|
const calldata = depositCalldata || createPhonyDepositCalldata(evmAddressForCalldata);
|
|
703
|
-
// For
|
|
704
|
-
// userAddress is the
|
|
705
|
-
// evmSignerAddress is the EVM address (for recipient parameter)
|
|
706
|
-
const recipientAddress =
|
|
732
|
+
// For non-EVM chains, we need to pass the EVM address as recipient in relay quotes
|
|
733
|
+
// userAddress is the source chain address (for user parameter)
|
|
734
|
+
// evmSignerAddress is the EVM address (for recipient parameter on the destination EVM chain)
|
|
735
|
+
const recipientAddress = isNonEvmChain && evmSignerAddress ? evmSignerAddress : userAddress;
|
|
707
736
|
// Try both providers in parallel
|
|
708
737
|
const [relayResult, debridgeResult] = await Promise.allSettled([
|
|
709
738
|
solveRelayUsdcAmount(srcChainId, srcToken, srcAmount, userAddress, calldata, maxImpactPercent, depositorAddress, recipientAddress),
|