diff --git a/package.json b/package.json index fe8695b..c249c19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "packageManager": "pnpm@9.4.0", - "version": "3.0.0-beta.3", + "version": "3.0.0-beta.4", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/alexSDK.ts b/src/alexSDK.ts index 1b6dcc2..cdde18b 100644 --- a/src/alexSDK.ts +++ b/src/alexSDK.ts @@ -11,7 +11,7 @@ import { getAllPossibleRoute } from './helpers/RouteHelper'; import { getYAmountFromXAmount } from './helpers/RateHelper'; import { fromEntries } from './utils/utils'; import type { AMMRoute } from './utils/ammRouteResolver'; -import { broadcastSponsoredTx, requiredStxAmountForSponsorTx, runSponsoredSpotTx } from './helpers/SponsorTxHelper'; +import { broadcastSponsoredTx, requiredStxAmountForSponsorTx, runSponsoredSpotTx, SponsoredTxError, SponsoredTxErrorCode } from './helpers/SponsorTxHelper'; /** * The AlexSDK class provides methods for interacting with a decentralized exchange (DEX) system, @@ -186,11 +186,14 @@ export class AlexSDK { to, route ) - const sponsorFeeAmount = await this.getAmountTo( + const sponsorFeeAmount = from === Currency.STX ? stxAmount : await this.getAmountTo( Currency.STX, stxAmount, from ) + if (sponsorFeeAmount > fromAmount) { + return BigInt(0) + } return getYAmountFromXAmount( from, to, @@ -258,11 +261,14 @@ export class AlexSDK { currencyY, route ) - const sponsorFeeAmount = await this.getAmountTo( + const sponsorFeeAmount = currencyX === Currency.STX ? stxAmount : await this.getAmountTo( Currency.STX, stxAmount, currencyX ) + if (sponsorFeeAmount > fromAmount) { + throw new SponsoredTxError(SponsoredTxErrorCode.insufficient_funds, 'Insufficient funds to cover sponsor fee') + } return runSponsoredSpotTx( stxAddress, currencyX, diff --git a/src/helpers/SponsorTxHelper.ts b/src/helpers/SponsorTxHelper.ts index 22f6f5f..bff3722 100644 --- a/src/helpers/SponsorTxHelper.ts +++ b/src/helpers/SponsorTxHelper.ts @@ -21,8 +21,8 @@ export function runSponsoredSpotTx( currencyX: Currency, currencyY: Currency, totalAmount: bigint, - feeAmount: bigint, minDy: bigint, + feeAmount: bigint, ammPools: PoolData[], mappings: TokenInfo[], customRoute?: AMMRouteSegment[] @@ -244,6 +244,8 @@ export enum SponsoredTxErrorCode { "invalid_nonce" = "invalid_nonce", // Worker failed to broadcast the tx "broadcast_error" = "broadcast_error", + // Insufficient funds to cover sponsor fee + "insufficient_funds" = "insufficient_funds", "unknown_error" = "unknown_error", }