Skip to content

Commit

Permalink
fix: gov special assets fix for 6.0 (#2247)
Browse files Browse the repository at this point in the history
* fix 6.0

* nil test cases
  • Loading branch information
toteki authored Sep 15, 2023
1 parent 3c4f695 commit 7c0b55b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
7 changes: 4 additions & 3 deletions x/leverage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
129 changes: 129 additions & 0 deletions x/leverage/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions x/leverage/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7c0b55b

Please sign in to comment.