Skip to content

Commit

Permalink
chore: finished logic with funding a single coin
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Apr 29, 2024
1 parent 75d80f0 commit bd272c9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
46 changes: 27 additions & 19 deletions x/funders/keeper/logic_funders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"cosmossdk.io/errors"
"cosmossdk.io/math"
"fmt"

"github.com/KYVENetwork/chain/x/funders/types"
Expand All @@ -25,7 +26,7 @@ func (k Keeper) GetTotalActiveFunding(ctx sdk.Context, poolId uint64) (amounts s
}
for _, address := range state.ActiveFunderAddresses {
funding, _ := k.GetFunding(ctx, address, poolId)
amounts.Add(funding.Amounts...)
amounts = amounts.Add(funding.Amounts...)
}
return
}
Expand All @@ -50,7 +51,7 @@ func (k Keeper) ChargeFundersOfPool(ctx sdk.Context, poolId uint64) (payouts sdk

// This is the amount every funding will be charged
for _, funding := range activeFundings {
payouts.Add(funding.ChargeOneBundle()...)
payouts = payouts.Add(funding.ChargeOneBundle()...)
if funding.Amounts.IsZero() {
fundingState.SetInactive(&funding)
}
Expand Down Expand Up @@ -99,33 +100,40 @@ func (k Keeper) GetLowestFunding(fundings []types.Funding, whitelist []*types.Wh
// - minimum funding per bundle
// - minimum funding amount
// - minimum funding multiple
func (k Keeper) ensureParamsCompatibility(ctx sdk.Context, msg *types.MsgFundPool) error {
func (k Keeper) ensureParamsCompatibility(ctx sdk.Context, funding types.Funding) error {
params := k.GetParams(ctx)

var w *types.WhitelistCoinEntry
minFundingAmounts := sdk.NewCoins()
minFundingAmountsPerBundle := sdk.NewCoins()

for _, entry := range params.CoinWhitelist {
if entry.CoinDenom == msg.Amount.Denom {
w = entry
break
}
minFundingAmounts = minFundingAmounts.Add(sdk.NewInt64Coin(entry.CoinDenom, int64(entry.MinFundingAmount)))
minFundingAmountsPerBundle = minFundingAmountsPerBundle.Add(sdk.NewInt64Coin(entry.CoinDenom, int64(entry.MinFundingAmountPerBundle)))
}

// throw error if a coin in amounts is not in the whitelist
if !funding.Amounts.DenomsSubsetOf(minFundingAmounts) {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrCoinNotWhitelisted.Error())
}

// throw error if coin is not in whitelist. we only check msg.amount here since we know from before
// that msg.amount and msg.amount_per_bundle is equal
if w == nil {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrCoinNotWhitelisted.Error(), msg.Amount.Denom)
// throw error if a coin in amounts per bundle is not in the whitelist
if !funding.AmountsPerBundle.DenomsSubsetOf(minFundingAmountsPerBundle) {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrCoinNotWhitelisted.Error())
}

if msg.Amount.Amount.Uint64() < w.MinFundingAmount {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingAmount.Error(), w.MinFundingAmount, msg.Amount.Denom)
// throw error if a coin is less than the minimum funding amount
if minFundingAmounts.IsAnyGT(funding.Amounts) {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingAmount.Error())
}

if msg.AmountPerBundle.Amount.Uint64() < w.MinFundingAmountPerBundle {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinAmountPerBundle.Error(), w.MinFundingAmountPerBundle, msg.Amount.Denom)
// throw error if a coin is less than the minimum funding amount per bundle
if minFundingAmountsPerBundle.IsAnyGT(funding.AmountsPerBundle) {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinAmountPerBundle.Error())
}

if msg.AmountPerBundle.Amount.Uint64()*params.MinFundingMultiple > msg.Amount.Amount.Uint64() {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingMultiple.Error(), msg.AmountPerBundle, params.MinFundingMultiple, msg.Amount)
// throw error if a coin can not fulfill the funding multiple threshold
if funding.AmountsPerBundle.MulInt(math.NewInt(int64(params.MinFundingMultiple))).IsAnyGT(funding.Amounts) {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingMultiple.Error())
}

