Skip to content

Commit

Permalink
Halborn fixes (#447)
Browse files Browse the repository at this point in the history
* HAL-02 pool creation fee

* add migration script for amm module params

* HAL-19 - add SlippageTrackDuration on amm params

* update error handling

* resolve incentive unit test

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
jelysn and cosmic-vagabond authored Apr 5, 2024
1 parent cee0420 commit d54acc4
Show file tree
Hide file tree
Showing 32 changed files with 741 additions and 234 deletions.
2 changes: 0 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,6 @@ func NewElysApp(
appCodec,
keys[ammmoduletypes.StoreKey],
tkeys[ammmoduletypes.TStoreKey],
app.GetSubspace(ammmoduletypes.ModuleName),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
&app.ParameterKeeper,
app.BankKeeper,
Expand Down Expand Up @@ -1499,7 +1498,6 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(commitmentmoduletypes.ModuleName)
paramsKeeper.Subspace(tokenomicsmoduletypes.ModuleName)
paramsKeeper.Subspace(burnermoduletypes.ModuleName)
paramsKeeper.Subspace(ammmoduletypes.ModuleName)
paramsKeeper.Subspace(perpetualmoduletypes.ModuleName)
paramsKeeper.Subspace(transferhooktypes.ModuleName)
paramsKeeper.Subspace(clockmoduletypes.ModuleName)
Expand Down
11 changes: 11 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37947,7 +37947,10 @@ paths:
properties:
pool_creation_fee:
type: string
slippage_track_duration:
type: string
format: uint64
title: 'default 1 week: 604,800'
description: >-
QueryParamsResponse is response type for the Query/Params RPC
method.
Expand Down Expand Up @@ -82967,6 +82970,8 @@ definitions:
type: string
recipient:
type: string
elys.amm.MsgUpdateParamsResponse:
type: object
elys.amm.MsgUpdatePoolParamsResponse:
type: object
properties:
Expand Down Expand Up @@ -83023,7 +83028,10 @@ definitions:
properties:
pool_creation_fee:
type: string
slippage_track_duration:
type: string
format: uint64
title: 'default 1 week: 604,800'
description: Params defines the parameters for the module.
elys.amm.Pool:
type: object
Expand Down Expand Up @@ -83462,7 +83470,10 @@ definitions:
properties:
pool_creation_fee:
type: string
slippage_track_duration:
type: string
format: uint64
title: 'default 1 week: 604,800'
description: QueryParamsResponse is response type for the Query/Params RPC method.
elys.amm.QuerySlippageTrackAllResponse:
type: object
Expand Down
6 changes: 5 additions & 1 deletion proto/elys/amm/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ option go_package = "github.com/elys-network/elys/x/amm/types";
message Params {
option (gogoproto.goproto_stringer) = false;

uint64 pool_creation_fee = 1;
string pool_creation_fee = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
uint64 slippage_track_duration = 2; // default 1 week: 604,800
}
9 changes: 8 additions & 1 deletion proto/elys/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ service Msg {
rpc SwapByDenom (MsgSwapByDenom ) returns (MsgSwapByDenomResponse );
rpc FeedMultipleExternalLiquidity (MsgFeedMultipleExternalLiquidity) returns (MsgFeedMultipleExternalLiquidityResponse);
rpc UpdatePoolParams (MsgUpdatePoolParams ) returns (MsgUpdatePoolParamsResponse );
rpc UpdateParams (MsgUpdateParams ) returns (MsgUpdateParamsResponse );
}
message MsgCreatePool {
string sender = 1;
Expand Down Expand Up @@ -129,7 +130,7 @@ message MsgSwapByDenomResponse {
}

message MsgUpdatePoolParams {
string sender = 1;
string authority = 1;
uint64 pool_id = 2;
PoolParams pool_params = 3;
}
Expand All @@ -139,3 +140,9 @@ message MsgUpdatePoolParamsResponse {
PoolParams pool_params = 2;
}

message MsgUpdateParams {
string authority = 1;
Params params = 2;
}

message MsgUpdateParamsResponse {}
9 changes: 0 additions & 9 deletions testutil/keeper/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/elys-network/elys/x/amm/keeper"
"github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/amm/types/mocks"
Expand All @@ -33,21 +32,13 @@ func AmmKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, *mocks.AccountedPoolK
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
transientStoreKey,
"AmmParams",
)

accountedPoolKeeper := mocks.NewAccountedPoolKeeper(t)
oracleKeeper := mocks.NewOracleKeeper(t)

k := keeper.NewKeeper(
cdc,
storeKey,
transientStoreKey,
paramsSubspace,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
nil,
nil,
Expand Down
3 changes: 2 additions & 1 deletion x/amm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ func (k Keeper) ExecuteSwapRequests(ctx sdk.Context) []sdk.Msg {
}

func (k Keeper) ClearOutdatedSlippageTrack(ctx sdk.Context) {
params := k.GetParams(ctx)
tracks := k.AllSlippageTracks(ctx)
for _, track := range tracks {
if track.Timestamp+86400*7 < uint64(ctx.BlockTime().Unix()) {
if track.Timestamp+params.SlippageTrackDuration < uint64(ctx.BlockTime().Unix()) {
k.DeleteSlippageTrack(ctx, track)
}
}
Expand Down
1 change: 0 additions & 1 deletion x/amm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ func (suite *KeeperTestSuite) TestExecuteSwapRequests() {
}
}

// TODO: test ClearOutdatedSlippageTrack
func (suite *KeeperTestSuite) TestClearOutdatedSlippageTrack() {
now := time.Now()
tracks := []types.OraclePoolSlippageTrack{
Expand Down
8 changes: 0 additions & 8 deletions x/amm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/elys-network/elys/x/amm/types"
commitmentkeeper "github.com/elys-network/elys/x/commitment/keeper"
Expand All @@ -19,7 +18,6 @@ type (
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
transientStoreKey storetypes.StoreKey
paramstore paramtypes.Subspace
authority string
hooks types.AmmHooks

Expand All @@ -37,7 +35,6 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
transientStoreKey storetypes.StoreKey,
ps paramtypes.Subspace,
authority string,

parameterKeeper *pkeeper.Keeper,
Expand All @@ -48,16 +45,11 @@ func NewKeeper(
assetProfileKeeper types.AssetProfileKeeper,
accountedPoolKeeper types.AccountedPoolKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
transientStoreKey: transientStoreKey,
paramstore: ps,
authority: authority,

parameterKeeper: parameterKeeper,
Expand Down
12 changes: 12 additions & 0 deletions x/amm/keeper/msg_server_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

// CreatePool attempts to create a pool returning the newly created pool ID or an error upon failure.
Expand All @@ -14,6 +15,17 @@ import (
func (k msgServer) CreatePool(goCtx context.Context, msg *types.MsgCreatePool) (*types.MsgCreatePoolResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Pay pool creation fee
sender := sdk.MustAccAddressFromBech32(msg.Sender)
params := k.GetParams(ctx)

if !params.PoolCreationFee.IsNil() && params.PoolCreationFee.IsPositive() {
feeCoins := sdk.Coins{sdk.NewCoin(ptypes.Elys, params.PoolCreationFee)}
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, feeCoins); err != nil {
return nil, err
}
}

poolId, err := k.Keeper.CreatePool(ctx, msg)
if err != nil {
return &types.MsgCreatePoolResponse{}, err
Expand Down
6 changes: 3 additions & 3 deletions x/amm/keeper/msg_server_create_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
}{
{
desc: "zero tvl pool creation",
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000), sdk.NewInt64Coin(ptypes.Elys, 1000000)},
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000), sdk.NewInt64Coin(ptypes.Elys, 11000000)},
poolParams: types.PoolParams{
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
Expand Down Expand Up @@ -50,7 +50,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
},
{
desc: "positive tvl pool creation",
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000), sdk.NewInt64Coin(ptypes.Elys, 10000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
poolParams: types.PoolParams{
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
Expand Down Expand Up @@ -78,7 +78,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
},
{
desc: "not enough balance to create pool",
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000)},
senderInitBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Eden, 1000000), sdk.NewInt64Coin(ptypes.Elys, 10000000)},
poolParams: types.PoolParams{
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
Expand Down
8 changes: 5 additions & 3 deletions x/amm/keeper/msg_server_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() {

// bootstrap accounts
sender := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())

params := suite.app.AmmKeeper.GetParams(suite.ctx)
// bootstrap balances
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, tc.poolInitBalance)
poolCreationFee := sdk.NewCoin(ptypes.Elys, params.PoolCreationFee)
coins := tc.poolInitBalance.Add(poolCreationFee)
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, coins)
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, sender, tc.poolInitBalance)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, sender, coins)
suite.Require().NoError(err)

// execute function
Expand Down
5 changes: 4 additions & 1 deletion x/amm/keeper/msg_server_feed_multiple_external_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func (k msgServer) FeedMultipleExternalLiquidity(goCtx context.Context, msg *typ
}

pool.PoolParams.ExternalLiquidityRatio = elRatio
k.SetPool(ctx, pool)
err = k.SetPool(ctx, pool)
if err != nil {
return nil, err
}
}

return &types.MsgFeedMultipleExternalLiquidityResponse{}, nil
Expand Down
21 changes: 21 additions & 0 deletions x/amm/keeper/msg_server_update_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper

import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/elys-network/elys/x/amm/types"
)

