Skip to content

Commit

Permalink
add proto changes, params.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinoBombino committed Feb 16, 2022
1 parent c4ca839 commit 865472a
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 57 deletions.
14 changes: 13 additions & 1 deletion proto/sifnode/clp/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ import "gogoproto/gogo.proto";
option go_package = "github.com/Sifchain/sifnode/x/clp/types";

// Params - used for initializing default parameter for clp at genesis
message Params { uint64 min_create_pool_threshold = 1; }
message Params {
uint64 min_create_pool_threshold = 1;
string pmtp_native_weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string pmtp_external_weight = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 pmtp_start_block = 4;
int64 pmtp_end_block = 5;
}
18 changes: 18 additions & 0 deletions proto/sifnode/clp/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ message Pool {
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"pool_units\""
];
string normalized_pmtp_native_weight = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"normalized_pmtp\""
];
string normalized_pmtp_external_weight = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"normalized_pmtp\""
];
string swap_price_native = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"swap_price \""
];
string swap_price_external = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"swap_price \""
];
}

message LiquidityProvider {
Expand Down
20 changes: 20 additions & 0 deletions x/clp/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ func (k Keeper) GetMinCreatePoolThreshold(ctx sdk.Context) (res uint64) {
return res
}

func (k Keeper) GetPmtpNativeWeight(ctx sdk.Context) (res sdk.Dec) {
k.paramstore.Get(ctx, types.KeyPmtpNativeWeight, &res)
return res
}

func (k Keeper) GetPmtpExternalWeight(ctx sdk.Context) (res sdk.Dec) {
k.paramstore.Get(ctx, types.KeyPmtpExternalWeight, &res)
return res
}

func (k Keeper) GetPmtpStartBlock(ctx sdk.Context) (res int64) {
k.paramstore.Get(ctx, types.KeyPmtpStartBlock, &res)
return res
}

func (k Keeper) GetPmtpEndBlock(ctx sdk.Context) (res int64) {
k.paramstore.Get(ctx, types.KeyPmtpEndBlock, &res)
return res
}

func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, &params)
return params
Expand Down
94 changes: 90 additions & 4 deletions x/clp/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import (
"bytes"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

// Default parameter namespace
const (
DefaultParamspace = ModuleName
DefaultMinCreatePoolThreshold uint64 = 100
DefaultPmtpStartBlock int64 = 0
DefaultPmtpEndBlock int64 = 0
)

// Parameter store keys
var (
KeyMinCreatePoolThreshold = []byte("MinCreatePoolThreshold")
KeyPmtpNativeWeight = []byte("PmtpNativeWeight")
KeyPmtpExternalWeight = []byte("PmtpExternalWeight")
KeyPmtpStartBlock = []byte("PmtpStartBlock")
KeyPmtpEndBlock = []byte("PmtpEndBlock")
)

var _ paramtypes.ParamSet = (*Params)(nil)
Expand All @@ -26,26 +33,61 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params object
func NewParams(minThreshold uint64) Params {
func NewParams(minThreshold uint64, pmtpNativeWeight, pmtpExternalWeight sdk.Dec, pmtpStartBlock, pmtpEndBlock int64) Params {
return Params{
MinCreatePoolThreshold: minThreshold,
PmtpNativeWeight: pmtpNativeWeight,
PmtpExternalWeight: pmtpExternalWeight,
PmtpStartBlock: pmtpStartBlock,
PmtpEndBlock: pmtpEndBlock,
}
}

// ParamSetPairs - Implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMinCreatePoolThreshold, &p.MinCreatePoolThreshold, validateMinCreatePoolThreshold),
paramtypes.NewParamSetPair(KeyPmtpNativeWeight, &p.PmtpNativeWeight, validatePmtpNativeWeight),
paramtypes.NewParamSetPair(KeyPmtpExternalWeight, &p.PmtpExternalWeight, validatePmtpExternalWeight),
paramtypes.NewParamSetPair(KeyPmtpStartBlock, &p.PmtpStartBlock, validatePmtpStartBlock),
paramtypes.NewParamSetPair(KeyPmtpEndBlock, &p.PmtpEndBlock, validatePmtpEndBlock),
}
}

// DefaultParams defines the parameters for this module
func DefaultParams() Params {
return NewParams(DefaultMinCreatePoolThreshold)
return Params{
MinCreatePoolThreshold: DefaultMinCreatePoolThreshold,
PmtpNativeWeight: sdk.NewDecWithPrec(5, 1),
PmtpExternalWeight: sdk.NewDecWithPrec(5, 1),
PmtpStartBlock: DefaultPmtpStartBlock,
PmtpEndBlock: DefaultPmtpEndBlock,
}
}

func (p Params) Validate() error {
return validateMinCreatePoolThreshold(p.MinCreatePoolThreshold)
func (p Params) Validate() error { // TODO determine all checks
if err := validateMinCreatePoolThreshold(p.MinCreatePoolThreshold); err != nil {
return err
}
if err := validatePmtpNativeWeight(p.PmtpNativeWeight); err != nil {
return err
}
if err := validatePmtpExternalWeight(p.PmtpExternalWeight); err != nil {
return err
}
if err := validatePmtpStartBlock(p.PmtpStartBlock); err != nil {
return err
}
if err := validatePmtpEndBlock(p.PmtpEndBlock); err != nil {
return err
}
if p.PmtpEndBlock < (p.PmtpStartBlock) {
return fmt.Errorf(
"end block (%d) must be after begin block (%d)",
p.PmtpEndBlock, p.PmtpStartBlock,
)
}
return nil
}

func validateMinCreatePoolThreshold(i interface{}) error {
Expand All @@ -59,6 +101,50 @@ func validateMinCreatePoolThreshold(i interface{}) error {
return nil
}

func validatePmtpNativeWeight(i interface{}) error { // TODO determine all checks
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("pmtp native weight threshold must be positive: %d", v)
}
return nil
}

func validatePmtpExternalWeight(i interface{}) error { // TODO determine all checks
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNegative() {
return fmt.Errorf("pmtp external weight threshold must be positive: %d", v)
}
return nil
}

func validatePmtpStartBlock(i interface{}) error { // TODO determine all checks
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("pmtp start block cannot be negative: %d", v)
}
return nil
}

func validatePmtpEndBlock(i interface{}) error { // TODO determine all checks
v, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < 0 {
return fmt.Errorf("pmtp end block cannot be negative: %d", v)
}
return nil
}

func (p Params) Equal(p2 Params) bool {
bz1 := ModuleCdc.MustMarshalLengthPrefixed(&p)
bz2 := ModuleCdc.MustMarshalLengthPrefixed(&p2)
Expand Down
Loading

0 comments on commit 865472a

Please sign in to comment.