From 8b13fc30ddc54dc964921f77d76caf2d28bdcf3a Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Fri, 24 Jan 2025 14:34:00 +0530 Subject: [PATCH 1/2] [Stablestake]: Withdrawal limit (#1110) * withdrawal limit * fix --- api/elys/stablestake/params.pulsar.go | 108 ++++++++++++++++++--- proto/elys/stablestake/params.proto | 5 + x/stablestake/keeper/msg_server_unbond.go | 2 +- x/stablestake/migrations/v8_migration.go | 13 +++ x/stablestake/module.go | 4 +- x/stablestake/types/params.go | 2 + x/stablestake/types/params.pb.go | 112 +++++++++++++++------- 7 files changed, 196 insertions(+), 50 deletions(-) create mode 100644 x/stablestake/migrations/v8_migration.go diff --git a/api/elys/stablestake/params.pulsar.go b/api/elys/stablestake/params.pulsar.go index d034746aa..c2822b06d 100644 --- a/api/elys/stablestake/params.pulsar.go +++ b/api/elys/stablestake/params.pulsar.go @@ -1007,6 +1007,7 @@ var ( fd_Params_health_gain_factor protoreflect.FieldDescriptor fd_Params_total_value protoreflect.FieldDescriptor fd_Params_max_leverage_ratio protoreflect.FieldDescriptor + fd_Params_max_withdraw_ratio protoreflect.FieldDescriptor ) func init() { @@ -1023,6 +1024,7 @@ func init() { fd_Params_health_gain_factor = md_Params.Fields().ByName("health_gain_factor") fd_Params_total_value = md_Params.Fields().ByName("total_value") fd_Params_max_leverage_ratio = md_Params.Fields().ByName("max_leverage_ratio") + fd_Params_max_withdraw_ratio = md_Params.Fields().ByName("max_withdraw_ratio") } var _ protoreflect.Message = (*fastReflection_Params)(nil) @@ -1156,6 +1158,12 @@ func (x *fastReflection_Params) Range(f func(protoreflect.FieldDescriptor, proto return } } + if x.MaxWithdrawRatio != "" { + value := protoreflect.ValueOfString(x.MaxWithdrawRatio) + if !f(fd_Params_max_withdraw_ratio, value) { + return + } + } } // Has reports whether a field is populated. @@ -1193,6 +1201,8 @@ func (x *fastReflection_Params) Has(fd protoreflect.FieldDescriptor) bool { return x.TotalValue != "" case "elys.stablestake.Params.max_leverage_ratio": return x.MaxLeverageRatio != "" + case "elys.stablestake.Params.max_withdraw_ratio": + return x.MaxWithdrawRatio != "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1231,6 +1241,8 @@ func (x *fastReflection_Params) Clear(fd protoreflect.FieldDescriptor) { x.TotalValue = "" case "elys.stablestake.Params.max_leverage_ratio": x.MaxLeverageRatio = "" + case "elys.stablestake.Params.max_withdraw_ratio": + x.MaxWithdrawRatio = "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1280,6 +1292,9 @@ func (x *fastReflection_Params) Get(descriptor protoreflect.FieldDescriptor) pro case "elys.stablestake.Params.max_leverage_ratio": value := x.MaxLeverageRatio return protoreflect.ValueOfString(value) + case "elys.stablestake.Params.max_withdraw_ratio": + value := x.MaxWithdrawRatio + return protoreflect.ValueOfString(value) default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1322,6 +1337,8 @@ func (x *fastReflection_Params) Set(fd protoreflect.FieldDescriptor, value proto x.TotalValue = value.Interface().(string) case "elys.stablestake.Params.max_leverage_ratio": x.MaxLeverageRatio = value.Interface().(string) + case "elys.stablestake.Params.max_withdraw_ratio": + x.MaxWithdrawRatio = value.Interface().(string) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1364,6 +1381,8 @@ func (x *fastReflection_Params) Mutable(fd protoreflect.FieldDescriptor) protore panic(fmt.Errorf("field total_value of message elys.stablestake.Params is not mutable")) case "elys.stablestake.Params.max_leverage_ratio": panic(fmt.Errorf("field max_leverage_ratio of message elys.stablestake.Params is not mutable")) + case "elys.stablestake.Params.max_withdraw_ratio": + panic(fmt.Errorf("field max_withdraw_ratio of message elys.stablestake.Params is not mutable")) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1399,6 +1418,8 @@ func (x *fastReflection_Params) NewField(fd protoreflect.FieldDescriptor) protor return protoreflect.ValueOfString("") case "elys.stablestake.Params.max_leverage_ratio": return protoreflect.ValueOfString("") + case "elys.stablestake.Params.max_withdraw_ratio": + return protoreflect.ValueOfString("") default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: elys.stablestake.Params")) @@ -1511,6 +1532,10 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { if l > 0 { n += 1 + l + runtime.Sov(uint64(l)) } + l = len(x.MaxWithdrawRatio) + if l > 0 { + n += 1 + l + runtime.Sov(uint64(l)) + } if x.unknownFields != nil { n += len(x.unknownFields) } @@ -1540,6 +1565,13 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { i -= len(x.unknownFields) copy(dAtA[i:], x.unknownFields) } + if len(x.MaxWithdrawRatio) > 0 { + i -= len(x.MaxWithdrawRatio) + copy(dAtA[i:], x.MaxWithdrawRatio) + i = runtime.EncodeVarint(dAtA, i, uint64(len(x.MaxWithdrawRatio))) + i-- + dAtA[i] = 0x62 + } if len(x.MaxLeverageRatio) > 0 { i -= len(x.MaxLeverageRatio) copy(dAtA[i:], x.MaxLeverageRatio) @@ -2003,6 +2035,38 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { } x.MaxLeverageRatio = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 12: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field MaxWithdrawRatio", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.MaxWithdrawRatio = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := runtime.Skip(dAtA[iNdEx:]) @@ -2175,6 +2239,7 @@ type Params struct { HealthGainFactor string `protobuf:"bytes,9,opt,name=health_gain_factor,json=healthGainFactor,proto3" json:"health_gain_factor,omitempty"` TotalValue string `protobuf:"bytes,10,opt,name=total_value,json=totalValue,proto3" json:"total_value,omitempty"` MaxLeverageRatio string `protobuf:"bytes,11,opt,name=max_leverage_ratio,json=maxLeverageRatio,proto3" json:"max_leverage_ratio,omitempty"` + MaxWithdrawRatio string `protobuf:"bytes,12,opt,name=max_withdraw_ratio,json=maxWithdrawRatio,proto3" json:"max_withdraw_ratio,omitempty"` } func (x *Params) Reset() { @@ -2274,6 +2339,13 @@ func (x *Params) GetMaxLeverageRatio() string { return "" } +func (x *Params) GetMaxWithdrawRatio() string { + if x != nil { + return x.MaxWithdrawRatio + } + return "" +} + var File_elys_stablestake_params_proto protoreflect.FileDescriptor var file_elys_stablestake_params_proto_rawDesc = []byte{ @@ -2335,8 +2407,8 @@ var file_elys_stablestake_params_proto_rawDesc = []byte{ 0x0a, 0x20, 0x01, 0x28, 0x09, 0x42, 0x2b, 0xc8, 0xde, 0x1f, 0x00, 0xda, 0xde, 0x1f, 0x15, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x6d, 0x61, 0x74, 0x68, 0x2e, 0x49, 0x6e, 0x74, 0xd2, 0xb4, 0x2d, 0x0a, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x49, - 0x6e, 0x74, 0x52, 0x0a, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xa4, - 0x07, 0x0a, 0x06, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x23, 0x0a, 0x0d, 0x64, 0x65, 0x70, + 0x6e, 0x74, 0x52, 0x0a, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x85, + 0x08, 0x0a, 0x06, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x23, 0x0a, 0x0d, 0x64, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x64, 0x65, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x44, 0x65, 0x6e, 0x6f, 0x6d, 0x12, 0x5a, 0x0a, 0x0f, 0x72, 0x65, 0x64, 0x65, 0x6d, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x72, 0x61, 0x74, @@ -2394,19 +2466,25 @@ var file_elys_stablestake_params_proto_rawDesc = []byte{ 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x6d, 0x61, 0x74, 0x68, 0x2e, 0x4c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x44, 0x65, 0x63, 0xd2, 0xb4, 0x2d, 0x0a, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x44, 0x65, 0x63, 0x52, 0x10, 0x6d, 0x61, 0x78, 0x4c, 0x65, 0x76, 0x65, 0x72, 0x61, 0x67, 0x65, - 0x52, 0x61, 0x74, 0x69, 0x6f, 0x42, 0xb7, 0x01, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x6c, - 0x79, 0x73, 0x2e, 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0x42, 0x0b, - 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x31, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x65, 0x6c, 0x79, 0x73, 0x2d, 0x6e, - 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x65, 0x6c, 0x79, 0x73, 0x2f, 0x61, 0x70, 0x69, 0x2f, - 0x65, 0x6c, 0x79, 0x73, 0x2f, 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, - 0xa2, 0x02, 0x03, 0x45, 0x53, 0x58, 0xaa, 0x02, 0x10, 0x45, 0x6c, 0x79, 0x73, 0x2e, 0x53, 0x74, - 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0xca, 0x02, 0x10, 0x45, 0x6c, 0x79, 0x73, - 0x5c, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0xe2, 0x02, 0x1c, 0x45, - 0x6c, 0x79, 0x73, 0x5c, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0x5c, - 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x11, 0x45, 0x6c, - 0x79, 0x73, 0x3a, 0x3a, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x61, 0x74, 0x69, 0x6f, 0x12, 0x5f, 0x0a, 0x12, 0x6d, 0x61, 0x78, 0x5f, 0x77, 0x69, 0x74, + 0x68, 0x64, 0x72, 0x61, 0x77, 0x5f, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x18, 0x0c, 0x20, 0x01, 0x28, + 0x09, 0x42, 0x31, 0xc8, 0xde, 0x1f, 0x00, 0xda, 0xde, 0x1f, 0x1b, 0x63, 0x6f, 0x73, 0x6d, 0x6f, + 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x6d, 0x61, 0x74, 0x68, 0x2e, 0x4c, 0x65, 0x67, + 0x61, 0x63, 0x79, 0x44, 0x65, 0x63, 0xd2, 0xb4, 0x2d, 0x0a, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2e, 0x44, 0x65, 0x63, 0x52, 0x10, 0x6d, 0x61, 0x78, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, + 0x77, 0x52, 0x61, 0x74, 0x69, 0x6f, 0x42, 0xb7, 0x01, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x65, + 0x6c, 0x79, 0x73, 0x2e, 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0x42, + 0x0b, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x31, + 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x65, 0x6c, 0x79, 0x73, 0x2d, + 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x65, 0x6c, 0x79, 0x73, 0x2f, 0x61, 0x70, 0x69, + 0x2f, 0x65, 0x6c, 0x79, 0x73, 0x2f, 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, + 0x65, 0xa2, 0x02, 0x03, 0x45, 0x53, 0x58, 0xaa, 0x02, 0x10, 0x45, 0x6c, 0x79, 0x73, 0x2e, 0x53, + 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0xca, 0x02, 0x10, 0x45, 0x6c, 0x79, + 0x73, 0x5c, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, 0xe2, 0x02, 0x1c, + 0x45, 0x6c, 0x79, 0x73, 0x5c, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, + 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x11, 0x45, + 0x6c, 0x79, 0x73, 0x3a, 0x3a, 0x53, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x73, 0x74, 0x61, 0x6b, 0x65, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/proto/elys/stablestake/params.proto b/proto/elys/stablestake/params.proto index 4fe5f0acb..9c616ac82 100644 --- a/proto/elys/stablestake/params.proto +++ b/proto/elys/stablestake/params.proto @@ -100,4 +100,9 @@ message Params { (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", (gogoproto.nullable) = false ]; + string max_withdraw_ratio = 12 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", + (gogoproto.nullable) = false + ]; } diff --git a/x/stablestake/keeper/msg_server_unbond.go b/x/stablestake/keeper/msg_server_unbond.go index 5d346f33e..1637f11d6 100644 --- a/x/stablestake/keeper/msg_server_unbond.go +++ b/x/stablestake/keeper/msg_server_unbond.go @@ -37,7 +37,7 @@ func (k msgServer) Unbond(goCtx context.Context, msg *types.MsgUnbond) (*types.M redemptionAmount := shareCoin.Amount.ToLegacyDec().Mul(redemptionRate).RoundInt() amountAfterRedemption := params.TotalValue.Sub(redemptionAmount) - maxAllowed := (params.TotalValue.ToLegacyDec().Mul(params.MaxLeverageRatio)).TruncateInt() + maxAllowed := (params.TotalValue.ToLegacyDec().Mul(params.MaxWithdrawRatio)).TruncateInt() if amountAfterRedemption.LT(maxAllowed) { return nil, types.ErrInvalidWithdraw } diff --git a/x/stablestake/migrations/v8_migration.go b/x/stablestake/migrations/v8_migration.go new file mode 100644 index 000000000..cb93f181a --- /dev/null +++ b/x/stablestake/migrations/v8_migration.go @@ -0,0 +1,13 @@ +package migrations + +import ( + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (m Migrator) V8Migration(ctx sdk.Context) error { + params := m.keeper.GetParams(ctx) + params.MaxWithdrawRatio = math.LegacyMustNewDecFromStr("0.9") + m.keeper.SetParams(ctx, params) + return nil +} diff --git a/x/stablestake/module.go b/x/stablestake/module.go index 43f47fd00..441584fd2 100644 --- a/x/stablestake/module.go +++ b/x/stablestake/module.go @@ -120,7 +120,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) m := migrations.NewMigrator(am.keeper) - err := cfg.RegisterMigration(types.ModuleName, 6, m.V7Migration) + err := cfg.RegisterMigration(types.ModuleName, 7, m.V8Migration) if err != nil { panic(err) } @@ -147,7 +147,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1 -func (AppModule) ConsensusVersion() uint64 { return 7 } +func (AppModule) ConsensusVersion() uint64 { return 8 } // BeginBlock contains the logic that is automatically triggered at the beginning of each block func (am AppModule) BeginBlock(goCtx context.Context) error { diff --git a/x/stablestake/types/params.go b/x/stablestake/types/params.go index cd4161a61..adf32cb80 100644 --- a/x/stablestake/types/params.go +++ b/x/stablestake/types/params.go @@ -2,6 +2,7 @@ package types import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "cosmossdk.io/math" @@ -21,6 +22,7 @@ func DefaultParams() Params { HealthGainFactor: math.LegacyOneDec(), TotalValue: math.ZeroInt(), MaxLeverageRatio: math.LegacyMustNewDecFromStr("0.7"), + MaxWithdrawRatio: math.LegacyMustNewDecFromStr("0.7"), } } diff --git a/x/stablestake/types/params.pb.go b/x/stablestake/types/params.pb.go index d061314fc..a710fd745 100644 --- a/x/stablestake/types/params.pb.go +++ b/x/stablestake/types/params.pb.go @@ -98,6 +98,7 @@ type Params struct { HealthGainFactor cosmossdk_io_math.LegacyDec `protobuf:"bytes,9,opt,name=health_gain_factor,json=healthGainFactor,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"health_gain_factor"` TotalValue cosmossdk_io_math.Int `protobuf:"bytes,10,opt,name=total_value,json=totalValue,proto3,customtype=cosmossdk.io/math.Int" json:"total_value"` MaxLeverageRatio cosmossdk_io_math.LegacyDec `protobuf:"bytes,11,opt,name=max_leverage_ratio,json=maxLeverageRatio,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"max_leverage_ratio"` + MaxWithdrawRatio cosmossdk_io_math.LegacyDec `protobuf:"bytes,12,opt,name=max_withdraw_ratio,json=maxWithdrawRatio,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"max_withdraw_ratio"` } func (m *Params) Reset() { *m = Params{} } @@ -155,38 +156,39 @@ func init() { func init() { proto.RegisterFile("elys/stablestake/params.proto", fileDescriptor_d2a5512d8c510b7b) } var fileDescriptor_d2a5512d8c510b7b = []byte{ - // 487 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x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92, 0x02, 0x66, 0xde, 0x9f, 0x54, 0xcf, 0x0f, 0xbe, 0xac, 0xb3, 0xee, 0x6a, - 0x8a, 0x56, 0x53, 0xb4, 0x9a, 0xa2, 0xff, 0x98, 0xa2, 0xaa, 0xdd, 0x0c, 0x4a, 0x9e, 0xe2, 0x39, - 0x16, 0x10, 0x63, 0xf5, 0x6f, 0x84, 0x72, 0x7a, 0xad, 0xdb, 0xcd, 0xa0, 0x1c, 0x35, 0xac, 0xa0, - 0x42, 0x1d, 0xbf, 0xbd, 0x98, 0xb9, 0xd6, 0xe5, 0xcc, 0xb5, 0x7e, 0xcd, 0x5c, 0xeb, 0xf3, 0xdc, - 0xed, 0x5c, 0xce, 0xdd, 0xce, 0x8f, 0xb9, 0xdb, 0x39, 0x7d, 0x11, 0x0b, 0x4a, 0xce, 0xc6, 0x5e, - 0xa8, 0x32, 0xbf, 0x5a, 0xad, 0x47, 0x12, 0xe9, 0x93, 0x2a, 0x26, 0xe6, 0xe0, 0x97, 0x4b, 0x8b, - 0x98, 0xa6, 0x39, 0xea, 0x71, 0xd7, 0x6c, 0xd1, 0x57, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xd7, - 0x7c, 0x05, 0x2b, 0xa9, 0x07, 0x00, 0x00, + // 504 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x95, 0x41, 0x6b, 0x13, 0x41, + 0x14, 0xc7, 0xb3, 0x56, 0x63, 0x3b, 0x4d, 0x6d, 0x5d, 0xaa, 0xac, 0x15, 0xb7, 0xb5, 0x5e, 0x0a, + 0xd2, 0xac, 0xe2, 0x37, 0x28, 0x41, 0x89, 0x44, 0x90, 0x3d, 0x54, 0x28, 0xc8, 0xf0, 0xb2, 0xfb, + 0xdc, 0x1d, 0xb2, 0x3b, 0xb3, 0xcc, 0xbc, 0x36, 0xc9, 0x07, 0xf0, 0xee, 0x87, 0xf1, 0x43,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0x08, 0x3e, 0xd5, + 0x2c, 0x2b, 0x38, 0x7a, 0x7f, 0x3e, 0xf1, 0x9d, 0x8b, 0x89, 0xef, 0xfc, 0x99, 0xf8, 0xce, 0xb7, + 0xa9, 0xdf, 0xb8, 0x98, 0xfa, 0x8d, 0x5f, 0x53, 0xbf, 0x71, 0xf2, 0x2a, 0x11, 0x94, 0x9e, 0xf6, + 0xdb, 0x91, 0xca, 0x83, 0x72, 0x77, 0x1f, 0x4a, 0xa4, 0xa1, 0xd2, 0x03, 0x7b, 0x08, 0x46, 0x73, + 0x9b, 0x9e, 0xc6, 0x05, 0x9a, 0x7e, 0xd3, 0xae, 0xe9, 0x37, 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, + 0x0e, 0x3d, 0x93, 0x4a, 0x0a, 0x08, 0x00, 0x00, } func (m *LegacyParams) Marshal() (dAtA []byte, err error) { @@ -324,6 +326,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size := m.MaxWithdrawRatio.Size() + i -= size + if _, err := m.MaxWithdrawRatio.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintParams(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x62 { size := m.MaxLeverageRatio.Size() i -= size @@ -503,6 +515,8 @@ func (m *Params) Size() (n int) { n += 1 + l + sovParams(uint64(l)) l = m.MaxLeverageRatio.Size() n += 1 + l + sovParams(uint64(l)) + l = m.MaxWithdrawRatio.Size() + n += 1 + l + sovParams(uint64(l)) return n } @@ -1271,6 +1285,40 @@ func (m *Params) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 12: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MaxWithdrawRatio", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthParams + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthParams + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.MaxWithdrawRatio.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) From 0a4e15e73bac1e66e47df6b2c6e01e389e46d906 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Fri, 24 Jan 2025 21:24:11 +0530 Subject: [PATCH 2/2] Fee collection in amm (#1121) * pay to tresury for exit pool * collect in join and exit --------- Co-authored-by: Abhinav Kumar <57705190+avkr003@users.noreply.github.com> --- x/amm/keeper/apply_exit_pool_state_change.go | 32 +++++++++++++++++++ x/amm/keeper/apply_join_pool_state_change.go | 33 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/x/amm/keeper/apply_exit_pool_state_change.go b/x/amm/keeper/apply_exit_pool_state_change.go index f1a24bd4d..54d7dbcb3 100644 --- a/x/amm/keeper/apply_exit_pool_state_change.go +++ b/x/amm/keeper/apply_exit_pool_state_change.go @@ -27,6 +27,38 @@ func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exite k.SetPool(ctx, pool) rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + weightRecoveryFeeAmount := sdkmath.ZeroInt() + poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress()) + // send half (weight breaking fee portion) of weight breaking fee to rebalance treasury + if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() { + params := k.GetParams(ctx) + rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee + weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) + + for _, coin := range exitCoins { + weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() + + if weightRecoveryFeeAmount.IsPositive() { + // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ + netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)} + + err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) + if err != nil { + return err + } + + err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) + if err != nil { + return err + } + + // Track amount in pool + weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) + k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(sdkmath.LegacyDec(weightRecoveryFeeAmount))))) + } + } + } if weightBalanceBonus.IsPositive() { // calculate treasury amounts to send as bonus diff --git a/x/amm/keeper/apply_join_pool_state_change.go b/x/amm/keeper/apply_join_pool_state_change.go index 9fa8e132f..06d65fe2f 100644 --- a/x/amm/keeper/apply_join_pool_state_change.go +++ b/x/amm/keeper/apply_join_pool_state_change.go @@ -2,6 +2,7 @@ package keeper import ( "cosmossdk.io/math" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/amm/types" ) @@ -26,6 +27,38 @@ func (k Keeper) ApplyJoinPoolStateChange( rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + weightRecoveryFeeAmount := sdkmath.ZeroInt() + poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress()) + // send half (weight breaking fee portion) of weight breaking fee to rebalance treasury + if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() { + params := k.GetParams(ctx) + rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee + weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) + for _, coin := range joinCoins { + weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() + + if weightRecoveryFeeAmount.IsPositive() { + // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ + netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)} + + err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) + if err != nil { + return err + } + + err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) + if err != nil { + return err + } + + // Track amount in pool + weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) + k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(sdkmath.LegacyDec(weightRecoveryFeeAmount))))) + } + } + } + if weightBalanceBonus.IsPositive() { // calculate treasury amounts to send as bonus weightBalanceBonusCoins := PortionCoins(joinCoins, weightBalanceBonus)