diff --git a/app/modules.go b/app/modules.go index 5a1a349f..cfcf3019 100644 --- a/app/modules.go +++ b/app/modules.go @@ -2,6 +2,7 @@ package app import ( "github.com/KYVENetwork/chain/x/funders" + fundersTypes "github.com/KYVENetwork/chain/x/funders/types" "github.com/cosmos/cosmos-sdk/types/module" // Auth @@ -154,6 +155,7 @@ var moduleAccountPermissions = map[string][]string{ poolTypes.ModuleName: nil, stakersTypes.ModuleName: nil, teamTypes.ModuleName: nil, + fundersTypes.ModuleName: nil, } // BlockedModuleAccountAddrs returns all the app's blocked module account addresses. diff --git a/x/funders/keeper/getters_funder.go b/x/funders/keeper/getters_funder.go index 4a92598a..c66bc7ed 100644 --- a/x/funders/keeper/getters_funder.go +++ b/x/funders/keeper/getters_funder.go @@ -28,9 +28,9 @@ func (k Keeper) GetFunder(ctx sdk.Context, funderAddress string) (funder types.F } // SetFunder sets a specific funder in the store from its index -func (k Keeper) setFunder(ctx sdk.Context, funder types.Funder) { +func (k Keeper) setFunder(ctx sdk.Context, funder *types.Funder) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FunderKeyPrefix) - b := k.cdc.MustMarshal(&funder) + b := k.cdc.MustMarshal(funder) store.Set(types.FunderKey( funder.Address, ), b) diff --git a/x/funders/keeper/getters_funding.go b/x/funders/keeper/getters_funding.go index da0d61c7..f6e430bd 100644 --- a/x/funders/keeper/getters_funding.go +++ b/x/funders/keeper/getters_funding.go @@ -13,7 +13,7 @@ func (k Keeper) DoesFundingExist(ctx sdk.Context, funderAddress string, poolId u } // GetFunding returns the funding -func (k Keeper) GetFunding(ctx sdk.Context, funderAddress string, poolId uint64) (funding *types.Funding, found bool) { +func (k Keeper) GetFunding(ctx sdk.Context, funderAddress string, poolId uint64) (funding types.Funding, found bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder) b := store.Get(types.FundingKeyByFunder( @@ -24,12 +24,12 @@ func (k Keeper) GetFunding(ctx sdk.Context, funderAddress string, poolId uint64) return funding, false } - k.cdc.MustUnmarshal(b, funding) + k.cdc.MustUnmarshal(b, &funding) return funding, true } // GetFundingsOfPool returns all fundings of a pool -func (k Keeper) GetFundingsOfPool(ctx sdk.Context, poolId uint64) (fundings []*types.Funding) { +func (k Keeper) GetFundingsOfPool(ctx sdk.Context, poolId uint64) (fundings []types.Funding) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByPool) iterator := sdk.KVStorePrefixIterator(store, types.FundingKeyByPoolOnly(poolId)) @@ -39,7 +39,7 @@ func (k Keeper) GetFundingsOfPool(ctx sdk.Context, poolId uint64) (fundings []*t for ; iterator.Valid(); iterator.Next() { var funding types.Funding k.cdc.MustUnmarshal(iterator.Value(), &funding) - fundings = append(fundings, &funding) + fundings = append(fundings, funding) } return fundings } diff --git a/x/funders/keeper/getters_funding_state.go b/x/funders/keeper/getters_funding_state.go index 2c513f5c..5e043984 100644 --- a/x/funders/keeper/getters_funding_state.go +++ b/x/funders/keeper/getters_funding_state.go @@ -38,7 +38,7 @@ func (k Keeper) setFundingState(ctx sdk.Context, fundingState types.FundingState ), b) } -func (k Keeper) GetActiveFundings(ctx sdk.Context, fundingState types.FundingState) (fundings []*types.Funding) { +func (k Keeper) GetActiveFundings(ctx sdk.Context, fundingState types.FundingState) (fundings []types.Funding) { for _, funder := range fundingState.ActiveFunderAddresses { funding, found := k.GetFunding(ctx, funder, fundingState.PoolId) if found { @@ -50,14 +50,14 @@ func (k Keeper) GetActiveFundings(ctx sdk.Context, fundingState types.FundingSta // GetLowestFunding returns the funding with the lowest amount // Precondition: len(fundings) > 0 -func (k Keeper) GetLowestFunding(fundings []*types.Funding) (lowestFunding *types.Funding, err error) { +func (k Keeper) GetLowestFunding(fundings []types.Funding) (lowestFunding *types.Funding, err error) { if len(fundings) == 0 { return nil, errors.New(fmt.Sprintf("no active fundings")) } for _, funding := range fundings { if funding.Amount < lowestFunding.Amount { - lowestFunding = funding + lowestFunding = &funding } } return lowestFunding, nil diff --git a/x/funders/keeper/logic_funders.go b/x/funders/keeper/logic_funders.go index 5e1c00f6..bc0b1271 100644 --- a/x/funders/keeper/logic_funders.go +++ b/x/funders/keeper/logic_funders.go @@ -18,7 +18,7 @@ func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payout uint // Get funding state for pool fundingState, found := k.GetFundingState(ctx, poolId) if !found { - return 0, errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrFundingStateDoesNotExist.Error(), poolId) + return 0, errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFundingStateDoesNotExist.Error(), poolId) } // If there are no active fundings we immediately return @@ -32,9 +32,9 @@ func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payout uint for _, funding := range activeFundings { payout += funding.ChargeOneBundle() if funding.Amount == 0 { - fundingState.SetInactive(funding) + fundingState.SetInactive(&funding) } - k.setFunding(ctx, funding) + k.setFunding(ctx, &funding) } // Save funding state diff --git a/x/funders/keeper/msg_server_create_funder.go b/x/funders/keeper/msg_server_create_funder.go index 2e0704bd..8bf2d464 100644 --- a/x/funders/keeper/msg_server_create_funder.go +++ b/x/funders/keeper/msg_server_create_funder.go @@ -21,7 +21,7 @@ func (k msgServer) CreateFunder(goCtx context.Context, msg *types.MsgCreateFunde } // Create new funder - k.setFunder(ctx, types.Funder{ + k.setFunder(ctx, &types.Funder{ Address: msg.Creator, Moniker: msg.Moniker, Identity: msg.Identity, diff --git a/x/funders/keeper/msg_server_defund_pool.go b/x/funders/keeper/msg_server_defund_pool.go index 9e0bd028..c1c095f2 100644 --- a/x/funders/keeper/msg_server_defund_pool.go +++ b/x/funders/keeper/msg_server_defund_pool.go @@ -20,7 +20,7 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) ( // Funding has to exist funding, found := k.GetFunding(ctx, msg.Creator, msg.PoolId) if !found { - return nil, errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrFundingDoesNotExist.Error(), msg.PoolId, msg.Creator) + return nil, errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFundingDoesNotExist.Error(), msg.PoolId, msg.Creator) } if funding.Amount == 0 { @@ -41,18 +41,20 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) ( funding.SubtractAmount(amount) if funding.Amount == 0 { - fundingState.SetInactive(funding) + fundingState.SetInactive(&funding) } fundingState.TotalAmount -= amount // Transfer tokens from this module to sender. - if err := util.TransferFromModuleToAddress(k.bankKeeper, ctx, types.ModuleName, msg.Creator, msg.Amount); err != nil { + // TODO: change module name to funders + //if err := util.TransferFromModuleToAddress(k.bankKeeper, ctx, types.ModuleName, msg.Creator, amount); err != nil { + if err := util.TransferFromModuleToAddress(k.bankKeeper, ctx, "pool", msg.Creator, amount); err != nil { return nil, err } // Save funding and funding state - k.setFunding(ctx, funding) - fundingState.SetActive(funding) + k.setFunding(ctx, &funding) + fundingState.SetActive(&funding) // Emit a defund event. _ = ctx.EventManager().EmitTypedEvent(&types.EventDefundPool{ diff --git a/x/funders/keeper/msg_server_fund_pool.go b/x/funders/keeper/msg_server_fund_pool.go index 591d0cc9..264b3f65 100644 --- a/x/funders/keeper/msg_server_fund_pool.go +++ b/x/funders/keeper/msg_server_fund_pool.go @@ -44,7 +44,7 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ // Funder has to exist if !k.DoesFunderExist(ctx, msg.Creator) { - return nil, errors.Wrapf(errorsTypes.ErrUnauthorized, types.ErrFunderDoesNotExist.Error(), msg.Creator) + return nil, errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFunderDoesNotExist.Error(), msg.Creator) } // Pool has to exist @@ -71,7 +71,7 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ funding.AddAmount(msg.Amount) } else { // If not, create new funding - funding = &types.Funding{ + funding = types.Funding{ FunderAddress: msg.Creator, PoolId: msg.PoolId, Amount: msg.Amount, @@ -109,10 +109,11 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ } } - // TODO: do I have to call ValidateBasic() here? // User is allowed to fund // Let's see if he has enough funds - if err := util.TransferFromAddressToModule(k.bankKeeper, ctx, msg.Creator, types.ModuleName, msg.Amount); err != nil { + // TODO: change module name to types.ModuleName + //if err := util.TransferFromAddressToModule(k.bankKeeper, ctx, msg.Creator, types.ModuleName, msg.Amount); err != nil { + if err := util.TransferFromAddressToModule(k.bankKeeper, ctx, msg.Creator, "pool", msg.Amount); err != nil { return nil, err } @@ -126,10 +127,10 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ } // Funding must be active - fundingState.SetActive(funding) + fundingState.SetActive(&funding) // Save funding and funding state - k.setFunding(ctx, funding) + k.setFunding(ctx, &funding) k.setFundingState(ctx, fundingState) // Emit a fund event. diff --git a/x/funders/keeper/msg_server_update_funder.go b/x/funders/keeper/msg_server_update_funder.go index 0c99fb11..d43933c6 100644 --- a/x/funders/keeper/msg_server_update_funder.go +++ b/x/funders/keeper/msg_server_update_funder.go @@ -15,11 +15,11 @@ func (k msgServer) UpdateFunder(goCtx context.Context, msg *types.MsgUpdateFunde // Error if funder does not exist if !k.DoesFunderExist(ctx, msg.Creator) { - return nil, errors.Wrap(errorsTypes.ErrInvalidRequest, types.ErrFunderDoesNotExist.Error()) + return nil, errors.Wrap(errorsTypes.ErrNotFound, types.ErrFunderDoesNotExist.Error()) } // Update funder - k.setFunder(ctx, types.Funder{ + k.setFunder(ctx, &types.Funder{ Address: msg.Creator, Moniker: msg.Moniker, Identity: msg.Identity, diff --git a/x/funders/types/message_defund_pool.go b/x/funders/types/message_defund_pool.go index fe611bb9..14c7a86b 100644 --- a/x/funders/types/message_defund_pool.go +++ b/x/funders/types/message_defund_pool.go @@ -38,11 +38,11 @@ func (msg *MsgDefundPool) ValidateBasic() error { return errors.Wrapf(errorsTypes.ErrInvalidAddress, "invalid creator address: %s", err) } - if util.ValidatePositiveNumber(msg.PoolId) != nil { + if util.ValidateNumber(msg.PoolId) != nil { return errors.Wrapf(errorsTypes.ErrInvalidRequest, "invalid pool id") } - if util.ValidateNumber(msg.Amount) != nil { + if util.ValidatePositiveNumber(msg.Amount) != nil { return errors.Wrapf(errorsTypes.ErrInvalidRequest, "invalid amount") } diff --git a/x/funders/types/message_fund_pool.go b/x/funders/types/message_fund_pool.go index 44aa69f0..a05298ac 100644 --- a/x/funders/types/message_fund_pool.go +++ b/x/funders/types/message_fund_pool.go @@ -38,7 +38,7 @@ func (msg *MsgFundPool) ValidateBasic() error { return errors.Wrapf(errorsTypes.ErrInvalidAddress, "invalid creator address: %s", err) } - if util.ValidatePositiveNumber(msg.PoolId) != nil { + if util.ValidateNumber(msg.PoolId) != nil { return errors.Wrapf(errorsTypes.ErrInvalidRequest, "invalid pool id") }