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

feat: emergency group leverage #2188

Merged
merged 29 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d3aa54
cosmetics
robert-zaremba Jul 27, 2023
e270652
checkers and helpers
robert-zaremba Jul 30, 2023
c41a196
leverage emergency checks
robert-zaremba Jul 30, 2023
c1f4d93
Merge branch 'main' into robert/emergency-group-leverage
robert-zaremba Aug 2, 2023
b7705fa
fix
robert-zaremba Aug 3, 2023
3c8858c
integrating ugov
robert-zaremba Aug 3, 2023
379427b
Merge branch 'main' into robert/emergency-group-leverage
robert-zaremba Aug 3, 2023
6933cf2
ugov interface
robert-zaremba Aug 4, 2023
118836c
Merge branch 'main' into robert/emergency-group-leverage
robert-zaremba Aug 4, 2023
3e9014c
rename checkers.IsGovAuthority to AssertGovAuthority
robert-zaremba Aug 7, 2023
251d496
add new IsGovAuthority
robert-zaremba Aug 7, 2023
982db8a
finish leverage integration
robert-zaremba Aug 8, 2023
0549500
finish DecMaxDiff test
robert-zaremba Aug 8, 2023
f886840
Merge branch 'main' into robert/emergency-group-leverage
robert-zaremba Aug 8, 2023
e3883dd
missing error
toteki Aug 8, 2023
579794b
add symbol denom to regDenom
toteki Aug 8, 2023
fdef585
sdk.Dec comparison
toteki Aug 8, 2023
3fb3647
DecMaxDiff simplify
toteki Aug 8, 2023
3174b5f
oops - revert add symbol denom early
toteki Aug 8, 2023
460d292
add emergency group address check
robert-zaremba Aug 8, 2023
de24a76
lint
robert-zaremba Aug 8, 2023
fd7c9d9
lint
robert-zaremba Aug 8, 2023
577027e
clean expected types
robert-zaremba Aug 8, 2023
f625cc1
checkers: add 0-1 range check
robert-zaremba Aug 8, 2023
e381ef7
fix tests
robert-zaremba Aug 8, 2023
d430e12
fix
robert-zaremba Aug 8, 2023
399e31b
genesis test
robert-zaremba Aug 8, 2023
b349be2
update ValidateProposal
robert-zaremba Aug 8, 2023
da05bba
Merge branch 'main' into robert/emergency-group-leverage
robert-zaremba Aug 8, 2023
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
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,14 @@ func New(
app.StakingKeeper,
distrtypes.ModuleName,
)

app.LeverageKeeper = leveragekeeper.NewKeeper(
appCodec,
keys[leveragetypes.ModuleName],
app.GetSubspace(leveragetypes.ModuleName),
app.BankKeeper,
app.OracleKeeper,
app.UGovKeeperB.EmergencyGroup,
cast.ToBool(appOpts.Get(leveragetypes.FlagEnableLiquidatorQuery)),
authtypes.NewModuleAddress(metoken.ModuleName),
)
Expand Down
13 changes: 13 additions & 0 deletions tests/tsdk/numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tsdk

import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// DecF creates a Dec based on a float number.
// MUST not be used in production code. Can only be used in tests.
func DecF(amount float64) sdk.Dec {
return sdk.MustNewDecFromStr(strconv.FormatFloat(amount, 'f', -1, 64))
}
16 changes: 16 additions & 0 deletions tests/tsdk/numbers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tsdk

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)

func TestDecF(t *testing.T) {
assert := assert.New(t)
assert.Equal(sdk.MustNewDecFromStr("10.20"), DecF(10.2))
assert.Equal(sdk.MustNewDecFromStr("0.002"), DecF(0.002))
assert.Equal(sdk.MustNewDecFromStr("0"), DecF(0))
assert.Equal(sdk.MustNewDecFromStr("-1.9"), DecF(-1.9))
}
29 changes: 29 additions & 0 deletions util/checkers/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package checkers

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"golang.org/x/exp/constraints"
)

func IntegerMaxDiff[T constraints.Integer](a, b, maxDiff T, note string) error {
var diff T
if a > b {
diff = a - b
} else {
diff = b - a
}
if diff > maxDiff {
return fmt.Errorf("%s, diff (=%v) is too big", note, diff)
}
return nil
}

