@@ -174,18 +174,37 @@ func (k Keeper) CheckHealthStopLossThenRepayAndClose(ctx sdk.Context, position *
174174 var coinsForAmm sdk.Coins
175175 if percentageExitLeverageFee .IsPositive () && len (coinsLeftAfterRepay ) > 0 {
176176 coinsForAmm = ammkeeper .PortionCoins (coinsLeftAfterRepay , percentageExitLeverageFee )
177- weightBreakingFeePortion := k .amm .GetParams (ctx ).WeightBreakingFeePortion
178177
179- coinsToAmmRebalancer := ammkeeper .PortionCoins (coinsForAmm , osmomath .BigDecFromDec (weightBreakingFeePortion ))
178+ totalFeeValue := weightBreakingFeeValue .Add (slippageValue ).Add (swapFeeValue ).Add (takerFeeValue )
179+ // 1. weightBreakingFeePortion
180+ weightBreakingFee := weightBreakingFeeValue .Quo (totalFeeValue )
181+ weightBreakingFeePortion := osmomath .BigDecFromDec (k .amm .GetParams (ctx ).WeightBreakingFeePortion ).Mul (weightBreakingFee )
182+
183+ coinsToAmmRebalancer := ammkeeper .PortionCoins (coinsForAmm , weightBreakingFeePortion )
180184 coinsToAmmPool := coinsForAmm .Sub (coinsToAmmRebalancer ... )
181185
186+ // Track weight breaking fee
187+ for _ , coin := range coinsToAmmPool {
188+ k .amm .TrackWeightBreakingSlippage (ctx , position .AmmPoolId , coin )
189+ }
190+
191+ // 2. slippageFeePortion
192+ slippageFee := slippageValue .Quo (totalFeeValue )
193+ coinsForSlippage := ammkeeper .PortionCoins (coinsForAmm , slippageFee )
194+
195+ // Track slippage fee
196+ for _ , coin := range coinsForSlippage {
197+ k .amm .TrackWeightBreakingSlippage (ctx , position .AmmPoolId , coin )
198+ k .amm .TrackSlippage (ctx , position .AmmPoolId , coin )
199+ }
200+
182201 // Very important to fetch this again, Updating ammPool
183202 ammPool , _ = k .amm .GetPool (ctx , position .AmmPoolId )
184- err = k .bankKeeper .SendCoins (ctx , position .GetPositionAddress (), sdk .MustAccAddressFromBech32 (ammPool .Address ), coinsToAmmPool )
203+ err = k .bankKeeper .SendCoins (ctx , position .GetPositionAddress (), sdk .MustAccAddressFromBech32 (ammPool .Address ), coinsToAmmPool . Add ( coinsForSlippage ... ) )
185204 if err != nil {
186205 return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
187206 }
188- err = k .amm .AddToPoolBalanceAndUpdateLiquidity (ctx , & ammPool , math .ZeroInt (), coinsToAmmPool )
207+ err = k .amm .AddToPoolBalanceAndUpdateLiquidity (ctx , & ammPool , math .ZeroInt (), coinsToAmmPool . Add ( coinsForSlippage ... ) )
189208 if err != nil {
190209 return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
191210 }
@@ -195,6 +214,29 @@ func (k Keeper) CheckHealthStopLossThenRepayAndClose(ctx sdk.Context, position *
195214 return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
196215 }
197216
217+ // 3. swapFeePortion
218+ swapFee := swapFeeValue .Quo (totalFeeValue )
219+ coinsForSwap := ammkeeper .PortionCoins (coinsForAmm , swapFee )
220+
221+ rebalanceTreasury := sdk .MustAccAddressFromBech32 (ammPool .GetRebalanceTreasury ())
222+ err = k .bankKeeper .SendCoins (ctx , position .GetPositionAddress (), rebalanceTreasury , coinsForSwap )
223+ if err != nil {
224+ return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
225+ }
226+
227+ err = k .amm .OnCollectFee (ctx , ammPool , coinsForSwap )
228+ if err != nil {
229+ return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
230+ }
231+
232+ // 4. takerFeePortion
233+ takerFee := takerFeeValue .Quo (totalFeeValue )
234+ coinsForTaker := ammkeeper .PortionCoins (coinsForAmm , takerFee )
235+
236+ err = k .bankKeeper .SendCoins (ctx , position .GetPositionAddress (), sdk .MustAccAddressFromBech32 (k .parameterKeeper .GetParams (ctx ).TakerFeeCollectionAddress ), coinsForTaker )
237+ if err != nil {
238+ return osmomath .ZeroBigDec (), math .ZeroInt (), sdk.Coins {}, math .ZeroInt (), sdk.Coins {}, osmomath .ZeroBigDec (), stopLossReached , osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), osmomath .ZeroBigDec (), err
239+ }
198240 }
199241
200242 // anything left over in position balance goes to user
0 commit comments