Skip to content

Commit

Permalink
feat: init params & swapped
Browse files Browse the repository at this point in the history
Signed-off-by: 170210 <[email protected]>
  • Loading branch information
170210 committed Apr 16, 2024
1 parent 4e0771a commit 9fc8271
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 8 deletions.
52 changes: 52 additions & 0 deletions x/fswap/keeper/fswap.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 21 additions & 3 deletions x/fswap/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params)
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&params)
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
}
55 changes: 55 additions & 0 deletions x/fswap/types/fswap.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions x/fswap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
48 changes: 43 additions & 5 deletions x/fswap/types/params.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,73 @@
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 {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// 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
}

Expand Down

0 comments on commit 9fc8271

Please sign in to comment.