From fe1436373d39947b34810254dc6f33785f12280e Mon Sep 17 00:00:00 2001 From: kogisin Date: Thu, 4 Aug 2022 11:16:15 +0900 Subject: [PATCH 1/3] fix: rename lfCoin to UnfarmingCoin and add validation check for the liquid coin denom --- proto/squad/liquidfarming/v1beta1/tx.proto | 10 +- x/liquidfarming/keeper/liquidfarm.go | 16 +-- x/liquidfarming/keeper/msg_server.go | 2 +- x/liquidfarming/types/msgs.go | 40 +++++--- x/liquidfarming/types/msgs_test.go | 22 ++++- x/liquidfarming/types/tx.pb.go | 107 ++++++++++----------- 6 files changed, 108 insertions(+), 89 deletions(-) diff --git a/proto/squad/liquidfarming/v1beta1/tx.proto b/proto/squad/liquidfarming/v1beta1/tx.proto index 0eeb6c63b..203442693 100644 --- a/proto/squad/liquidfarming/v1beta1/tx.proto +++ b/proto/squad/liquidfarming/v1beta1/tx.proto @@ -53,10 +53,9 @@ message MsgUnfarm { string farmer = 2; - cosmos.base.v1beta1.Coin lf_coin = 3 [ + cosmos.base.v1beta1.Coin unfarming_coin = 3 [ (gogoproto.nullable) = false, - (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin", - (gogoproto.customname) = "LFCoin" + (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin" ]; } @@ -71,10 +70,9 @@ message MsgUnfarmAndWithdraw { string farmer = 2; - cosmos.base.v1beta1.Coin lf_coin = 3 [ + cosmos.base.v1beta1.Coin unfarming_coin = 3 [ (gogoproto.nullable) = false, - (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin", - (gogoproto.customname) = "LFCoin" + (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin" ]; } diff --git a/x/liquidfarming/keeper/liquidfarm.go b/x/liquidfarming/keeper/liquidfarm.go index 2cfc4656f..d85fedb7e 100644 --- a/x/liquidfarming/keeper/liquidfarm.go +++ b/x/liquidfarming/keeper/liquidfarm.go @@ -100,14 +100,14 @@ type UnfarmInfo struct { // Unfarm handles types.MsgUnfarm to unfarm LFCoin. // The logic doesn't check whether or not liquid farm exists because it can be removed for some reason and // farmers still need to be able to unfarm their pool coin. -func (k Keeper) Unfarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress, lfCoin sdk.Coin) (UnfarmInfo, error) { +func (k Keeper) Unfarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress, unfarmingCoin sdk.Coin) (UnfarmInfo, error) { reserveAddr := types.LiquidFarmReserveAddress(poolId) lfCoinDenom := types.LiquidFarmCoinDenom(poolId) lfCoinBalance := k.bankKeeper.SpendableCoins(ctx, farmer).AmountOf(lfCoinDenom) - if lfCoinBalance.LT(lfCoin.Amount) { + if lfCoinBalance.LT(unfarmingCoin.Amount) { return UnfarmInfo{}, - sdkerrors.Wrapf(types.ErrInsufficientUnfarmingAmount, "%s is smaller than %s", lfCoinBalance, lfCoin.Amount) + sdkerrors.Wrapf(types.ErrInsufficientUnfarmingAmount, "%s is smaller than %s", lfCoinBalance, unfarmingCoin.Amount) } pool, found := k.liquidityKeeper.GetPool(ctx, poolId) @@ -121,14 +121,14 @@ func (k Keeper) Unfarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress, lf unfarmFee := sdk.ZeroInt() // TODO: TBD // UnfarmedAmount = TotalStakedLPAmount / TotalSupplyLFAmount * UnfarmingLFAmount * (1 - UnfarmFee) - unfarmedAmt := lpCoinTotalStaked.Quo(lfCoinTotalSupply).Mul(lfCoin.Amount).Mul(sdk.OneInt().Sub(unfarmFee)) + unfarmedAmt := lpCoinTotalStaked.Quo(lfCoinTotalSupply).Mul(unfarmingCoin.Amount).Mul(sdk.OneInt().Sub(unfarmFee)) unfarmedCoin := sdk.NewCoin(pool.PoolCoinDenom, unfarmedAmt) // Send the unfarming LFCoin to module account and burn them - if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, farmer, types.ModuleName, sdk.NewCoins(lfCoin)); err != nil { + if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, farmer, types.ModuleName, sdk.NewCoins(unfarmingCoin)); err != nil { return UnfarmInfo{}, err } - if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(lfCoin)); err != nil { + if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(unfarmingCoin)); err != nil { return UnfarmInfo{}, err } @@ -145,7 +145,7 @@ func (k Keeper) Unfarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress, lf types.EventTypeUnfarm, sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)), sdk.NewAttribute(types.AttributeKeyFarmer, farmer.String()), - sdk.NewAttribute(types.AttributeKeyUnfarmingCoin, lfCoin.String()), + sdk.NewAttribute(types.AttributeKeyUnfarmingCoin, unfarmingCoin.String()), sdk.NewAttribute(types.AttributeKeyUnfarmedCoin, unfarmedCoin.String()), ), }) @@ -160,7 +160,7 @@ func (k Keeper) Unfarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress, lf // UnfarmAndWithdraw handles types.MsgUnfarmAndWithdraw to unfarm LFCoin and withdraw pool coin from the pool. func (k Keeper) UnfarmAndWithdraw(ctx sdk.Context, msg *types.MsgUnfarmAndWithdraw) error { - unfarmInfo, err := k.Unfarm(ctx, msg.PoolId, msg.GetFarmer(), msg.LFCoin) + unfarmInfo, err := k.Unfarm(ctx, msg.PoolId, msg.GetFarmer(), msg.UnfarmingCoin) if err != nil { return sdkerrors.Wrapf(err, "unable to unfarm") } diff --git a/x/liquidfarming/keeper/msg_server.go b/x/liquidfarming/keeper/msg_server.go index c49af9f46..d5dbadbdb 100644 --- a/x/liquidfarming/keeper/msg_server.go +++ b/x/liquidfarming/keeper/msg_server.go @@ -46,7 +46,7 @@ func (m msgServer) CancelQueuedFarming(goCtx context.Context, msg *types.MsgCanc func (m msgServer) Unfarm(goCtx context.Context, msg *types.MsgUnfarm) (*types.MsgUnfarmResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - if _, err := m.Keeper.Unfarm(ctx, msg.PoolId, msg.GetFarmer(), msg.LFCoin); err != nil { + if _, err := m.Keeper.Unfarm(ctx, msg.PoolId, msg.GetFarmer(), msg.UnfarmingCoin); err != nil { return nil, err } diff --git a/x/liquidfarming/types/msgs.go b/x/liquidfarming/types/msgs.go index 4b32570cb..16bebc636 100644 --- a/x/liquidfarming/types/msgs.go +++ b/x/liquidfarming/types/msgs.go @@ -73,11 +73,11 @@ func (msg MsgFarm) GetFarmer() sdk.AccAddress { } // NewMsgUnfarm creates a new MsgUnfarm -func NewMsgUnfarm(poolId uint64, farmer string, lfCoin sdk.Coin) *MsgUnfarm { +func NewMsgUnfarm(poolId uint64, farmer string, unfarmingCoin sdk.Coin) *MsgUnfarm { return &MsgUnfarm{ - PoolId: poolId, - Farmer: farmer, - LFCoin: lfCoin, + PoolId: poolId, + Farmer: farmer, + UnfarmingCoin: unfarmingCoin, } } @@ -92,11 +92,15 @@ func (msg MsgUnfarm) ValidateBasic() error { if msg.PoolId == 0 { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid pool id") } - if !msg.LFCoin.IsPositive() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "liquid farming coin must be positive") + if !msg.UnfarmingCoin.IsPositive() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unfarming coin must be positive") + } + if err := msg.UnfarmingCoin.Validate(); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid unfarming coin: %v", err) } - if err := msg.LFCoin.Validate(); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid liquid farming coin: %v", err) + expCoinDenom := LiquidFarmCoinDenom(msg.PoolId) + if msg.UnfarmingCoin.Denom != expCoinDenom { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected denom: %s, but got: %s", expCoinDenom, msg.UnfarmingCoin.Denom) } return nil } @@ -122,11 +126,11 @@ func (msg MsgUnfarm) GetFarmer() sdk.AccAddress { } // NewMsgUnfarmAndWithdraw creates a new MsgUnfarmAndWithdraw -func NewMsgUnfarmAndWithdraw(poolId uint64, farmer string, lfCoin sdk.Coin) *MsgUnfarmAndWithdraw { +func NewMsgUnfarmAndWithdraw(poolId uint64, farmer string, unfarmingCoin sdk.Coin) *MsgUnfarmAndWithdraw { return &MsgUnfarmAndWithdraw{ - PoolId: poolId, - Farmer: farmer, - LFCoin: lfCoin, + PoolId: poolId, + Farmer: farmer, + UnfarmingCoin: unfarmingCoin, } } @@ -141,11 +145,15 @@ func (msg MsgUnfarmAndWithdraw) ValidateBasic() error { if msg.PoolId == 0 { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid pool id") } - if !msg.LFCoin.IsPositive() { - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "liquid farming coin must be positive") + if !msg.UnfarmingCoin.IsPositive() { + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "unfarming coin must be positive") + } + if err := msg.UnfarmingCoin.Validate(); err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid unfarming coin: %v", err) } - if err := msg.LFCoin.Validate(); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid liquid farming coin: %v", err) + expCoinDenom := LiquidFarmCoinDenom(msg.PoolId) + if msg.UnfarmingCoin.Denom != expCoinDenom { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected denom: %s, but got: %s", expCoinDenom, msg.UnfarmingCoin.Denom) } return nil } diff --git a/x/liquidfarming/types/msgs_test.go b/x/liquidfarming/types/msgs_test.go index bcd454a1f..a9e51485a 100644 --- a/x/liquidfarming/types/msgs_test.go +++ b/x/liquidfarming/types/msgs_test.go @@ -92,9 +92,16 @@ func TestMsgUnfarm(t *testing.T) { { "invalid lf coin", func(msg *types.MsgUnfarm) { - msg.LFCoin = sdk.NewInt64Coin("lf1", 0) + msg.UnfarmingCoin = sdk.NewInt64Coin("lf1", 0) }, - "liquid farming coin must be positive: invalid request", + "unfarming coin must be positive: invalid request", + }, + { + "invalid lf coin denom", + func(msg *types.MsgUnfarm) { + msg.UnfarmingCoin = sdk.NewInt64Coin("pool1", 100_000) + }, + "expected denom: lf1, but got: pool1: invalid request", }, } { t.Run(tc.name, func(t *testing.T) { @@ -143,9 +150,16 @@ func TestMsgUnfarmAndWithdraw(t *testing.T) { { "invalid lf coin", func(msg *types.MsgUnfarmAndWithdraw) { - msg.LFCoin = sdk.NewInt64Coin("lf1", 0) + msg.UnfarmingCoin = sdk.NewInt64Coin("lf1", 0) + }, + "unfarming coin must be positive: invalid request", + }, + { + "invalid lf coin denom", + func(msg *types.MsgUnfarmAndWithdraw) { + msg.UnfarmingCoin = sdk.NewInt64Coin("pool1", 100_000) }, - "liquid farming coin must be positive: invalid request", + "expected denom: lf1, but got: pool1: invalid request", }, } { t.Run(tc.name, func(t *testing.T) { diff --git a/x/liquidfarming/types/tx.pb.go b/x/liquidfarming/types/tx.pb.go index 4cdf091ca..efd3d0f9f 100644 --- a/x/liquidfarming/types/tx.pb.go +++ b/x/liquidfarming/types/tx.pb.go @@ -109,9 +109,9 @@ var xxx_messageInfo_MsgFarmResponse proto.InternalMessageInfo // MsgUnfarm defines a SDK message for unfarming LFCoin. type MsgUnfarm struct { - PoolId uint64 `protobuf:"varint,1,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"` - Farmer string `protobuf:"bytes,2,opt,name=farmer,proto3" json:"farmer,omitempty"` - LFCoin types.Coin `protobuf:"bytes,3,opt,name=lf_coin,json=lfCoin,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coin" json:"lf_coin"` + PoolId uint64 `protobuf:"varint,1,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"` + Farmer string `protobuf:"bytes,2,opt,name=farmer,proto3" json:"farmer,omitempty"` + UnfarmingCoin types.Coin `protobuf:"bytes,3,opt,name=unfarming_coin,json=unfarmingCoin,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coin" json:"unfarming_coin"` } func (m *MsgUnfarm) Reset() { *m = MsgUnfarm{} } @@ -186,9 +186,9 @@ var xxx_messageInfo_MsgUnfarmResponse proto.InternalMessageInfo // MsgUnfarmAndWithdraw defines a SDK message for unfarming LFCoin. type MsgUnfarmAndWithdraw struct { - PoolId uint64 `protobuf:"varint,1,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"` - Farmer string `protobuf:"bytes,2,opt,name=farmer,proto3" json:"farmer,omitempty"` - LFCoin types.Coin `protobuf:"bytes,3,opt,name=lf_coin,json=lfCoin,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coin" json:"lf_coin"` + PoolId uint64 `protobuf:"varint,1,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"` + Farmer string `protobuf:"bytes,2,opt,name=farmer,proto3" json:"farmer,omitempty"` + UnfarmingCoin types.Coin `protobuf:"bytes,3,opt,name=unfarming_coin,json=unfarmingCoin,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coin" json:"unfarming_coin"` } func (m *MsgUnfarmAndWithdraw) Reset() { *m = MsgUnfarmAndWithdraw{} } @@ -511,46 +511,45 @@ func init() { } var fileDescriptor_db3f4cd082b3b711 = []byte{ - // 624 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x96, 0xb1, 0x6f, 0xd3, 0x40, - 0x14, 0xc6, 0x73, 0xb4, 0xb8, 0xf4, 0x52, 0x40, 0x71, 0x4a, 0x08, 0x06, 0x39, 0x51, 0x54, 0x41, - 0x40, 0xd4, 0x26, 0xad, 0x84, 0x54, 0x98, 0x48, 0x45, 0x24, 0x24, 0x2c, 0x95, 0x48, 0x08, 0xa9, - 0x4b, 0xb1, 0x73, 0x67, 0xf7, 0x84, 0xe3, 0x4b, 0x7c, 0x36, 0xb4, 0x23, 0x13, 0x8c, 0xfc, 0x09, - 0x0c, 0x4c, 0x2c, 0x08, 0xb1, 0x32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 0xff, 0x97, 0xc3, 0xf2, 0x96, 0x26, 0x08, 0x00, 0x00, + // 602 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x96, 0x31, 0x6f, 0xd3, 0x4e, + 0x18, 0xc6, 0x73, 0xff, 0xe6, 0x9f, 0x92, 0x37, 0x05, 0x14, 0xa7, 0x84, 0x60, 0x90, 0x13, 0x45, + 0x15, 0x04, 0x44, 0x6d, 0xd2, 0x4a, 0x48, 0xc0, 0x44, 0x2a, 0x2a, 0x31, 0x58, 0x2a, 0x91, 0x10, + 0x52, 0x97, 0x62, 0xe7, 0x2e, 0xd7, 0x13, 0x89, 0x2f, 0xf1, 0xd9, 0xd0, 0x8e, 0x4c, 0x30, 0xf2, + 0x11, 0x18, 0x98, 0x58, 0x18, 0x10, 0x3b, 0x62, 0xea, 0xd8, 0x91, 0x09, 0x50, 0xf2, 0x45, 0x90, + 0xed, 0xcb, 0x35, 0xd0, 0xa8, 0x09, 0x91, 0x3a, 0x74, 0x8a, 0x2f, 0x7e, 0xde, 0xe7, 0x7d, 0x7e, + 0xd2, 0xbd, 0xe7, 0x83, 0x15, 0xd1, 0x0f, 0x1d, 0x6c, 0x75, 0x58, 0x3f, 0x64, 0xb8, 0xed, 0xf8, + 0x5d, 0xe6, 0x51, 0xeb, 0x65, 0xdd, 0x25, 0x81, 0x53, 0xb7, 0x82, 0x3d, 0xb3, 0xe7, 0xf3, 0x80, + 0x6b, 0x57, 0x63, 0x95, 0xf9, 0x87, 0xca, 0x94, 0x2a, 0x7d, 0x99, 0x72, 0xca, 0x63, 0x9d, 0x15, + 0x3d, 0x25, 0x25, 0xba, 0xd1, 0xe2, 0xa2, 0xcb, 0x85, 0xe5, 0x3a, 0x82, 0x28, 0xc3, 0x16, 0x67, + 0x9e, 0x7c, 0x7f, 0x6b, 0xfc, 0x7d, 0x3f, 0x24, 0xfe, 0xbe, 0x52, 0xf5, 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f5, 0x99, 0xa5, 0xa3, 0x56, 0x8d, 0xad, 0x83, 0x81, + 0x81, 0x0e, 0x07, 0x06, 0xfa, 0x35, 0x30, 0xd0, 0xbb, 0xa1, 0x91, 0x3a, 0x1c, 0x1a, 0xa9, 0xef, + 0x43, 0x23, 0xb5, 0x7d, 0xf7, 0xd8, 0xc8, 0x45, 0xde, 0xab, 0x1d, 0xc7, 0x15, 0x56, 0x72, 0x11, + 0xd8, 0xfb, 0xeb, 0x2a, 0x10, 0x8f, 0xa1, 0x9b, 0x89, 0xbf, 0xc3, 0xeb, 0xbf, 0x03, 0x00, 0x00, + 0xff, 0xff, 0x2b, 0xf2, 0x20, 0x9b, 0x2e, 0x08, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -914,7 +913,7 @@ func (m *MsgUnfarm) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.LFCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.UnfarmingCoin.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -982,7 +981,7 @@ func (m *MsgUnfarmAndWithdraw) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.LFCoin.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.UnfarmingCoin.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -1274,7 +1273,7 @@ func (m *MsgUnfarm) Size() (n int) { if l > 0 { n += 1 + l + sovTx(uint64(l)) } - l = m.LFCoin.Size() + l = m.UnfarmingCoin.Size() n += 1 + l + sovTx(uint64(l)) return n } @@ -1301,7 +1300,7 @@ func (m *MsgUnfarmAndWithdraw) Size() (n int) { if l > 0 { n += 1 + l + sovTx(uint64(l)) } - l = m.LFCoin.Size() + l = m.UnfarmingCoin.Size() n += 1 + l + sovTx(uint64(l)) return n } @@ -1666,7 +1665,7 @@ func (m *MsgUnfarm) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field LFCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field UnfarmingCoin", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -1693,7 +1692,7 @@ func (m *MsgUnfarm) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.LFCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.UnfarmingCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -1850,7 +1849,7 @@ func (m *MsgUnfarmAndWithdraw) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field LFCoin", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field UnfarmingCoin", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -1877,7 +1876,7 @@ func (m *MsgUnfarmAndWithdraw) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.LFCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.UnfarmingCoin.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex From 9ebe946254ea9318611fc7b9e60786e8a3c8757a Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 5 Aug 2022 13:04:22 +0900 Subject: [PATCH 2/3] chore: fix RewardsAuctions query command, apply updated spec to store LiquidFarm object --- x/liquidfarming/abci.go | 39 +++++++++++++++++++ x/liquidfarming/client/cli/query.go | 6 +-- x/liquidfarming/keeper/genesis.go | 3 ++ x/liquidfarming/keeper/grpc_query.go | 6 +-- x/liquidfarming/keeper/hooks.go | 2 +- x/liquidfarming/keeper/keeper.go | 11 ------ x/liquidfarming/keeper/keeper_test.go | 4 +- x/liquidfarming/keeper/liquidfarm.go | 51 +++++++++++++------------ x/liquidfarming/keeper/store.go | 55 ++++++++++++++++++++++++++- x/liquidfarming/keeper/store_test.go | 1 - x/liquidfarming/module.go | 4 +- x/liquidfarming/types/keys.go | 12 +++++- x/liquidfarming/types/keys_test.go | 6 +++ 13 files changed, 147 insertions(+), 53 deletions(-) create mode 100644 x/liquidfarming/abci.go diff --git a/x/liquidfarming/abci.go b/x/liquidfarming/abci.go new file mode 100644 index 000000000..fe32f0cce --- /dev/null +++ b/x/liquidfarming/abci.go @@ -0,0 +1,39 @@ +package liquidfarming + +import ( + "fmt" + "time" + + "github.com/cosmos/cosmos-sdk/telemetry" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmosquad-labs/squad/v2/x/liquidfarming/keeper" + "github.com/cosmosquad-labs/squad/v2/x/liquidfarming/types" +) + +func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + + liquidFarmSet := map[uint64]types.LiquidFarm{} + for _, liquidFarm := range k.GetAllLiquidFarms(ctx) { + liquidFarmSet[liquidFarm.PoolId] = liquidFarm + } + + paramsLiquidFarmSet := map[uint64]types.LiquidFarm{} + for _, liquidFarm := range k.GetParams(ctx).LiquidFarms { + liquidFarmSet[liquidFarm.PoolId] = liquidFarm + } + + for poolId := range paramsLiquidFarmSet { + delete(liquidFarmSet, poolId) + } + + if len(liquidFarmSet) != 0 { + // Means that LiquidFarm is removed in params + fmt.Println("Removed") + } + if len(paramsLiquidFarmSet) != 0 { + // Means that LiquidFarm is newly added in params + fmt.Println("Added") + } +} diff --git a/x/liquidfarming/client/cli/query.go b/x/liquidfarming/client/cli/query.go index 7aabb219c..35ecada89 100644 --- a/x/liquidfarming/client/cli/query.go +++ b/x/liquidfarming/client/cli/query.go @@ -223,14 +223,14 @@ $ %s query %s queued-farmings --farmer %s1zaavvzxez0elundtn32qnk9lkm8kmcszzsv80v func NewQueryRewardsAuctionsCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "rewards-auctions", - Args: cobra.NoArgs, + Use: "rewards-auctions [pool-id]", + Args: cobra.ExactArgs(1), Short: "Query all rewards auctions for the liquidfarm", Long: strings.TrimSpace( fmt.Sprintf(`Query all rewards auctions for the liquidfarm on a network. Example: -$ %s query %s rewards-auctions +$ %s query %s rewards-auctions 1 `, version.AppName, types.ModuleName, ), diff --git a/x/liquidfarming/keeper/genesis.go b/x/liquidfarming/keeper/genesis.go index 50af39035..ab37ea33a 100644 --- a/x/liquidfarming/keeper/genesis.go +++ b/x/liquidfarming/keeper/genesis.go @@ -24,6 +24,9 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { k.SetParams(ctx, genState.Params) + for _, liquidFarm := range genState.Params.LiquidFarms { + k.SetLiquidFarm(ctx, liquidFarm) + } for _, record := range genState.QueuedFarmingRecords { farmerAddr, err := sdk.AccAddressFromBech32(record.Farmer) if err != nil { diff --git a/x/liquidfarming/keeper/grpc_query.go b/x/liquidfarming/keeper/grpc_query.go index be94f43a0..9795ebfbe 100644 --- a/x/liquidfarming/keeper/grpc_query.go +++ b/x/liquidfarming/keeper/grpc_query.go @@ -38,10 +38,9 @@ func (k Querier) LiquidFarms(c context.Context, req *types.QueryLiquidFarmsReque } ctx := sdk.UnwrapSDKContext(c) - params := k.GetParams(ctx) liquidFarmsRes := []types.LiquidFarmResponse{} - for _, liquidFarm := range params.LiquidFarms { + for _, liquidFarm := range k.GetAllLiquidFarms(ctx) { reserveAcc := types.LiquidFarmReserveAddress(liquidFarm.PoolId) poolCoinDenom := liquiditytypes.PoolCoinDenom(liquidFarm.PoolId) queuedAmt := k.farmingKeeper.GetAllQueuedCoinsByFarmer(ctx, reserveAcc).AmountOf(poolCoinDenom) @@ -72,10 +71,9 @@ func (k Querier) LiquidFarm(c context.Context, req *types.QueryLiquidFarmRequest } ctx := sdk.UnwrapSDKContext(c) - params := k.GetParams(ctx) liquidFarmRes := types.LiquidFarmResponse{} - for _, liquidFarm := range params.LiquidFarms { + for _, liquidFarm := range k.GetAllLiquidFarms(ctx) { if liquidFarm.PoolId == req.PoolId { reserveAcc := types.LiquidFarmReserveAddress(liquidFarm.PoolId) poolCoinDenom := liquiditytypes.PoolCoinDenom(liquidFarm.PoolId) diff --git a/x/liquidfarming/keeper/hooks.go b/x/liquidfarming/keeper/hooks.go index 44603baa9..309803a02 100644 --- a/x/liquidfarming/keeper/hooks.go +++ b/x/liquidfarming/keeper/hooks.go @@ -80,7 +80,7 @@ func (h Hooks) AfterStaked(ctx sdk.Context, stakingAcc sdk.AccAddress, stakingCo // It creates the first rewards auction if liquid farm doesn't have any auction before. // If there is an ongoing rewards auction, finish the auction and create the next one. func (h Hooks) AfterAllocateRewards(ctx sdk.Context) { - for _, liquidFarm := range h.k.GetParams(ctx).LiquidFarms { + for _, liquidFarm := range h.k.GetAllLiquidFarms(ctx) { poolId := liquidFarm.PoolId auctionId := h.k.GetLastRewardsAuctionId(ctx, poolId) diff --git a/x/liquidfarming/keeper/keeper.go b/x/liquidfarming/keeper/keeper.go index 2dce6650c..50dc41967 100644 --- a/x/liquidfarming/keeper/keeper.go +++ b/x/liquidfarming/keeper/keeper.go @@ -66,14 +66,3 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { k.paramSpace.SetParamSet(ctx, ¶ms) } - -// GetLiquidFarm returns LiquidFarm object by the given pool id. -func (k Keeper) GetLiquidFarm(ctx sdk.Context, poolId uint64) (liquidFarm types.LiquidFarm, found bool) { - params := k.GetParams(ctx) - for _, lf := range params.LiquidFarms { - if lf.PoolId == poolId { - return lf, true - } - } - return types.LiquidFarm{}, false -} diff --git a/x/liquidfarming/keeper/keeper_test.go b/x/liquidfarming/keeper/keeper_test.go index 9513c4321..a582d3386 100644 --- a/x/liquidfarming/keeper/keeper_test.go +++ b/x/liquidfarming/keeper/keeper_test.go @@ -147,9 +147,7 @@ func (s *KeeperTestSuite) deposit(depositor sdk.AccAddress, poolId uint64, depos func (s *KeeperTestSuite) createLiquidFarm(liquidFarm types.LiquidFarm) types.LiquidFarm { s.T().Helper() - params := s.keeper.GetParams(s.ctx) - params.LiquidFarms = append(params.LiquidFarms, liquidFarm) - s.keeper.SetParams(s.ctx, params) + s.keeper.SetLiquidFarm(s.ctx, liquidFarm) return liquidFarm } diff --git a/x/liquidfarming/keeper/liquidfarm.go b/x/liquidfarming/keeper/liquidfarm.go index d85fedb7e..f0c5b7298 100644 --- a/x/liquidfarming/keeper/liquidfarm.go +++ b/x/liquidfarming/keeper/liquidfarm.go @@ -12,46 +12,47 @@ import ( liquiditytypes "github.com/cosmosquad-labs/squad/v2/x/liquidity/types" ) -// Farm handles types.MsgFarm to liquid farm. -func (k Keeper) Farm(ctx sdk.Context, msg *types.MsgFarm) error { - params := k.GetParams(ctx) - - poolId := uint64(0) - minFarmAmt := sdk.ZeroInt() - for _, liquidFarm := range params.LiquidFarms { - if liquidFarm.PoolId == msg.PoolId { - poolId = liquidFarm.PoolId - minFarmAmt = liquidFarm.MinimumFarmAmount - break - } - } - if poolId == 0 { +// ValidateMsgFarm validates types.MsgFarm. +func (k Keeper) ValidateMsgFarm(ctx sdk.Context, msg *types.MsgFarm) error { + liquidFarm, found := k.GetLiquidFarm(ctx, msg.PoolId) + if !found { return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "liquid farm by pool %d not found", msg.PoolId) } + minFarmAmt := liquidFarm.MinimumFarmAmount if msg.FarmingCoin.Amount.LT(minFarmAmt) { return sdkerrors.Wrapf(types.ErrSmallerThanMinimumAmount, "%s is smaller than %s", msg.FarmingCoin.Amount, minFarmAmt) } - pool, found := k.liquidityKeeper.GetPool(ctx, poolId) + pool, found := k.liquidityKeeper.GetPool(ctx, liquidFarm.PoolId) if !found { - return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "pool %d not found", poolId) + return sdkerrors.Wrapf(sdkerrors.ErrNotFound, "pool %d not found", liquidFarm.PoolId) } if pool.PoolCoinDenom != msg.FarmingCoin.Denom { return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected denom %s, but got %s", pool.PoolCoinDenom, msg.FarmingCoin.Denom) } - farmerAddr := msg.GetFarmer() - farmingCoin := msg.FarmingCoin - reserveAddr := types.LiquidFarmReserveAddress(poolId) - - poolCoinBalance := k.bankKeeper.SpendableCoins(ctx, farmerAddr).AmountOf(pool.PoolCoinDenom) - if poolCoinBalance.LT(farmingCoin.Amount) { + poolCoinBalance := k.bankKeeper.SpendableCoins(ctx, msg.GetFarmer()).AmountOf(pool.PoolCoinDenom) + if poolCoinBalance.LT(msg.FarmingCoin.Amount) { return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "%s is smaller than %s", poolCoinBalance, minFarmAmt) } - // Reserve farming coins + return nil +} + +// Farm handles types.MsgFarm to liquid farm. +func (k Keeper) Farm(ctx sdk.Context, msg *types.MsgFarm) error { + if err := k.ValidateMsgFarm(ctx, msg); err != nil { + return err + } + + params := k.GetParams(ctx) + farmerAddr := msg.GetFarmer() + farmingCoin := msg.FarmingCoin + reserveAddr := types.LiquidFarmReserveAddress(msg.PoolId) + + // Reserve the farming coin if err := k.bankKeeper.SendCoins(ctx, farmerAddr, reserveAddr, sdk.NewCoins(farmingCoin)); err != nil { return sdkerrors.Wrap(err, "reserve farming coin") } @@ -66,7 +67,7 @@ func (k Keeper) Farm(ctx sdk.Context, msg *types.MsgFarm) error { ctx.GasMeter().ConsumeGas(sdk.Gas(numQueuedFarmings)*params.DelayedFarmGasFee, "DelayedFarmGasFee") } - // Stake with the reserve account in the farming module + // Stake in the farming module with the reserve account if err := k.farmingKeeper.Stake(ctx, reserveAddr, sdk.NewCoins(farmingCoin)); err != nil { return err } @@ -74,7 +75,7 @@ func (k Keeper) Farm(ctx sdk.Context, msg *types.MsgFarm) error { currentEpochDays := k.farmingKeeper.GetCurrentEpochDays(ctx) endTime := ctx.BlockTime().Add(time.Duration(currentEpochDays) * farmingtypes.Day) // current time + epoch days - k.SetQueuedFarming(ctx, endTime, pool.PoolCoinDenom, farmerAddr, types.QueuedFarming{ + k.SetQueuedFarming(ctx, endTime, liquiditytypes.PoolCoinDenom(msg.PoolId), farmerAddr, types.QueuedFarming{ PoolId: msg.PoolId, Amount: msg.FarmingCoin.Amount, }) diff --git a/x/liquidfarming/keeper/store.go b/x/liquidfarming/keeper/store.go index 8ba25acf4..e103e9495 100644 --- a/x/liquidfarming/keeper/store.go +++ b/x/liquidfarming/keeper/store.go @@ -10,7 +10,42 @@ import ( "github.com/cosmosquad-labs/squad/v2/x/liquidfarming/types" ) -// GetQueuedFarming returns a queued farming object for the given end time, farming coin denom and farmer. +// GetLiquidFarm returns liquid farm object by the given pool id. +func (k Keeper) GetLiquidFarm(ctx sdk.Context, poolId uint64) (liquidFarm types.LiquidFarm, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(types.GetLiquidFarmKey(poolId)) + if bz == nil { + return + } + k.cdc.MustUnmarshal(bz, &liquidFarm) + found = true + return +} + +// GetAllLiquidFarms returns all liquid farm objects stored in the store. +func (k Keeper) GetAllLiquidFarms(ctx sdk.Context) []types.LiquidFarm { + liquidFarms := []types.LiquidFarm{} + k.IterateLiquidFarms(ctx, func(liquidFarm types.LiquidFarm) (stop bool) { + liquidFarms = append(liquidFarms, liquidFarm) + return false + }) + return liquidFarms +} + +// SetLiquidFarm stores liquid farm object with the given pool idl. +func (k Keeper) SetLiquidFarm(ctx sdk.Context, liquidFarm types.LiquidFarm) { + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshal(&liquidFarm) + store.Set(types.GetLiquidFarmKey(liquidFarm.PoolId), bz) +} + +// DeleteLiquidFarm deletes the liquid farm object from the store. +func (k Keeper) DeleteLiquidFarm(ctx sdk.Context, liquidFarm types.LiquidFarm) { + store := ctx.KVStore(k.storeKey) + store.Delete(types.GetLiquidFarmKey(liquidFarm.PoolId)) +} + +// GetQueuedFarming returns a queued farming object by the given end time, farming coin denom and farmer. func (k Keeper) GetQueuedFarming(ctx sdk.Context, endTime time.Time, farmingCoinDenom string, farmerAcc sdk.AccAddress) (queuedFarming types.QueuedFarming, found bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.GetQueuedFarmingKey(endTime, farmingCoinDenom, farmerAcc)) @@ -32,7 +67,7 @@ func (k Keeper) GetQueuedFarmingsByFarmer(ctx sdk.Context, farmerAcc sdk.AccAddr return queuedFarmings } -// SetQueuedFarming stores a queued farming with the given end time, farming coin denom, and farmer address. +// SetQueuedFarming stores a queued farming by the given end time, farming coin denom, and farmer address. func (k Keeper) SetQueuedFarming(ctx sdk.Context, endTime time.Time, farmingCoinDenom string, farmerAcc sdk.AccAddress, queuedFarming types.QueuedFarming) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(&queuedFarming) @@ -151,6 +186,22 @@ func (k Keeper) SetWinningBid(ctx sdk.Context, bid types.Bid, auctionId uint64) store.Set(types.GetWinningBidKey(bid.PoolId, auctionId), bz) } +// IterateLiquidFarms iterates through all liquid farm objects +// stored in the store and invokes callback function for each item. +// Stops the iteration when the callback function for each time. +func (k Keeper) IterateLiquidFarms(ctx sdk.Context, cb func(liquidFarm types.LiquidFarm) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iter := sdk.KVStorePrefixIterator(store, types.LiquidFarmKeyPrefix) + defer iter.Close() + for ; iter.Valid(); iter.Next() { + var liquidFarm types.LiquidFarm + k.cdc.MustUnmarshal(iter.Value(), &liquidFarm) + if cb(liquidFarm) { + break + } + } +} + // IterateQueuedFarmings iterates through all queued farming objects // stored in the store and invokes callback function for each item. // Stops the iteration when the callback function returns true. diff --git a/x/liquidfarming/keeper/store_test.go b/x/liquidfarming/keeper/store_test.go index e0a7a7a32..71c48eb86 100644 --- a/x/liquidfarming/keeper/store_test.go +++ b/x/liquidfarming/keeper/store_test.go @@ -19,7 +19,6 @@ func (s *KeeperTestSuite) TestIterateQueuedFarmingsByFarmerAndDenomReverse() { s.createPair(farmerAcc, "denom1", "denom2", true) s.createPool(farmerAcc, 1, sdk.NewCoins(sdk.NewInt64Coin("denom1", 100000000), sdk.NewInt64Coin("denom2", 100000000)), true) s.createLiquidFarm(types.NewLiquidFarm(poolId, sdk.ZeroInt(), sdk.ZeroInt())) - s.Require().Len(s.keeper.GetParams(s.ctx).LiquidFarms, 1) for seed := int64(0); seed <= 5; seed++ { r := rand.New(rand.NewSource(seed)) diff --git a/x/liquidfarming/module.go b/x/liquidfarming/module.go index f7770a65e..03708243d 100644 --- a/x/liquidfarming/module.go +++ b/x/liquidfarming/module.go @@ -162,7 +162,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw func (AppModule) ConsensusVersion() uint64 { return 1 } // BeginBlock executes all ABCI BeginBlock logic respective to the module. -func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} +func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { + BeginBlocker(ctx, am.keeper) +} // EndBlock executes all ABCI EndBlock logic respective to the module. It // returns no validator updates. diff --git a/x/liquidfarming/types/keys.go b/x/liquidfarming/types/keys.go index a52dc20bf..785914a6a 100644 --- a/x/liquidfarming/types/keys.go +++ b/x/liquidfarming/types/keys.go @@ -27,8 +27,10 @@ const ( var ( LastRewardsAuctionIdKey = []byte{0xe1} // key to retrieve the latest rewards auction id - QueuedFarmingKeyPrefix = []byte{0xe4} - QueuedFarmingIndexKeyPrefix = []byte{0xe5} + LiquidFarmKeyPrefix = []byte{0xe3} + + QueuedFarmingKeyPrefix = []byte{0xe5} + QueuedFarmingIndexKeyPrefix = []byte{0xe6} RewardsAuctionKeyPrefix = []byte{0xe7} @@ -36,6 +38,12 @@ var ( WinningBidKeyPrefix = []byte{0xeb} ) +// GetLiquidFarmKey returns the store key to retrieve the liquid farm object +// by the given pool id. +func GetLiquidFarmKey(poolId uint64) []byte { + return append(LiquidFarmKeyPrefix, sdk.Uint64ToBigEndian(poolId)...) +} + // GetLastRewardsAuctionIdKey returns the store key to retrieve the last rewards auction // by the given pool id. func GetLastRewardsAuctionIdKey(poolId uint64) []byte { diff --git a/x/liquidfarming/types/keys_test.go b/x/liquidfarming/types/keys_test.go index 173937e61..fb6cd20a7 100644 --- a/x/liquidfarming/types/keys_test.go +++ b/x/liquidfarming/types/keys_test.go @@ -28,6 +28,12 @@ func (s *keysTestSuite) TestGetLastRewardsAuctionIdKey() { s.Require().Equal([]byte{0xe1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, types.GetLastRewardsAuctionIdKey(10)) } +func (s *keysTestSuite) TestGetLiquidFarmKey() { + s.Require().Equal([]byte{0xe3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, types.GetLiquidFarmKey(0)) + s.Require().Equal([]byte{0xe3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9}, types.GetLiquidFarmKey(9)) + s.Require().Equal([]byte{0xe3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, types.GetLiquidFarmKey(10)) +} + func (s *keysTestSuite) TestGetQueuedFarmingKey() { testCases := []struct { endTime time.Time From 6347f1dad2d234b352fb3ba0cd2b6d8ee0fb5e32 Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 5 Aug 2022 13:09:38 +0900 Subject: [PATCH 3/3] fix: rename reward-auction to rewards-auction query command --- x/liquidfarming/client/cli/query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/liquidfarming/client/cli/query.go b/x/liquidfarming/client/cli/query.go index 35ecada89..dac3fdcc8 100644 --- a/x/liquidfarming/client/cli/query.go +++ b/x/liquidfarming/client/cli/query.go @@ -272,14 +272,14 @@ $ %s query %s rewards-auctions 1 func NewQueryRewardsAuctionCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "reward-auction [pool-id] [auction-id]", + Use: "rewards-auction [pool-id] [auction-id]", Args: cobra.ExactArgs(2), Short: "Query the specific reward auction", Long: strings.TrimSpace( fmt.Sprintf(`Query the specific reward auction on a network. Example: -$ %s query %s reward-auction 1 1 +$ %s query %s rewards-auction 1 1 `, version.AppName, types.ModuleName, ),