Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Amm]: Add a New Field for Slippage and Weight Breaking Fee on Trades #1035

Merged
merged 9 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
753 changes: 718 additions & 35 deletions api/elys/amm/pool.pulsar.go

Large diffs are not rendered by default.

355 changes: 216 additions & 139 deletions api/elys/masterchef/query.pulsar.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions proto/elys/amm/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ message PoolExtraInfo {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string lp_saved_apr = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

message OraclePoolSlippageTrack {
Expand All @@ -57,3 +62,13 @@ message OraclePoolSlippageTrack {
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message WeightBreakingSlippage {
uint64 pool_id = 1;
string date = 2;
string amount = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}
7 changes: 6 additions & 1 deletion proto/elys/masterchef/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ message PoolRewards {
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
cosmos.base.v1beta1.Coin eden_forward = 4 [ (gogoproto.nullable) = false ];
string external_rewards_apr = 5 [
string rewards_usd_apr = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string external_rewards_apr = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
Expand Down
1 change: 1 addition & 0 deletions x/accountedpool/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/accountedpool/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions x/amm/keeper/keeper_swap_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ func (k Keeper) InternalSwapExactAmountIn(
// track slippage
k.TrackSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenOutCoin.Denom, slippageAmount.RoundInt()))

if pool.PoolParams.UseOracle {
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenOutCoin.Denom, slippageAmount.RoundInt()))
}

return tokenOutAmount, nil
}
8 changes: 8 additions & 0 deletions x/amm/keeper/query_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package keeper

import (
"context"

"github.com/cosmos/cosmos-sdk/runtime"

"cosmossdk.io/math"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -15,9 +17,15 @@ import (
func (k Keeper) PoolExtraInfo(ctx sdk.Context, pool types.Pool) types.PoolExtraInfo {
tvl, _ := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
lpTokenPrice, _ := pool.LpTokenPrice(ctx, k.oracleKeeper, k.accountedPoolKeeper)
avg := k.GetWeightBreakingSlippageAvg(ctx, pool.PoolId)
apr := math.LegacyZeroDec()
if tvl.IsPositive() {
apr = avg.Mul(math.LegacyNewDec(52)).Quo(tvl)
}
return types.PoolExtraInfo{
Tvl: tvl,
LpTokenPrice: lpTokenPrice,
LpSavedApr: apr,
}
}

Expand Down
84 changes: 84 additions & 0 deletions x/amm/keeper/track_weight_breaking_slippage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package keeper

import (
"cosmossdk.io/math"
"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)

func (k Keeper) GetWeightAndSlippageFee(ctx sdk.Context, poolId uint64, date string) types.WeightBreakingSlippage {
track := types.WeightBreakingSlippage{}
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), []byte(types.WeightAndSlippagePrefix))
bz := store.Get(types.WeightAndSlippageFeeKey(poolId, date))
if len(bz) == 0 {
return types.WeightBreakingSlippage{
PoolId: poolId,
Date: date,
Amount: math.LegacyZeroDec(),
}
}

k.cdc.MustUnmarshal(bz, &track)
return track

Check warning on line 24 in x/amm/keeper/track_weight_breaking_slippage.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/track_weight_breaking_slippage.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}

func (k Keeper) SetWeightAndSlippageFee(ctx sdk.Context, track types.WeightBreakingSlippage) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), []byte(types.WeightAndSlippagePrefix))
bz := k.cdc.MustMarshal(&track)
store.Set(types.WeightAndSlippageFeeKey(track.PoolId, track.Date), bz)

Check warning on line 30 in x/amm/keeper/track_weight_breaking_slippage.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/track_weight_breaking_slippage.go#L27-L30

Added lines #L27 - L30 were not covered by tests
}

func (k Keeper) DeleteWeightAndSlippageFee(ctx sdk.Context, poolId uint64, date string) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), []byte(types.WeightAndSlippagePrefix))
store.Delete(types.WeightAndSlippageFeeKey(poolId, date))

Check warning on line 35 in x/amm/keeper/track_weight_breaking_slippage.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/track_weight_breaking_slippage.go#L33-L35

Added lines #L33 - L35 were not covered by tests
}

