diff --git a/src/helpers/FeeHelper.ts b/src/helpers/FeeHelper.ts index 85861a8..168db02 100644 --- a/src/helpers/FeeHelper.ts +++ b/src/helpers/FeeHelper.ts @@ -52,32 +52,6 @@ export async function getLiquidityProviderFee( }).then(unwrapResponse); } - const reachableInAmm1_1 = AMMSwapPool.reachableInAMM( - tokenX, - tokenY, - ammV1_1Pools - ); - if (reachableInAmm1_1.type === 'fromAmm') { - return await readonlyCall( - 'swap-helper-bridged-v1-1', - 'fee-helper-from-amm', - { - 'token-x': reachableInAmm1_1.tokenX, - 'token-y': reachableInAmm1_1.tokenY, - 'token-z': reachableInAmm1_1.tokenZ, - 'factor-x': reachableInAmm1_1.factorX, - } - ).then(unwrapResponse); - } - if (reachableInAmm1_1.type === 'toAmm') { - return await readonlyCall('swap-helper-bridged-v1-1', 'fee-helper-to-amm', { - 'token-x': reachableInAmm1_1.tokenX, - 'token-y': reachableInAmm1_1.tokenY, - 'token-z': reachableInAmm1_1.tokenZ, - 'factor-y': reachableInAmm1_1.factorY, - }).then(unwrapResponse); - } - const ammRoute = AMMSwapPool.getRoute(tokenX, tokenY, ammPools); if (ammRoute.length === 1) { return await readonlyCall('amm-swap-pool', 'fee-helper', { @@ -120,6 +94,32 @@ export async function getLiquidityProviderFee( }).then(unwrapResponse); } + const reachableInAmm1_1 = AMMSwapPool.reachableInAMM( + tokenX, + tokenY, + ammV1_1Pools + ); + if (reachableInAmm1_1.type === 'fromAmm') { + return await readonlyCall( + 'swap-helper-bridged-v1-1', + 'fee-helper-from-amm', + { + 'token-x': reachableInAmm1_1.tokenX, + 'token-y': reachableInAmm1_1.tokenY, + 'token-z': reachableInAmm1_1.tokenZ, + 'factor-x': reachableInAmm1_1.factorX, + } + ).then(unwrapResponse); + } + if (reachableInAmm1_1.type === 'toAmm') { + return await readonlyCall('swap-helper-bridged-v1-1', 'fee-helper-to-amm', { + 'token-x': reachableInAmm1_1.tokenX, + 'token-y': reachableInAmm1_1.tokenY, + 'token-z': reachableInAmm1_1.tokenZ, + 'factor-y': reachableInAmm1_1.factorY, + }).then(unwrapResponse); + } + const reachableInAmm = AMMSwapPool.reachableInAMM(tokenX, tokenY, ammPools); if (reachableInAmm.type === 'fromAmm') { return await readonlyCall('swap-helper-bridged', 'fee-helper-from-amm', { diff --git a/src/helpers/RateHelper.ts b/src/helpers/RateHelper.ts index ddc80be..59e4421 100644 --- a/src/helpers/RateHelper.ts +++ b/src/helpers/RateHelper.ts @@ -56,34 +56,6 @@ export const getYAmountFromXAmount = async ( dx: fromAmount, }).then(unwrapResponse); } - const reachableInAMMV1_1 = AMMSwapPool.reachableInAMM( - tokenX, - tokenY, - ammV1_1Tokens - ); - if (reachableInAMMV1_1.type === 'fromAmm') { - return await readonlyCall( - 'swap-helper-bridged-v1-1', - 'get-helper-from-amm', - { - dx: fromAmount, - 'token-x': reachableInAMMV1_1.tokenX, - 'token-y': reachableInAMMV1_1.tokenY, - 'token-z': reachableInAMMV1_1.tokenZ, - 'factor-x': reachableInAMMV1_1.factorX, - } - ).then(unwrapResponse); - } - if (reachableInAMMV1_1.type === 'toAmm') { - return await readonlyCall('swap-helper-bridged-v1-1', 'get-helper-to-amm', { - dx: fromAmount, - 'token-x': reachableInAMMV1_1.tokenX, - 'token-y': reachableInAMMV1_1.tokenY, - 'token-z': reachableInAMMV1_1.tokenZ, - 'factor-y': reachableInAMMV1_1.factorY, - }).then(unwrapResponse); - } - const ammRoute = AMMSwapPool.getRoute(tokenX, tokenY, ammPools); if (ammRoute.length === 1) { return await readonlyCall('amm-swap-pool', 'get-helper', { @@ -129,6 +101,33 @@ export const getYAmountFromXAmount = async ( dx: fromAmount, }).then(unwrapResponse); } + const reachableInAMMV1_1 = AMMSwapPool.reachableInAMM( + tokenX, + tokenY, + ammV1_1Tokens + ); + if (reachableInAMMV1_1.type === 'fromAmm') { + return await readonlyCall( + 'swap-helper-bridged-v1-1', + 'get-helper-from-amm', + { + dx: fromAmount, + 'token-x': reachableInAMMV1_1.tokenX, + 'token-y': reachableInAMMV1_1.tokenY, + 'token-z': reachableInAMMV1_1.tokenZ, + 'factor-x': reachableInAMMV1_1.factorX, + } + ).then(unwrapResponse); + } + if (reachableInAMMV1_1.type === 'toAmm') { + return await readonlyCall('swap-helper-bridged-v1-1', 'get-helper-to-amm', { + dx: fromAmount, + 'token-x': reachableInAMMV1_1.tokenX, + 'token-y': reachableInAMMV1_1.tokenY, + 'token-z': reachableInAMMV1_1.tokenZ, + 'factor-y': reachableInAMMV1_1.factorY, + }).then(unwrapResponse); + } const reachableInAMM = AMMSwapPool.reachableInAMM(tokenX, tokenY, ammPools); if (reachableInAMM.type === 'fromAmm') { return await readonlyCall('swap-helper-bridged', 'get-helper-from-amm', { diff --git a/src/helpers/SwapHelper.ts b/src/helpers/SwapHelper.ts index cb9baf9..2e1a393 100644 --- a/src/helpers/SwapHelper.ts +++ b/src/helpers/SwapHelper.ts @@ -239,86 +239,6 @@ export function runSpot( ); } - const reachableInAMMV1_1 = AMMSwapPool.reachableInAMM( - currencyX, - currencyY, - ammPools - ); - if (reachableInAMMV1_1.type === 'fromAmm') { - return composeTx( - 'swap-helper-bridged-v1-1', - 'swap-helper-from-amm', - { - 'token-x-trait': reachableInAMMV1_1.tokenX, - 'token-y-trait': reachableInAMMV1_1.tokenY, - 'token-z-trait': reachableInAMMV1_1.tokenZ, - dx: fromAmount, - 'min-dz': minDy, - 'factor-x': reachableInAMMV1_1.factorX, - }, - [ - transfer(stxAddress, currencyX, fromAmount), - ...middleSteps.flatMap((middle, index) => [ - transfer( - index === 0 ? AlexVaultV1_1 : AlexVault, - middle, - BigInt(0), - FungibleConditionCode.GreaterEqual - ), - transfer( - stxAddress, - middle, - BigInt(0), - FungibleConditionCode.GreaterEqual - ), - ]), - transfer( - AlexVault, - currencyY, - minDy, - FungibleConditionCode.GreaterEqual - ), - ] - ); - } - if (reachableInAMMV1_1.type === 'toAmm') { - return composeTx( - 'swap-helper-bridged-v1-1', - 'swap-helper-to-amm', - { - 'token-x-trait': reachableInAMMV1_1.tokenX, - 'token-y-trait': reachableInAMMV1_1.tokenY, - 'token-z-trait': reachableInAMMV1_1.tokenZ, - dx: fromAmount, - 'min-dz': minDy, - 'factor-y': reachableInAMMV1_1.factorY, - }, - [ - transfer(stxAddress, currencyX, fromAmount), - ...middleSteps.flatMap((middle) => [ - transfer( - AlexVault, - middle, - BigInt(0), - FungibleConditionCode.GreaterEqual - ), - transfer( - stxAddress, - middle, - BigInt(0), - FungibleConditionCode.GreaterEqual - ), - ]), - transfer( - AlexVaultV1_1, - currencyY, - minDy, - FungibleConditionCode.GreaterEqual - ), - ] - ); - } - const ammRoute = AMMSwapPool.getRoute(currencyX, currencyY, ammPools); if (ammRoute.length === 1) { return composeTx( @@ -493,6 +413,86 @@ export function runSpot( ); } + const reachableInAMMV1_1 = AMMSwapPool.reachableInAMM( + currencyX, + currencyY, + ammPools + ); + if (reachableInAMMV1_1.type === 'fromAmm') { + return composeTx( + 'swap-helper-bridged-v1-1', + 'swap-helper-from-amm', + { + 'token-x-trait': reachableInAMMV1_1.tokenX, + 'token-y-trait': reachableInAMMV1_1.tokenY, + 'token-z-trait': reachableInAMMV1_1.tokenZ, + dx: fromAmount, + 'min-dz': minDy, + 'factor-x': reachableInAMMV1_1.factorX, + }, + [ + transfer(stxAddress, currencyX, fromAmount), + ...middleSteps.flatMap((middle, index) => [ + transfer( + index === 0 ? AlexVaultV1_1 : AlexVault, + middle, + BigInt(0), + FungibleConditionCode.GreaterEqual + ), + transfer( + stxAddress, + middle, + BigInt(0), + FungibleConditionCode.GreaterEqual + ), + ]), + transfer( + AlexVault, + currencyY, + minDy, + FungibleConditionCode.GreaterEqual + ), + ] + ); + } + if (reachableInAMMV1_1.type === 'toAmm') { + return composeTx( + 'swap-helper-bridged-v1-1', + 'swap-helper-to-amm', + { + 'token-x-trait': reachableInAMMV1_1.tokenX, + 'token-y-trait': reachableInAMMV1_1.tokenY, + 'token-z-trait': reachableInAMMV1_1.tokenZ, + dx: fromAmount, + 'min-dz': minDy, + 'factor-y': reachableInAMMV1_1.factorY, + }, + [ + transfer(stxAddress, currencyX, fromAmount), + ...middleSteps.flatMap((middle) => [ + transfer( + AlexVault, + middle, + BigInt(0), + FungibleConditionCode.GreaterEqual + ), + transfer( + stxAddress, + middle, + BigInt(0), + FungibleConditionCode.GreaterEqual + ), + ]), + transfer( + AlexVaultV1_1, + currencyY, + minDy, + FungibleConditionCode.GreaterEqual + ), + ] + ); + } + const reachableInAMM = AMMSwapPool.reachableInAMM( currencyX, currencyY, diff --git a/src/utils/bridgeHelperResolver.ts b/src/utils/bridgeHelperResolver.ts index 0d003a2..52a211e 100644 --- a/src/utils/bridgeHelperResolver.ts +++ b/src/utils/bridgeHelperResolver.ts @@ -11,6 +11,9 @@ export function bridgeHelperResolver( if (bridgingToken.includes(from) || bridgingToken.includes(to)) { return { type: "none" as const } } + if (bridgingToken.includes(from) || bridgingToken.includes(to)) { + return { type: "none" as const } + } const fromPool = ammPools.find((a) => { if (!AMMSwapPool.breakDown(a).includes(from)) return false; const [x, y] = AMMSwapPool.breakDown(a);