func DecMaxDiff(a, b, maxDiff sdk.Dec, note string) error {
diff := a.Sub(b).Abs()
if diff.GT(maxDiff) {
return fmt.Errorf("%s, diff (=%v) is too big", note, diff)
}
return nil
}
60 changes: 60 additions & 0 deletions util/checkers/number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package checkers

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/umee-network/umee/v5/tests/tsdk"
)

func TestNumberDiff(t *testing.T) {
assert := assert.New(t)

assert.NoError(IntegerMaxDiff(1, 1, 0, ""))
assert.NoError(IntegerMaxDiff(-1, -1, 0, ""))
assert.NoError(IntegerMaxDiff(0, 0, 0, ""))

assert.Error(IntegerMaxDiff(1, -1, 0, ""))
assert.Error(IntegerMaxDiff(1, -1, -1, ""))
assert.Error(IntegerMaxDiff(-1, 1, 0, ""))
assert.Error(IntegerMaxDiff(-1, 1, -1, ""))

assert.NoError(IntegerMaxDiff(1, -1, 2, ""))
assert.NoError(IntegerMaxDiff(1, -1, 3, ""))
assert.NoError(IntegerMaxDiff(1, -1, 100, ""))
assert.NoError(IntegerMaxDiff(1, -1, 60000000, ""))

assert.NoError(IntegerMaxDiff(-1, 1, 2, ""))
assert.NoError(IntegerMaxDiff(-1, 1, 3, ""))
assert.NoError(IntegerMaxDiff(-1, 1, 100, ""))
assert.NoError(IntegerMaxDiff(-1, 1, 60000000, ""))
}

