Skip to content

Commit

Permalink
wip: fix amino
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Apr 25, 2023
1 parent d112845 commit 6efa42d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
26 changes: 22 additions & 4 deletions x/alliance/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
16 changes: 16 additions & 0 deletions x/alliance/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions x/alliance/types/tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 6efa42d

Please sign in to comment.