diff --git a/x/leverage/keeper/msg_server.go b/x/leverage/keeper/msg_server.go index 832906b800..55768e9748 100644 --- a/x/leverage/keeper/msg_server.go +++ b/x/leverage/keeper/msg_server.go @@ -579,9 +579,10 @@ func (s msgServer) GovUpdateSpecialAssets( for _, b := range set.Assets { if a != b { pair := types.SpecialAssetPair{ - Collateral: a, - Borrow: b, - CollateralWeight: set.CollateralWeight, + Collateral: a, + Borrow: b, + CollateralWeight: set.CollateralWeight, + LiquidationThreshold: set.LiquidationThreshold, } // sets or overrides (or deletes on zero collateral weight) each pair if err := s.keeper.SetSpecialAssetPair(ctx, pair); err != nil { diff --git a/x/leverage/keeper/msg_server_test.go b/x/leverage/keeper/msg_server_test.go index 27e7fcba00..df9a8981fc 100644 --- a/x/leverage/keeper/msg_server_test.go +++ b/x/leverage/keeper/msg_server_test.go @@ -187,6 +187,135 @@ func (s *IntegrationTestSuite) TestUpdateRegistry() { } } +func (s *IntegrationTestSuite) TestUpdateSpecialAssets() { + govAccAddr := checkers.GovModuleAddr + + testCases := []struct { + name string + req *types.MsgGovUpdateSpecialAssets + expectErr bool + errMsg string + }{ + { + "empty", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{}, + Pairs: []types.SpecialAssetPair{}, + }, + true, + "empty", + }, + { + "invalid set", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{ + { + Assets: []string{"test1", "test2"}, + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + }, + }, + Pairs: []types.SpecialAssetPair{}, + }, + true, + "nil", + }, + { + "valid set", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{ + { + Assets: []string{"test1", "test2"}, + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + LiquidationThreshold: sdk.MustNewDecFromStr("0.9"), + }, + }, + Pairs: []types.SpecialAssetPair{}, + }, + false, + "", + }, + { + "invalid pair", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{}, + Pairs: []types.SpecialAssetPair{ + { + Borrow: "test1", + Collateral: "test2", + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + }, + }, + }, + true, + "nil", + }, + { + "valid pair", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{}, + Pairs: []types.SpecialAssetPair{ + { + Borrow: "test1", + Collateral: "test2", + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + LiquidationThreshold: sdk.MustNewDecFromStr("0.9"), + }, + }, + }, + false, + "", + }, + { + "valid set and pair", + &types.MsgGovUpdateSpecialAssets{ + Authority: govAccAddr, + Description: "test", + Sets: []types.SpecialAssetSet{ + { + Assets: []string{"test1", "test2"}, + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + LiquidationThreshold: sdk.MustNewDecFromStr("0.9"), + }, + }, + Pairs: []types.SpecialAssetPair{ + { + Borrow: "test1", + Collateral: "test2", + CollateralWeight: sdk.MustNewDecFromStr("0.8"), + LiquidationThreshold: sdk.MustNewDecFromStr("0.9"), + }, + }, + }, + false, + "", + }, + } + + for _, tc := range testCases { + s.Run(tc.name, func() { + err := tc.req.ValidateBasic() + if err == nil { + _, err = s.msgSrvr.GovUpdateSpecialAssets(s.ctx, tc.req) + } + if tc.expectErr { + s.Require().ErrorContains(err, tc.errMsg) + } else { + s.Require().NoError(err) + } + }) + } +} + func (s *IntegrationTestSuite) TestMsgSupply() { type testCase struct { msg string diff --git a/x/leverage/types/token.go b/x/leverage/types/token.go index 165a22a930..3959efddd2 100644 --- a/x/leverage/types/token.go +++ b/x/leverage/types/token.go @@ -11,8 +11,10 @@ import ( "github.com/umee-network/umee/v6/util/coin" ) -var halfDec = sdk.MustNewDecFromStr("0.5") -var one = sdk.OneDec() +var ( + halfDec = sdk.MustNewDecFromStr("0.5") + one = sdk.OneDec() +) // ValidateBaseDenom validates a denom and ensures it is not a uToken. func ValidateBaseDenom(denom string) error { @@ -175,6 +177,11 @@ func (p SpecialAssetPair) Validate() error { return err } + if p.CollateralWeight.IsNil() || p.LiquidationThreshold.IsNil() { + return fmt.Errorf("nil collateral weight or liquidation threshold for asset pair (%s,%s)", + p.Borrow, p.Collateral) + } + // Collateral Weight is non-negative and less than 1. if p.CollateralWeight.IsNegative() || p.CollateralWeight.GTE(sdk.OneDec()) { return fmt.Errorf("invalid collateral rate: %s", p.CollateralWeight) @@ -202,6 +209,10 @@ func (s SpecialAssetSet) Validate() error { denoms[a] = true } + if s.CollateralWeight.IsNil() || s.LiquidationThreshold.IsNil() { + return fmt.Errorf("nil collateral weight or liquidation threshold for asset set %s)", s.Assets) + } + // Collateral Weight is non-negative and less than 1. if s.CollateralWeight.IsNegative() || s.CollateralWeight.GTE(sdk.OneDec()) { return fmt.Errorf("invalid collateral rate: %s", s.CollateralWeight)