func (k Keeper) AddWeightAndSlippageFee(ctx sdk.Context, track types.WeightBreakingSlippage) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), []byte(types.WeightAndSlippagePrefix))

trackTotal := types.WeightBreakingSlippage{}
bz := store.Get(types.WeightAndSlippageFeeKey(track.PoolId, track.Date))
if len(bz) == 0 {
trackTotal = track
} else {
k.cdc.MustUnmarshal(bz, &trackTotal)
trackTotal.Amount = trackTotal.Amount.Add(track.Amount)
}

bz = k.cdc.MustMarshal(&trackTotal)
store.Set(types.WeightAndSlippageFeeKey(track.PoolId, track.Date), bz)
}

func (k Keeper) TrackWeightBreakingSlippage(ctx sdk.Context, poolId uint64, token sdk.Coin) {
price := k.oracleKeeper.GetAssetPriceFromDenom(ctx, token.Denom)
track := types.WeightBreakingSlippage{
PoolId: poolId,
Date: ctx.BlockTime().Format("2006-01-02"),
Amount: price.Mul(math.LegacyNewDecFromInt(token.Amount)),
}
k.AddWeightAndSlippageFee(ctx, track)
}

// Returns last 7 days avg for weight breaking and slippage
func (k Keeper) GetWeightBreakingSlippageAvg(ctx sdk.Context, poolId uint64) math.LegacyDec {
start := ctx.BlockTime()
count := math.ZeroInt()
total := math.LegacyZeroDec()

for i := 0; i < 7; i++ {
date := start.AddDate(0, 0, i*-1).Format("2006-01-02")
info := k.GetWeightAndSlippageFee(ctx, poolId, date)

if info.Amount.IsPositive() {
total = total.Add(info.Amount)
count = count.Add(math.OneInt())
}

Check warning on line 77 in x/amm/keeper/track_weight_breaking_slippage.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/track_weight_breaking_slippage.go#L75-L77

Added lines #L75 - L77 were not covered by tests
}

if count.IsZero() {
return math.LegacyZeroDec()
}
return total.Quo(math.LegacyNewDecFromInt(count))

Check warning on line 83 in x/amm/keeper/track_weight_breaking_slippage.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/track_weight_breaking_slippage.go#L83

Added line #L83 was not covered by tests
}
10 changes: 10 additions & 0 deletions x/amm/keeper/update_pool_for_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ func (k Keeper) UpdatePoolForSwap(
if err != nil {
return err
}

// Track amount in pool
weightRecoveryFeeAmountForPool := sdkmath.ZeroInt()
weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion))
if givenOut {
weightRecoveryFeeAmountForPool = oracleInAmount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt()
} else {
weightRecoveryFeeAmountForPool = tokenIn.Amount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt()
}
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenIn.Denom, weightRecoveryFeeAmountForPool))
}

}
Expand Down
7 changes: 7 additions & 0 deletions x/amm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
// OraclePoolSlippageTrackPrefix is the prefix to retrieve slippage tracked
OraclePoolSlippageTrackPrefix = "OraclePool/slippage/track/value/"

WeightAndSlippagePrefix = "WeightBreakingFee/slippage/value/"

// DenomLiquidityKeyPrefix is the prefix to retrieve all DenomLiquidity
DenomLiquidityKeyPrefix = "DenomLiquidity/value/"

Expand Down Expand Up @@ -57,6 +59,11 @@
return append(sdk.Uint64ToBigEndian(poolId), sdk.Uint64ToBigEndian(timestamp)...)
}

func WeightAndSlippageFeeKey(poolId uint64, date string) []byte {
dateBytes := []byte(date)
return append(sdk.Uint64ToBigEndian(poolId), dateBytes...)

Check warning on line 64 in x/amm/types/keys.go

View check run for this annotation

Codecov / codecov/patch

x/amm/types/keys.go#L62-L64

Added lines #L62 - L64 were not covered by tests
}

// DenomLiquidityKey returns the store key to retrieve a DenomLiquidity from the index fields
func DenomLiquidityKey(denom string) []byte {
var key []byte
Expand Down
Loading
Loading