refactor(x/protocol): remove Accounts.String() (#19815)

This commit is contained in:
Julián Toledano 2024-03-21 12:21:19 +01:00 committed by GitHub
parent 13db28a39b
commit 9933a444c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 160 additions and 88 deletions

View File

@ -53,7 +53,12 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}
k := keeper.NewKeeper(in.Codec, in.Environment, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, authority.String())
authorityAddr, err := in.AccountKeeper.AddressCodec().BytesToString(authority)
if err != nil {
panic(err)
}
k := keeper.NewKeeper(in.Codec, in.Environment, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, authorityAddr)
m := NewAppModule(in.Codec, k, in.AccountKeeper, in.BankKeeper)
return ModuleOutputs{

View File

@ -54,8 +54,12 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error
func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) {
var cf []*types.ContinuousFund
err := k.ContinuousFund.Walk(ctx, nil, func(key sdk.AccAddress, value types.ContinuousFund) (stop bool, err error) {
recipient, err := k.authKeeper.AddressCodec().BytesToString(key)
if err != nil {
return true, err
}
cf = append(cf, &types.ContinuousFund{
Recipient: key.String(),
Recipient: recipient,
Percentage: value.Percentage,
Expiry: value.Expiry,
})
@ -67,8 +71,12 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error)
var budget []*types.Budget
err = k.BudgetProposal.Walk(ctx, nil, func(key sdk.AccAddress, value types.Budget) (stop bool, err error) {
recipient, err := k.authKeeper.AddressCodec().BytesToString(key)
if err != nil {
return true, err
}
budget = append(budget, &types.Budget{
RecipientAddress: key.String(),
RecipientAddress: recipient,
TotalBudget: value.TotalBudget,
ClaimedAmount: value.ClaimedAmount,
StartTime: value.StartTime,

View File

@ -6,6 +6,7 @@ import (
"cosmossdk.io/math"
"cosmossdk.io/x/protocolpool/types"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -14,6 +15,8 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
period := time.Duration(60) * time.Second
zeroCoin := sdk.NewCoin("foo", math.ZeroInt())
nextClaimFrom := startTime.Add(period)
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
suite.Require().NoError(err)
testCases := []struct {
name string
preRun func()
@ -34,7 +37,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
{
name: "no budget proposal found",
req: &types.QueryUnclaimedBudgetRequest{
Address: recipientAddr.String(),
Address: recipientStrAddr,
},
expErr: true,
expErrMsg: "no budget proposal found for address",
@ -44,7 +47,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
preRun: func() {
// Prepare a valid budget proposal
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -54,7 +57,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
suite.Require().NoError(err)
},
req: &types.QueryUnclaimedBudgetRequest{
Address: recipientAddr.String(),
Address: recipientStrAddr,
},
expErr: false,
unclaimedFunds: &fooCoin,
@ -72,7 +75,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
preRun: func() {
// Prepare a valid budget proposal
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -83,7 +86,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
// Claim the funds once
msg := &types.MsgClaimBudget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
}
suite.mockSendCoinsFromModuleToAccount(recipientAddr)
_, err = suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -91,7 +94,7 @@ func (suite *KeeperTestSuite) TestUnclaimedBudget() {
},
req: &types.QueryUnclaimedBudgetRequest{
Address: recipientAddr.String(),
Address: recipientStrAddr,
},
expErr: false,
unclaimedFunds: &fooCoin2,

View File

@ -113,16 +113,21 @@ func (k Keeper) GetCommunityPool(ctx context.Context) (sdk.Coins, error) {
return k.bankKeeper.GetAllBalances(ctx, moduleAccount.GetAddress()), nil
}
func (k Keeper) withdrawContinuousFund(ctx context.Context, recipient sdk.AccAddress) (sdk.Coin, error) {
func (k Keeper) withdrawContinuousFund(ctx context.Context, recipientAddr string) (sdk.Coin, error) {
recipient, err := k.authKeeper.AddressCodec().StringToBytes(recipientAddr)
if err != nil {
return sdk.Coin{}, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
cf, err := k.ContinuousFund.Get(ctx, recipient)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return sdk.Coin{}, fmt.Errorf("no continuous fund found for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("no continuous fund found for recipient: %s", recipientAddr)
}
return sdk.Coin{}, fmt.Errorf("get continuous fund failed for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("get continuous fund failed for recipient: %s", recipientAddr)
}
if cf.Expiry != nil && cf.Expiry.Before(k.environment.HeaderService.GetHeaderInfo(ctx).Time) {
return sdk.Coin{}, fmt.Errorf("cannot withdraw continuous funds: continuous fund expired for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("cannot withdraw continuous funds: continuous fund expired for recipient: %s", recipientAddr)
}
toDistributeAmount, err := k.ToDistribute.Get(ctx)
@ -138,15 +143,20 @@ func (k Keeper) withdrawContinuousFund(ctx context.Context, recipient sdk.AccAdd
}
// withdraw continuous fund
withdrawnAmount, err := k.withdrawRecipientFunds(ctx, recipient)
withdrawnAmount, err := k.withdrawRecipientFunds(ctx, recipientAddr)
if err != nil {
return sdk.Coin{}, fmt.Errorf("error while withdrawing recipient funds for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("error while withdrawing recipient funds for recipient: %s", recipientAddr)
}
return withdrawnAmount, nil
}
func (k Keeper) withdrawRecipientFunds(ctx context.Context, recipient sdk.AccAddress) (sdk.Coin, error) {
func (k Keeper) withdrawRecipientFunds(ctx context.Context, recipientAddr string) (sdk.Coin, error) {
recipient, err := k.authKeeper.AddressCodec().StringToBytes(recipientAddr)
if err != nil {
return sdk.Coin{}, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
// get allocated continuous fund
fundsAllocated, err := k.RecipientFundDistribution.Get(ctx, recipient)
if err != nil {
@ -165,7 +175,7 @@ func (k Keeper) withdrawRecipientFunds(ctx context.Context, recipient sdk.AccAdd
withdrawnAmount := sdk.NewCoin(denom, fundsAllocated)
err = k.DistributeFromStreamFunds(ctx, sdk.NewCoins(withdrawnAmount), recipient)
if err != nil {
return sdk.Coin{}, fmt.Errorf("error while distributing funds to the recipient %s: %v", recipient.String(), err)
return sdk.Coin{}, fmt.Errorf("error while distributing funds to the recipient %s: %v", recipientAddr, err)
}
// reset fund distribution
@ -257,8 +267,12 @@ func (k Keeper) iterateAndUpdateFundsDistribution(ctx context.Context, toDistrib
// Calculate totalPercentageToBeDistributed and store values
err := k.RecipientFundPercentage.Walk(ctx, nil, func(key sdk.AccAddress, value math.Int) (stop bool, err error) {
addr, err := k.authKeeper.AddressCodec().BytesToString(key)
if err != nil {
return true, err
}
totalPercentageToBeDistributed = totalPercentageToBeDistributed.Add(value)
recipientFundMap[key.String()] = value
recipientFundMap[addr] = value
return false, nil
})
if err != nil {
@ -306,9 +320,14 @@ func (k Keeper) iterateAndUpdateFundsDistribution(ctx context.Context, toDistrib
return k.ToDistribute.Set(ctx, math.ZeroInt())
}
func (k Keeper) claimFunds(ctx context.Context, recipient sdk.AccAddress) (amount sdk.Coin, err error) {
func (k Keeper) claimFunds(ctx context.Context, recipientAddr string) (amount sdk.Coin, err error) {
recipient, err := k.authKeeper.AddressCodec().StringToBytes(recipientAddr)
if err != nil {
return sdk.Coin{}, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
// get claimable funds from distribution info
amount, err = k.getClaimableFunds(ctx, recipient)
amount, err = k.getClaimableFunds(ctx, recipientAddr)
if err != nil {
return sdk.Coin{}, fmt.Errorf("error getting claimable funds: %w", err)
}
@ -322,11 +341,16 @@ func (k Keeper) claimFunds(ctx context.Context, recipient sdk.AccAddress) (amoun
return amount, nil
}
func (k Keeper) getClaimableFunds(ctx context.Context, recipient sdk.AccAddress) (amount sdk.Coin, err error) {
func (k Keeper) getClaimableFunds(ctx context.Context, recipientAddr string) (amount sdk.Coin, err error) {
recipient, err := k.authKeeper.AddressCodec().StringToBytes(recipientAddr)
if err != nil {
return sdk.Coin{}, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
budget, err := k.BudgetProposal.Get(ctx, recipient)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return sdk.Coin{}, fmt.Errorf("no budget found for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("no budget found for recipient: %s", recipientAddr)
}
return sdk.Coin{}, err
}
@ -340,7 +364,7 @@ func (k Keeper) getClaimableFunds(ctx context.Context, recipient sdk.AccAddress)
return sdk.Coin{}, err
}
// Return the end of the budget
return sdk.Coin{}, fmt.Errorf("budget ended for recipient: %s", recipient.String())
return sdk.Coin{}, fmt.Errorf("budget ended for recipient: %s", recipientAddr)
}
}

View File

@ -68,13 +68,16 @@ func (s *KeeperTestSuite) SetupTest() {
stakingKeeper.EXPECT().BondDenom(ctx).Return("stake", nil).AnyTimes()
s.stakingKeeper = stakingKeeper
authority, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.GovModuleName))
s.Require().NoError(err)
poolKeeper := poolkeeper.NewKeeper(
encCfg.Codec,
environment,
accountKeeper,
bankKeeper,
stakingKeeper,
authtypes.NewModuleAddress(types.GovModuleName).String(),
authority,
)
s.ctx = ctx
s.poolKeeper = poolKeeper

View File

@ -26,12 +26,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
}
func (k MsgServer) ClaimBudget(ctx context.Context, msg *types.MsgClaimBudget) (*types.MsgClaimBudgetResponse, error) {
recipient, err := k.Keeper.authKeeper.AddressCodec().StringToBytes(msg.RecipientAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
amount, err := k.claimFunds(ctx, recipient)
amount, err := k.claimFunds(ctx, msg.RecipientAddress)
if err != nil {
return nil, err
}
@ -164,12 +159,7 @@ func (k MsgServer) CreateContinuousFund(ctx context.Context, msg *types.MsgCreat
}
func (k MsgServer) WithdrawContinuousFund(ctx context.Context, msg *types.MsgWithdrawContinuousFund) (*types.MsgWithdrawContinuousFundResponse, error) {
recipient, err := k.Keeper.authKeeper.AddressCodec().StringToBytes(msg.RecipientAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid recipient address: %s", err)
}
amount, err := k.withdrawContinuousFund(ctx, recipient)
amount, err := k.withdrawContinuousFund(ctx, msg.RecipientAddress)
if err != nil {
return nil, err
}
@ -202,7 +192,7 @@ func (k MsgServer) CancelContinuousFund(ctx context.Context, msg *types.MsgCance
}
// withdraw funds if any are allocated
withdrawnFunds, err := k.withdrawRecipientFunds(ctx, recipient)
withdrawnFunds, err := k.withdrawRecipientFunds(ctx, msg.RecipientAddress)
if err != nil && !errorspkg.Is(err, types.ErrNoRecipientFund) {
return nil, fmt.Errorf("error while withdrawing already allocated funds for recipient %s: %v", msg.RecipientAddress, err)
}

View File

@ -8,6 +8,7 @@ import (
"cosmossdk.io/math"
"cosmossdk.io/x/protocolpool/types"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -24,6 +25,8 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
invalidStartTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
period := time.Duration(60) * time.Second
zeroPeriod := time.Duration(0) * time.Second
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
suite.Require().NoError(err)
testCases := map[string]struct {
input *types.MsgSubmitBudgetProposal
expErr bool
@ -44,7 +47,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"empty authority": {
input: &types.MsgSubmitBudgetProposal{
Authority: "",
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -56,7 +59,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"invalid authority": {
input: &types.MsgSubmitBudgetProposal{
Authority: "invalid_authority",
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -68,7 +71,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"invalid budget": {
input: &types.MsgSubmitBudgetProposal{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &invalidCoin,
StartTime: &startTime,
Tranches: 2,
@ -80,7 +83,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"invalid start time": {
input: &types.MsgSubmitBudgetProposal{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &invalidStartTime,
Tranches: 2,
@ -92,7 +95,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"invalid tranches": {
input: &types.MsgSubmitBudgetProposal{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 0,
@ -104,7 +107,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"invalid period": {
input: &types.MsgSubmitBudgetProposal{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -116,7 +119,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
"all good": {
input: &types.MsgSubmitBudgetProposal{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -144,6 +147,8 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
func (suite *KeeperTestSuite) TestMsgClaimBudget() {
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-70 * time.Second)
period := time.Duration(60) * time.Second
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
suite.Require().NoError(err)
testCases := map[string]struct {
preRun func()
@ -167,7 +172,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(3600 * time.Second)
// Prepare the budget proposal with a future start time
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -185,7 +190,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-50 * time.Second)
// Prepare the budget proposal with start time and a short period
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 1,
@ -202,7 +207,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
preRun: func() {
// Prepare the budget proposal with valid start time and period
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -219,7 +224,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
preRun: func() {
// Prepare the budget proposal with valid start time and period
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -230,7 +235,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
// Claim the funds once
msg := &types.MsgClaimBudget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
}
suite.mockSendCoinsFromModuleToAccount(recipientAddr)
_, err = suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -247,7 +252,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
oneMonthPeriod := time.Duration(oneMonthInSeconds) * time.Second
// Prepare the budget proposal with valid start time and period of 1 month (in seconds)
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTimeBeforeMonth,
Tranches: 2,
@ -258,7 +263,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
// Claim the funds once
msg := &types.MsgClaimBudget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
}
suite.mockSendCoinsFromModuleToAccount(recipientAddr)
_, err = suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -278,7 +283,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
preRun: func() {
// Prepare the budget proposal with valid start time and period
budget := types.Budget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
TotalBudget: &fooCoin,
StartTime: &startTime,
Tranches: 2,
@ -289,7 +294,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
// Claim the funds once
msg := &types.MsgClaimBudget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
}
suite.mockSendCoinsFromModuleToAccount(recipientAddr)
_, err = suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -303,7 +308,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
// Claim the funds twice
msg = &types.MsgClaimBudget{
RecipientAddress: recipientAddr.String(),
RecipientAddress: recipientStrAddr,
}
suite.mockSendCoinsFromModuleToAccount(recipientAddr)
_, err = suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -321,8 +326,10 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
if tc.preRun != nil {
tc.preRun()
}
addr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(tc.recipientAddress)
suite.Require().NoError(err)
msg := &types.MsgClaimBudget{
RecipientAddress: tc.recipientAddress.String(),
RecipientAddress: addr,
}
suite.mockSendCoinsFromModuleToAccount(tc.recipientAddress)
resp, err := suite.msgServer.ClaimBudget(suite.ctx, msg)
@ -338,9 +345,16 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
}
func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
addressCodec := codectestutil.CodecOptions{}.GetAddressCodec()
recipient := sdk.AccAddress([]byte("recipientAddr1__________________"))
recipientStrAddr, err := addressCodec.BytesToString(recipient)
suite.Require().NoError(err)
recipient2 := sdk.AccAddress([]byte("recipientAddr2___________________"))
recipient2StrAddr, err := addressCodec.BytesToString(recipient2)
suite.Require().NoError(err)
recipient3 := sdk.AccAddress([]byte("recipientAddr3___________________"))
recipient3StrAddr, err := addressCodec.BytesToString(recipient3)
suite.Require().NoError(err)
testCases := map[string]struct {
preRun func()
recipientAddress []sdk.AccAddress
@ -366,7 +380,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -384,7 +398,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
percentage, err = math.LegacyNewDecFromStr("0.9")
suite.Require().NoError(err)
cf = types.ContinuousFund{
Recipient: recipient2.String(),
Recipient: recipient2StrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -412,7 +426,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
suite.Require().NoError(err)
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(-1) * time.Second)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -431,7 +445,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -456,7 +470,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
percentage, err := math.LegacyNewDecFromStr("0.2")
suite.Require().NoError(err)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
}
// Set continuous fund
@ -484,7 +498,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -514,7 +528,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipient.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -532,7 +546,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
percentage2, err := math.LegacyNewDecFromStr("0.2")
suite.Require().NoError(err)
cf = types.ContinuousFund{
Recipient: recipient2.String(),
Recipient: recipient2StrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -550,7 +564,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
percentage3, err := math.LegacyNewDecFromStr("0.3")
suite.Require().NoError(err)
cf = types.ContinuousFund{
Recipient: recipient3.String(),
Recipient: recipient3StrAddr,
Percentage: percentage2,
Expiry: &expiry,
}
@ -581,8 +595,10 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
if tc.preRun != nil {
tc.preRun()
}
addr, err := addressCodec.BytesToString(tc.recipientAddress[0])
suite.Require().NoError(err)
msg := &types.MsgWithdrawContinuousFund{
RecipientAddress: tc.recipientAddress[0].String(),
RecipientAddress: addr,
}
suite.mockWithdrawContinuousFund()
@ -617,6 +633,8 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
invalidExpirty := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
suite.Require().NoError(err)
testCases := map[string]struct {
preRun func()
input *types.MsgCreateContinuousFund
@ -636,7 +654,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"empty authority": {
input: &types.MsgCreateContinuousFund{
Authority: "",
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
},
@ -646,7 +664,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"invalid authority": {
input: &types.MsgCreateContinuousFund{
Authority: "invalid_authority",
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
},
@ -656,7 +674,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"zero percentage": {
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: math.LegacyNewDec(0),
Expiry: &expiry,
},
@ -666,7 +684,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"negative percentage": {
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: negativePercentage,
Expiry: &expiry,
},
@ -676,7 +694,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"invalid percentage": {
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: math.LegacyNewDec(1),
Expiry: &expiry,
},
@ -686,7 +704,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"invalid expiry": {
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &invalidExpirty,
},
@ -696,7 +714,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"all good": {
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
},
@ -705,10 +723,12 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
"total funds percentage > 100": {
preRun: func() {
percentage, err := math.LegacyNewDecFromStr("0.9")
suite.Require().NoError(err)
recipient2 := sdk.AccAddress([]byte("recipientAddr2___________________"))
recipient2StrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipient2)
suite.Require().NoError(err)
cf := types.ContinuousFund{
Recipient: recipient2.String(),
Recipient: recipient2StrAddr,
Percentage: percentage,
Expiry: &time.Time{},
}
@ -720,7 +740,7 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
},
input: &types.MsgCreateContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
},
@ -752,8 +772,11 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
// canceling a fund with no recipient found, canceling a fund with unclaimed funds for the recipient,
// and canceling a fund with no errors.
func (suite *KeeperTestSuite) TestCancelContinuousFund() {
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
suite.Require().NoError(err)
recipient2 := sdk.AccAddress([]byte("recipientAddr2___________________"))
recipient2StrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipient2)
suite.Require().NoError(err)
testCases := map[string]struct {
preRun func()
recipientAddr sdk.AccAddress
@ -792,7 +815,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -810,7 +833,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
percentage, err = math.LegacyNewDecFromStr("0.3")
suite.Require().NoError(err)
cf = types.ContinuousFund{
Recipient: recipient2.String(),
Recipient: recipient2StrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -832,7 +855,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
// withdraw funds for fund request 2
suite.mockWithdrawContinuousFund()
msg := &types.MsgWithdrawContinuousFund{RecipientAddress: recipient2.String()}
msg := &types.MsgWithdrawContinuousFund{RecipientAddress: recipient2StrAddr}
_, err = suite.msgServer.WithdrawContinuousFund(suite.ctx, msg)
suite.Require().NoError(err)
},
@ -852,7 +875,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipientAddr.String(),
Recipient: recipientStrAddr,
Percentage: percentage,
Expiry: &expiry,
}
@ -875,9 +898,11 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
if tc.preRun != nil {
tc.preRun()
}
addr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(tc.recipientAddr)
suite.Require().NoError(err)
msg := &types.MsgCancelContinuousFund{
Authority: suite.poolKeeper.GetAuthority(),
RecipientAddress: tc.recipientAddr.String(),
RecipientAddress: addr,
}
resp, err := suite.msgServer.CancelContinuousFund(suite.ctx, msg)
if tc.expErr {

View File

@ -72,7 +72,11 @@ func SimulateMsgFundCommunityPool(txConfig client.TxConfig, ak types.AccountKeep
}
}
msg := types.NewMsgFundCommunityPool(fundAmount, funder.Address.String())
funderAddr, err := ak.AddressCodec().BytesToString(funder.Address)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgFundCommunityPool{}), "unable to get funder address"), nil, err
}
msg := types.NewMsgFundCommunityPool(fundAmount, funderAddr)
txCtx := simulation.OperationInput{
R: r,

View File

@ -28,7 +28,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
}
}
func SimulateMsgCommunityPoolSpend(r *rand.Rand, _ []simtypes.Account, _ coreaddress.Codec) (sdk.Msg, error) {
func SimulateMsgCommunityPoolSpend(r *rand.Rand, _ []simtypes.Account, cdc coreaddress.Codec) (sdk.Msg, error) {
// use the default gov module account address as authority
var authority sdk.AccAddress = address.Module("gov")
@ -37,12 +37,20 @@ func SimulateMsgCommunityPoolSpend(r *rand.Rand, _ []simtypes.Account, _ coreadd
coins, err := sdk.ParseCoinsNormalized("100stake,2testtoken")
if err != nil {
panic(err)
return nil, err
}
authorityAddr, err := cdc.BytesToString(authority)
if err != nil {
return nil, err
}
recipentAddr, err := cdc.BytesToString(acc.Address)
if err != nil {
return nil, err
}
return &pooltypes.MsgCommunityPoolSpend{
Authority: authority.String(),
Recipient: acc.Address.String(),
Authority: authorityAddr,
Recipient: recipentAddr,
Amount: coins,
}, nil
}

View File

@ -19,7 +19,7 @@ func TestProposalMsgs(t *testing.T) {
// initialize parameters
s := rand.NewSource(1)
r := rand.New(s)
addressCodec := codectestutil.CodecOptions{}.GetAddressCodec()
accounts := simtypes.RandomAccounts(r, 3)
// execute ProposalMsgs function
@ -32,7 +32,7 @@ func TestProposalMsgs(t *testing.T) {
assert.Equal(t, simulation.OpWeightMsgCommunityPoolSpend, w0.AppParamsKey())
assert.Equal(t, simulation.DefaultWeightMsgCommunityPoolSpend, w0.DefaultWeight())
msg, err := w0.MsgSimulatorFn()(r, accounts, codectestutil.CodecOptions{}.GetAddressCodec())
msg, err := w0.MsgSimulatorFn()(r, accounts, addressCodec)
assert.NilError(t, err)
msgCommunityPoolSpend, ok := msg.(*pooltypes.MsgCommunityPoolSpend)
assert.Assert(t, ok)
@ -40,6 +40,8 @@ func TestProposalMsgs(t *testing.T) {
coins, err := sdk.ParseCoinsNormalized("100stake,2testtoken")
assert.NilError(t, err)
assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgCommunityPoolSpend.Authority)
authAddr, err := addressCodec.BytesToString(address.Module("gov"))
assert.NilError(t, err)
assert.Equal(t, authAddr, msgCommunityPoolSpend.Authority)
assert.Assert(t, msgCommunityPoolSpend.Amount.Equal(coins))
}