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: support message based proposals (backport #1372) #1373

Merged
merged 3 commits into from
May 9, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/fswap) [\#1363](https://github.com/Finschia/finschia-sdk/pull/1363) introduce new event for MakeSwapProposal
* (x/fbridge) [\#1366](https://github.com/Finschia/finschia-sdk/pull/1366) Set target denom as module parameters
* (x/fbridge) [\#1369](https://github.com/Finschia/finschia-sdk/pull/1369) Add the event of `SetBridgeStatus`
* (x/fswap) [\#1372](https://github.com/Finschia/finschia-sdk/pull/1372) support message based proposals

### Bug Fixes
* (x/auth) [#1281](https://github.com/Finschia/finschia-sdk/pull/1281) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. (backport #1274)
Expand Down
49 changes: 30 additions & 19 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,6 @@
- [Msg](#lbm.foundation.v1.Msg)

- [lbm/fswap/v1/fswap.proto](#lbm/fswap/v1/fswap.proto)
- [MakeSwapProposal](#lbm.fswap.v1.MakeSwapProposal)
- [Swap](#lbm.fswap.v1.Swap)
- [SwapStats](#lbm.fswap.v1.SwapStats)
- [Swapped](#lbm.fswap.v1.Swapped)
Expand All @@ -1002,6 +1001,8 @@
- [Query](#lbm.fswap.v1.Query)

- [lbm/fswap/v1/tx.proto](#lbm/fswap/v1/tx.proto)
- [MsgSetSwap](#lbm.fswap.v1.MsgSetSwap)
- [MsgSetSwapResponse](#lbm.fswap.v1.MsgSetSwapResponse)
- [MsgSwap](#lbm.fswap.v1.MsgSwap)
- [MsgSwapAll](#lbm.fswap.v1.MsgSwapAll)
- [MsgSwapAllResponse](#lbm.fswap.v1.MsgSwapAllResponse)
Expand Down Expand Up @@ -14555,24 +14556,6 @@ Msg defines the foundation Msg service.



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

### MakeSwapProposal
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` | [Swap](#lbm.fswap.v1.Swap) | | |
| `to_denom_metadata` | [cosmos.bank.v1beta1.Metadata](#cosmos.bank.v1beta1.Metadata) | | |






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

### Swap
Expand Down Expand Up @@ -14856,6 +14839,33 @@ GenesisState defines the fswap module's genesis state.



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

### MsgSetSwap



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `authority` | [string](#string) | | authority is the address of the privileged account. |
| `swap` | [Swap](#lbm.fswap.v1.Swap) | | |
| `to_denom_metadata` | [cosmos.bank.v1beta1.Metadata](#cosmos.bank.v1beta1.Metadata) | | |






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

### MsgSetSwapResponse







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

### MsgSwap
Expand Down Expand Up @@ -14925,6 +14935,7 @@ GenesisState defines the fswap module's genesis state.
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `Swap` | [MsgSwap](#lbm.fswap.v1.MsgSwap) | [MsgSwapResponse](#lbm.fswap.v1.MsgSwapResponse) | | |
| `SwapAll` | [MsgSwapAll](#lbm.fswap.v1.MsgSwapAll) | [MsgSwapAllResponse](#lbm.fswap.v1.MsgSwapAllResponse) | | |
| `SetSwap` | [MsgSetSwap](#lbm.fswap.v1.MsgSetSwap) | [MsgSetSwapResponse](#lbm.fswap.v1.MsgSetSwapResponse) | | |

<!-- end services -->

Expand Down
19 changes: 0 additions & 19 deletions proto/lbm/fswap/v1/fswap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/bank/v1beta1/bank.proto";

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

string from_denom = 1;
string to_denom = 2;
string amount_cap_for_to_denom = 3
Expand All @@ -20,25 +16,10 @@ message Swap {
}

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 MakeSwapProposal {
option (gogoproto.goproto_stringer) = false;

string title = 1;
string description = 2;
Swap swap = 3 [(gogoproto.nullable) = false];
cosmos.bank.v1beta1.Metadata to_denom_metadata = 4
[(gogoproto.moretags) = "yaml:\"denom_metadata\"", (gogoproto.nullable) = false];
}

message Swapped {
option (gogoproto.goproto_stringer) = false;
cosmos.base.v1beta1.Coin from_coin_amount = 1
[(gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin", (gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin to_coin_amount = 2
Expand Down
13 changes: 13 additions & 0 deletions proto/lbm/fswap/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/bank/v1beta1/bank.proto";
import "lbm/fswap/v1/fswap.proto";

service Msg {
rpc Swap(MsgSwap) returns (MsgSwapResponse);
rpc SwapAll(MsgSwapAll) returns (MsgSwapAllResponse);
rpc SetSwap(MsgSetSwap) returns (MsgSetSwapResponse);
}

message MsgSwap {
Expand All @@ -30,3 +33,13 @@ message MsgSwapAll {
}

message MsgSwapAllResponse {}

message MsgSetSwap {
// authority is the address of the privileged account.
string authority = 1;
Swap swap = 2 [(gogoproto.nullable) = false];
cosmos.bank.v1beta1.Metadata to_denom_metadata = 3
[(gogoproto.moretags) = "yaml:\"denom_metadata\"", (gogoproto.nullable) = false];
}

message MsgSetSwapResponse {}
7 changes: 2 additions & 5 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import (
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"
fswapclient "github.com/Finschia/finschia-sdk/x/fswap/client"
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"
Expand Down Expand Up @@ -139,7 +138,6 @@ var (
upgradeclient.ProposalHandler,
upgradeclient.CancelProposalHandler,
foundationclient.ProposalHandler,
fswapclient.ProposalHandler,
),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
Expand Down Expand Up @@ -349,16 +347,15 @@ func NewSimApp(
app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter())

fswapConfig := fswaptypes.DefaultConfig()
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, app.BankKeeper)
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, fswaptypes.DefaultAuthority().String(), app.BankKeeper)

// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
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.NewSwapHandler(app.FswapKeeper))
AddRoute(foundation.RouterKey, foundationkeeper.NewFoundationProposalsHandler(app.FoundationKeeper))

govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
Expand Down
79 changes: 36 additions & 43 deletions x/fswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
bank "github.com/Finschia/finschia-sdk/x/bank/types"
"github.com/Finschia/finschia-sdk/x/fswap/types"
govcli "github.com/Finschia/finschia-sdk/x/gov/client/cli"
gov "github.com/Finschia/finschia-sdk/x/gov/types"
)

const (
Expand All @@ -36,6 +34,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(
CmdTxMsgSwap(),
CmdTxMsgSwapAll(),
CmdMsgSetSwap(),
)

return cmd
Expand Down Expand Up @@ -115,21 +114,21 @@ func CmdTxMsgSwapAll() *cobra.Command {
return cmd
}

// NewCmdMakeSwapProposal implements a command handler for submitting a swap init proposal transaction.
func NewCmdMakeSwapProposal() *cobra.Command {
// CmdMsgSetSwap implements a command handler for submitting a swap init proposal transaction.
func CmdMsgSetSwap() *cobra.Command {
cmd := &cobra.Command{
Use: "make-swap [messages-json]",
Args: cobra.ExactArgs(1),
Short: "todo",
Use: "set-swap [authority] [metadata-json]",
Args: cobra.ExactArgs(2),
Short: "Set a swap",
Long: `
Parameters:
messages-json: messages in json format that will be executed if the proposal is accepted.
metadata-json: messages in json format that will be executed if the proposal is accepted.

Example of the content of messages-json:
Example of the content of metadata-json:

{
"metadata": {
"description": "the base coin of Finschia mainnet",
"description": "example of to-denom is finschia cony",
"denom_units": [
{
"denom": "cony",
Expand All @@ -152,19 +151,11 @@ Example of the content of messages-json:
}
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

from := clientCtx.GetFromAddress()

title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
if err := validateGenerateOnly(cmd); err != nil {
return err
}

description, err := cmd.Flags().GetString(govcli.FlagDescription)
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
Expand All @@ -178,10 +169,12 @@ Example of the content of messages-json:
if err != nil {
return err
}

amountCapStr, err := cmd.Flags().GetString(FlagAmountCapForToDenom)
if err != nil {
return err
}

amountCap, ok := sdk.NewIntFromString(amountCapStr)
if !ok {
return sdkerrors.ErrInvalidRequest.Wrapf("failed to parse %s %s", FlagAmountCapForToDenom, amountCap.String())
Expand All @@ -202,42 +195,42 @@ Example of the content of messages-json:
SwapRate: swapRateDec,
}

toDenomMetadata, err := parseToDenomMetadata(args[0])
if err != nil {
return err
}

content := types.NewMakeSwapProposal(title, description, swap, toDenomMetadata)

depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
authority := args[0]
toDenomMetadata, err := parseToDenomMetadata(args[1])
if err != nil {
return err
}

msg, err := gov.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
msg := types.MsgSetSwap{
Authority: authority,
Swap: swap,
ToDenomMetadata: toDenomMetadata,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal")
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")
cmd.Flags().String(FlagFromDenom, "", "cony")
cmd.Flags().String(FlagToDenom, "", "PDT")
cmd.Flags().String(FlagAmountCapForToDenom, "0", "tbd")
cmd.Flags().String(FlagSwapRate, "0", "tbd")
cmd.Flags().String(FlagFromDenom, "", "set fromDenom string, ex) cony")
cmd.Flags().String(FlagToDenom, "", "set toDenom string, ex) peb")
cmd.Flags().String(FlagAmountCapForToDenom, "0", "set integer value for limit cap for the amount to swap to to-denom, ex 1000000000")
cmd.Flags().String(FlagSwapRate, "0", "set swap rate for swap from fromDenom to toDenom, ex(rate for cony to peb) 148079656000000")

flags.AddTxFlagsToCmd(cmd)
return cmd
}

func validateGenerateOnly(cmd *cobra.Command) error {
generateOnly, err := cmd.Flags().GetBool(flags.FlagGenerateOnly)
if err != nil {
return err
}
if !generateOnly {
return fmt.Errorf("you must use it with the flag --%s", flags.FlagGenerateOnly)
}
return nil
}

func parseToDenomMetadata(jsonDenomMetadata string) (bank.Metadata, error) {
type toDenomMeta struct {
Metadata bank.Metadata `json:"metadata"`
Expand Down
8 changes: 0 additions & 8 deletions x/fswap/client/proposal_handler.go

This file was deleted.

18 changes: 0 additions & 18 deletions x/fswap/codec/codec.go

This file was deleted.

27 changes: 0 additions & 27 deletions x/fswap/handler.go

This file was deleted.

Loading
Loading