From 9fc8271994895c1de6fd57ee7f6a8c9f198a3a3a Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 16 Apr 2024 18:04:44 +0900 Subject: [PATCH] feat: init params & swapped Signed-off-by: 170210 --- x/fswap/keeper/fswap.go | 52 +++++++++++++++++++++++++++++++++++++ x/fswap/keeper/params.go | 24 +++++++++++++++--- x/fswap/types/fswap.go | 55 ++++++++++++++++++++++++++++++++++++++++ x/fswap/types/keys.go | 6 +++++ x/fswap/types/params.go | 48 +++++++++++++++++++++++++++++++---- 5 files changed, 177 insertions(+), 8 deletions(-) create mode 100644 x/fswap/keeper/fswap.go create mode 100644 x/fswap/types/fswap.go diff --git a/x/fswap/keeper/fswap.go b/x/fswap/keeper/fswap.go new file mode 100644 index 0000000000..6311c185ee --- /dev/null +++ b/x/fswap/keeper/fswap.go @@ -0,0 +1,52 @@ +package keeper + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +// GetSwapped get all parameters as types.Swapped +func (k Keeper) GetSwapped(ctx sdk.Context) types.Swapped { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.SwappedKey}) + var swapped types.Swapped + //todo + if bz == nil { + panic(types.ErrParamsNotFound) + } + k.cdc.MustUnmarshal(bz, &swapped) + return swapped +} + +// SetSwapped set the types.Swapped +func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error { + store := ctx.KVStore(k.storeKey) + bz, err := k.cdc.Marshal(&swapped) + if err != nil { + return err + } + store.Set([]byte{types.SwappedKey}, bz) + return nil +} + +func (k Keeper) GetTotalSupply(ctx sdk.Context) sdk.DecCoin { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.TotalSupplyKey}) + var totalSupply sdk.DecCoin + if bz == nil { + //todo + panic(types.ErrParamsNotFound) + } + k.cdc.MustUnmarshal(bz, &totalSupply) + return totalSupply +} + +func (k Keeper) SetTotalSupply(ctx sdk.Context, totalSupply sdk.DecCoin) error { + store := ctx.KVStore(k.storeKey) + bz, err := k.cdc.Marshal(&totalSupply) + if err != nil { + return err + } + store.Set([]byte{types.TotalSupplyKey}, bz) + return nil +} diff --git a/x/fswap/keeper/params.go b/x/fswap/keeper/params.go index 77d2823f2e..25ea6b4851 100644 --- a/x/fswap/keeper/params.go +++ b/x/fswap/keeper/params.go @@ -7,10 +7,28 @@ import ( // GetParams get all parameters as types.Params func (k Keeper) GetParams(ctx sdk.Context) types.Params { - return types.NewParams() + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.ParamsKey}) + var params types.Params + if bz == nil { + panic(types.ErrParamsNotFound) + } + k.cdc.MustUnmarshal(bz, ¶ms) + return params } // SetParams set the params -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { - k.paramstore.SetParamSet(ctx, ¶ms) +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { + store := ctx.KVStore(k.storeKey) + bz, err := k.cdc.Marshal(¶ms) + if err != nil { + return err + } + store.Set([]byte{types.ParamsKey}, bz) + return nil +} + +func (k Keeper) NewCoinDenom(ctx sdk.Context) string { + params := k.GetParams(ctx) + return params.NewCoinDenom } diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go new file mode 100644 index 0000000000..df3fe2e06c --- /dev/null +++ b/x/fswap/types/fswap.go @@ -0,0 +1,55 @@ +package types + +import ( + "errors" + fmt "fmt" + "strings" + + sdk "github.com/Finschia/finschia-sdk/types" +) + +// NewSwapped creates a new Swapped instance +func NewSwapped( + oldCoin sdk.Coin, + newCoin sdk.Coin, +) Swapped { + return Swapped{ + OldCoinAmount: oldCoin, + NewCoinAmount: newCoin, + } +} + +// DefaultSwapped returns an initial Swapped object +func DefaultSwapped() Swapped { + return NewSwapped( + sdk.NewCoin("cony", sdk.NewInt(0)), + sdk.NewCoin(DefaultNewCoinDenom, sdk.NewInt(0)), + ) +} + +func validateCoin(i interface{}) error { + v, ok := i.(sdk.Coin) + if !ok { + return fmt.Errorf("invalid coin amount: %T", i) + } + + if strings.TrimSpace(v.Denom) == "" { + return errors.New("denom cannot be blank") + } + if err := sdk.ValidateDenom(v.Denom); err != nil { + return err + } + + return nil +} + +// Validate validates the set of swapped +func (s Swapped) Validate() error { + if err := validateCoin(s.OldCoinAmount); err != nil { + return err + } + if err := validateCoin(s.NewCoinAmount); err != nil { + return err + } + return nil +} diff --git a/x/fswap/types/keys.go b/x/fswap/types/keys.go index 18047ccfb2..6089626eaf 100644 --- a/x/fswap/types/keys.go +++ b/x/fswap/types/keys.go @@ -17,6 +17,12 @@ const ( MemStoreKey = "mem_fswap" ) +var ( + ParamsKey = byte(0x00) + SwappedKey = byte(0x01) + TotalSupplyKey = byte(0x02) +) + func KeyPrefix(p string) []byte { return []byte(p) } diff --git a/x/fswap/types/params.go b/x/fswap/types/params.go index f3d55e7f9e..f8159f3e08 100644 --- a/x/fswap/types/params.go +++ b/x/fswap/types/params.go @@ -1,12 +1,23 @@ package types import ( + "errors" + fmt "fmt" + "strings" + "gopkg.in/yaml.v2" + sdk "github.com/Finschia/finschia-sdk/types" paramtypes "github.com/Finschia/finschia-sdk/x/params/types" ) -var _ paramtypes.ParamSet = (*Params)(nil) +const ( + DefaultNewCoinDenom string = "PDT" +) + +var ( + ParamStoreKeyNewDenom = []byte("NewDenom") +) // ParamKeyTable the param key table for launch module func ParamKeyTable() paramtypes.KeyTable { @@ -14,22 +25,49 @@ func ParamKeyTable() paramtypes.KeyTable { } // NewParams creates a new Params instance -func NewParams() Params { - return Params{} +func NewParams( + newCoinDenom string, +) Params { + return Params{ + NewCoinDenom: newCoinDenom, + } } // DefaultParams returns a default set of parameters func DefaultParams() Params { - return NewParams() + return NewParams( + DefaultNewCoinDenom, + ) } // ParamSetPairs get the params.ParamSet func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{} + return paramtypes.ParamSetPairs{ + paramtypes.NewParamSetPair(ParamStoreKeyNewDenom, &p.NewCoinDenom, validateNewCoinDenom), + } +} + +func validateNewCoinDenom(i interface{}) error { + v, ok := i.(string) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if strings.TrimSpace(v) == "" { + return errors.New("new denom cannot be blank") + } + if err := sdk.ValidateDenom(v); err != nil { + return err + } + + return nil } // Validate validates the set of params func (p Params) Validate() error { + if err := validateNewCoinDenom(p.NewCoinDenom); err != nil { + return err + } return nil }