Skip to content

Commit

Permalink
fix: fixed some smaller issues
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Sep 27, 2023
1 parent cca1fee commit 88add2d
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 29 deletions.
2 changes: 2 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions x/funders/keeper/getters_funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions x/funders/keeper/getters_funding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions x/funders/keeper/getters_funding_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions x/funders/keeper/logic_funders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/funders/keeper/msg_server_create_funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions x/funders/keeper/msg_server_defund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
13 changes: 7 additions & 6 deletions x/funders/keeper/msg_server_fund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions x/funders/keeper/msg_server_update_funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions x/funders/types/message_defund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion x/funders/types/message_fund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit 88add2d

Please sign in to comment.