func (k msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if k.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

k.Keeper.SetParams(ctx, *msg.Params)
return &types.MsgUpdateParamsResponse{}, nil
}
9 changes: 6 additions & 3 deletions x/amm/keeper/msg_server_update_pool_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ func (k Keeper) UpdatePoolParams(ctx sdk.Context, poolId uint64, poolParams type
}

pool.PoolParams = poolParams
k.SetPool(ctx, pool)
err := k.SetPool(ctx, pool)
if err != nil {
return 0, types.PoolParams{}, err
}

return pool.PoolId, pool.PoolParams, nil
}

func (k msgServer) UpdatePoolParams(goCtx context.Context, msg *types.MsgUpdatePoolParams) (*types.MsgUpdatePoolParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if k.authority != msg.Sender {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Sender)
if k.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

poolId, poolParams, err := k.Keeper.UpdatePoolParams(ctx, msg.PoolId, *msg.PoolParams)
Expand Down
9 changes: 6 additions & 3 deletions x/amm/keeper/msg_server_update_pool_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() {
sender := authtypes.NewModuleAddress(govtypes.ModuleName)

// bootstrap balances
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, tc.senderInitBalance)
params := suite.app.AmmKeeper.GetParams(suite.ctx)
poolCreationFee := sdk.NewCoin(ptypes.Elys, params.PoolCreationFee)
coins := tc.senderInitBalance.Add(poolCreationFee)
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, coins)
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, sender, tc.senderInitBalance)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, sender, coins)
suite.Require().NoError(err)

