Skip to content

Commit

Permalink
chore: further implemented sdk.Coins
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Apr 29, 2024
1 parent fd52592 commit 75d80f0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 132 deletions.
12 changes: 6 additions & 6 deletions proto/kyve/funders/v1beta1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ message EventFundPool {
uint64 pool_id = 1;
// address is the account address of the pool funder.
string address = 2;
// amounts is a list of coins that the funder has funded
repeated cosmos.base.v1beta1.Coin amounts = 3 [
// amount is the amount the funder has funded
cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
// amounts_per_bundle is a list of amounts per coin per bundle
repeated cosmos.base.v1beta1.Coin amounts_per_bundle = 4 [
// amount_per_bundle is the amount the funder pays for each finalized bundle
cosmos.base.v1beta1.Coin amount_per_bundle = 4 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
}

Expand Down
47 changes: 19 additions & 28 deletions x/funders/keeper/logic_funders.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package keeper

import (
"fmt"
globalTypes "github.com/KYVENetwork/chain/x/global/types"

"cosmossdk.io/errors"
"fmt"

"github.com/KYVENetwork/chain/util"
"github.com/KYVENetwork/chain/x/funders/types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -21,40 +18,40 @@ func (k Keeper) CreateFundingState(ctx sdk.Context, poolId uint64) {
k.SetFundingState(ctx, &fundingState)
}

func (k Keeper) GetTotalActiveFunding(ctx sdk.Context, poolId uint64) (amount uint64) {
func (k Keeper) GetTotalActiveFunding(ctx sdk.Context, poolId uint64) (amounts sdk.Coins) {
state, found := k.GetFundingState(ctx, poolId)
if !found {
return 0
return sdk.NewCoins()
}
for _, address := range state.ActiveFunderAddresses {
funding, _ := k.GetFunding(ctx, address, poolId)
amount += funding.Amount
amounts.Add(funding.Amounts...)
}
return amount
return
}

// ChargeFundersOfPool charges all funders of a pool with their amount_per_bundle
// If the amount is lower than the amount_per_bundle,
// the max amount is charged and the funder is removed from the active funders list.
// The amount is transferred from the funders to the pool module account where it can be paid out.
// If there are no more active funders, an event is emitted.
func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payout uint64, err error) {
func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payouts sdk.Coins, err error) {
// Get funding state for pool
fundingState, found := k.GetFundingState(ctx, poolId)
if !found {
return 0, errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFundingStateDoesNotExist.Error(), poolId)
return sdk.NewCoins(), errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFundingStateDoesNotExist.Error(), poolId)
}

// If there are no active fundings we immediately return
activeFundings := k.GetActiveFundings(ctx, fundingState)
if len(activeFundings) == 0 {
return 0, nil
return sdk.NewCoins(), nil
}

// This is the amount every funding will be charged
for _, funding := range activeFundings {
payout += funding.ChargeOneBundle()
if funding.Amount == 0 {
payouts.Add(funding.ChargeOneBundle()...)
if funding.Amounts.IsZero() {
fundingState.SetInactive(&funding)
}
k.SetFunding(ctx, &funding)
Expand All @@ -71,36 +68,31 @@ func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payout uint
}

// Move funds to pool module account
if payout > 0 {
err = util.TransferFromModuleToModule(k.bankKeeper, ctx, types.ModuleName, pooltypes.ModuleName, payout)
if err != nil {
return 0, err
if !payouts.IsZero() {
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, pooltypes.ModuleName, payouts); err != nil {
return sdk.NewCoins(), err
}
}

return payout, nil
return
}

// GetLowestFunding returns the funding with the lowest amount
// Precondition: len(fundings) > 0
func (k Keeper) GetLowestFunding(ctx sdk.Context, fundings []types.Funding, whitelist []*types.WhitelistCoinEntry) (lowestFunding *types.Funding, err error) {
func (k Keeper) GetLowestFunding(fundings []types.Funding, whitelist []*types.WhitelistCoinEntry) (lowestFunding *types.Funding, err error) {
if len(fundings) == 0 {
return nil, fmt.Errorf("no active fundings")
}



lowestFundingIndex := 0
for i := range fundings {
if fundings[i].GetScore(params.CoinWhitelist) < fundings[lowestFundingIndex].GetScore(params.CoinWhitelist) {
if fundings[i].GetScore(whitelist) < fundings[lowestFundingIndex].GetScore(whitelist) {
lowestFundingIndex = i
}
}
return &fundings[lowestFundingIndex], nil
}



// ensureParamsCompatibility checks compatibility of the provided funding with the pool params.
// i.e.
// - coin is in whitelist
Expand Down Expand Up @@ -139,8 +131,6 @@ func (k Keeper) ensureParamsCompatibility(ctx sdk.Context, msg *types.MsgFundPoo
return nil
}

func (k Keeper)

// ensureFreeSlot makes sure that a funder can add funding to a given pool.
// If this is not possible an appropriate error is returned.
// A pool has a fixed amount of funding-slots. If there are still free slots
Expand All @@ -159,7 +149,7 @@ func (k Keeper) ensureFreeSlot(ctx sdk.Context, newFunding *types.Funding, fundi

params := k.GetParams(ctx)

lowestFunding, err := k.GetLowestFunding(ctx, activeFundings, params.CoinWhitelist)
lowestFunding, err := k.GetLowestFunding(activeFundings, params.CoinWhitelist)
if err != nil {
return err
}
Expand All @@ -180,14 +170,15 @@ func (k Keeper) ensureFreeSlot(ctx sdk.Context, newFunding *types.Funding, fundi
return err
}

lowestFunding.Amounts.Sub(lowestFunding.Amounts...)
fundingState.SetInactive(lowestFunding)
k.SetFunding(ctx, lowestFunding)

// Emit a defund event.
_ = ctx.EventManager().EmitTypedEvent(&types.EventDefundPool{
PoolId: fundingState.PoolId,
Address: lowestFunding.FunderAddress,
Amounts: lowestFunding.Amounts,
Amounts: lowestFunding.Amounts,
})

return nil
Expand Down
11 changes: 6 additions & 5 deletions x/funders/keeper/msg_server_defund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) (
return nil, errors.Wrapf(errorsTypes.ErrNotFound, types.ErrFundingDoesNotExist.Error(), msg.PoolId, msg.Creator)
}

if funding.Amount == 0 {
if !msg.Amounts.IsAllLTE(funding.Amounts) {
return nil, errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrFundingIsUsedUp.Error(), msg.PoolId, msg.Creator)
}

Expand All @@ -35,8 +35,8 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) (
}

// Subtract amount from funding
amount := funding.SubtractAmount(msg.Amount)
if funding.Amount == 0 {
funding.Amounts.Sub(msg.Amounts...)
if funding.Amounts.IsZero() {
fundingState.SetInactive(&funding)
} else {
// If funding is not fully revoked, check if updated funding is still compatible with params.
Expand All @@ -46,7 +46,8 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) (
}

// Transfer tokens from this module to sender.
if err := util.TransferFromModuleToAddress(k.bankKeeper, ctx, types.ModuleName, msg.Creator, amount); err != nil {
recipient := sdk.MustAccAddressFromBech32(msg.Creator)
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, recipient, msg.Amounts); err != nil {
return nil, err
}

Expand All @@ -58,7 +59,7 @@ func (k msgServer) DefundPool(goCtx context.Context, msg *types.MsgDefundPool) (
_ = ctx.EventManager().EmitTypedEvent(&types.EventDefundPool{
PoolId: msg.PoolId,
Address: msg.Creator,
Amount: amount,
Amounts: msg.Amounts,
})

return &types.MsgDefundPoolResponse{}, nil
Expand Down
4 changes: 2 additions & 2 deletions x/funders/keeper/msg_server_fund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"cosmossdk.io/errors"
"github.com/KYVENetwork/chain/util"
"github.com/KYVENetwork/chain/x/funders/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errorsTypes "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -86,7 +85,8 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ
}

// All checks passed, transfer funds from funder to module
if err := util.TransferFromAddressToModule(k.bankKeeper, ctx, msg.Creator, types.ModuleName, msg.Amount); err != nil {
sender := sdk.MustAccAddressFromBech32(msg.Creator)
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.NewCoins(msg.Amount)); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 75d80f0

Please sign in to comment.