From 6efa42d8c35aa288b536564c2e516a67aeea3289 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Tue, 25 Apr 2023 16:47:31 +0300 Subject: [PATCH] wip: fix amino --- x/alliance/types/codec.go | 26 ++++++++++++++++++++++---- x/alliance/types/msg.go | 16 ++++++++++++++++ x/alliance/types/tests/types_test.go | 13 +++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/x/alliance/types/codec.go b/x/alliance/types/codec.go index 0fc8c4a4..0d80f785 100644 --- a/x/alliance/types/codec.go +++ b/x/alliance/types/codec.go @@ -2,17 +2,20 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" ) func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgDelegate{}, "alliance/MsgDelegate", nil) - cdc.RegisterConcrete(&MsgRedelegate{}, "alliance/MsgRedelegate", nil) - cdc.RegisterConcrete(&MsgUndelegate{}, "alliance/MsgUndelegate", nil) - cdc.RegisterConcrete(&MsgClaimDelegationRewards{}, "alliance/MsgClaimDelegationRewards", nil) + legacy.RegisterAminoMsg(cdc, &MsgDelegate{}, "alliance/MsgDelegate") + legacy.RegisterAminoMsg(cdc, &MsgRedelegate{}, "alliance/MsgRedelegate") + legacy.RegisterAminoMsg(cdc, &MsgUndelegate{}, "alliance/MsgUndelegate") + legacy.RegisterAminoMsg(cdc, &MsgClaimDelegationRewards{}, "alliance/MsgClaimDelegationRewards") cdc.RegisterConcrete(&MsgCreateAllianceProposal{}, "alliance/MsgCreateAllianceProposal", nil) cdc.RegisterConcrete(&MsgUpdateAllianceProposal{}, "alliance/MsgUpdateAllianceProposal", nil) @@ -34,3 +37,18 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } + +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(amino) +) + +func init() { + RegisterLegacyAminoCodec(amino) + cryptocodec.RegisterCrypto(amino) + sdk.RegisterLegacyAminoCodec(amino) + + // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be + // used to properly serialize MsgGrant and MsgExec instances + RegisterLegacyAminoCodec(authzcodec.Amino) +} diff --git a/x/alliance/types/msg.go b/x/alliance/types/msg.go index 5a902528..9d7fdffd 100644 --- a/x/alliance/types/msg.go +++ b/x/alliance/types/msg.go @@ -28,6 +28,10 @@ func NewMsgDelegate(delegatorAddress, validatorAddress string, amount sdk.Coin) } } +func (msg MsgDelegate) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) +} + func (m MsgDelegate) ValidateBasic() error { if !m.Amount.Amount.GT(sdk.ZeroInt()) { return status.Errorf(codes.InvalidArgument, "Alliance delegation amount must be more than zero") @@ -54,6 +58,10 @@ func NewMsgRedelegate(delegatorAddress, validatorSrcAddress, validatorDstAddress } } +func (msg MsgRedelegate) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) +} + func (m MsgRedelegate) ValidateBasic() error { if m.Amount.Amount.LTE(sdk.ZeroInt()) { return status.Errorf(codes.InvalidArgument, "Alliance redelegation amount must be more than zero") @@ -79,6 +87,10 @@ func NewMsgUndelegate(delegatorAddress, validatorAddress string, amount sdk.Coin } } +func (msg MsgUndelegate) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) +} + func (m MsgUndelegate) ValidateBasic() error { if m.Amount.Amount.LTE(sdk.ZeroInt()) { return status.Errorf(codes.InvalidArgument, "Alliance undelegate amount must be more than zero") @@ -103,6 +115,10 @@ func (m *MsgClaimDelegationRewards) ValidateBasic() error { return nil } +func (msg MsgClaimDelegationRewards) GetSignBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) +} + func (m *MsgClaimDelegationRewards) GetSigners() []sdk.AccAddress { signer, err := sdk.AccAddressFromBech32(m.DelegatorAddress) if err != nil { diff --git a/x/alliance/types/tests/types_test.go b/x/alliance/types/tests/types_test.go index 3320f285..09bade55 100644 --- a/x/alliance/types/tests/types_test.go +++ b/x/alliance/types/tests/types_test.go @@ -147,3 +147,16 @@ func TestInvalidProposalsContent(t *testing.T) { }) } } + +func TestMsgSendGetSignBytes(t *testing.T) { + coins := sdk.NewCoin("uluna", sdk.OneInt()) + msg := types.NewMsgDelegate( + "terra...delegator", + "terra...validator", + coins, + ) + res := msg.GetSignBytes() + + expected := `{"type":"alliance/MsgDelegate","value":{"amount":{"amount":"1","denom":"uluna"},"delegator_address":"terra...delegator","validator_address":"terra...validator"}}` + require.Equal(t, expected, string(res)) +}