// execute function
Expand Down Expand Up @@ -184,7 +187,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() {
resp2, err := msgServer.UpdatePoolParams(
sdk.WrapSDKContext(suite.ctx),
&types.MsgUpdatePoolParams{
Sender: sender.String(),
Authority: sender.String(),
PoolId: pools[0].PoolId,
PoolParams: &tc.updatedPoolParams,
})
Expand Down
25 changes: 13 additions & 12 deletions x/amm/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import (
"github.com/elys-network/elys/x/amm/types"
)

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
k.PoolCreationFee(ctx),
)
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)

b := store.Get([]byte(types.ParamsKey))
if b == nil {
return
}

k.cdc.MustUnmarshal(b, &params)
return
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}

// PoolCreationFee returns the PoolCreationFee param
func (k Keeper) PoolCreationFee(ctx sdk.Context) (res uint64) {
k.paramstore.Get(ctx, types.KeyPoolCreationFee, &res)
return
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&params)
store.Set([]byte(types.ParamsKey), b)
}
1 change: 0 additions & 1 deletion x/amm/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ func TestGetParams(t *testing.T) {
k.SetParams(ctx, params)

require.EqualValues(t, params, k.GetParams(ctx))
require.EqualValues(t, params.PoolCreationFee, k.PoolCreationFee(ctx))
}
11 changes: 11 additions & 0 deletions x/amm/migrations/v4_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package migrations

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

func (m Migrator) V4Migration(ctx sdk.Context) error {
m.keeper.SetParams(ctx, types.DefaultParams())
return nil
}
Loading

0 comments on commit d54acc4

Please sign in to comment.