@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.
Files changed (2) hide show
  1. package/dist/bridge.js +65 -36
  2. 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
- // Get initial quote to estimate expected output
417
- // Note: Matches Svelte relay_solve_uusdc_amount line 116-125
418
- // user is the sender address (Solana address for Solana swaps, EVM address for EVM swaps)
419
- // recipient is the EVM signer address (always EVM, matches Svelte line 122)
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, // Use recipientAddress if provided (for Solana), otherwise use userAddress (for EVM)
425
+ recipient: recipientAddress || userAddress,
423
426
  referrer: 'silentswap',
424
- originChainId, // Use originChainId from getRelayOriginAsset (matches relay_origin_asset)
427
+ originChainId,
425
428
  destinationChainId: NI_CHAIN_ID_AVALANCHE,
426
- originCurrency, // Use originCurrency from getRelayOriginAsset (matches relay_origin_asset)
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
- // Overshoot by +10 USDC to +5% (max +100 USDC)
433
- // Use BigNumber to ensure integer values (matching Svelte implementation)
434
- // Note: Svelte app uses 10_000000n (10 USDC with 6 decimals) minimum
435
- // 10_000000n = 10 * 10^6 = 10,000,000 (10 USDC in micro units)
436
- // Convert numeric constants to strings for BigNumber (matching Svelte's bigint usage)
437
- const TEN_USDC_MICRO = '10000000'; // 10 USDC in micro units (6 decimals)
438
- const HUNDRED_USDC_MICRO = '100000000'; // 100 USDC in micro units (6 decimals)
439
- let targetUsdcOut = BigInt(BigNumber.max(BigNumber(baseUsdcOut.toString()).plus(TEN_USDC_MICRO), // 10 USDC in micro units (6 decimals)
440
- BigNumber.min(BigNumber(baseUsdcOut.toString()).plus(HUNDRED_USDC_MICRO), // 100 USDC in micro units (6 decimals)
441
- BigNumber(baseUsdcOut.toString()).times(1.05))).toFixed(0));
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, // Use originChainId from getRelayOriginAsset (matches relay_origin_asset)
458
+ originChainId,
448
459
  destinationChainId: NI_CHAIN_ID_AVALANCHE,
449
- originCurrency, // Use originCurrency from getRelayOriginAsset (matches relay_origin_asset)
460
+ originCurrency,
450
461
  destinationCurrency: S0X_ADDR_USDC_AVALANCHE,
451
462
  amount: targetUsdcOut.toString(),
452
463
  tradeType: 'EXACT_OUTPUT',
453
- recipient: recipientAddress || depositorAddress, // Use recipientAddress if provided (for Solana), otherwise use 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
- return [BigInt(quote.details.currencyOut.amount), amountIn, ''];
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 swaps)
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 chain
693
- const isSolanaChain = isSolanaChainId(srcChainId);
694
- // For Solana chains, we need an EVM address for deposit calldata
695
- // userAddress is the Solana address (for relay quote user parameter)
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 || (!isSolanaChain && userAddress.startsWith('0x') ? userAddress : undefined);
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 Solana swaps.');
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 Solana chains, we need to pass the EVM address as recipient in relay quotes
704
- // userAddress is the Solana address (for user parameter)
705
- // evmSignerAddress is the EVM address (for recipient parameter)
706
- const recipientAddress = isSolanaChain && evmSignerAddress ? evmSignerAddress : userAddress;
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),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@silentswap/sdk",
3
3
  "type": "module",
4
- "version": "0.0.89",
4
+ "version": "0.0.91",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "files": [