Skip to content

Commit

Permalink
Configurable symmetry threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
timlind committed Apr 27, 2022
1 parent ed3411b commit efe30b5
Show file tree
Hide file tree
Showing 14 changed files with 746 additions and 166 deletions.
4 changes: 4 additions & 0 deletions proto/sifnode/clp/v1/querier.proto
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ message ParamsReq {}

message ParamsRes {
Params params = 1;
string symmetry_threshold = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

message RewardParamsReq {}
Expand Down
13 changes: 12 additions & 1 deletion proto/sifnode/clp/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ service Msg {
rpc ModifyPmtpRates(MsgModifyPmtpRates) returns (MsgModifyPmtpRatesResponse);
rpc UpdatePmtpParams(MsgUpdatePmtpParams) returns (MsgUpdatePmtpParamsResponse);
rpc UpdateStakingRewardParams(MsgUpdateStakingRewardParams) returns (MsgUpdateStakingRewardParamsResponse);
rpc SetSymmetryThreshold(MsgSetSymmetryThreshold) returns (MsgSetSymmetryThresholdResponse);
}

//message MsgUpdateStakingRewardParams{
Expand Down Expand Up @@ -198,4 +199,14 @@ message MsgAddRewardPeriodRequest {
repeated RewardPeriod reward_periods = 2;
}

message MsgAddRewardPeriodResponse {}
message MsgAddRewardPeriodResponse {}

message MsgSetSymmetryThreshold {
string signer = 1;
string threshold = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

message MsgSetSymmetryThresholdResponse {}
1 change: 1 addition & 0 deletions x/clp/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
FlagNewPolicy = "newPolicy"
FlagMintParams = "mint-params"
FlagMinter = "minter"
FlagSymmetryThreshold = "threshold"
)

// common flagsets to add to various functions
Expand Down
36 changes: 35 additions & 1 deletion x/clp/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cli
import (
"encoding/json"
"fmt"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"io/ioutil"
"path/filepath"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

"log"

"github.com/Sifchain/sifnode/x/clp/types"
Expand Down Expand Up @@ -41,6 +42,7 @@ func GetTxCmd() *cobra.Command {
GetCmdModifyPmtpRates(),
GetCmdUpdatePmtpParams(),
GetCmdUpdateStakingRewards(),
GetCmdSetSymmetryThreshold(),
)

return clpTxCmd
Expand Down Expand Up @@ -113,6 +115,38 @@ func GetCmdUpdateRewardParams() *cobra.Command {
return cmd
}

func GetCmdSetSymmetryThreshold() *cobra.Command {
cmd := &cobra.Command{
Use: "set-symmetry-threshold",
Short: "Set symmetry threshold",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
signer := clientCtx.GetFromAddress()
if err != nil {
return err
}
threshold, err := sdk.NewDecFromStr(viper.GetString(FlagSymmetryThreshold))
if err != nil {
return err
}
msg := types.MsgSetSymmetryThreshold{
Signer: signer.String(),
Threshold: threshold,
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
cmd.Flags().String(FlagSymmetryThreshold, "", "")
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func GetCmdCreatePool() *cobra.Command {
cmd := &cobra.Command{
Use: "create-pool --from [key] --symbol [asset-symbol] --nativeAmount [amount] --externalAmount [amount]",
Expand Down
4 changes: 2 additions & 2 deletions x/clp/keeper/calculations.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func CalculateWithdrawalFromUnits(poolUnits sdk.Uint, nativeAssetBalance string,
// units = ((P (a R + A r))/(2 A R))*slidAdjustment

func CalculatePoolUnits(oldPoolUnits, nativeAssetBalance, externalAssetBalance, nativeAssetAmount,
externalAssetAmount sdk.Uint, normalizationFactor sdk.Dec, adjustExternalToken bool) (sdk.Uint, sdk.Uint, error) {
externalAssetAmount sdk.Uint, normalizationFactor sdk.Dec, adjustExternalToken bool, symmetryThreshold sdk.Dec) (sdk.Uint, sdk.Uint, error) {
nf := sdk.NewUintFromBigInt(normalizationFactor.RoundInt().BigInt())

if adjustExternalToken {
Expand Down Expand Up @@ -282,7 +282,7 @@ func CalculatePoolUnits(oldPoolUnits, nativeAssetBalance, externalAssetBalance,
}
slipAdjustment = sdk.NewDec(1).Sub(slipAdjustment)

if sdk.OneDec().Sub(slipAdjustment).GT(sdk.NewDecWithPrec(1, 4)) {
if sdk.OneDec().Sub(slipAdjustment).GT(symmetryThreshold) {
return sdk.ZeroUint(), sdk.ZeroUint(), types.ErrAsymmetricAdd
}

Expand Down
2 changes: 2 additions & 0 deletions x/clp/keeper/calculations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
tc.externalAssetAmount,
tc.normalizationFactor,
tc.adjustExternalToken,
sdk.NewDecWithPrec(1, 4),
)
})
return
Expand All @@ -1114,6 +1115,7 @@ func TestKeeper_CalculatePoolUnits(t *testing.T) {
tc.externalAssetAmount,
tc.normalizationFactor,
tc.adjustExternalToken,
sdk.NewDecWithPrec(1, 4),
)

if tc.errString != nil {
Expand Down
3 changes: 2 additions & 1 deletion x/clp/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ func (k Querier) GetPmtpParams(c context.Context, _ *types.PmtpParamsReq) (*type
func (k Querier) GetParams(c context.Context, _ *types.ParamsReq) (*types.ParamsRes, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.Keeper.GetParams(ctx)
threshold := k.Keeper.GetSymmetryThreshold(ctx)

return &types.ParamsRes{Params: &params}, nil
return &types.ParamsRes{Params: &params, SymmetryThreshold: threshold}, nil
}

func (k Querier) GetRewardParams(c context.Context, _ *types.RewardParamsReq) (*types.RewardParamsRes, error) {
Expand Down
18 changes: 18 additions & 0 deletions x/clp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

tokenregistrytypes "github.com/Sifchain/sifnode/x/tokenregistry/types"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"

Expand Down Expand Up @@ -97,3 +98,20 @@ func (k Keeper) GetNormalizationFactorFromAsset(ctx sdk.Context, asset types.Ass
}
return k.GetNormalizationFactor(registryEntry.Decimals)
}

func (k Keeper) GetSymmetryThreshold(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.SymmetryThresholdPrefix)
if bz == nil {
return sdk.NewDecWithPrec(1, 4)
}
var setThreshold types.MsgSetSymmetryThreshold
k.cdc.MustUnmarshal(bz, &setThreshold)
return setThreshold.Threshold
}

func (k Keeper) SetSymmetryThreshold(ctx sdk.Context, setThreshold *types.MsgSetSymmetryThreshold) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(setThreshold)
store.Set(types.SymmetryThresholdPrefix, bz)
}
23 changes: 21 additions & 2 deletions x/clp/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ type msgServer struct {
Keeper
}

func (k msgServer) SetSymmetryThreshold(goCtx context.Context, threshold *types.MsgSetSymmetryThreshold) (*types.MsgSetSymmetryThresholdResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
signer, err := sdk.AccAddressFromBech32(threshold.Signer)
if err != nil {
return nil, err
}
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_CLPDEX, signer) {
return nil, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", threshold.Signer))
}

k.Keeper.SetSymmetryThreshold(sdk.UnwrapSDKContext(goCtx), threshold)

return &types.MsgSetSymmetryThresholdResponse{}, nil
}

func (k msgServer) UpdateStakingRewardParams(goCtx context.Context, msg *types.MsgUpdateStakingRewardParams) (*types.MsgUpdateStakingRewardParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
signer, err := sdk.AccAddressFromBech32(msg.Signer)
Expand Down Expand Up @@ -668,7 +683,9 @@ func (k msgServer) CreatePool(goCtx context.Context, msg *types.MsgCreatePool) (
nativeBalance := msg.NativeAssetAmount
externalBalance := msg.ExternalAssetAmount
normalizationFactor, adjustExternalToken := k.GetNormalizationFactor(eAsset.Decimals)
poolUnits, lpunits, err := CalculatePoolUnits(sdk.ZeroUint(), sdk.ZeroUint(), sdk.ZeroUint(), nativeBalance, externalBalance, normalizationFactor, adjustExternalToken)
symmetryThreshold := k.GetSymmetryThreshold(ctx)
poolUnits, lpunits, err := CalculatePoolUnits(sdk.ZeroUint(), sdk.ZeroUint(), sdk.ZeroUint(),
nativeBalance, externalBalance, normalizationFactor, adjustExternalToken, symmetryThreshold)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrUnableToCreatePool, err.Error())
}
Expand Down Expand Up @@ -718,14 +735,16 @@ func (k msgServer) AddLiquidity(goCtx context.Context, msg *types.MsgAddLiquidit
return nil, types.ErrPoolDoesNotExist
}
normalizationFactor, adjustExternalToken := k.GetNormalizationFactor(eAsset.Decimals)
symmetryThreshold := k.GetSymmetryThreshold(ctx)
newPoolUnits, lpUnits, err := CalculatePoolUnits(
pool.PoolUnits,
pool.NativeAssetBalance,
pool.ExternalAssetBalance,
msg.NativeAssetAmount,
msg.ExternalAssetAmount,
normalizationFactor,
adjustExternalToken)
adjustExternalToken,
symmetryThreshold)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions x/clp/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
PmtpEpochPrefix = []byte{0x04} // Key to store the Epoch
PmtpParamsPrefix = []byte{0x05} // Key to store the Pmtp params
RewardParamPrefix = []byte{0x06}
SymmetryThresholdPrefix = []byte{0x07}
)

// Generates a key for storing a specific pool
Expand Down
29 changes: 29 additions & 0 deletions x/clp/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
_ sdk.Msg = &MsgModifyPmtpRates{}
_ sdk.Msg = &MsgUpdatePmtpParams{}
_ sdk.Msg = &MsgUpdateStakingRewardParams{}
_ sdk.Msg = &MsgSetSymmetryThreshold{}
)

func (m MsgUpdateStakingRewardParams) Route() string {
Expand Down Expand Up @@ -449,3 +450,31 @@ func (m MsgUnlockLiquidityRequest) GetSigners() []sdk.AccAddress {
}
return []sdk.AccAddress{addr}
}

func (m MsgSetSymmetryThreshold) Route() string {
return RouterKey
}

func (m MsgSetSymmetryThreshold) Type() string {
return "set_symmetry_threshold"
}

func (m MsgSetSymmetryThreshold) ValidateBasic() error {
if m.Signer != "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}

return nil
}

func (m MsgSetSymmetryThreshold) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

func (m MsgSetSymmetryThreshold) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}
Loading

0 comments on commit efe30b5

Please sign in to comment.