Skip to content

Commit

Permalink
chore: update proto, introduce config, clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed May 2, 2024
1 parent e2ac84c commit 00e23a9
Show file tree
Hide file tree
Showing 26 changed files with 1,054 additions and 502 deletions.
47 changes: 38 additions & 9 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,9 @@
- [EventSwapCoins](#lbm.fswap.v1.EventSwapCoins)

- [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto)
- [SwapInit](#lbm.fswap.v1.SwapInit)
- [SwapInitProposal](#lbm.fswap.v1.SwapInitProposal)
- [Swap](#lbm.fswap.v1.Swap)
- [SwapProposal](#lbm.fswap.v1.SwapProposal)
- [SwapStats](#lbm.fswap.v1.SwapStats)
- [Swapped](#lbm.fswap.v1.Swapped)

- [lbm/fswap/v1/genesis.proto](#lbm/fswap/v1/genesis.proto)
Expand Down Expand Up @@ -12750,9 +12751,9 @@ Msg defines the foundation Msg service.



<a name="lbm.fswap.v1.SwapInit"></a>
<a name="lbm.fswap.v1.Swap"></a>

### SwapInit
### Swap



Expand All @@ -12768,17 +12769,32 @@ Msg defines the foundation Msg service.



<a name="lbm.fswap.v1.SwapInitProposal"></a>
<a name="lbm.fswap.v1.SwapProposal"></a>

### SwapInitProposal
### SwapProposal
From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `title` | [string](#string) | | |
| `description` | [string](#string) | | |
| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | | |
| `swap` | [Swap](#lbm.fswap.v1.Swap) | | |






<a name="lbm.fswap.v1.SwapStats"></a>

### SwapStats



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap_count` | [int32](#int32) | | |



Expand Down Expand Up @@ -12825,8 +12841,9 @@ GenesisState defines the fswap module's genesis state.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap_init` | [SwapInit](#lbm.fswap.v1.SwapInit) | repeated | |
| `swapped` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | |
| `swaps` | [Swap](#lbm.fswap.v1.Swap) | repeated | |
| `swap_stats` | [SwapStats](#lbm.fswap.v1.SwapStats) | | |
| `swappeds` | [Swapped](#lbm.fswap.v1.Swapped) | repeated | |



Expand Down Expand Up @@ -12855,6 +12872,12 @@ GenesisState defines the fswap module's genesis state.



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `fromDenom` | [string](#string) | | |
| `toDenom` | [string](#string) | | |





Expand All @@ -12881,6 +12904,12 @@ GenesisState defines the fswap module's genesis state.



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `fromDenom` | [string](#string) | | |
| `toDenom` | [string](#string) | | |





Expand Down
17 changes: 12 additions & 5 deletions proto/lbm/fswap/v1/fswap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

message SwapInit {
message Swap {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

Expand All @@ -18,14 +18,21 @@ message SwapInit {
[(gogoproto.customtype) = "github.com/Finschia/finschia-sdk/types.Int", (gogoproto.nullable) = false];
}

message SwapStats {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

int32 swap_count = 1;
}

// From cosmos-sdk 0.46.0 they deprecated this way, but currently finschia-sdk based on 0.45.10
message SwapInitProposal {
message SwapProposal {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

string title = 1;
string description = 2;
SwapInit swap_init = 3 [(gogoproto.nullable) = false];
string title = 1;
string description = 2;
Swap swap = 3 [(gogoproto.nullable) = false];
}

message Swapped {
Expand Down
5 changes: 3 additions & 2 deletions proto/lbm/fswap/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "lbm/fswap/v1/fswap.proto";

// GenesisState defines the fswap module's genesis state.
message GenesisState {
repeated SwapInit swap_init = 1 [(gogoproto.nullable) = false];
repeated Swapped swapped = 2 [(gogoproto.nullable) = false];
repeated Swap swaps = 1 [(gogoproto.nullable) = false];
SwapStats swap_stats = 2 [(gogoproto.nullable) = false];
repeated Swapped swappeds = 3 [(gogoproto.nullable) = false];
}
10 changes: 8 additions & 2 deletions proto/lbm/fswap/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ service Query {
}
}

message QuerySwappedRequest {}
message QuerySwappedRequest {
string fromDenom = 1;
string toDenom = 2;
}
message QuerySwappedResponse {
cosmos.base.v1beta1.Coin from_coin_amount = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
cosmos.base.v1beta1.Coin to_coin_amount = 2
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
}

message QueryTotalSwappableToCoinAmountRequest {}
message QueryTotalSwappableToCoinAmountRequest {
string fromDenom = 1;
string toDenom = 2;
}
message QueryTotalSwappableToCoinAmountResponse {
cosmos.base.v1beta1.Coin swappable_amount = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
Expand Down
6 changes: 2 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ func NewSimApp(
app.ClassKeeper = classkeeper.NewKeeper(appCodec, keys[class.StoreKey])
app.TokenKeeper = tokenkeeper.NewKeeper(appCodec, keys[token.StoreKey], app.ClassKeeper)
app.CollectionKeeper = collectionkeeper.NewKeeper(appCodec, keys[collection.StoreKey], app.ClassKeeper)

/**** Phase 1 ****/
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper)
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswaptypes.DefaultConfig(), app.AccountKeeper, app.BankKeeper)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
Expand All @@ -345,7 +343,7 @@ func NewSimApp(
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper)).
AddRoute(fswaptypes.RouterKey, fswap.NewSwapInitHandler(app.FswapKeeper))
AddRoute(fswaptypes.RouterKey, fswap.NewSwapHandler(app.FswapKeeper))

govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
Expand Down
2 changes: 1 addition & 1 deletion x/fswap/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func GetQueryCmd(queryRoute string) *cobra.Command {
// Group fswap queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Use: queryRoute,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
Expand Down
4 changes: 2 additions & 2 deletions x/fswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ func parseArgsToContent(cmd *cobra.Command) (gov.Content, error) {
return nil, err
}

swapInit := types.SwapInit{
swapInit := types.Swap{
FromDenom: from_denom,
ToDenom: to_denom,
AmountCapForToDenom: sdk.NewInt(amount_limit),
SwapMultiple: sdk.NewInt(swap_rate),
}

content := types.NewSwapInitProposal(title, description, swapInit)
content := types.NewSwapProposal(title, description, swapInit)
return content, nil
}
14 changes: 7 additions & 7 deletions x/fswap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
}
}

// NewSwapInitHandler creates a governance handler to manage new proposal types.
// It enables SwapInit to propose an fswap init
func NewSwapInitHandler(k keeper.Keeper) govtypes.Handler {
// NewSwapHandler creates a governance handler to manage new proposal types.
// It enables Swap to propose a swap init
func NewSwapHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.SwapInitProposal:
return handleSwapInit(ctx, k, c)
case *types.SwapProposal:
return handleSwap(ctx, k, c)

default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized sawp proposal content type: %T", c)
}
}
}

func handleSwapInit(ctx sdk.Context, k keeper.Keeper, p *types.SwapInitProposal) error {
return k.SwapInit(ctx, p.SwapInit)
func handleSwap(ctx sdk.Context, k keeper.Keeper, p *types.SwapProposal) error {
return k.MakeSwap(ctx, p.Swap)
}
36 changes: 26 additions & 10 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) error {
if len(genState.GetSwapInit()) > 1 {
return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapInit")
if err := genState.Validate(); err != nil {
return err
}
if len(genState.GetSwapped()) > 1 {
return types.ErrSwapCanNotBeInitializedTwice.Wrap("cannot initialize genesis state, there are more than 1 swapped")

if len(genState.GetSwaps()) > k.config.MaxSwaps && !k.isUnlimited() {
return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapInits", k.config.MaxSwaps)
}

if len(genState.GetSwappeds()) > k.config.MaxSwaps && !k.isUnlimited() {
return types.ErrCanNotHaveMoreSwap.Wrapf("cannot initialize genesis state, there are more than %d swapped", k.config.MaxSwaps)
}
for _, swapInit := range genState.GetSwapInit() {
if err := k.setSwapInit(ctx, swapInit); err != nil {

for _, swap := range genState.GetSwaps() {
if err := k.MakeSwap(ctx, swap); err != nil {
panic(err)
}
}
for _, swapped := range genState.GetSwapped() {
if err := swapped.Validate(); err != nil {

for _, swapped := range genState.GetSwappeds() {
if err := swapped.ValidateBasic(); err != nil {
panic(err)
}
}
return nil
}

func (k Keeper) isUnlimited() bool {
return k.config.MaxSwaps == 0
}

// ExportGenesis returns the module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
stats, err := k.getSwapStats(ctx)
if err != nil {
panic(err)
}
return &types.GenesisState{
SwapInit: k.getAllSwapInits(ctx),
Swapped: k.getAllSwapped(ctx),
Swaps: k.getAllSwaps(ctx),
SwapStats: stats,
Swappeds: k.getAllSwapped(ctx),
}
}
8 changes: 6 additions & 2 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
"fmt"

"github.com/Finschia/finschia-sdk/x/fswap/types"
)

Expand All @@ -11,7 +13,9 @@ func (s *KeeperTestSuite) TestInitAndExportGenesis() {
s.Require().NoError(err)

exportGenesis := s.keeper.ExportGenesis(ctx)
fmt.Println(len(exportGenesis.GetSwaps()))
s.Require().Equal(defaultGenesis, exportGenesis)
s.Require().Equal(defaultGenesis.GetSwapInit(), exportGenesis.GetSwapInit())
s.Require().Equal(defaultGenesis.GetSwapped(), exportGenesis.GetSwapped())
s.Require().Equal(defaultGenesis.GetSwaps(), exportGenesis.GetSwaps())
s.Require().Equal(defaultGenesis.GetSwapStats(), exportGenesis.GetSwapStats())
s.Require().Equal(defaultGenesis.GetSwappeds(), exportGenesis.GetSwappeds())
}
8 changes: 4 additions & 4 deletions x/fswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func NewQueryServer(keeper Keeper) *QueryServer {
}
}

func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) {
func (s QueryServer) Swapped(ctx context.Context, req *types.QuerySwappedRequest) (*types.QuerySwappedResponse, error) {
c := sdk.UnwrapSDKContext(ctx)

swapped, err := s.Keeper.getSwapped(c)
swapped, err := s.Keeper.getSwapped(c, req.GetFromDenom(), req.GetToDenom())
if err != nil {
return nil, err
}
Expand All @@ -33,10 +33,10 @@ func (s QueryServer) Swapped(ctx context.Context, _ *types.QuerySwappedRequest)
}, nil
}

func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, _ *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) {
func (s QueryServer) TotalSwappableToCoinAmount(ctx context.Context, req *types.QueryTotalSwappableToCoinAmountRequest) (*types.QueryTotalSwappableToCoinAmountResponse, error) {
c := sdk.UnwrapSDKContext(ctx)

amount, err := s.Keeper.getSwappableNewCoinAmount(c)
amount, err := s.Keeper.getSwappableNewCoinAmount(c, req.GetFromDenom(), req.GetToDenom())
if err != nil {
return &types.QueryTotalSwappableToCoinAmountResponse{}, err
}
Expand Down
Loading

0 comments on commit 00e23a9

Please sign in to comment.