Skip to content

Commit

Permalink
feat: wip, done(keeper, query, msg_server)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Apr 24, 2024
1 parent fd4945a commit f74fa62
Show file tree
Hide file tree
Showing 31 changed files with 875 additions and 316 deletions.
12 changes: 6 additions & 6 deletions proto/lbm/fswap/v1/fswap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
16 changes: 16 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -138,6 +142,7 @@ var (
vesting.AppModuleBasic{},
tokenmodule.AppModuleBasic{},
collectionmodule.AppModuleBasic{},
fswap.AppModuleBasic{},
)

// module account permissions
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -410,6 +422,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion x/foundation/keeper/internal/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 53 additions & 0 deletions x/fswap/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 0 additions & 24 deletions x/fswap/genesis.go

This file was deleted.

1 change: 0 additions & 1 deletion x/fswap/genesis_test.go

This file was deleted.

33 changes: 33 additions & 0 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
13 changes: 13 additions & 0 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 23 additions & 7 deletions x/fswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion x/fswap/keeper/grpc_query_params_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions x/fswap/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit f74fa62

Please sign in to comment.