diff --git a/CHANGELOG.md b/CHANGELOG.md index 04ff53deb7..c7271e2220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/server) [\#1337](https://github.com/Finschia/finschia-sdk/pull/1337) fix panic when defining minimum gas config as `100stake;100uatom`. Use a `,` delimiter instead of `;`. Fixes the server config getter to use the correct delimiter (backport cosmos/cosmos-sdk#18537) * (x/fbridge) [\#1361](https://github.com/Finschia/finschia-sdk/pull/1361) Fixes fbridge auth checking bug * (x/fswap) [\#1365](https://github.com/Finschia/finschia-sdk/pull/1365) fix update swap keys for possibly overlapped keys(`(hello,world) should be different to (hel,loworld)`) +* (x/fswap, x/fbridge) [\#1378](https://github.com/Finschia/finschia-sdk/pull/1378) Fix bug where amino is not supported in fswap and fbridge ### Removed diff --git a/client/docs/swagger-ui/swagger.yaml b/client/docs/swagger-ui/swagger.yaml index de9fab3eff..e91fe4bf05 100644 --- a/client/docs/swagger-ui/swagger.yaml +++ b/client/docs/swagger-ui/swagger.yaml @@ -29943,6 +29943,11 @@ paths: type: string format: uint64 title: default period of the proposal to update the role + target_denom: + type: string + description: >- + target denom of the bridge module. This is the base denom + of Finschia normally. default: description: An unexpected error response schema: @@ -50463,6 +50468,11 @@ definitions: type: string format: uint64 title: default period of the proposal to update the role + target_denom: + type: string + description: >- + target denom of the bridge module. This is the base denom of Finschia + normally. lbm.fbridge.v1.ProvisionData: type: object properties: @@ -50661,6 +50671,11 @@ definitions: type: string format: uint64 title: default period of the proposal to update the role + target_denom: + type: string + description: >- + target denom of the bridge module. This is the base denom of + Finschia normally. lbm.fbridge.v1.QueryProposalResponse: type: object properties: diff --git a/x/fbridge/module/module.go b/x/fbridge/module/module.go index 72d43c88f3..2d6fdce60e 100644 --- a/x/fbridge/module/module.go +++ b/x/fbridge/module/module.go @@ -141,10 +141,10 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Deprecated: Route does nothing. -func (am AppModule) Route() sdk.Route { return sdk.NewRoute("", nil) } +func (am AppModule) Route() sdk.Route { return sdk.NewRoute(types.RouterKey, nil) } // Deprecated: QuerierRoute does nothing. -func (am AppModule) QuerierRoute() string { return "" } +func (am AppModule) QuerierRoute() string { return types.QuerierRoute } // Deprecated: LegacyQuerierHandler does nothing. func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return nil } diff --git a/x/fbridge/types/codec.go b/x/fbridge/types/codec.go index 9dd9374b4d..850760afc8 100644 --- a/x/fbridge/types/codec.go +++ b/x/fbridge/types/codec.go @@ -51,6 +51,7 @@ var ( ) func init() { + RegisterLegacyAminoCodec(Amino) cryptocodec.RegisterCrypto(Amino) codec.RegisterEvidences(Amino) sdk.RegisterLegacyAminoCodec(Amino) diff --git a/x/fbridge/types/keys.go b/x/fbridge/types/keys.go index 81653c6456..9bed3ae822 100644 --- a/x/fbridge/types/keys.go +++ b/x/fbridge/types/keys.go @@ -15,6 +15,12 @@ const ( // StoreKey is the store key string for fbridge StoreKey = ModuleName + // RouterKey is the message route for fbridge + RouterKey = ModuleName + + // QuerierRoute defines the module's query routing key + QuerierRoute = ModuleName + // MemStoreKey is the in-memory store key string for fbridge MemStoreKey = "mem_" + StoreKey ) diff --git a/x/fbridge/types/msgs.go b/x/fbridge/types/msgs.go index 12b0af12bc..43e71026e4 100644 --- a/x/fbridge/types/msgs.go +++ b/x/fbridge/types/msgs.go @@ -26,102 +26,222 @@ func (m MsgUpdateParams) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgUpdateParams) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgUpdateParams) Route() string { + return RouterKey +} + func (m MsgTransfer) ValidateBasic() error { return nil } func (m MsgTransfer) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.Sender)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgTransfer) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgTransfer) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgTransfer) Route() string { + return RouterKey +} + func (m MsgProvision) ValidateBasic() error { return nil } func (m MsgProvision) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgProvision) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgProvision) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgProvision) Route() string { + return RouterKey +} + func (m MsgHoldTransfer) ValidateBasic() error { return nil } func (m MsgHoldTransfer) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgHoldTransfer) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgHoldTransfer) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgHoldTransfer) Route() string { + return RouterKey +} + func (m MsgReleaseTransfer) ValidateBasic() error { return nil } func (m MsgReleaseTransfer) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgReleaseTransfer) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgReleaseTransfer) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgReleaseTransfer) Route() string { + return RouterKey +} + func (m MsgRemoveProvision) ValidateBasic() error { return nil } func (m MsgRemoveProvision) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgRemoveProvision) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgRemoveProvision) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgRemoveProvision) Route() string { + return RouterKey +} + func (m MsgClaimBatch) ValidateBasic() error { return nil } func (m MsgClaimBatch) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgClaimBatch) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgClaimBatch) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgClaimBatch) Route() string { + return RouterKey +} + func (m MsgClaim) ValidateBasic() error { return nil } func (m MsgClaim) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgClaim) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgClaim) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgClaim) Route() string { + return RouterKey +} + func (m MsgSuggestRole) ValidateBasic() error { return nil } func (m MsgSuggestRole) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgSuggestRole) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgSuggestRole) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgSuggestRole) Route() string { + return RouterKey +} + func (m MsgAddVoteForRole) ValidateBasic() error { return nil } func (m MsgAddVoteForRole) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.From)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgAddVoteForRole) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } +// Type implements the LegacyMsg.Type method. +func (m MsgAddVoteForRole) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgAddVoteForRole) Route() string { + return RouterKey +} + func (m MsgSetBridgeStatus) ValidateBasic() error { return nil } func (m MsgSetBridgeStatus) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{sdk.MustAccAddressFromBech32(m.Guardian)} } +// GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m MsgSetBridgeStatus) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } + +// Type implements the LegacyMsg.Type method. +func (m MsgSetBridgeStatus) Type() string { + return sdk.MsgTypeURL(&m) +} + +// Route implements the LegacyMsg.Route method. +func (m MsgSetBridgeStatus) Route() string { + return RouterKey +} diff --git a/x/fbridge/types/msgs_test.go b/x/fbridge/types/msgs_test.go new file mode 100644 index 0000000000..87166e0ad4 --- /dev/null +++ b/x/fbridge/types/msgs_test.go @@ -0,0 +1,148 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx" + fbridgetypes "github.com/Finschia/finschia-sdk/x/fbridge/types" +) + +func TestAminoJSON(t *testing.T) { + tx := legacytx.StdTx{} + + addrs := []string{ + "link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze", + "link1aydj7vdxljxq7cn2dlkrhrcwf5py8dnuqp32qd", + } + toAddr := "0xf7bAc63fc7CEaCf0589F25454Ecf5C2ce904997c" + + testCase := map[string]struct { + msg legacytx.LegacyMsg + expectedType string + expected string + }{ + "MsgUpdateParam": { + &fbridgetypes.MsgUpdateParams{ + Authority: addrs[0], + Params: fbridgetypes.Params{ + OperatorTrustLevel: fbridgetypes.Fraction{ + Numerator: uint64(2), + Denominator: uint64(3), + }, + GuardianTrustLevel: fbridgetypes.Fraction{ + Numerator: uint64(2), + Denominator: uint64(3), + }, + JudgeTrustLevel: fbridgetypes.Fraction{ + Numerator: uint64(2), + Denominator: uint64(3), + }, + TimelockPeriod: uint64(86400000000000), + ProposalPeriod: uint64(3600000000000), + TargetDenom: "kaia", + }, + }, + "/lbm.fbridge.v1.MsgUpdateParams", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/fbridge/MsgUpdateParams\",\"value\":{\"authority\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"params\":{\"guardian_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"judge_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"operator_trust_level\":{\"denominator\":\"3\",\"numerator\":\"2\"},\"proposal_period\":\"3600000000000\",\"target_denom\":\"kaia\",\"timelock_period\":\"86400000000000\"}}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgTransfer": { + &fbridgetypes.MsgTransfer{ + Sender: addrs[0], + Receiver: toAddr, + Amount: sdk.NewInt(1000000), + }, + "/lbm.fbridge.v1.MsgTransfer", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgTransfer\",\"value\":{\"amount\":\"1000000\",\"receiver\":\"0xf7bAc63fc7CEaCf0589F25454Ecf5C2ce904997c\",\"sender\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgProvision": { + &fbridgetypes.MsgProvision{ + From: addrs[0], + Seq: uint64(1), + Sender: addrs[0], + Receiver: addrs[1], + Amount: sdk.NewInt(1000000), + }, + "/lbm.fbridge.v1.MsgProvision", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgProvision\",\"value\":{\"amount\":\"1000000\",\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"receiver\":\"link1aydj7vdxljxq7cn2dlkrhrcwf5py8dnuqp32qd\",\"sender\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"seq\":\"1\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgHoldTransfer": { + &fbridgetypes.MsgHoldTransfer{ + From: addrs[0], + Seq: 100, + }, + "/lbm.fbridge.v1.MsgHoldTransfer", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgHoldTransfer\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"seq\":\"100\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgReleaseTransfer": { + &fbridgetypes.MsgReleaseTransfer{ + From: addrs[0], + Seq: 200, + }, + "/lbm.fbridge.v1.MsgReleaseTransfer", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgReleaseTransfer\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"seq\":\"200\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgRemoveProvision": { + &fbridgetypes.MsgRemoveProvision{ + From: addrs[0], + Seq: 300, + }, + "/lbm.fbridge.v1.MsgRemoveProvision", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgRemoveProvision\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"seq\":\"300\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgClaimBatch": { + &fbridgetypes.MsgClaimBatch{ + From: addrs[0], + MaxClaims: 50, + }, + "/lbm.fbridge.v1.MsgClaimBatch", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgClaimBatch\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"max_claims\":\"50\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgClaim": { + &fbridgetypes.MsgClaim{ + From: addrs[0], + Seq: 400, + }, + "/lbm.fbridge.v1.MsgClaim", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgClaim\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"seq\":\"400\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgSuggestRole": { + &fbridgetypes.MsgSuggestRole{ + From: addrs[0], + Target: addrs[1], + Role: fbridgetypes.RoleGuardian, + }, + "/lbm.fbridge.v1.MsgSuggestRole", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSuggestRole\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"role\":1,\"target\":\"link1aydj7vdxljxq7cn2dlkrhrcwf5py8dnuqp32qd\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgAddVoteForRole": { + &fbridgetypes.MsgAddVoteForRole{ + From: addrs[0], + ProposalId: 0, + Option: fbridgetypes.OptionYes, + }, + "/lbm.fbridge.v1.MsgAddVoteForRole", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgAddVoteForRole\",\"value\":{\"from\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"option\":1}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgSetBridgeStatus": { + &fbridgetypes.MsgSetBridgeStatus{ + Guardian: addrs[0], + Status: fbridgetypes.StatusActive, + }, + "/lbm.fbridge.v1.MsgSetBridgeStatus", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSetBridgeStatus\",\"value\":{\"guardian\":\"link1zf469e6y5zvsvkjz8vpr27j6txseyfnsh3ydze\",\"status\":1}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + } + + for name, tc := range testCase { + tc := tc + t.Run(name, func(t *testing.T) { + tx.Msgs = []sdk.Msg{tc.msg} + require.Equal(t, fbridgetypes.RouterKey, tc.msg.Route()) + require.Equal(t, tc.expectedType, tc.msg.Type()) + require.Equal(t, tc.expected, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{tc.msg}, "memo"))) + }) + } +} diff --git a/x/fswap/types/keys.go b/x/fswap/types/keys.go index d69f08c9e0..a50232e104 100644 --- a/x/fswap/types/keys.go +++ b/x/fswap/types/keys.go @@ -7,7 +7,7 @@ const ( // StoreKey defines the primary module store key StoreKey = ModuleName - // RouterKey is the message route for slashing + // RouterKey is the message route for fswap RouterKey = ModuleName // QuerierRoute defines the module's query routing key diff --git a/x/fswap/types/msgs.go b/x/fswap/types/msgs.go index 936aa1cdb4..f8ca3407c2 100644 --- a/x/fswap/types/msgs.go +++ b/x/fswap/types/msgs.go @@ -3,6 +3,7 @@ package types import ( sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/foundation/codec" ) var ( @@ -47,6 +48,20 @@ func (m *MsgSwap) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m)) } +// Type implements the LegacyMsg.Type method. +func (m *MsgSwap) Type() string { + return sdk.MsgTypeURL(m) +} + +// Route implements the LegacyMsg.Route method. +func (m *MsgSwap) Route() string { + return RouterKey +} + +func (m *MsgSwap) GetSignerBytes() []byte { + return sdk.MustSortJSON(codec.ModuleCdc.MustMarshalJSON(m)) +} + // ValidateBasic Implements Msg. func (m *MsgSwapAll) ValidateBasic() error { _, err := sdk.AccAddressFromBech32(m.FromAddress) @@ -74,6 +89,16 @@ func (m *MsgSwapAll) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{from} } +// Type implements the LegacyMsg.Type method. +func (m *MsgSwapAll) Type() string { + return sdk.MsgTypeURL(m) +} + +// Route implements the LegacyMsg.Route method. +func (m *MsgSwapAll) Route() string { + return RouterKey +} + // GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m *MsgSwapAll) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m)) @@ -102,6 +127,16 @@ func (m *MsgSetSwap) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{signer} } +// Type implements the LegacyMsg.Type method. +func (m *MsgSetSwap) Type() string { + return sdk.MsgTypeURL(m) +} + +// Route implements the LegacyMsg.Route method. +func (m *MsgSetSwap) Route() string { + return RouterKey +} + // GetSignBytes implements the LegacyMsg.GetSignBytes method. func (m *MsgSetSwap) GetSignBytes() []byte { return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m)) diff --git a/x/fswap/types/msgs_test.go b/x/fswap/types/msgs_test.go new file mode 100644 index 0000000000..1ae1c71636 --- /dev/null +++ b/x/fswap/types/msgs_test.go @@ -0,0 +1,85 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx" + banktypes "github.com/Finschia/finschia-sdk/x/bank/types" + fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func TestAminoJSON(t *testing.T) { + tx := legacytx.StdTx{} + + sender := "link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj" + + swapRate, _ := sdk.NewDecFromStr("148.079656") + + testCase := map[string]struct { + msg legacytx.LegacyMsg + expectedType string + expected string + }{ + "MsgSwap": { + &fswaptypes.MsgSwap{ + FromAddress: sender, + FromCoinAmount: sdk.Coin{ + Denom: "cony", + Amount: sdk.NewInt(100000), + }, + ToDenom: "pdt", + }, + "/lbm.fswap.v1.MsgSwap", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSwap\",\"value\":{\"from_address\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"from_coin_amount\":{\"amount\":\"100000\",\"denom\":\"cony\"},\"to_denom\":\"pdt\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgSwapAll": { + &fswaptypes.MsgSwapAll{ + FromAddress: sender, + FromDenom: "cony", + ToDenom: "pdt", + }, + "/lbm.fswap.v1.MsgSwapAll", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSwapAll\",\"value\":{\"from_address\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"from_denom\":\"cony\",\"to_denom\":\"pdt\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + "MsgSetSwap": { + &fswaptypes.MsgSetSwap{ + Authority: sender, + Swap: fswaptypes.Swap{ + FromDenom: "cony", + ToDenom: "pdt", + AmountCapForToDenom: sdk.NewInt(1000000000000000), + SwapRate: swapRate, + }, + ToDenomMetadata: banktypes.Metadata{ + Description: "test", + DenomUnits: []*banktypes.DenomUnit{ + { + Denom: "kaia", + Exponent: 0, + Aliases: nil, + }, + }, + Base: "kaia", + Display: "KAIA", + Name: "Kaia", + Symbol: "KAIA", + }, + }, + "/lbm.fswap.v1.MsgSetSwap", + "{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/MsgSetSwap\",\"value\":{\"authority\":\"link15sdc7wdajsps42fky3j6mnvr4tj9fv6w3hkqkj\",\"swap\":{\"amount_cap_for_to_denom\":\"1000000000000000\",\"from_denom\":\"cony\",\"swap_rate\":\"148.079656000000000000\",\"to_denom\":\"pdt\"},\"to_denom_metadata\":{\"base\":\"kaia\",\"denom_units\":[{\"denom\":\"kaia\"}],\"description\":\"test\",\"display\":\"KAIA\",\"name\":\"Kaia\",\"symbol\":\"KAIA\"}}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", + }, + } + + for name, tc := range testCase { + tc := tc + t.Run(name, func(t *testing.T) { + tx.Msgs = []sdk.Msg{tc.msg} + require.Equal(t, fswaptypes.RouterKey, tc.msg.Route()) + require.Equal(t, tc.expectedType, tc.msg.Type()) + require.Equal(t, tc.expected, string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{tc.msg}, "memo"))) + }) + } +}