Skip to content

Commit

Permalink
Dev 1930 set max value for pool (#863)
Browse files Browse the repository at this point in the history
* feat: add param

* add check

* fix: migration and test

* update

* update

* update proto

* update

* update

* fix: proto

* update migration

* update

* update

* update

* Update pool.proto

* update

* test(leveragelp): fix tests

* fix(leveragelp): migration

* fix(leveragelp): fix data structure

* fix(leveragelp): new pool

* fix(leveragelp): migration

* fix(leveragelp): fix tests and doc

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
cryptokage1996 and cosmic-vagabond authored Oct 25, 2024
1 parent 9482221 commit 28bb08f
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 310 deletions.
10 changes: 0 additions & 10 deletions proto/elys/incentive/genesis.proto

This file was deleted.

113 changes: 0 additions & 113 deletions proto/elys/incentive/query.proto

This file was deleted.

36 changes: 0 additions & 36 deletions proto/elys/incentive/tx.proto

This file was deleted.

12 changes: 7 additions & 5 deletions proto/elys/leveragelp/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ message Pool {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string max_leveragelp_percent = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

message LegacyPool {
Expand All @@ -27,14 +31,12 @@ message LegacyPool {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
bool enabled = 3;
bool closed = 4;
string leveraged_lp_amount = 5 [
string leveraged_lp_amount = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string leverage_max = 6 [
string leverage_max = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
}
15 changes: 14 additions & 1 deletion x/leveragelp/keeper/msg_server_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"
"strconv"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -33,7 +34,19 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons
borrowRatio = borrowed.ToLegacyDec().Add(msg.Leverage.Mul(msg.CollateralAmount.ToLegacyDec())).
Quo(params.TotalValue.ToLegacyDec())
}
if borrowRatio.GTE(params.MaxLeverageRatio) {

var pool_leveragelp sdk.Dec
pool, found := k.GetPool(ctx, msg.AmmPoolId)
if !found {
return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, fmt.Sprintf("poolId: %d", msg.AmmPoolId))
}
amm_pool, found := k.amm.GetPool(ctx, msg.AmmPoolId)
if !found {
return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, fmt.Sprintf("poolId: %d", msg.AmmPoolId))
}
pool_leveragelp = pool.LeveragedLpAmount.ToLegacyDec().Quo(amm_pool.TotalShares.Amount.ToLegacyDec()).Mul(sdk.NewDec(100))

if pool_leveragelp.GTE(pool.MaxLeveragelpPercent) || borrowRatio.GTE(params.MaxLeverageRatio) {
return nil, errorsmod.Wrap(types.ErrMaxLeverageLpExists, "no new position can be open")
}

Expand Down
32 changes: 25 additions & 7 deletions x/leveragelp/keeper/msg_server_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func initializeForOpen(suite *KeeperTestSuite, addresses []sdk.AccAddress, asset1, asset2 string) {
fee := sdk.MustNewDecFromStr("0.0002")
issueAmount := sdk.NewInt(10_000_000_000_000)
issueAmount := sdk.NewInt(10_000_000_000_000_000)
for _, address := range addresses {
coins := sdk.NewCoins(
sdk.NewCoin(ptypes.ATOM, issueAmount),
Expand Down Expand Up @@ -48,11 +48,11 @@ func initializeForOpen(suite *KeeperTestSuite, addresses []sdk.AccAddress, asset
},
PoolAssets: []ammtypes.PoolAsset{
{
Token: sdk.NewInt64Coin(asset1, 100_000_000),
Token: sdk.NewInt64Coin(asset1, 100_000_000_000_000),
Weight: sdk.NewInt(50),
},
{
Token: sdk.NewInt64Coin(asset2, 1000_000_000),
Token: sdk.NewInt64Coin(asset2, 1000_000_000_000_000),
Weight: sdk.NewInt(50),
},
},
Expand Down Expand Up @@ -137,7 +137,7 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
CollateralAmount: sdk.NewInt(1000),
AmmPoolId: 10,
AmmPoolId: 1,
Leverage: sdk.MustNewDecFromStr("2.0"),
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
Expand Down Expand Up @@ -171,7 +171,7 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
expectErr: true,
expectErrMsg: "invalid pool id",
expectErrMsg: "pool does not exis",
prerequisiteFunction: func() {
pool := types.NewPool(2, math.LegacyMustNewDecFromStr("10"))
suite.app.LeveragelpKeeper.SetPool(suite.ctx, pool)
Expand All @@ -189,11 +189,29 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
expectErr: true,
expectErrMsg: "invalid pool id",
expectErrMsg: "pool does not exis",
prerequisiteFunction: func() {
suite.SetupCoinPrices(suite.ctx)
},
},
{name: "Pool not enabled",
input: &types.MsgOpen{
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
CollateralAmount: sdk.NewInt(1000),
AmmPoolId: 2,
Leverage: sdk.MustNewDecFromStr("2.0"),
StopLossPrice: sdk.MustNewDecFromStr("100.0"),
},
expectErr: true,
expectErrMsg: "denom does not exist in pool",
prerequisiteFunction: func() {
pool := types.NewPool(2, sdk.NewDec(60))
suite.app.LeveragelpKeeper.SetPool(suite.ctx, pool)
amm_pool := ammtypes.Pool{PoolId: 2, TotalShares: sdk.Coin{Amount: sdk.NewInt(100)}}
suite.app.AmmKeeper.SetPool(suite.ctx, amm_pool)
},
},
{"Collateral asset not equal to base currency",
&types.MsgOpen{
Creator: addresses[0].String(),
Expand Down Expand Up @@ -258,7 +276,7 @@ func (suite *KeeperTestSuite) TestOpen_PoolWithBaseCurrencyAsset() {
&types.MsgOpen{
Creator: addresses[0].String(),
CollateralAsset: ptypes.BaseCurrency,
CollateralAmount: sdk.NewInt(1_000_000_000_000),
CollateralAmount: sdk.NewInt(1_000_000_000_000_000_000),
AmmPoolId: 1,
Leverage: sdk.MustNewDecFromStr("2.0"),
StopLossPrice: sdk.MustNewDecFromStr("50.0"),
Expand Down
10 changes: 10 additions & 0 deletions x/leveragelp/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ func (k Keeper) GetPool(ctx sdk.Context, poolId uint64) (val types.Pool, found b
k.cdc.MustUnmarshal(b, &val)
return val, true
}

func (k Keeper) DeleteLegacyPool(ctx sdk.Context, poolId uint64) error {
store := ctx.KVStore(k.storeKey)
key := types.PoolKey(poolId)
if !store.Has(key) {
return types.ErrPositionDoesNotExist
}
store.Delete(key)
return nil
}
23 changes: 23 additions & 0 deletions x/leveragelp/migrations/v15_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/leveragelp/types"
)

func (m Migrator) V15Migration(ctx sdk.Context) error {
legacyPools := m.keeper.GetAllLegacyPools(ctx)

for _, legacyPool := range legacyPools {
newPool := types.Pool{
AmmPoolId: legacyPool.AmmPoolId,
Health: legacyPool.Health,
LeveragedLpAmount: legacyPool.LeveragedLpAmount,
LeverageMax: legacyPool.LeverageMax,
MaxLeveragelpPercent: sdk.NewDec(60),
}
m.keeper.SetPool(ctx, newPool)
}

return nil
}
8 changes: 2 additions & 6 deletions x/leveragelp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,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, 12, m.V13Migration)
if err != nil {
panic(err)
}
err = cfg.RegisterMigration(types.ModuleName, 13, m.V14Migration)
err := cfg.RegisterMigration(types.ModuleName, 14, m.V15Migration)
if err != nil {
panic(err)
}
Expand All @@ -148,7 +144,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 14 }
func (AppModule) ConsensusVersion() uint64 { return 15 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
18 changes: 1 addition & 17 deletions x/leveragelp/spec/07_pool_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ Managing pool status is crucial. Two key states are whether a pool is enabled or
#### Pool Status Functions

```go
func (k Keeper) IsPoolEnabled(ctx sdk.Context, poolId uint64) bool {
pool, found := k.GetPool(ctx, poolId)
if (!found) {
pool = types.NewPool(poolId)
k.SetPool(ctx, pool)
}
return pool.Enabled
}

func (k Keeper) IsPoolClosed(ctx sdk.Context, poolId uint64) bool {
pool, found := k.GetPool(ctx, poolId)
if (!found) {
Expand All @@ -32,18 +23,11 @@ func (k Keeper) IsPoolClosed(ctx sdk.Context, poolId uint64) bool {

#### Functionality

1. **IsPoolEnabled**:

- Checks if a pool is enabled.
- If not found, initializes and sets a new pool.
- An enabled pool is processed by the module.

2. **IsPoolClosed**:
1. **IsPoolClosed**:
- Checks if a pool is closed.
- If not found, initializes and sets a new pool.
- A closed pool prevents new positions but allows existing ones to be processed.

#### Key Differences

- **Enabled**: The pool is either processed or excluded entirely.
- **Closed**: Only affects the opening of new positions; existing positions continue as usual.
Loading

0 comments on commit 28bb08f

Please sign in to comment.