func TestDecDiff(t *testing.T) {
assert := assert.New(t)
decMaxDiff := func(a, b, maxDiff float64) error {
return DecMaxDiff(tsdk.DecF(a), tsdk.DecF(b), tsdk.DecF(maxDiff), "")
}

assert.NoError(decMaxDiff(1, 1, 0))
assert.NoError(decMaxDiff(-1, -1, 0))
assert.NoError(decMaxDiff(0, 0, 0))

assert.NoError(decMaxDiff(0.0001, 0.0001, 0))
assert.NoError(decMaxDiff(-0.0001, -0.0001, 0))

assert.Error(decMaxDiff(1, -1, 0))
assert.Error(decMaxDiff(1, -1, -1))
assert.Error(decMaxDiff(-1, 1, 0))
assert.Error(decMaxDiff(-1, 1, -1))

assert.NoError(decMaxDiff(1, -1, 2))
assert.NoError(decMaxDiff(1, -1, 3))
assert.NoError(decMaxDiff(1, -1, 100))
assert.NoError(decMaxDiff(1, -1, 60000000))

assert.NoError(decMaxDiff(-1, 1, 2))
assert.NoError(decMaxDiff(-1, 1, 3))
assert.NoError(decMaxDiff(-1, 1, 100))
assert.NoError(decMaxDiff(-1, 1, 60000000))
}
21 changes: 14 additions & 7 deletions util/checkers/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,28 @@ func init() {

const minProposalTitleLen = 3

// IsGovAuthority errors is the authority is the gov module address
func IsGovAuthority(authority string) error {
if govModuleAddr == "" {
return sdkerrors.ErrLogic.Wrap("govModuleAddrs in the checkers package must be set before using this function")
}
if authority != govModuleAddr {
// AssertGovAuthority errors is the authority is not the gov module address. Panics if
// the gov module address is not set during the package initialization.
func AssertGovAuthority(authority string) error {
if !IsGovAuthority(authority) {
return govtypes.ErrInvalidSigner.Wrapf(
"expected %s, got %s", govModuleAddr, authority)
}
return nil
}

// IsGovAuthority returns true if the authority is the gov module address. Panics if
// the gov module address is not set during the package initialization.
func IsGovAuthority(authority string) bool {
if govModuleAddr == "" {
panic("govModuleAddrs in the checkers package must be set before using this function")
}
return authority == govModuleAddr
}

// ValidateProposal checks the format of the title, description, and authority of a gov message.
func ValidateProposal(title, description, authority string) error {
if err := IsGovAuthority(authority); err != nil {
if err := AssertGovAuthority(authority); err != nil {
return err
}
if len(strings.TrimSpace(title)) < minProposalTitleLen {
Expand Down
2 changes: 1 addition & 1 deletion util/checkers/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestIsGovAuthority(t *testing.T) {
}

for i, tc := range tcs {
err := IsGovAuthority(tc.auth)
err := AssertGovAuthority(tc.auth)
if tc.isErr {
require.ErrorIs(err, govtypes.ErrInvalidSigner, "[test: %d] expected error", i)
} else {
Expand Down
4 changes: 2 additions & 2 deletions x/incentive/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func NewMsgGovSetParams(authority string, params Params) *MsgGovSetParams {
}

func (msg MsgGovSetParams) ValidateBasic() error {
if err := checkers.IsGovAuthority(msg.Authority); err != nil {
if err := checkers.AssertGovAuthority(msg.Authority); err != nil {
return err
}
return msg.Params.Validate()
Expand Down Expand Up @@ -194,7 +194,7 @@ func NewMsgGovCreatePrograms(authority string, programs []IncentiveProgram) *Msg
}

func (msg MsgGovCreatePrograms) ValidateBasic() error {
if err := checkers.IsGovAuthority(msg.Authority); err != nil {
if err := checkers.AssertGovAuthority(msg.Authority); err != nil {
return err
}
if len(msg.Programs) == 0 {
Expand Down
1 change: 1 addition & 0 deletions x/leverage/keeper/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewTestKeeper(
paramSpace,
bk,
ok,
nil, // TODO
enableLiquidatorQuery,
authtypes.NewModuleAddress(metoken.ModuleName),
)
Expand Down
11 changes: 7 additions & 4 deletions x/leverage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Keeper struct {
paramSpace paramtypes.Subspace
bankKeeper types.BankKeeper
oracleKeeper types.OracleKeeper
ugov types.UgovBuilder
liquidatorQueryEnabled bool
meTokenAddr sdk.AccAddress

Expand All @@ -31,8 +32,9 @@ func NewKeeper(
cdc codec.Codec,
storeKey storetypes.StoreKey,
paramSpace paramtypes.Subspace,
bk types.BankKeeper,
ok types.OracleKeeper,
b types.BankKeeper,
o types.OracleKeeper,
ugov types.UgovBuilder,
enableLiquidatorQuery bool,
meTokenAddr sdk.AccAddress,
) Keeper {
Expand All @@ -45,8 +47,9 @@ func NewKeeper(
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace,
bankKeeper: bk,
oracleKeeper: ok,
bankKeeper: b,
oracleKeeper: o,
ugov: ugov,
liquidatorQueryEnabled: enableLiquidatorQuery,
meTokenAddr: meTokenAddr,
}
Expand Down
23 changes: 7 additions & 16 deletions x/leverage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package keeper

import (
"context"
"strings"

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

"github.com/umee-network/umee/v5/util/checkers"
"github.com/umee-network/umee/v5/util/coin"
"github.com/umee-network/umee/v5/util/sdkutil"
"github.com/umee-network/umee/v5/x/leverage/types"
Expand Down Expand Up @@ -540,30 +540,21 @@ func (s msgServer) GovUpdateRegistry(
msg *types.MsgGovUpdateRegistry,
) (*types.MsgGovUpdateRegistryResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
regdTkDenoms := make(map[string]bool)
regdSymDenoms := make(map[string]bool)

regDenoms := make(map[string]types.Token)
registeredTokens := s.keeper.GetAllRegisteredTokens(ctx)
for _, token := range registeredTokens {
regdTkDenoms[token.BaseDenom] = true
regdSymDenoms[strings.ToUpper(token.SymbolDenom)] = true
}

// update the token settings
err := s.keeper.SaveOrUpdateTokenSettingsToRegistry(ctx, msg.UpdateTokens, regdTkDenoms, regdSymDenoms, true)
if err != nil {
return nil, err
regDenoms[token.BaseDenom] = token
regDenoms[token.SymbolDenom] = token
}

// adds the new token settings
err = s.keeper.SaveOrUpdateTokenSettingsToRegistry(ctx, msg.AddTokens, regdTkDenoms, regdSymDenoms, false)
byEmergencyGroup := !checkers.IsGovAuthority(msg.Authority)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
err := s.keeper.UpdateTokenRegistry(ctx, msg.UpdateTokens, msg.AddTokens, regDenoms, byEmergencyGroup)
if err != nil {
return nil, err
}

// cleans blacklisted tokens from the registry if they have not been supplied
err = s.keeper.CleanTokenRegistry(ctx)
if err != nil {
if err := s.keeper.CleanTokenRegistry(ctx); err != nil {
return nil, err
}

Expand Down
Loading