-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sponsorship): added types package
- Loading branch information
Showing
16 changed files
with
612 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/msgservice" | ||
) | ||
|
||
// RegisterInterfaces registers interfaces types with the interface registry. | ||
func RegisterInterfaces(reg types.InterfaceRegistry) { | ||
reg.RegisterImplementations( | ||
(*sdk.Msg)(nil), | ||
&MsgUpdateParams{}, | ||
&MsgVote{}, | ||
) | ||
msgservice.RegisterMsgServiceDesc(reg, &_Msg_serviceDesc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package types | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
) | ||
|
||
var ( | ||
hundred = math.NewInt(100) | ||
|
||
DefaultMinAllocationWeight = math.NewInt(10) // 10% | ||
DefaultMinVotingPower = math.NewInt(1) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package types | ||
|
||
import errorsmod "cosmossdk.io/errors" | ||
|
||
var ( | ||
ErrInvalidGaugeWeight = errorsmod.Register(ModuleName, 1, "invalid gauge weight") | ||
ErrInvalidDistribution = errorsmod.Register(ModuleName, 2, "invalid gauge weight distribution") | ||
ErrInvalidParams = errorsmod.Register(ModuleName, 3, "invalid params") | ||
ErrInvalidGenesis = errorsmod.Register(ModuleName, 4, "invalid genesis") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package types | ||
|
||
func DefaultGenesis() *GenesisState { | ||
return &GenesisState{ | ||
Params: DefaultParams(), | ||
} | ||
} | ||
|
||
func (g GenesisState) Validate() error { | ||
err := g.Params.Validate() | ||
if err != nil { | ||
return ErrInvalidGenesis.Wrapf(err.Error()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dymensionxyz/dymension/v3/x/sponsorship/types" | ||
) | ||
|
||
func TestGenesis(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *types.GenesisState | ||
errorIs error | ||
errorContains string | ||
}{ | ||
{ | ||
name: "Valid input", | ||
input: &types.GenesisState{ | ||
Params: types.Params{ | ||
MinAllocationWeight: math.NewInt(20), | ||
MinVotingPower: math.NewInt(20), | ||
}, | ||
}, | ||
errorIs: nil, | ||
errorContains: "", | ||
}, | ||
{ | ||
name: "Default is valid", | ||
input: types.DefaultGenesis(), | ||
errorIs: nil, | ||
errorContains: "", | ||
}, | ||
{ | ||
name: "Invalid params, MinAllocationWeight < 0", | ||
input: &types.GenesisState{ | ||
Params: types.Params{ | ||
MinAllocationWeight: math.NewInt(-20), | ||
MinVotingPower: math.NewInt(20), | ||
}, | ||
}, | ||
errorIs: types.ErrInvalidGenesis, | ||
errorContains: "MinAllocationWeight must be >= 0", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.input.Validate() | ||
|
||
expectError := tt.errorIs != nil | ||
switch expectError { | ||
case true: | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, tt.errorIs) | ||
require.Contains(t, err.Error(), tt.errorContains) | ||
case false: | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
package types | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
func (m *MsgVote) ValidateBasic() error { | ||
//TODO implement me | ||
panic("implement me") | ||
func (m MsgVote) ValidateBasic() error { | ||
_, err := sdk.AccAddressFromBech32(m.Voter) | ||
if err != nil { | ||
return sdkerrors.ErrInvalidAddress.Wrapf( | ||
"voter '%s' must be a valid bech32 address: %s", | ||
m.Voter, err.Error(), | ||
) | ||
} | ||
|
||
err = ValidateGaugeWeights(m.Weights) | ||
if err != nil { | ||
return ErrInvalidDistribution.Wrap(err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m MsgVote) GetSigners() []sdk.AccAddress { | ||
signer, _ := sdk.AccAddressFromBech32(m.Voter) | ||
return []sdk.AccAddress{signer} | ||
} | ||
|
||
func (m MsgUpdateParams) ValidateBasic() error { | ||
_, err := sdk.AccAddressFromBech32(m.Authority) | ||
if err != nil { | ||
return sdkerrors.ErrInvalidAddress.Wrapf( | ||
"authority '%s' must be a valid bech32 address: %s", | ||
m.Authority, err.Error(), | ||
) | ||
} | ||
|
||
err = m.NewParams.Validate() | ||
if err != nil { | ||
return ErrInvalidParams.Wrap(err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MsgVote) GetSigners() []sdk.AccAddress { | ||
//TODO implement me | ||
panic("implement me") | ||
func (m MsgUpdateParams) GetSigners() []sdk.AccAddress { | ||
signer, _ := sdk.AccAddressFromBech32(m.Authority) | ||
return []sdk.AccAddress{signer} | ||
} |
Oops, something went wrong.