Skip to content

Commit

Permalink
fix: prioritize amm pool over bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang1992 committed Mar 9, 2024
1 parent 1835e49 commit b350caa
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 134 deletions.
52 changes: 26 additions & 26 deletions src/helpers/FeeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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', {
Expand Down
55 changes: 27 additions & 28 deletions src/helpers/RateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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', {
Expand Down
160 changes: 80 additions & 80 deletions src/helpers/SwapHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/utils/bridgeHelperResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b350caa

Please sign in to comment.