Skip to content

Commit 291b89d

Browse files
authored
Merge pull request #3362 from Sifchain/feature/dlp-discounted-sent-amount
fix: 🐛 DLP discounted sent amount
2 parents 4cec682 + ce8b67b commit 291b89d

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

x/clp/keeper/calculations.go

+6
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,9 @@ func GetSwapFee(sentAmount sdk.Uint,
598598
}
599599
return swapResult
600600
}
601+
602+
func CalculateDiscountedSentAmount(sentAmount sdk.Uint, swapFeeRate sdk.Dec) sdk.Uint {
603+
discountedSentAmount := sentAmount.Sub(sdk.Uint(sdk.NewDecFromBigInt(sentAmount.BigInt()).Mul(swapFeeRate).RoundInt()))
604+
605+
return discountedSentAmount
606+
}

x/clp/keeper/msg_server.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ func (k msgServer) Swap(goCtx context.Context, msg *types.MsgSwap) (*types.MsgSw
615615
if k.GetLiquidityProtectionParams(ctx).IsActive {
616616
if types.StringCompare(sAsset.Denom, types.NativeSymbol) {
617617
// selling rowan
618-
k.MustUpdateLiquidityProtectionThreshold(ctx, true, msg.SentAmount, price)
618+
discountedSentAmount := CalculateDiscountedSentAmount(msg.SentAmount, swapFeeRate)
619+
k.MustUpdateLiquidityProtectionThreshold(ctx, true, discountedSentAmount, price)
619620
}
620621

621622
if types.StringCompare(rAsset.Denom, types.NativeSymbol) {
@@ -705,7 +706,8 @@ func (k msgServer) AddLiquidity(goCtx context.Context, msg *types.MsgAddLiquidit
705706
return nil, types.ErrReachedMaxRowanLiquidityThreshold
706707
}
707708

708-
k.MustUpdateLiquidityProtectionThreshold(ctx, true, swapAmount, price)
709+
discountedSentAmount := CalculateDiscountedSentAmount(swapAmount, sellNativeSwapFeeRate)
710+
k.MustUpdateLiquidityProtectionThreshold(ctx, true, discountedSentAmount, price)
709711
}
710712

711713
case BuyNative:

x/clp/keeper/msg_server_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ func TestMsgServer_Swap(t *testing.T) {
667667
poolAssetPermissions: []tokenregistrytypes.Permission{tokenregistrytypes.Permission_CLP},
668668
nativeAssetPermissions: []tokenregistrytypes.Permission{tokenregistrytypes.Permission_CLP},
669669
currentRowanLiquidityThreshold: sdk.NewUint(1000),
670-
expectedRunningThresholdEnd: sdk.NewUint(900),
670+
expectedRunningThresholdEnd: sdk.NewUint(910),
671671
maxRowanLiquidityThresholdAsset: "rowan",
672672
maxRowanLiquidityThreshold: sdk.NewUint(2000),
673673
swapFeeParams: types.SwapFeeParams{
@@ -1606,7 +1606,7 @@ func TestMsgServer_AddLiquidity(t *testing.T) {
16061606
liquidityProtectionActive: true,
16071607
maxRowanLiquidityThreshold: sdk.NewUint(1336005328924242545),
16081608
currentRowanLiquidityThreshold: sdk.NewUint(1336005328924242544),
1609-
expectedUpdatedRowanLiquidityThreshold: sdk.NewUint(0),
1609+
expectedUpdatedRowanLiquidityThreshold: sdk.NewUint(4008015986772728),
16101610
expectedPoolUnits: sdk.NewUintFromString("23662660951949037742990437"),
16111611
expectedLPUnits: sdk.NewUintFromString("401491654050052483"),
16121612
},

0 commit comments

Comments
 (0)