return nil
Expand Down Expand Up @@ -170,7 +178,7 @@ func (k Keeper) ensureFreeSlot(ctx sdk.Context, newFunding *types.Funding, fundi
return err
}

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

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 @@ -53,7 +53,7 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ
funding, found := k.GetFunding(ctx, msg.Creator, msg.PoolId)
if found {
// If so, update funding amounts
funding.Amounts.Add(msg.Amount)
funding.Amounts = funding.Amounts.Add(msg.Amount)

// If the amount per bundle is set, update it
if msg.AmountPerBundle.IsPositive() {
Expand All @@ -73,7 +73,7 @@ func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*typ
}

// Check if updated (or new) funding is compatible with module params
if err := k.ensureParamsCompatibility(ctx, msg); err != nil {
if err := k.ensureParamsCompatibility(ctx, funding); err != nil {
return nil, err
}

Expand Down
12 changes: 5 additions & 7 deletions x/funders/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ var (
ErrFunderAlreadyExists = errors.Register(ModuleName, 1100, "funder with address %v already exists")
ErrFunderDoesNotExist = errors.Register(ModuleName, 1101, "funder with address %v does not exist")
ErrFundsTooLow = errors.Register(ModuleName, 1102, "minimum funding amount of %vkyve not reached")
ErrMinAmountPerBundle = errors.Register(ModuleName, 1103, "minimum amount per bundle of %d %s not reached")
ErrMinFundingAmount = errors.Register(ModuleName, 1104, "minimum funding amount %d %s not reached")
ErrMinAmountPerBundle = errors.Register(ModuleName, 1103, "minimum amount per bundle of coin not reached")
ErrMinFundingAmount = errors.Register(ModuleName, 1104, "minimum funding amount of coin not reached")
ErrFundingDoesNotExist = errors.Register(ModuleName, 1105, "funding for pool %v and funder %v does not exist")
ErrFundingIsUsedUp = errors.Register(ModuleName, 1106, "funding for pool %v and funder %v is used up")
ErrFundingStateDoesNotExist = errors.Register(ModuleName, 1107, "funding state for pool %v does not exist")
ErrMinFundingMultiple = errors.Register(ModuleName, 1108, "per_bundle_amount (%dkyve) times min_funding_multiple (%d) is smaller than funded_amount (%vkyve)")
ErrInvalidAmountLength = errors.Register(ModuleName, 1109, "funding amounts has length %d while amounts_per_bundle has length %d")
ErrAmountsPerBundleNoSubset = errors.Register(ModuleName, 1110, "amounts_per_bundle is no subset of amounts")
ErrCoinNotWhitelisted = errors.Register(ModuleName, 1111, "coin of denom %s not in whitelist")
ErrDifferentDenom = errors.Register(ModuleName, 1111, "found denom %s in amount and denom %s in amount_per_bundle")
ErrMinFundingMultiple = errors.Register(ModuleName, 1108, "per_bundle_amount times min_funding_multiple is smaller than funded_amount")
ErrCoinNotWhitelisted = errors.Register(ModuleName, 1109, "coin not in whitelist")
ErrDifferentDenom = errors.Register(ModuleName, 1110, "found denom %s in amount and denom %s in amount_per_bundle")
)
11 changes: 7 additions & 4 deletions x/funders/types/funders.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ func (f *Funding) GetScore(whitelist []*WhitelistCoinEntry) (score uint64) {
return
}

func (f *Funding) ChargeOneBundle() (amounts sdk.Coins) {
funded := f.Amounts.Sub(f.AmountsPerBundle...)
amounts.Add(funded...)
f.TotalFunded.Add(funded...)
func (f *Funding) ChargeOneBundle() (payouts sdk.Coins) {
amounts := f.Amounts
f.Amounts = f.Amounts.Sub(f.AmountsPerBundle...)
// we add the diff here in case amount per bundle is greater than amount
diff := amounts.Sub(f.Amounts...)
payouts.Add(diff...)
f.TotalFunded.Add(diff...)
return
}

Expand Down

0 comments on commit bd272c9

Please sign in to comment.