Skip to content

Commit

Permalink
Add unit test on LiquidityRatioFromPriceDepth & resolve message codec…
Browse files Browse the repository at this point in the history
… registration issue
  • Loading branch information
jelysn committed Sep 6, 2023
1 parent 21be7da commit efbd239
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
26 changes: 26 additions & 0 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
package keeper_test

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

func (suite *KeeperTestSuite) TestLiquidityRatioFromPriceDepth() {
depth := sdk.NewDecWithPrec(1, 2) // 1%
suite.Require().Equal("0.005012562893380046", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(2, 2) // 2%
suite.Require().Equal("0.010050506338833466", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(5, 2) // 5%
suite.Require().Equal("0.025320565519103610", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(10, 2) // 10%
suite.Require().Equal("0.051316701949486201", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(30, 2) // 30%
suite.Require().Equal("0.163339973465924452", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(50, 2) // 50%
suite.Require().Equal("0.292893218813452476", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(70, 2) // 70%
suite.Require().Equal("0.452277442494833887", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(90, 2) // 90%
suite.Require().Equal("0.683772233983162067", keeper.LiquidityRatioFromPriceDepth(depth).String())
depth = sdk.NewDecWithPrec(100, 2) // 100%
suite.Require().Equal("1.000000000000000000", keeper.LiquidityRatioFromPriceDepth(depth).String())
}
10 changes: 2 additions & 8 deletions x/amm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,19 @@ func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgJoinPool{}, "amm/JoinPool", nil)
cdc.RegisterConcrete(&MsgExitPool{}, "amm/ExitPool", nil)
cdc.RegisterConcrete(&MsgSwapExactAmountIn{}, "amm/SwapExactAmountIn", nil)
cdc.RegisterConcrete(&MsgFeedMultipleExternalLiquidity{}, "amm/FeedMultipleExternalLiquidity", nil)
cdc.RegisterConcrete(&MsgSwapExactAmountOut{}, "amm/SwapExactAmountOut", nil)
// this line is used by starport scaffolding # 2
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreatePool{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgJoinPool{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgExitPool{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgSwapExactAmountIn{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgSwapExactAmountOut{},
&MsgFeedMultipleExternalLiquidity{},
)
// this line is used by starport scaffolding # 3

Expand Down
45 changes: 45 additions & 0 deletions x/amm/types/message_feed_multiple_external_liquidity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgFeedMultipleExternalLiquidity = "feed_multiple_external_liquidity"

var _ sdk.Msg = &MsgFeedMultipleExternalLiquidity{}

func NewMsgFeedMultipleExternalLiquidity(sender string) *MsgFeedMultipleExternalLiquidity {
return &MsgFeedMultipleExternalLiquidity{
Sender: sender,
}
}

func (msg *MsgFeedMultipleExternalLiquidity) Route() string {
return RouterKey
}

func (msg *MsgFeedMultipleExternalLiquidity) Type() string {
return TypeMsgSwapExactAmountOut
}

func (msg *MsgFeedMultipleExternalLiquidity) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}

func (msg *MsgFeedMultipleExternalLiquidity) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgFeedMultipleExternalLiquidity) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
return nil
}
2 changes: 2 additions & 0 deletions x/incentive/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
ctypes "github.com/elys-network/elys/x/commitment/types"
oracletypes "github.com/elys-network/elys/x/oracle/types"
)

// CommitmentKeeper
Expand Down Expand Up @@ -97,4 +98,5 @@ type AmmKeeper interface {
// OracleKeeper defines the expected interface needed to retrieve price info
type OracleKeeper interface {
GetAssetPriceFromDenom(ctx sdk.Context, denom string) sdk.Dec
GetPriceFeeder(ctx sdk.Context, feeder string) (val oracletypes.PriceFeeder, found bool)
}

0 comments on commit efbd239

Please sign in to comment.