Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fswap module's basic functionality(MsgSwap, MsgSwapAll, Query) #1341

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ require (
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
Expand All @@ -99,20 +102,22 @@ require (
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
Expand Down
69 changes: 52 additions & 17 deletions go.sum

Large diffs are not rendered by default.

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.

17 changes: 17 additions & 0 deletions x/fswap/keeper/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import sdk "github.com/Finschia/finschia-sdk/types"

type (
AccountKeeper interface {
GetModuleAddress(name string) sdk.AccAddress
}

BankKeeper interface {
HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SendCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
}
)
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)
}
29 changes: 22 additions & 7 deletions x/fswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ 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(_ context.Context, _ *types.QueryTotalSwappableAmountRequest) (*types.QueryTotalSwappableAmountResponse, error) {
// s.Keeper.GetSwappableNewCoinAmount()
return nil, nil
}
1 change: 0 additions & 1 deletion x/fswap/keeper/grpc_query_params_test.go

This file was deleted.

51 changes: 51 additions & 0 deletions x/fswap/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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": {
swapReq: &types.MsgSwapRequest{
FromAddress: s.accWithOldCoin.String(),
Amount: sdk.NewCoin(s.keeper.OldDenom(), sdk.NewInt(100)),
},
expectedBalanceWithoutMultiply: 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.

// s.Require().NoError(err)

//s.Require().Equal(expectedLimit, res.SwappableNewCoinAmount)

Check failure on line 50 in x/fswap/keeper/grpc_query_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
}
Loading
Loading