Skip to content

Commit

Permalink
Zero swap fee when weight is broken & the tx is recovery tx (#181)
Browse files Browse the repository at this point in the history
zero fee when weight is broken
  • Loading branch information
jelysn authored Sep 4, 2023
1 parent 98685e5 commit ed993e6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion x/amm/keeper/keeper_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn() {
suite.Require().Equal(liquidity.Liquidity, tc.expPoolBalance.AmountOf(tc.tokenIn.Denom))
liquidity, found = suite.app.AmmKeeper.GetDenomLiquidity(suite.ctx, tc.tokenOut.Denom)
suite.Require().True(found)
suite.Require().Equal(liquidity.Liquidity, tc.expPoolBalance.AmountOf(tc.tokenOut.Denom))
suite.Require().True(liquidity.Liquidity.LTE(tc.expPoolBalance.AmountOf(tc.tokenOut.Denom)))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/route_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (suite *KeeperTestSuite) TestRouteExactAmountIn() {
suite.Require().Equal(liquidity.Liquidity, tc.expPoolBalance.AmountOf(tc.tokenIn.Denom))
liquidity, found = suite.app.AmmKeeper.GetDenomLiquidity(suite.ctx, tc.tokenOut.Denom)
suite.Require().True(found)
suite.Require().Equal(liquidity.Liquidity, tc.expPoolBalance.AmountOf(tc.tokenOut.Denom))
suite.Require().True(liquidity.Liquidity.LTE(tc.expPoolBalance.AmountOf(tc.tokenOut.Denom)))
}
})
}
Expand Down
17 changes: 14 additions & 3 deletions x/amm/keeper/update_pool_for_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ func (k Keeper) UpdatePoolForSwap(
return err, sdk.ZeroInt()
}

swapFeeInCoins := PortionCoins(tokensIn, swapFeeIn)
// apply swap fee when weight balance bonus is not available
swapFeeInCoins := sdk.Coins{}
if weightBalanceBonus.IsZero() {
swapFeeInCoins = PortionCoins(tokensIn, swapFeeIn)
}

if swapFeeInCoins.IsAllPositive() {
rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, swapFeeInCoins)
Expand All @@ -50,7 +55,11 @@ func (k Keeper) UpdatePoolForSwap(
return err, sdk.ZeroInt()
}

swapFeeOutCoins := PortionCoins(tokensOut, swapFeeOut)
// apply swap fee when weight balance bonus is not available
swapFeeOutCoins := sdk.Coins{}
if weightBalanceBonus.IsZero() {
swapFeeOutCoins = PortionCoins(tokensOut, swapFeeOut)
}
if swapFeeOutCoins.IsAllPositive() {
rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
err = k.bankKeeper.SendCoins(ctx, sender, rebalanceTreasury, swapFeeOutCoins)
Expand All @@ -63,6 +72,8 @@ func (k Keeper) UpdatePoolForSwap(
}
}

swapFeeCoins := swapFeeInCoins.Add(swapFeeOutCoins...)

// calculate treasury amount to send as bonus
rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, tokenOut.Denom).Amount
Expand All @@ -86,7 +97,7 @@ func (k Keeper) UpdatePoolForSwap(
}
k.RecordTotalLiquidityIncrease(ctx, tokensIn)
k.RecordTotalLiquidityDecrease(ctx, tokensOut)
k.RecordTotalLiquidityDecrease(ctx, swapFeeInCoins)
k.RecordTotalLiquidityDecrease(ctx, swapFeeCoins)

return nil, swapFeeOutCoins.AmountOf(tokenOut.Denom)
}
1 change: 0 additions & 1 deletion x/amm/types/swap_in_amt_given_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (p *Pool) SwapInAmtGivenOut(
weightBalanceBonus = sdk.ZeroDec()
if initialWeightDistance.GT(p.PoolParams.ThresholdWeightDifference) && distanceDiff.IsNegative() {
weightBalanceBonus = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff).Abs()
// TODO: we might skip swap fee in case it's a balance recovery operation
}
tokenAmountInInt := inAmountAfterSlippage.
Mul(sdk.OneDec().Add(weightBreakingFee)).
Expand Down
1 change: 0 additions & 1 deletion x/amm/types/swap_out_amt_given_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ func (p *Pool) SwapOutAmtGivenIn(
weightBalanceBonus = sdk.ZeroDec()
if initialWeightDistance.GT(p.PoolParams.ThresholdWeightDifference) && distanceDiff.IsNegative() {
weightBalanceBonus = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff).Abs()
// TODO: we might skip swap fee in case it's a balance recovery operation
}
tokenAmountOutInt := outAmountAfterSlippage.
Mul(sdk.OneDec().Sub(weightBreakingFee)).
Expand Down

0 comments on commit ed993e6

Please sign in to comment.