diff --git a/proto/lbm/fswap/v1/fswap.proto b/proto/lbm/fswap/v1/fswap.proto index 827f5f3e32..6b056d0b57 100644 --- a/proto/lbm/fswap/v1/fswap.proto +++ b/proto/lbm/fswap/v1/fswap.proto @@ -4,11 +4,11 @@ package lbm.fswap.v1; option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types"; import "gogoproto/gogo.proto"; -import "cosmos/base/v1beta1/coin.proto"; -message Swapped{ - cosmos.base.v1beta1.Coin old_coin_amount = 1 - [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; - cosmos.base.v1beta1.Coin new_coin_amount = 2 - [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"]; +message Swapped { + option (gogoproto.goproto_stringer) = false; + string old_coin_amount = 1 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; + string new_coin_amount = 2 + [(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false]; } diff --git a/simapp/app.go b/simapp/app.go index a86dd1c611..9be1b8a54c 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -72,6 +72,10 @@ import ( foundationclient "github.com/Finschia/finschia-sdk/x/foundation/client" foundationkeeper "github.com/Finschia/finschia-sdk/x/foundation/keeper" foundationmodule "github.com/Finschia/finschia-sdk/x/foundation/module" + "github.com/Finschia/finschia-sdk/x/fswap" + fswapconfig "github.com/Finschia/finschia-sdk/x/fswap/config" + fswapkeeper "github.com/Finschia/finschia-sdk/x/fswap/keeper" + fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" "github.com/Finschia/finschia-sdk/x/genutil" genutiltypes "github.com/Finschia/finschia-sdk/x/genutil/types" "github.com/Finschia/finschia-sdk/x/gov" @@ -138,6 +142,7 @@ var ( vesting.AppModuleBasic{}, tokenmodule.AppModuleBasic{}, collectionmodule.AppModuleBasic{}, + fswap.AppModuleBasic{}, ) // module account permissions @@ -150,6 +155,7 @@ var ( stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking}, stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, + fswaptypes.ModuleName: {authtypes.Burner, authtypes.Minter}, } // module accounts that are allowed to receive tokens @@ -197,6 +203,7 @@ type SimApp struct { ClassKeeper classkeeper.Keeper TokenKeeper tokenkeeper.Keeper CollectionKeeper collectionkeeper.Keeper + FswapKeeper fswapkeeper.Keeper // the module manager mm *module.Manager @@ -250,6 +257,7 @@ func NewSimApp( token.StoreKey, collection.StoreKey, authzkeeper.StoreKey, + fswaptypes.StoreKey, ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) // NOTE: The testingkey is just mounted for testing purposes. Actual applications should @@ -352,6 +360,9 @@ func NewSimApp( // If evidence needs to be handled for the app, set routes in router here and seal app.EvidenceKeeper = *evidenceKeeper + cfg := fswapconfig.DefaultConfig() + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], cfg, app.AccountKeeper, app.BankKeeper) + /**** Module Options ****/ // NOTE: we may consider parsing `appOpts` inside module constructors. For the moment @@ -383,6 +394,7 @@ func NewSimApp( tokenmodule.NewAppModule(appCodec, app.TokenKeeper), collectionmodule.NewAppModule(appCodec, app.CollectionKeeper), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), + fswap.NewAppModule(appCodec, app.FswapKeeper, app.AccountKeeper, app.BankKeeper), ) // During begin block slashing happens after distr.BeginBlocker so that @@ -410,6 +422,7 @@ func NewSimApp( vestingtypes.ModuleName, token.ModuleName, collection.ModuleName, + fswaptypes.ModuleName, ) app.mm.SetOrderEndBlockers( crisistypes.ModuleName, @@ -431,6 +444,7 @@ func NewSimApp( foundation.ModuleName, token.ModuleName, collection.ModuleName, + fswaptypes.ModuleName, ) // NOTE: The genutils module must occur after staking so that pools are @@ -458,6 +472,7 @@ func NewSimApp( vestingtypes.ModuleName, token.ModuleName, collection.ModuleName, + fswaptypes.ModuleName, ) // Uncomment if you want to set a custom migration order here. @@ -722,6 +737,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable()) paramsKeeper.Subspace(crisistypes.ModuleName) paramsKeeper.Subspace(foundation.ModuleName) + paramsKeeper.Subspace(fswaptypes.ModuleName) return paramsKeeper } diff --git a/x/foundation/keeper/internal/keeper_test.go b/x/foundation/keeper/internal/keeper_test.go index 1b81a82a20..c95d1b6387 100644 --- a/x/foundation/keeper/internal/keeper_test.go +++ b/x/foundation/keeper/internal/keeper_test.go @@ -14,7 +14,7 @@ import ( sdk "github.com/Finschia/finschia-sdk/types" authtypes "github.com/Finschia/finschia-sdk/x/auth/types" "github.com/Finschia/finschia-sdk/x/foundation" - keeper "github.com/Finschia/finschia-sdk/x/foundation/keeper" + "github.com/Finschia/finschia-sdk/x/foundation/keeper" "github.com/Finschia/finschia-sdk/x/foundation/keeper/internal" govtypes "github.com/Finschia/finschia-sdk/x/gov/types" minttypes "github.com/Finschia/finschia-sdk/x/mint/types" diff --git a/x/fswap/config/config.go b/x/fswap/config/config.go new file mode 100644 index 0000000000..5ef95e09a3 --- /dev/null +++ b/x/fswap/config/config.go @@ -0,0 +1,53 @@ +package config + +import ( + "math/big" + + "github.com/Finschia/finschia-sdk/types" +) + +type FswapConfig interface { + OldDenom() string + NewDenom() string + SwapCap() types.Int + SwapMultiple() types.Int +} + +func DefaultConfig() *Config { + oldDenom := "cony" + newDenom := "peb" + defaultCap, _ := big.NewInt(0).SetString("1151185567094084523856000000", 10) + swapCap := types.NewIntFromBigInt(defaultCap) + num, _ := big.NewInt(0).SetString("148079656000000", 10) + swapMultiple := types.NewIntFromBigInt(num) + + return &Config{ + oldDenom, + newDenom, + swapCap, + swapMultiple, + } +} + +type Config struct { + oldDenom string + newDenom string + swapCap types.Int + swapMultiple types.Int +} + +func (c Config) OldDenom() string { + return c.oldDenom +} + +func (c Config) NewDenom() string { + return c.newDenom +} + +func (c Config) SwapCap() types.Int { + return c.swapCap +} + +func (c Config) SwapMultiple() types.Int { + return c.swapMultiple +} diff --git a/x/fswap/genesis.go b/x/fswap/genesis.go deleted file mode 100644 index af232aad36..0000000000 --- a/x/fswap/genesis.go +++ /dev/null @@ -1,24 +0,0 @@ -package fswap - -import ( - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/fswap/keeper" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -// InitGenesis initializes the capability module's state from a provided genesis -// state. -func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { - // this line is used by starport scaffolding # genesis/module/init - k.SetParams(ctx, genState.Params) -} - -// ExportGenesis returns the capability module's exported genesis. -func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { - genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) - - // this line is used by starport scaffolding # genesis/module/export - - return genesis -} diff --git a/x/fswap/genesis_test.go b/x/fswap/genesis_test.go deleted file mode 100644 index 43bc7afc7c..0000000000 --- a/x/fswap/genesis_test.go +++ /dev/null @@ -1 +0,0 @@ -package fswap_test diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go new file mode 100644 index 0000000000..9dfc9d452d --- /dev/null +++ b/x/fswap/keeper/genesis.go @@ -0,0 +1,33 @@ +package keeper + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +// InitGenesis initializes the module's state accWithOldCoin a provided genesis +// state. +func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { + if err := k.SetParams(ctx, genState.Params); err != nil { + panic(err) + } + if err := k.SetSwapped(ctx, genState.Swapped); err != nil { + panic(err) + } +} + +// ExportGenesis returns the capability module's exported genesis. +func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + params, err := k.GetParams(ctx) + if err != nil { + panic(err) + } + swapped, err := k.GetSwapped(ctx) + if err != nil { + panic(err) + } + return &types.GenesisState{ + Params: params, + Swapped: swapped, + } +} diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go new file mode 100644 index 0000000000..a07e183364 --- /dev/null +++ b/x/fswap/keeper/genesis_test.go @@ -0,0 +1,13 @@ +package keeper_test + +import ( + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *KeeperTestSuite) TestInitAndExportGenesis() { + s.keeper.InitGenesis(s.ctx, *types.DefaultGenesis()) + got := s.keeper.ExportGenesis(s.ctx) + s.Require().NotNil(got) + s.Require().Equal(types.DefaultParams(), got.Params) + s.Require().Equal(types.DefaultSwapped(), got.Swapped) +} diff --git a/x/fswap/keeper/grpc_query.go b/x/fswap/keeper/grpc_query.go index b55b7c1155..a1467b0e83 100644 --- a/x/fswap/keeper/grpc_query.go +++ b/x/fswap/keeper/grpc_query.go @@ -3,17 +3,33 @@ package keeper import ( "context" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -var _ types.QueryServer = Keeper{} +var _ types.QueryServer = QueryServer{} -// Swapped implements types.QueryServer. -func (k Keeper) Swapped(context.Context, *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { - panic("unimplemented") +type QueryServer struct { + Keeper } -// TotalNewCurrencySwapLimit implements types.QueryServer. -func (k Keeper) TotalNewCurrencySwapLimit(context.Context, *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { - panic("unimplemented") +func NewQueryServer(keeper Keeper) *QueryServer { + return &QueryServer{ + keeper, + } +} + +func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) { + c := sdk.UnwrapSDKContext(ctx) + swapped, err := s.Keeper.GetSwapped(c) + if err != nil { + return nil, err + } + return &types.QuerySwappedResponse{Swapped: swapped}, nil +} + +func (s QueryServer) TotalNewCurrencySwapLimit(ctx context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) { + //c := sdk.UnwrapSDKContext(ctx) + //s.Keeper.GetSwappableNewCoinAmount() + return nil, nil } diff --git a/x/fswap/keeper/grpc_query_params_test.go b/x/fswap/keeper/grpc_query_params_test.go deleted file mode 100644 index 9429264902..0000000000 --- a/x/fswap/keeper/grpc_query_params_test.go +++ /dev/null @@ -1 +0,0 @@ -package keeper_test diff --git a/x/fswap/keeper/grpc_query_test.go b/x/fswap/keeper/grpc_query_test.go new file mode 100644 index 0000000000..f8d8abacb0 --- /dev/null +++ b/x/fswap/keeper/grpc_query_test.go @@ -0,0 +1,53 @@ +package keeper_test + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *KeeperTestSuite) TestQuerySwap() { + testCases := map[string]struct { + swapReq *types.MsgSwapRequest + expectedBalanceWithoutMultiply sdk.Int + }{ + "valid request": { + &types.MsgSwapRequest{ + s.accWithOldCoin.String(), + sdk.NewCoin(s.keeper.OldDenom(), sdk.NewInt(100)), + }, + sdk.NewInt(100), + }, + } + for name, tc := range testCases { + s.Run(name, func() { + s.Require().NoError(tc.swapReq.ValidateBasic()) + ctx, _ := s.ctx.CacheContext() + res, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.swapReq) + s.Require().NoError(err) + s.Require().NotNil(res) + swapped, err := s.queryServer.Swapped(sdk.WrapSDKContext(ctx), &types.QuerySwappedRequest{}) + s.Require().NoError(err) + + expectedOldAmount := tc.expectedBalanceWithoutMultiply + expectedNewAmount := tc.expectedBalanceWithoutMultiply.Mul(s.keeper.SwapMultiple()) + actualOldCoinAmount := swapped.GetSwapped().OldCoinAmount + actualNewCoinAmount := swapped.GetSwapped().NewCoinAmount + s.Require().Equal(expectedOldAmount, actualOldCoinAmount) + s.Require().Equal(expectedNewAmount, actualNewCoinAmount) + }) + } +} + +func (s *KeeperTestSuite) TestQueryTotalNewCurrencySwapLimit() { + // TODO: Need to confirm, it may not necessary + // Can be calculated by query Params.SwappableNewCoinAmount and query Swapped.NewCoinAmount + + // SwappableNewCoinAmount << as param and after first set it'll become constant value + // Why user want to know constant?? use may want to remaining swappable balance. + + //ctx, _ := s.ctx.CacheContext() + //res, err := s.queryServer.TotalNewCurrencySwapLimit(sdk.WrapSDKContext(ctx), &types.QueryTotalSwappableAmountRequest{}) + //s.Require().NoError(err) + //expectedLimit := sdk.NewCoin(s.keeper.NewDenom(), s.keeper.SwapCap()) + //s.Require().Equal(expectedLimit, res.SwappableNewCoinAmount) +} diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 7855532e5f..7c406046f9 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -1,91 +1,89 @@ package keeper import ( - "errors" + "fmt" + "strings" "github.com/Finschia/finschia-sdk/codec" storetypes "github.com/Finschia/finschia-sdk/store/types" sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/config" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -// TODO: move const to proper place -const ( - OldDenom = "cony" - NewDenom = "TBD" - SwapMultiple = 123 -) - type Keeper struct { cdc codec.BinaryCodec storeKey storetypes.StoreKey + config.FswapConfig + AccountKeeper BankKeeper } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak AccountKeeper, bk BankKeeper) *Keeper { - return &Keeper{ +func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, cfg config.FswapConfig, ak AccountKeeper, bk BankKeeper) Keeper { + if len(strings.TrimSpace(cfg.NewDenom())) == 0 { + panic("new denom must be provided") + } + //if strings.Compare(strings.TrimSpace(cfg.NewDenom()), cfg.SwapCap().Denom) != 0 { + // panic("new denom does not match new denom") + //} + return Keeper{ cdc, storeKey, + cfg, ak, bk, } } -func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) error { - // 1. Check Balance - // 2. Check swapCap - // 3. Send coin to module - // 4. Burn & Mint TBD coins(with multiple) - // 5. Send back coins to addr - // 6. Update swapped state - if ok := k.HasBalance(ctx, addr, amount); !ok { - return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "not enough amount: %s", amount) +func (k Keeper) Swap(ctx sdk.Context, addr sdk.AccAddress, oldCoin sdk.Coin) error { + if ok := k.HasBalance(ctx, addr, oldCoin); !ok { + return sdkerrors.ErrInsufficientFunds } - multipleAmount := amount.Amount.Mul(sdk.NewInt(SwapMultiple)) - newAmount := sdk.NewCoin(NewDenom, multipleAmount) + newAmount := oldCoin.Amount.Mul(k.SwapMultiple()) + newCoin := sdk.NewCoin(k.NewDenom(), newAmount) if err := k.checkSwapCap(ctx, newAmount); err != nil { return err } moduleAddr := k.GetModuleAddress(types.ModuleName) - if err := k.SendCoins(ctx, addr, moduleAddr, sdk.NewCoins(amount)); err != nil { + if err := k.SendCoins(ctx, addr, moduleAddr, sdk.NewCoins(oldCoin)); err != nil { return err } - if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(amount)); err != nil { + if err := k.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(oldCoin)); err != nil { return err } - if err := k.MintCoins(ctx, types.ModuleName, sdk.NewCoins(newAmount)); err != nil { + if err := k.MintCoins(ctx, types.ModuleName, sdk.NewCoins(newCoin)); err != nil { return err } - if err := k.SendCoins(ctx, moduleAddr, addr, sdk.NewCoins(newAmount)); err != nil { + if err := k.SendCoins(ctx, moduleAddr, addr, sdk.NewCoins(newCoin)); err != nil { return err } - if err := k.updateSwapped(ctx, amount, newAmount); err != nil { + if err := k.updateSwapped(ctx, oldCoin.Amount, newAmount); err != nil { return err } if err := ctx.EventManager().EmitTypedEvent(&types.EventSwapCoins{ Address: addr.String(), - OldCoinAmount: amount, - NewCoinAmount: newAmount, + OldCoinAmount: oldCoin.Amount, + NewCoinAmount: newCoin.Amount, }); err != nil { - panic(err) + return err } return nil } func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress) error { - balance := k.GetBalance(ctx, addr, OldDenom) + balance := k.GetBalance(ctx, addr, k.OldDenom()) if balance.IsZero() { - return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "zero balance for %s", OldDenom) + return sdkerrors.ErrInsufficientFunds } if err := k.Swap(ctx, addr, balance); err != nil { @@ -94,7 +92,7 @@ func (k Keeper) SwapAll(ctx sdk.Context, addr sdk.AccAddress) error { return nil } -func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount sdk.Coin, newAmount sdk.Coin) error { +func (k Keeper) updateSwapped(ctx sdk.Context, oldAmount sdk.Int, newAmount sdk.Int) error { prevSwapped, err := k.GetSwapped(ctx) if err != nil { return err @@ -125,37 +123,47 @@ func (k Keeper) GetSwapped(ctx sdk.Context) (types.Swapped, error) { return swapped, nil } -func (k Keeper) setSwapCap(ctx sdk.Context) error { - // TODO(bjs): how to set it only once - //store := ctx.KVStore(k.storeKey) +func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error { + store := ctx.KVStore(k.storeKey) + key := swappedKey() + bz, err := k.cdc.Marshal(&swapped) + if err != nil { + return err + } + store.Set(key, bz) return nil } -func (k Keeper) getSwapCap(ctx sdk.Context) (sdk.Coin, error) { - store := ctx.KVStore(k.storeKey) - bz := store.Get(swapCapKey()) - if bz == nil { - return sdk.Coin{}, errors.New("swap cap not found") +func (k Keeper) checkSwapCap(ctx sdk.Context, newCoinAmount sdk.Int) error { + swapped, err := k.GetSwapped(ctx) + if err != nil { + return err } - swapCap := sdk.Coin{} - if err := k.cdc.Unmarshal(bz, &swapCap); err != nil { - return sdk.Coin{}, err + if k.SwapCap().LT(swapped.NewCoinAmount.Add(newCoinAmount)) { + return fmt.Errorf("cann't swap more because of swapCap limit, amount=%s", newCoinAmount.String()) } - return swapCap, nil + + return nil } -func (k Keeper) checkSwapCap(ctx sdk.Context, amountNewCoin sdk.Coin) error { - swapCap, err := k.getSwapCap(ctx) - if err != nil { - return err +func (k Keeper) GetSwappableNewCoinAmount(ctx sdk.Context) sdk.Coin { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.SwappableNewCoinAmountKey}) + var swappableNewCoinAmount sdk.Coin + if bz == nil { + panic(types.ErrSwappableNewCoinAmountNotFound) } - swapped, err := k.GetSwapped(ctx) + k.cdc.MustUnmarshal(bz, &swappableNewCoinAmount) + return swappableNewCoinAmount +} + +func (k Keeper) SetSwappableNewCoinAmount(ctx sdk.Context, swappableNewCoinAmount sdk.Coin) error { + store := ctx.KVStore(k.storeKey) + bz, err := k.cdc.Marshal(&swappableNewCoinAmount) if err != nil { return err } - if swapCap.IsLT(swapped.GetNewCoinAmount().Add(amountNewCoin)) { - return errors.New("not enough swap coin amount") - } + store.Set([]byte{types.SwappableNewCoinAmountKey}, bz) return nil } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 8e3f6e9ca1..9e790e8dab 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -1,8 +1,172 @@ -package keeper +package keeper_test import ( + "context" + "fmt" "testing" + + "github.com/stretchr/testify/suite" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + + "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" + "github.com/Finschia/finschia-sdk/simapp" + "github.com/Finschia/finschia-sdk/testutil/testdata" + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/keeper" + "github.com/Finschia/finschia-sdk/x/fswap/types" + minttypes "github.com/Finschia/finschia-sdk/x/mint/types" ) -func TestNewKeeper(t *testing.T) { +type KeeperTestSuite struct { + suite.Suite + + ctx sdk.Context + goCtx context.Context + keeper keeper.Keeper + queryServer types.QueryServer + msgServer types.MsgServer + + accWithOldCoin sdk.AccAddress + accWithNewCoin sdk.AccAddress + initBalance sdk.Int +} + +func (s *KeeperTestSuite) createRandomAccounts(n int) []sdk.AccAddress { + seenAddresses := make(map[string]bool, n) + addresses := make([]sdk.AccAddress, n) + for i := range addresses { + var address sdk.AccAddress + for { + pk := secp256k1.GenPrivKey().PubKey() + address = sdk.AccAddress(pk.Address()) + if !seenAddresses[address.String()] { + seenAddresses[address.String()] = true + break + } + } + addresses[i] = address + } + return addresses +} + +func (s *KeeperTestSuite) SetupTest() { + checkTx := false + app := simapp.Setup(checkTx) + testdata.RegisterInterfaces(app.InterfaceRegistry()) + testdata.RegisterMsgServer(app.MsgServiceRouter(), testdata.MsgServerImpl{}) + s.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{}) + s.goCtx = sdk.WrapSDKContext(s.ctx) + s.keeper = app.FswapKeeper + s.queryServer = keeper.NewQueryServer(s.keeper) + s.msgServer = keeper.NewMsgServer(s.keeper) + + s.createAccountsWithInitBalance(app) + app.AccountKeeper.GetModuleAccount(s.ctx, types.ModuleName) +} + +func (s *KeeperTestSuite) createAccountsWithInitBalance(app *simapp.SimApp) { + addresses := []*sdk.AccAddress{ + &s.accWithOldCoin, + &s.accWithNewCoin, + } + for i, address := range s.createRandomAccounts(len(addresses)) { + *addresses[i] = address + } + s.initBalance = sdk.NewInt(123456789) + minter := app.AccountKeeper.GetModuleAccount(s.ctx, minttypes.ModuleName).GetAddress() + oldAmount := sdk.NewCoins(sdk.NewCoin(s.keeper.OldDenom(), s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, oldAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithOldCoin, oldAmount)) + + newAmount := sdk.NewCoins(sdk.NewCoin(s.keeper.NewDenom(), s.initBalance)) + s.Require().NoError(app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, newAmount)) + s.Require().NoError(app.BankKeeper.SendCoins(s.ctx, minter, s.accWithNewCoin, newAmount)) +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, &KeeperTestSuite{}) +} + +func (s *KeeperTestSuite) TestSwap() { + testCases := map[string]struct { + from sdk.AccAddress + amountToSwap sdk.Int + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "swap some": { + s.accWithOldCoin, + sdk.NewInt(100), + sdk.NewInt(100), + false, + nil, + }, + "swap all the balance": { + s.accWithOldCoin, + s.initBalance, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + s.accWithNewCoin, + sdk.NewInt(100), + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + fmt.Printf("tc name=%s, %s\n", name, s.keeper.GetBalance(ctx, tc.from, s.keeper.OldDenom())) + err := s.keeper.Swap(ctx, tc.from, sdk.NewCoin(s.keeper.OldDenom(), tc.amountToSwap)) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.keeper.NewDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.keeper.SwapMultiple()) + s.Require().Equal(expectedAmount, actualAmount) + }) + } +} + +func (s *KeeperTestSuite) TestSwapAll() { + testCases := map[string]struct { + from sdk.AccAddress + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + }{ + "account holding old coin": { + s.accWithOldCoin, + s.initBalance, + false, + nil, + }, + "account holding new coin only": { + s.accWithNewCoin, + s.initBalance, + true, + sdkerrors.ErrInsufficientFunds, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + err := s.keeper.SwapAll(ctx, tc.from) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + actualAmount := s.keeper.GetBalance(ctx, tc.from, s.keeper.NewDenom()).Amount + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.keeper.SwapMultiple()) + s.Require().Equal(expectedAmount, actualAmount) + }) + } } diff --git a/x/fswap/keeper/keys.go b/x/fswap/keeper/keys.go index 3a3a85d382..f360c7bca4 100644 --- a/x/fswap/keeper/keys.go +++ b/x/fswap/keeper/keys.go @@ -1,17 +1,14 @@ package keeper -import "cosmossdk.io/collections" - var ( - paramsKeyValue = collections.NewPrefix(0) - swappedKeyValue = collections.NewPrefix(1) - swapCapKeyValue = collections.NewPrefix(2) + paramsKeyValue = []byte{0x0} + swappedKeyValue = []byte{0x01} ) -func swappedKey() []byte { - return swappedKeyValue.Bytes() +func paramsKey() []byte { + return paramsKeyValue } -func swapCapKey() []byte { - return swapCapKeyValue.Bytes() +func swappedKey() []byte { + return swappedKeyValue } diff --git a/x/fswap/keeper/msg_server.go b/x/fswap/keeper/msg_server.go index 41b76b832a..8b736033e0 100644 --- a/x/fswap/keeper/msg_server.go +++ b/x/fswap/keeper/msg_server.go @@ -3,27 +3,44 @@ package keeper import ( "context" + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/types" ) -type msgServer struct { - Keeper -} +var _ types.MsgServer = MsgServer{} -// NewMsgServerImpl returns an implementation of the MsgServer interface -// for the provided Keeper. -func NewMsgServerImpl(keeper Keeper) types.MsgServer { - return &msgServer{Keeper: keeper} +type MsgServer struct { + keeper Keeper } -var _ types.MsgServer = msgServer{} +func NewMsgServer(keeper Keeper) *MsgServer { + return &MsgServer{keeper} +} -// Swap implements types.MsgServer. -func (m msgServer) Swap(context.Context, *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { - panic("unimplemented") +func (s MsgServer) Swap(ctx context.Context, req *types.MsgSwapRequest) (*types.MsgSwapResponse, error) { + if req.GetAmount().Denom != s.keeper.OldDenom() { + return nil, sdkerrors.ErrInvalidCoins + } + c := sdk.UnwrapSDKContext(ctx) + from, err := sdk.AccAddressFromBech32(req.FromAddress) + if err != nil { + return nil, err + } + if err := s.keeper.Swap(c, from, req.GetAmount()); err != nil { + return nil, err + } + return &types.MsgSwapResponse{}, nil } -// SwapAll implements types.MsgServer. -func (m msgServer) SwapAll(context.Context, *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { - panic("unimplemented") +func (s MsgServer) SwapAll(ctx context.Context, req *types.MsgSwapAllRequest) (*types.MsgSwapAllResponse, error) { + c := sdk.UnwrapSDKContext(ctx) + from, err := sdk.AccAddressFromBech32(req.FromAddress) + if err != nil { + return nil, err + } + if err := s.keeper.SwapAll(c, from); err != nil { + return nil, err + } + return &types.MsgSwapAllResponse{}, nil } diff --git a/x/fswap/keeper/msg_server_test.go b/x/fswap/keeper/msg_server_test.go index 9429264902..0b0c277af7 100644 --- a/x/fswap/keeper/msg_server_test.go +++ b/x/fswap/keeper/msg_server_test.go @@ -1 +1,125 @@ package keeper_test + +import ( + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/Finschia/finschia-sdk/testutil" + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *KeeperTestSuite) TestMsgSwap() { + testCases := map[string]struct { + req *types.MsgSwapRequest + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + expectedEvents sdk.Events + }{ + "valid request": { + &types.MsgSwapRequest{ + s.accWithOldCoin.String(), + sdk.NewCoin(s.keeper.OldDenom(), sdk.NewInt(100)), + }, + sdk.NewInt(100), + false, + nil, + sdk.Events{ + sdk.Event{ + Type: "lbm.fswap.v1.EventSwapCoins", + Attributes: []abci.EventAttribute{ + {Key: []byte("address"), Value: testutil.W(s.accWithOldCoin.String()), Index: false}, + {Key: []byte("new_coin_amount"), Value: testutil.W(sdk.NewInt(100).Mul(s.keeper.SwapMultiple()).String()), Index: false}, + {Key: []byte("old_coin_amount"), Value: testutil.W(sdk.NewInt(100).String()), Index: false}, + }, + }, + }, + }, + "invalid request(try with swap with newCoin)": { + &types.MsgSwapRequest{ + s.accWithOldCoin.String(), + sdk.NewCoin(s.keeper.NewDenom(), sdk.NewInt(100)), + }, + sdk.NewInt(100), + true, + sdkerrors.ErrInvalidCoins, + sdk.Events{}, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + s.Require().NoError(tc.req.ValidateBasic()) + from, err := sdk.AccAddressFromBech32(tc.req.FromAddress) + s.Require().NoError(err) + ctx, _ := s.ctx.CacheContext() + + res, err := s.msgServer.Swap(sdk.WrapSDKContext(ctx), tc.req) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + s.Require().NotNil(res) + + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.keeper.SwapMultiple()) + actualAmount := s.keeper.GetBalance(ctx, from, s.keeper.NewDenom()).Amount + s.Require().Equal(expectedAmount, actualAmount) + + events := ctx.EventManager().Events() + s.Require().Contains(events, tc.expectedEvents[0]) + }) + } +} + +func (s *KeeperTestSuite) TestMsgSwapAll() { + testCases := map[string]struct { + req *types.MsgSwapAllRequest + expectedBalanceWithoutMultiply sdk.Int + shouldThrowError bool + expectedError error + expectedEvents sdk.Events + }{ + "valid request": { + &types.MsgSwapAllRequest{ + FromAddress: s.accWithOldCoin.String(), + }, + s.initBalance, + false, + nil, + sdk.Events{ + sdk.Event{ + Type: "lbm.fswap.v1.EventSwapCoins", + Attributes: []abci.EventAttribute{ + {Key: []byte("address"), Value: testutil.W(s.accWithOldCoin.String()), Index: false}, + {Key: []byte("new_coin_amount"), Value: testutil.W(s.initBalance.Mul(s.keeper.SwapMultiple()).String()), Index: false}, + {Key: []byte("old_coin_amount"), Value: testutil.W(s.initBalance.String()), Index: false}, + }, + }, + }, + }, + } + for name, tc := range testCases { + s.Run(name, func() { + s.Require().NoError(tc.req.ValidateBasic()) + from, err := sdk.AccAddressFromBech32(tc.req.FromAddress) + s.Require().NoError(err) + ctx, _ := s.ctx.CacheContext() + + res, err := s.msgServer.SwapAll(sdk.WrapSDKContext(ctx), tc.req) + if tc.shouldThrowError { + s.Require().ErrorIs(err, tc.expectedError) + return + } + s.Require().NoError(err) + s.Require().NotNil(res) + + expectedAmount := tc.expectedBalanceWithoutMultiply.Mul(s.keeper.SwapMultiple()) + actualAmount := s.keeper.GetBalance(ctx, from, s.keeper.NewDenom()).Amount + s.Require().Equal(expectedAmount, actualAmount) + + events := ctx.EventManager().Events() + s.Require().Contains(events, tc.expectedEvents[0]) + }) + } +} diff --git a/x/fswap/keeper/params.go b/x/fswap/keeper/params.go index 5a539d551a..9f8c7d27c8 100644 --- a/x/fswap/keeper/params.go +++ b/x/fswap/keeper/params.go @@ -6,11 +6,27 @@ import ( ) // GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx sdk.Context) types.Params { - return types.NewParams() +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { + store := ctx.KVStore(k.storeKey) + bz := store.Get([]byte{types.ParamsKey}) + var params types.Params + if bz == nil { + return types.Params{}, types.ErrParamsNotFound + } + err := k.cdc.Unmarshal(bz, ¶ms) + if err != nil { + return types.Params{}, err + } + return params, nil } // 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 } diff --git a/x/fswap/keeper/params_test.go b/x/fswap/keeper/params_test.go deleted file mode 100644 index 9429264902..0000000000 --- a/x/fswap/keeper/params_test.go +++ /dev/null @@ -1 +0,0 @@ -package keeper_test diff --git a/x/fswap/keeper/query_params_test.go b/x/fswap/keeper/query_params_test.go deleted file mode 100644 index 9429264902..0000000000 --- a/x/fswap/keeper/query_params_test.go +++ /dev/null @@ -1 +0,0 @@ -package keeper_test diff --git a/x/fswap/module.go b/x/fswap/module.go index 0e60e1d03d..badbb5e748 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -19,8 +19,9 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.EndBlockAppModule = AppModule{} ) // ---------------------------------------------------------------------------- @@ -92,15 +93,15 @@ type AppModule struct { AppModuleBasic keeper keeper.Keeper - accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper + accountKeeper keeper.AccountKeeper + bankKeeper keeper.BankKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, - accountKeeper types.AccountKeeper, - bankKeeper types.BankKeeper, + accountKeeper keeper.AccountKeeper, + bankKeeper keeper.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: NewAppModuleBasic(cdc), @@ -131,7 +132,8 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd // RegisterServices registers a GRPC query service to respond to the // module-specific GRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { - types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper)) + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper)) } // RegisterInvariants registers the capability module's invariants. @@ -144,14 +146,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra // Initialize global index to index in genesis state cdc.MustUnmarshalJSON(gs, &genState) - InitGenesis(ctx, am.keeper, genState) + am.keeper.InitGenesis(ctx, genState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the capability module's exported genesis state as raw JSON bytes. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - genState := ExportGenesis(ctx, am.keeper) + genState := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(genState) } diff --git a/x/fswap/module_simulation.go b/x/fswap/module_simulation.go index 48b26e84a0..70f7369bc4 100644 --- a/x/fswap/module_simulation.go +++ b/x/fswap/module_simulation.go @@ -1 +1,43 @@ package fswap + +import ( + "math/rand" + + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/types/module" + simtypes "github.com/Finschia/finschia-sdk/types/simulation" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +var _ module.AppModuleSimulation = AppModule{} + +// GenerateGenesisState creates a randomized GenState of the module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + fswapGenesis := types.GenesisState{ + Params: types.DefaultParams(), + Swapped: types.DefaultSwapped(), + } + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&fswapGenesis) +} + +// RegisterStoreDecoder registers a decoder. +func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// WeightedOperations returns the all the gov module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + operations := make([]simtypes.WeightedOperation, 0) + + // this line is used by starport scaffolding # simapp/module/operation + + return operations +} + +// RandomizedParams creates randomized slashing param changes for the simulator. +func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { + return nil +} diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index c0b627de88..90a43d2bb3 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,5 +8,7 @@ import ( // x/fswap module sentinel errors var ( - ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") + ErrParamsNotFound = sdkerrors.Register(ModuleName, 1100, "params does not exist") + ErrSwappedNotFound = sdkerrors.Register(ModuleName, 1101, "swapped does not exist") + ErrSwappableNewCoinAmountNotFound = sdkerrors.Register(ModuleName, 1102, "swappable new coin amount does not exist") ) diff --git a/x/fswap/types/event.pb.go b/x/fswap/types/event.pb.go index 5808cc6e7f..81b3be3f8d 100644 --- a/x/fswap/types/event.pb.go +++ b/x/fswap/types/event.pb.go @@ -4,13 +4,15 @@ package types import ( - fmt "fmt" - types "github.com/Finschia/finschia-sdk/types" - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" - io "io" - math "math" + "fmt" + "io" + "math" math_bits "math/bits" + + _ "github.com/Finschia/finschia-sdk/types" + github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" + _ "github.com/gogo/protobuf/gogoproto" + "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. @@ -28,9 +30,9 @@ type EventSwapCoins struct { // holder's address Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` // amount of the old currency - OldCoinAmount types.Coin `protobuf:"bytes,2,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` + OldCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,2,opt,name=old_coin_amount,json=oldCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"old_coin_amount"` // amount of the new currency - NewCoinAmount types.Coin `protobuf:"bytes,3,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` + NewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,3,opt,name=new_coin_amount,json=newCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"new_coin_amount"` } func (m *EventSwapCoins) Reset() { *m = EventSwapCoins{} } @@ -73,20 +75,6 @@ func (m *EventSwapCoins) GetAddress() string { return "" } -func (m *EventSwapCoins) GetOldCoinAmount() types.Coin { - if m != nil { - return m.OldCoinAmount - } - return types.Coin{} -} - -func (m *EventSwapCoins) GetNewCoinAmount() types.Coin { - if m != nil { - return m.NewCoinAmount - } - return types.Coin{} -} - func init() { proto.RegisterType((*EventSwapCoins)(nil), "lbm.fswap.v1.EventSwapCoins") } @@ -94,25 +82,25 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/event.proto", fileDescriptor_92d5edbd64a725af) } var fileDescriptor_92d5edbd64a725af = []byte{ - // 288 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0x3f, 0x4e, 0xc3, 0x30, - 0x1c, 0x85, 0xe3, 0x22, 0x81, 0x08, 0xff, 0xa4, 0x88, 0x21, 0x74, 0x70, 0x2b, 0xa6, 0x4a, 0x08, - 0x5b, 0xa1, 0x27, 0xa0, 0x08, 0xc4, 0x5c, 0x36, 0x96, 0xca, 0x4e, 0xdc, 0x34, 0x22, 0xf1, 0x2f, - 0xaa, 0x5d, 0x07, 0x6e, 0xc1, 0xcc, 0x11, 0x38, 0x49, 0xc7, 0x8e, 0x4c, 0x80, 0x92, 0x8b, 0x20, - 0x3b, 0x41, 0x88, 0x89, 0x89, 0xed, 0x59, 0xcf, 0xef, 0xfb, 0x24, 0xdb, 0x0f, 0x73, 0x5e, 0xd0, - 0xb9, 0xaa, 0x58, 0x49, 0x4d, 0x44, 0x85, 0x11, 0x52, 0x93, 0x72, 0x09, 0x1a, 0x82, 0xfd, 0x9c, - 0x17, 0xc4, 0x35, 0xc4, 0x44, 0xfd, 0xe3, 0x14, 0x52, 0x70, 0x05, 0xb5, 0xa9, 0xbd, 0xd3, 0xc7, - 0x31, 0xa8, 0x02, 0x14, 0xe5, 0x4c, 0x09, 0x6a, 0x22, 0x2e, 0x34, 0x8b, 0x68, 0x0c, 0x99, 0x6c, - 0xfb, 0xd3, 0x97, 0x9e, 0x7f, 0x78, 0x6d, 0x99, 0x77, 0x15, 0x2b, 0xaf, 0x20, 0x93, 0x2a, 0x08, - 0xfd, 0x1d, 0x96, 0x24, 0x4b, 0xa1, 0x54, 0x88, 0x86, 0x68, 0xb4, 0x3b, 0xfd, 0x3e, 0x06, 0xc6, - 0x3f, 0x82, 0x3c, 0x99, 0xd9, 0xf9, 0x8c, 0x15, 0xb0, 0x92, 0x3a, 0xec, 0x0d, 0xd1, 0x68, 0xef, - 0xe2, 0x84, 0xb4, 0x1a, 0x62, 0x35, 0xa4, 0xd3, 0x10, 0x8b, 0x9b, 0x8c, 0xd7, 0xef, 0x03, 0xef, - 0xf5, 0x63, 0x70, 0x96, 0x66, 0x7a, 0xb1, 0xe2, 0x24, 0x86, 0x82, 0xde, 0x64, 0x52, 0xc5, 0x8b, - 0x8c, 0xd1, 0x79, 0x17, 0xce, 0x55, 0xf2, 0x40, 0xf5, 0x53, 0x29, 0x94, 0x1b, 0x4d, 0x0f, 0x20, - 0x4f, 0x6c, 0xb8, 0x74, 0x12, 0xeb, 0x95, 0xa2, 0xfa, 0xe5, 0xdd, 0xfa, 0x1f, 0xaf, 0x14, 0xd5, - 0x8f, 0x77, 0x72, 0xbb, 0xae, 0x31, 0xda, 0xd4, 0x18, 0x7d, 0xd6, 0x18, 0x3d, 0x37, 0xd8, 0xdb, - 0x34, 0xd8, 0x7b, 0x6b, 0xb0, 0x77, 0x4f, 0xfe, 0xa4, 0x3e, 0x76, 0x7f, 0xe6, 0xe8, 0x7c, 0xdb, - 0xbd, 0xf6, 0xf8, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x2b, 0x08, 0x6b, 0x7c, 0xcd, 0x01, 0x00, 0x00, + // 274 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0xd0, 0xcf, 0x4a, 0xc3, 0x40, + 0x10, 0x06, 0xf0, 0xac, 0x82, 0x62, 0xf0, 0x0f, 0x04, 0x0f, 0xa1, 0x87, 0xad, 0x78, 0x12, 0xc1, + 0x1d, 0xa2, 0x4f, 0x60, 0x45, 0xd1, 0x6b, 0xbd, 0xf5, 0x52, 0x76, 0x93, 0x6d, 0x1a, 0x4c, 0x76, + 0x42, 0x67, 0x9b, 0xe8, 0x5b, 0xf8, 0x58, 0x3d, 0xf6, 0x28, 0x1e, 0x8a, 0x24, 0x2f, 0x22, 0x9b, + 0x56, 0xc4, 0x93, 0xd0, 0xdb, 0x2c, 0xdf, 0xec, 0x6f, 0xe0, 0xf3, 0xc3, 0x5c, 0x15, 0x30, 0xa1, + 0x5a, 0x96, 0x50, 0x45, 0xa0, 0x2b, 0x6d, 0xac, 0x28, 0x67, 0x68, 0x31, 0x38, 0xcc, 0x55, 0x21, + 0xba, 0x44, 0x54, 0x51, 0xef, 0x34, 0xc5, 0x14, 0xbb, 0x00, 0xdc, 0xb4, 0xde, 0xe9, 0xf1, 0x18, + 0xa9, 0x40, 0x02, 0x25, 0x49, 0x43, 0x15, 0x29, 0x6d, 0x65, 0x04, 0x31, 0x66, 0x66, 0x9d, 0x9f, + 0x37, 0xcc, 0x3f, 0xbe, 0x77, 0xe6, 0x73, 0x2d, 0xcb, 0x3b, 0xcc, 0x0c, 0x05, 0xa1, 0xbf, 0x2f, + 0x93, 0x64, 0xa6, 0x89, 0x42, 0x76, 0xc6, 0x2e, 0x0e, 0x86, 0x3f, 0xcf, 0x60, 0xe4, 0x9f, 0x60, + 0x9e, 0x8c, 0xdd, 0xf7, 0xb1, 0x2c, 0x70, 0x6e, 0x6c, 0xb8, 0xe3, 0x36, 0x06, 0xd7, 0x8b, 0x55, + 0xdf, 0xfb, 0x5c, 0xf5, 0x2f, 0xd3, 0xcc, 0x4e, 0xe7, 0x4a, 0xc4, 0x58, 0xc0, 0x43, 0x66, 0x28, + 0x9e, 0x66, 0x12, 0x26, 0x9b, 0xe1, 0x8a, 0x92, 0x17, 0xb0, 0x6f, 0xa5, 0x26, 0xf1, 0x64, 0xec, + 0xf0, 0x08, 0xf3, 0xc4, 0x1d, 0xbc, 0xed, 0x20, 0x67, 0x1b, 0x5d, 0xff, 0xb1, 0x77, 0xb7, 0xb7, + 0x8d, 0xae, 0x7f, 0xed, 0xc1, 0xe3, 0xa2, 0xe1, 0x6c, 0xd9, 0x70, 0xf6, 0xd5, 0x70, 0xf6, 0xde, + 0x72, 0x6f, 0xd9, 0x72, 0xef, 0xa3, 0xe5, 0xde, 0x48, 0xfc, 0x8b, 0xbe, 0x6e, 0xba, 0xef, 0x70, + 0xb5, 0xd7, 0xb5, 0x76, 0xf3, 0x1d, 0x00, 0x00, 0xff, 0xff, 0x66, 0x87, 0x53, 0x6b, 0x95, 0x01, + 0x00, 0x00, } func (m *EventSwapCoins) Marshal() (dAtA []byte, err error) { @@ -136,21 +124,21 @@ func (m *EventSwapCoins) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { + size := m.NewCoinAmount.Size() + i -= size + if _, err := m.NewCoinAmount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i -= size i = encodeVarintEvent(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x1a { - size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { + size := m.OldCoinAmount.Size() + i -= size + if _, err := m.OldCoinAmount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i -= size i = encodeVarintEvent(dAtA, i, uint64(size)) } i-- @@ -264,7 +252,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowEvent @@ -274,15 +262,16 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthEvent } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthEvent } @@ -297,7 +286,7 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowEvent @@ -307,15 +296,16 @@ func (m *EventSwapCoins) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthEvent } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthEvent } diff --git a/x/fswap/types/expected_keepers.go b/x/fswap/types/expected_keepers.go deleted file mode 100644 index 6134a117d7..0000000000 --- a/x/fswap/types/expected_keepers.go +++ /dev/null @@ -1,18 +0,0 @@ -package types - -import ( - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/auth/types" -) - -// AccountKeeper defines the expected account keeper used for simulations (noalias) -type AccountKeeper interface { - GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI - // Methods imported from account should be defined here -} - -// BankKeeper defines the expected interface needed to retrieve account balances. -type BankKeeper interface { - SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins - // Methods imported from bank should be defined here -} diff --git a/x/fswap/types/fswap.go b/x/fswap/types/fswap.go new file mode 100644 index 0000000000..399929e0b4 --- /dev/null +++ b/x/fswap/types/fswap.go @@ -0,0 +1,59 @@ +package types + +import ( + "fmt" + + "gopkg.in/yaml.v2" + + sdk "github.com/Finschia/finschia-sdk/types" +) + +// NewSwapped creates a new Swapped instance +func NewSwapped( + oldCoinAmount sdk.Int, + newCoinAmount sdk.Int, +) Swapped { + return Swapped{ + OldCoinAmount: oldCoinAmount, + NewCoinAmount: newCoinAmount, + } +} + +// DefaultSwapped returns an initial Swapped object +func DefaultSwapped() Swapped { + return NewSwapped(sdk.ZeroInt(), sdk.ZeroInt()) +} + +func validateCoinAmount(i interface{}) error { + v, ok := i.(sdk.Int) + if !ok { + return fmt.Errorf("invalid coin amount: %T", i) + } + + if v.IsNil() { + return fmt.Errorf("coin amount must be not nil") + } + + if v.LT(sdk.ZeroInt()) { + return fmt.Errorf("coin amount cannot be lower than 0") + } + + return nil +} + +// Validate validates the set of swapped +func (s Swapped) Validate() error { + if err := validateCoinAmount(s.OldCoinAmount); err != nil { + return err + } + if err := validateCoinAmount(s.NewCoinAmount); err != nil { + return err + } + return nil +} + +// String implements the Stringer interface. +func (s Swapped) String() string { + out, _ := yaml.Marshal(s) + return string(out) +} diff --git a/x/fswap/types/fswap.pb.go b/x/fswap/types/fswap.pb.go index f53d058797..1923c31269 100644 --- a/x/fswap/types/fswap.pb.go +++ b/x/fswap/types/fswap.pb.go @@ -5,7 +5,7 @@ package types import ( fmt "fmt" - types "github.com/Finschia/finschia-sdk/types" + github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" io "io" @@ -25,13 +25,12 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Swapped struct { - OldCoinAmount types.Coin `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"old_coin_amount"` - NewCoinAmount types.Coin `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,castrepeated=github.com/Finschia/finschia-sdk/types.Coin" json:"new_coin_amount"` + OldCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,1,opt,name=old_coin_amount,json=oldCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"old_coin_amount"` + NewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,2,opt,name=new_coin_amount,json=newCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"new_coin_amount"` } -func (m *Swapped) Reset() { *m = Swapped{} } -func (m *Swapped) String() string { return proto.CompactTextString(m) } -func (*Swapped) ProtoMessage() {} +func (m *Swapped) Reset() { *m = Swapped{} } +func (*Swapped) ProtoMessage() {} func (*Swapped) Descriptor() ([]byte, []int) { return fileDescriptor_42ca60eaf37a2b67, []int{0} } @@ -62,20 +61,6 @@ func (m *Swapped) XXX_DiscardUnknown() { var xxx_messageInfo_Swapped proto.InternalMessageInfo -func (m *Swapped) GetOldCoinAmount() types.Coin { - if m != nil { - return m.OldCoinAmount - } - return types.Coin{} -} - -func (m *Swapped) GetNewCoinAmount() types.Coin { - if m != nil { - return m.NewCoinAmount - } - return types.Coin{} -} - func init() { proto.RegisterType((*Swapped)(nil), "lbm.fswap.v1.Swapped") } @@ -83,24 +68,22 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/fswap.proto", fileDescriptor_42ca60eaf37a2b67) } var fileDescriptor_42ca60eaf37a2b67 = []byte{ - // 262 bytes of a gzipped FileDescriptorProto + // 230 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xc8, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0x84, 0x30, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0x78, 0x72, 0x92, 0x72, 0xf5, 0x20, 0x02, 0x65, 0x86, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, - 0x60, 0x09, 0x7d, 0x10, 0x0b, 0xa2, 0x46, 0x4a, 0x2e, 0x39, 0xbf, 0x38, 0x37, 0xbf, 0x58, 0x3f, - 0x29, 0xb1, 0x38, 0x55, 0xbf, 0xcc, 0x30, 0x29, 0xb5, 0x24, 0xd1, 0x50, 0x3f, 0x39, 0x3f, 0x33, - 0x0f, 0x22, 0xaf, 0xf4, 0x93, 0x91, 0x8b, 0x3d, 0xb8, 0x3c, 0xb1, 0xa0, 0x20, 0x35, 0x45, 0xa8, - 0x8c, 0x8b, 0x3f, 0x3f, 0x27, 0x25, 0x1e, 0x24, 0x1b, 0x9f, 0x98, 0x9b, 0x5f, 0x9a, 0x57, 0x22, - 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x6d, 0x24, 0xa9, 0x07, 0x31, 0x45, 0x0f, 0x64, 0x8a, 0x1e, 0xd4, - 0x14, 0x3d, 0xe7, 0xfc, 0xcc, 0x3c, 0x27, 0xe3, 0x13, 0xf7, 0xe4, 0x19, 0x56, 0xdd, 0x97, 0xd7, - 0x4e, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x77, 0xcb, 0xcc, 0x2b, 0x4e, - 0xce, 0xc8, 0x4c, 0xd4, 0x4f, 0x83, 0x32, 0x74, 0x8b, 0x53, 0xb2, 0xf5, 0x4b, 0x2a, 0x0b, 0x52, - 0x8b, 0xc1, 0x9a, 0x82, 0x78, 0xf3, 0x73, 0x52, 0x40, 0x0c, 0x47, 0xb0, 0x25, 0x20, 0x7b, 0xf3, - 0x52, 0xcb, 0x51, 0xec, 0x65, 0xa2, 0x8d, 0xbd, 0x79, 0xa9, 0xe5, 0x08, 0x7b, 0x9d, 0x3c, 0x4e, - 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, 0x18, - 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, 0x58, 0x8e, 0x21, 0x4a, 0x8f, 0xa0, 0xa9, 0x15, 0xd0, 0x28, - 0x01, 0x9b, 0x9e, 0xc4, 0x06, 0x0e, 0x4c, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x5c, 0x78, - 0x6f, 0xce, 0xac, 0x01, 0x00, 0x00, + 0x60, 0x09, 0x7d, 0x10, 0x0b, 0xa2, 0x46, 0xe9, 0x38, 0x23, 0x17, 0x7b, 0x70, 0x79, 0x62, 0x41, + 0x41, 0x6a, 0x8a, 0x50, 0x14, 0x17, 0x7f, 0x7e, 0x4e, 0x4a, 0x7c, 0x72, 0x7e, 0x66, 0x5e, 0x7c, + 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0xa7, 0x93, 0xd1, 0x89, 0x7b, + 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0x6b, 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, + 0xea, 0xbb, 0x65, 0xe6, 0x15, 0x27, 0x67, 0x64, 0x26, 0xea, 0xa7, 0x41, 0x19, 0xba, 0xc5, 0x29, + 0xd9, 0xfa, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x7a, 0x9e, 0x79, 0x25, 0x41, 0xbc, 0xf9, 0x39, 0x29, + 0xce, 0xf9, 0x99, 0x79, 0x8e, 0x60, 0x83, 0x40, 0x66, 0xe7, 0xa5, 0x96, 0xa3, 0x98, 0xcd, 0x44, + 0xbe, 0xd9, 0x79, 0xa9, 0xe5, 0x08, 0xb3, 0xad, 0x58, 0x66, 0x2c, 0x90, 0x67, 0x70, 0xf2, 0x38, + 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, + 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0x3d, 0x82, 0x46, 0x57, 0x40, 0x03, + 0x10, 0x6c, 0x45, 0x12, 0x1b, 0x38, 0x68, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x82, 0xb7, + 0x84, 0x0e, 0x5a, 0x01, 0x00, 0x00, } func (m *Swapped) Marshal() (dAtA []byte, err error) { @@ -124,21 +107,21 @@ func (m *Swapped) MarshalToSizedBuffer(dAtA []byte) (int, error) { var l int _ = l { - size, err := m.NewCoinAmount.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { + size := m.NewCoinAmount.Size() + i -= size + if _, err := m.NewCoinAmount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i -= size i = encodeVarintFswap(dAtA, i, uint64(size)) } i-- dAtA[i] = 0x12 { - size, err := m.OldCoinAmount.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { + size := m.OldCoinAmount.Size() + i -= size + if _, err := m.OldCoinAmount.MarshalTo(dAtA[i:]); err != nil { return 0, err } - i -= size i = encodeVarintFswap(dAtA, i, uint64(size)) } i-- @@ -209,7 +192,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field OldCoinAmount", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowFswap @@ -219,15 +202,16 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthFswap } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthFswap } @@ -242,7 +226,7 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field NewCoinAmount", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowFswap @@ -252,15 +236,16 @@ func (m *Swapped) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthFswap } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthFswap } diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 8df94bae23..4e5b68856a 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,24 +1,26 @@ package types -import ( -// this line is used by starport scaffolding # genesis/types/import -) - -// DefaultIndex is the default capability global index -const DefaultIndex uint64 = 1 +// NewGenesis creates a new genesis state +func NewGenesisState(params Params, swapped Swapped) *GenesisState { + return &GenesisState{ + Params: params, + Swapped: swapped, + } +} // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { - return &GenesisState{ - // this line is used by starport scaffolding # genesis/types/default - Params: DefaultParams(), - } + return NewGenesisState(DefaultParams(), DefaultSwapped()) } // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { - // this line is used by starport scaffolding # genesis/types/validate - - return gs.Params.Validate() + if err := gs.Params.Validate(); err != nil { + return err + } + if err := gs.Swapped.Validate(); err != nil { + return err + } + return nil } diff --git a/x/fswap/types/genesis.pb.go b/x/fswap/types/genesis.pb.go index af2475beea..5b76f03a14 100644 --- a/x/fswap/types/genesis.pb.go +++ b/x/fswap/types/genesis.pb.go @@ -83,22 +83,22 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/genesis.proto", fileDescriptor_94e309cb1db27661) } var fileDescriptor_94e309cb1db27661 = []byte{ - // 231 bytes of a gzipped FileDescriptorProto + // 230 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xca, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0xcb, 0xe9, 0x95, - 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x49, - 0x14, 0xfd, 0x05, 0x89, 0x45, 0x89, 0xb9, 0x50, 0xed, 0x52, 0x12, 0x28, 0x52, 0x10, 0x73, 0xc0, - 0x32, 0x4a, 0x95, 0x5c, 0x3c, 0xee, 0x10, 0x9b, 0x82, 0x4b, 0x12, 0x4b, 0x52, 0x85, 0x8c, 0xb8, - 0xd8, 0x20, 0x3a, 0x25, 0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x44, 0xf4, 0x90, 0x6d, 0xd6, 0x0b, - 0x00, 0xcb, 0x39, 0xb1, 0x9c, 0xb8, 0x27, 0xcf, 0x10, 0x04, 0x55, 0x29, 0x64, 0xca, 0xc5, 0x0e, - 0x92, 0x2f, 0x48, 0x4d, 0x91, 0x60, 0x02, 0x6b, 0x12, 0x45, 0xd5, 0x14, 0x0c, 0x91, 0x84, 0xea, - 0x82, 0xa9, 0x75, 0xf2, 0x38, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, - 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0xbd, - 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xb7, 0xcc, 0xbc, 0xe2, 0xe4, - 0x8c, 0xcc, 0x44, 0xfd, 0x34, 0x28, 0x43, 0xb7, 0x38, 0x25, 0x5b, 0xbf, 0x02, 0xea, 0x9b, 0x92, - 0xca, 0x82, 0xd4, 0xe2, 0x24, 0x36, 0xb0, 0x5f, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x23, - 0x15, 0x87, 0x48, 0x42, 0x01, 0x00, 0x00, + 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x29, 0x09, + 0x14, 0xfd, 0x10, 0xc5, 0x10, 0x19, 0x49, 0x14, 0x99, 0x82, 0xc4, 0xa2, 0xc4, 0x5c, 0xa8, 0xc1, + 0x4a, 0x95, 0x5c, 0x3c, 0xee, 0x10, 0x9b, 0x82, 0x4b, 0x12, 0x4b, 0x52, 0x85, 0x8c, 0xb8, 0xd8, + 0x20, 0xf2, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x22, 0x7a, 0xc8, 0x36, 0xeb, 0x05, 0x80, + 0xe5, 0x9c, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0x82, 0xaa, 0x14, 0x32, 0xe5, 0x62, 0x07, 0xc9, + 0x17, 0xa4, 0xa6, 0x48, 0x30, 0x81, 0x35, 0x89, 0xa2, 0x6a, 0x0a, 0x86, 0x48, 0x42, 0x75, 0xc1, + 0xd4, 0x3a, 0x79, 0x9c, 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, + 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x5e, 0x7a, + 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0xbe, 0x5b, 0x66, 0x5e, 0x71, 0x72, 0x46, + 0x66, 0xa2, 0x7e, 0x1a, 0x94, 0xa1, 0x5b, 0x9c, 0x92, 0xad, 0x5f, 0x01, 0xf5, 0x4e, 0x49, 0x65, + 0x41, 0x6a, 0x71, 0x12, 0x1b, 0xd8, 0x2f, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcb, 0xd0, + 0xcc, 0x0f, 0x42, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { diff --git a/x/fswap/types/keys.go b/x/fswap/types/keys.go index 18047ccfb2..3e34d8cd60 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) + SwappableNewCoinAmountKey = 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 f4b9ae24dc..fbfedf3534 100644 --- a/x/fswap/types/params.go +++ b/x/fswap/types/params.go @@ -2,20 +2,29 @@ package types import ( "gopkg.in/yaml.v2" + + sdk "github.com/Finschia/finschia-sdk/types" ) // NewParams creates a new Params instance -func NewParams() Params { - return Params{} +func NewParams( + swappableNewCoinAmount sdk.Int, +) Params { + return Params{ + SwappableNewCoinAmount: swappableNewCoinAmount, + } } // DefaultParams returns a default set of parameters func DefaultParams() Params { - return NewParams() + return NewParams(sdk.ZeroInt()) } // Validate validates the set of params func (p Params) Validate() error { + if err := validateCoinAmount(p.SwappableNewCoinAmount); err != nil { + return err + } return nil } diff --git a/x/fswap/types/params.pb.go b/x/fswap/types/params.pb.go index fd779ed5a6..8e9887228b 100644 --- a/x/fswap/types/params.pb.go +++ b/x/fswap/types/params.pb.go @@ -5,6 +5,7 @@ package types import ( fmt "fmt" + github_com_Finschia_finschia_sdk_types "github.com/Finschia/finschia-sdk/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" io "io" @@ -25,8 +26,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Params defines the parameters for the module. type Params struct { - // new denomination for new coin after swap - NewCoinDenom string `protobuf:"bytes,1,opt,name=new_coin_denom,json=newCoinDenom,proto3" json:"new_coin_denom,omitempty"` + SwappableNewCoinAmount github_com_Finschia_finschia_sdk_types.Int `protobuf:"bytes,1,opt,name=swappable_new_coin_amount,json=swappableNewCoinAmount,proto3,customtype=github.com/Finschia/finschia-sdk/types.Int" json:"swappable_new_coin_amount"` } func (m *Params) Reset() { *m = Params{} } @@ -61,13 +61,6 @@ func (m *Params) XXX_DiscardUnknown() { var xxx_messageInfo_Params proto.InternalMessageInfo -func (m *Params) GetNewCoinDenom() string { - if m != nil { - return m.NewCoinDenom - } - return "" -} - func init() { proto.RegisterType((*Params)(nil), "lbm.fswap.v1.Params") } @@ -75,20 +68,22 @@ func init() { func init() { proto.RegisterFile("lbm/fswap/v1/params.proto", fileDescriptor_15e620b81032c44d) } var fileDescriptor_15e620b81032c44d = []byte{ - // 194 bytes of a gzipped FileDescriptorProto + // 225 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0x49, 0xca, 0xd5, 0x4f, 0x2b, 0x2e, 0x4f, 0x2c, 0xd0, 0x2f, 0x33, 0xd4, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xc9, 0x49, 0xca, 0xd5, 0x03, 0x4b, 0xe9, 0x95, 0x19, - 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0x25, 0x13, 0x2e, - 0xb6, 0x00, 0xb0, 0x1e, 0x21, 0x15, 0x2e, 0xbe, 0xbc, 0xd4, 0xf2, 0xf8, 0xe4, 0xfc, 0xcc, 0xbc, - 0xf8, 0x94, 0xd4, 0xbc, 0xfc, 0x5c, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x9e, 0xbc, 0xd4, - 0x72, 0xe7, 0xfc, 0xcc, 0x3c, 0x17, 0x90, 0x98, 0x15, 0xcb, 0x8c, 0x05, 0xf2, 0x0c, 0x4e, 0x1e, - 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, - 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x97, 0x9e, 0x59, 0x92, 0x51, - 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0xef, 0x96, 0x99, 0x57, 0x9c, 0x9c, 0x91, 0x99, 0xa8, 0x9f, - 0x06, 0x65, 0xe8, 0x16, 0xa7, 0x64, 0xeb, 0x57, 0x40, 0x5d, 0x5b, 0x52, 0x59, 0x90, 0x5a, 0x9c, - 0xc4, 0x06, 0x76, 0x86, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x90, 0xbf, 0xad, 0x40, 0xc7, 0x00, - 0x00, 0x00, + 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x25, 0xf4, 0x41, 0x2c, 0x88, 0x1a, 0xa5, 0x5a, 0x2e, + 0xb6, 0x00, 0xb0, 0x1e, 0xa1, 0x5c, 0x2e, 0x49, 0x90, 0xd2, 0x82, 0xc4, 0xa4, 0x9c, 0xd4, 0xf8, + 0xbc, 0xd4, 0xf2, 0xf8, 0xe4, 0xfc, 0xcc, 0xbc, 0xf8, 0xc4, 0xdc, 0xfc, 0xd2, 0xbc, 0x12, 0x09, + 0x46, 0x05, 0x46, 0x0d, 0x4e, 0x27, 0xa3, 0x13, 0xf7, 0xe4, 0x19, 0x6e, 0xdd, 0x93, 0xd7, 0x4a, + 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x77, 0xcb, 0xcc, 0x2b, 0x4e, 0xce, + 0xc8, 0x4c, 0xd4, 0x4f, 0x83, 0x32, 0x74, 0x8b, 0x53, 0xb2, 0xf5, 0x4b, 0x2a, 0x0b, 0x52, 0x8b, + 0xf5, 0x3c, 0xf3, 0x4a, 0x82, 0xc4, 0xe0, 0x86, 0xfa, 0xa5, 0x96, 0x3b, 0xe7, 0x67, 0xe6, 0x39, + 0x82, 0x4d, 0xb4, 0x62, 0x99, 0xb1, 0x40, 0x9e, 0xc1, 0xc9, 0xe3, 0xc4, 0x23, 0x39, 0xc6, 0x0b, + 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, + 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0xf4, 0x08, 0xda, 0x51, 0x01, 0xf5, 0x36, 0xd8, 0xae, 0x24, 0x36, + 0xb0, 0x7f, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x53, 0xb6, 0x13, 0xee, 0x10, 0x01, 0x00, + 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -111,13 +106,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.NewCoinDenom) > 0 { - i -= len(m.NewCoinDenom) - copy(dAtA[i:], m.NewCoinDenom) - i = encodeVarintParams(dAtA, i, uint64(len(m.NewCoinDenom))) - i-- - dAtA[i] = 0xa + { + size := m.SwappableNewCoinAmount.Size() + i -= size + if _, err := m.SwappableNewCoinAmount.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintParams(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0xa return len(dAtA) - i, nil } @@ -138,10 +136,8 @@ func (m *Params) Size() (n int) { } var l int _ = l - l = len(m.NewCoinDenom) - if l > 0 { - n += 1 + l + sovParams(uint64(l)) - } + l = m.SwappableNewCoinAmount.Size() + n += 1 + l + sovParams(uint64(l)) return n } @@ -182,7 +178,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field NewCoinDenom", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SwappableNewCoinAmount", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -210,7 +206,9 @@ func (m *Params) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.NewCoinDenom = string(dAtA[iNdEx:postIndex]) + if err := m.SwappableNewCoinAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } iNdEx = postIndex default: iNdEx = preIndex