Skip to content

Commit

Permalink
test: refactored fund_pool_test for coins
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Apr 29, 2024
1 parent a5dc9dc commit 89b071a
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 224 deletions.
21 changes: 11 additions & 10 deletions testutil/integration/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"time"

"cosmossdk.io/store"
Expand Down Expand Up @@ -452,7 +453,7 @@ func (suite *KeeperTestSuite) VerifyFundersModuleIntegrity() {
Expect(found).To(BeTrue())

// check if funding is active
if funding.Amount > 0 {
if !funding.Amounts.IsZero() {
key := string(funderstypes.FundingKeyByFunder(funding.FunderAddress, funding.PoolId))
allActiveFundings[key] = true
}
Expand Down Expand Up @@ -485,28 +486,28 @@ func (suite *KeeperTestSuite) VerifyFundersModuleIntegrity() {
}

func (suite *KeeperTestSuite) VerifyFundersModuleAssetsIntegrity() {
expectedBalance := uint64(0)
expectedBalance := sdk.NewCoins()
for _, funding := range suite.App().FundersKeeper.GetAllFundings(suite.Ctx()) {
expectedBalance += funding.Amount
expectedBalance = expectedBalance.Add(funding.Amounts...)
}

expectedFundingStateTotalAmount := uint64(0)
expectedFundingStateTotalAmount := sdk.NewCoins()
for _, fundingState := range suite.App().FundersKeeper.GetAllFundingStates(suite.Ctx()) {
activeFundings := suite.App().FundersKeeper.GetActiveFundings(suite.Ctx(), fundingState)
totalAmount := uint64(0)
totalAmount := sdk.NewCoins()
for _, activeFunding := range activeFundings {
totalAmount += activeFunding.Amount
totalAmount = totalAmount.Add(activeFunding.Amounts...)
}
totalActiveFunding := suite.App().FundersKeeper.GetTotalActiveFunding(suite.ctx, fundingState.PoolId)
Expect(totalAmount).To(Equal(totalActiveFunding))
expectedFundingStateTotalAmount += totalAmount
expectedFundingStateTotalAmount = expectedFundingStateTotalAmount.Add(totalAmount...)
}

// total amount of fundings should be equal to the amount of the funders module account
moduleAcc := suite.App().AccountKeeper.GetModuleAccount(suite.Ctx(), funderstypes.ModuleName).GetAddress()
actualBalance := suite.App().BankKeeper.GetBalance(suite.Ctx(), moduleAcc, globalTypes.Denom).Amount.Uint64()
Expect(actualBalance).To(Equal(expectedBalance))
Expect(actualBalance).To(Equal(expectedFundingStateTotalAmount))
actualBalance := suite.App().BankKeeper.GetAllBalances(suite.Ctx(), moduleAcc)
Expect(actualBalance.String()).To(Equal(expectedBalance.String()))
Expect(actualBalance.String()).To(Equal(expectedFundingStateTotalAmount.String()))
}

// ========================
Expand Down
9 changes: 9 additions & 0 deletions testutil/integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func (suite *KeeperTestSuite) GetBalanceFromAddress(address string) uint64 {
return uint64(balance.Amount.Int64())
}

func (suite *KeeperTestSuite) GetBalancesFromAddress(address string) sdk.Coins {
accAddress, err := sdk.AccAddressFromBech32(address)
if err != nil {
return sdk.NewCoins()
}

return suite.App().BankKeeper.GetAllBalances(suite.Ctx(), accAddress)
}

func (suite *KeeperTestSuite) GetBalanceFromPool(poolId uint64) uint64 {
pool, found := suite.App().PoolKeeper.GetPool(suite.Ctx(), poolId)
if !found {
Expand Down
153 changes: 85 additions & 68 deletions x/funders/keeper/logic_funders_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"cosmossdk.io/math"
i "github.com/KYVENetwork/chain/testutil/integration"
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
globaltypes "github.com/KYVENetwork/chain/x/global/types"
Expand Down Expand Up @@ -71,14 +72,14 @@ var _ = Describe("logic_funders.go", Ordered, func() {
s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.ALICE,
PoolId: 0,
Amounts: sdk.NewCoins(sdk.NewInt64Coin(i.A_DENOM, 100*i.T_KYVE)),
AmountsPerBundle: sdk.NewCoins(sdk.NewInt64Coin(i.A_DENOM, 1*i.T_KYVE)),
Amounts: i.ACoins(100 * i.T_KYVE),
AmountsPerBundle: i.ACoins(1 * i.T_KYVE),
})
s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.BOB,
PoolId: 0,
Amounts: sdk.NewCoins(sdk.NewInt64Coin(i.A_DENOM, 50*i.T_KYVE)),
AmountsPerBundle: sdk.NewCoins(sdk.NewInt64Coin(i.A_DENOM, 10*i.T_KYVE)),
Amounts: i.ACoins(50 * i.T_KYVE),
AmountsPerBundle: i.ACoins(10 * i.T_KYVE),
})

fundersBalance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAcc, globaltypes.Denom).Amount.Uint64()
Expand Down Expand Up @@ -123,14 +124,14 @@ var _ = Describe("logic_funders.go", Ordered, func() {
s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.ALICE,
PoolId: 0,
Amounts: sdk.NewCoins(sdk.NewInt64Coin(i.B_DENOM, 1000*i.T_KYVE)),
AmountsPerBundle: sdk.NewCoins(sdk.NewInt64Coin(i.B_DENOM, 20*i.T_KYVE)),
Amounts: i.ACoins(1000 * i.T_KYVE),
AmountsPerBundle: i.ACoins(20 * i.T_KYVE),
})
s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.BOB,
PoolId: 0,
Amounts: sdk.NewCoins(sdk.NewInt64Coin(i.C_DENOM, 100*i.T_KYVE)),
AmountsPerBundle: sdk.NewCoins(sdk.NewInt64Coin(i.C_DENOM, 2*i.T_KYVE)),
Amounts: i.ACoins(100 * i.T_KYVE),
AmountsPerBundle: i.ACoins(2 * i.T_KYVE),
})

// ACT
Expand Down Expand Up @@ -176,24 +177,24 @@ var _ = Describe("logic_funders.go", Ordered, func() {

fundingAlice, foundAlice := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
Expect(foundAlice).To(BeTrue())
Expect(fundingAlice.Amount).To(Equal(95 * i.KYVE))
Expect(fundingAlice.TotalFunded).To(Equal(5 * i.KYVE))
Expect(fundingAlice.Amounts.String()).To(Equal(i.ACoins(95 * i.T_KYVE).String()))
Expect(fundingAlice.TotalFunded.String()).To(Equal(i.ACoins(5 * i.T_KYVE).String()))

fundingBob, foundBob := s.App().FundersKeeper.GetFunding(s.Ctx(), i.BOB, 0)
Expect(foundBob).To(BeTrue())
Expect(fundingBob.Amount).To(Equal(0 * i.KYVE))
Expect(fundingBob.TotalFunded).To(Equal(50 * i.KYVE))
Expect(fundingBob.Amounts.IsZero()).To(BeTrue())
Expect(fundingBob.TotalFunded.String()).To(Equal(i.ACoins(50 * i.T_KYVE).String()))

fundersBalance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAcc, globaltypes.Denom).Amount.Uint64()
poolBalance := s.App().BankKeeper.GetBalance(s.Ctx(), poolModuleAcc, globaltypes.Denom).Amount.Uint64()
Expect(fundersBalance).To(Equal(95 * i.KYVE))
Expect(poolBalance).To(Equal(55 * i.KYVE))
fundersBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), fundersModuleAcc)
poolBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), poolModuleAcc)
Expect(fundersBalance.String()).To(Equal(i.ACoins(95 * i.T_KYVE).String()))
Expect(poolBalance.String()).To(Equal(i.ACoins(55 * i.T_KYVE).String()))
})

It("Charge funders until all funders run out of funds", func() {
// ARRANGE
funding, _ := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
funding.AmountPerBundle = 10 * i.KYVE
funding.AmountsPerBundle = i.ACoins(10 * i.T_KYVE)
s.App().FundersKeeper.SetFunding(s.Ctx(), &funding)

// ACT / ASSERT
Expand All @@ -203,66 +204,66 @@ var _ = Describe("logic_funders.go", Ordered, func() {

payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)
Expect(err).NotTo(HaveOccurred())
Expect(payout).To(Equal(20 * i.KYVE))
Expect(payout.String()).To(Equal(i.ACoins(10 * i.T_KYVE).String()))
}
fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(1))
Expect(fundingState.ActiveFunderAddresses[0]).To(Equal(i.ALICE))

fundingAlice, foundAlice := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
Expect(foundAlice).To(BeTrue())
Expect(fundingAlice.Amount).To(Equal(50 * i.KYVE))
Expect(fundingAlice.TotalFunded).To(Equal(50 * i.KYVE))
Expect(fundingAlice.Amounts.String()).To(Equal(i.ACoins(50 * i.T_KYVE).String()))
Expect(fundingAlice.TotalFunded.String()).To(Equal(i.ACoins(50 * i.T_KYVE).String()))

fundingBob, foundBob := s.App().FundersKeeper.GetFunding(s.Ctx(), i.BOB, 0)
Expect(foundBob).To(BeTrue())
Expect(fundingBob.Amount).To(Equal(0 * i.KYVE))
Expect(fundingBob.TotalFunded).To(Equal(50 * i.KYVE))
Expect(fundingBob.Amounts.IsZero()).To(BeTrue())
Expect(fundingBob.TotalFunded.String()).To(Equal(i.ACoins(50 * i.T_KYVE).String()))

for range [5]struct{}{} {
fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(1))

payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)
Expect(err).NotTo(HaveOccurred())
Expect(payout).To(Equal(10 * i.KYVE))
Expect(payout.String()).To(Equal(i.ACoins(10 * i.T_KYVE)))
}
fundingState, _ = s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(0))

fundingAlice, foundAlice = s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
Expect(foundAlice).To(BeTrue())
Expect(fundingAlice.Amount).To(Equal(0 * i.KYVE))
Expect(fundingAlice.TotalFunded).To(Equal(100 * i.KYVE))
Expect(fundingAlice.Amounts.IsZero()).To(Equal(BeTrue()))
Expect(fundingAlice.TotalFunded.String()).To(Equal(i.ACoins(100 * i.T_KYVE).String()))

fundingBob, foundBob = s.App().FundersKeeper.GetFunding(s.Ctx(), i.BOB, 0)
Expect(foundBob).To(BeTrue())
Expect(fundingBob.Amount).To(Equal(0 * i.KYVE))
Expect(fundingBob.TotalFunded).To(Equal(50 * i.KYVE))
Expect(fundingBob.Amounts.IsZero()).To(Equal(BeTrue()))
Expect(fundingBob.TotalFunded.String()).To(Equal(i.ACoins(50 * i.T_KYVE).String()))

payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)
Expect(err).NotTo(HaveOccurred())
Expect(payout).To(Equal(0 * i.KYVE))
Expect(payout.IsZero()).To(Equal(BeTrue()))

fundingState, _ = s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(0))

fundersBalance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAcc, globaltypes.Denom).Amount.Uint64()
poolBalance := s.App().BankKeeper.GetBalance(s.Ctx(), poolModuleAcc, globaltypes.Denom).Amount.Uint64()
Expect(fundersBalance).To(Equal(0 * i.KYVE))
Expect(poolBalance).To(Equal(150 * i.KYVE))
fundersBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), fundersModuleAcc)
poolBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), poolModuleAcc)
Expect(fundersBalance.IsZero()).To(Equal(BeTrue()))
Expect(poolBalance.String()).To(Equal(i.ACoins(150 * i.T_KYVE).String()))
})

It("Charge funder with less funds than amount_per_bundle", func() {
// ARRANGE
funding, _ := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
funding.AmountPerBundle = 105 * i.KYVE
funding.AmountsPerBundle = i.ACoins(105 * i.T_KYVE)
s.App().FundersKeeper.SetFunding(s.Ctx(), &funding)

// ACT
payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)
Expect(err).NotTo(HaveOccurred())
Expect(payout).To(Equal(110 * i.KYVE))
Expect(payout.String()).To(Equal(i.ACoins(110 * i.T_KYVE).String()))

// ASSERT
fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expand All @@ -271,82 +272,98 @@ var _ = Describe("logic_funders.go", Ordered, func() {

fundingAlice, foundAlice := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
Expect(foundAlice).To(BeTrue())
Expect(fundingAlice.Amount).To(Equal(0 * i.KYVE))
Expect(fundingAlice.TotalFunded).To(Equal(100 * i.KYVE))
Expect(fundingAlice.Amounts.IsZero()).To(Equal(BeTrue()))
Expect(fundingAlice.TotalFunded.String()).To(Equal(i.ACoins(100 * i.T_KYVE).String()))

fundingBob, foundBob := s.App().FundersKeeper.GetFunding(s.Ctx(), i.BOB, 0)
Expect(foundBob).To(BeTrue())
Expect(fundingBob.Amount).To(Equal(40 * i.KYVE))
Expect(fundingBob.TotalFunded).To(Equal(10 * i.KYVE))
Expect(fundingBob.Amounts.String()).To(Equal(i.ACoins(40 * i.T_KYVE).String()))
Expect(fundingBob.TotalFunded.String()).To(Equal(i.ACoins(10 * i.T_KYVE).String()))

fundersBalance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAcc, globaltypes.Denom).Amount.Uint64()
poolBalance := s.App().BankKeeper.GetBalance(s.Ctx(), poolModuleAcc, globaltypes.Denom).Amount.Uint64()
Expect(fundersBalance).To(Equal(40 * i.KYVE))
Expect(poolBalance).To(Equal(110 * i.KYVE))
fundersBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), fundersModuleAcc)
poolBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), poolModuleAcc)
Expect(fundersBalance.String()).To(Equal(i.ACoins(40 * i.T_KYVE).String()))
Expect(poolBalance.String()).To(Equal(i.ACoins(110 * i.T_KYVE).String()))
})

It("Charge without fundings", func() {
// ARRANGE
s.RunTxFundersSuccess(&funderstypes.MsgDefundPool{
Creator: i.ALICE,
PoolId: 0,
Amount: 100 * i.KYVE,
Amounts: i.ACoins(100 * i.T_KYVE),
})
s.RunTxFundersSuccess(&funderstypes.MsgDefundPool{
Creator: i.BOB,
PoolId: 0,
Amount: 50 * i.KYVE,
Amounts: i.ACoins(50 * i.T_KYVE),
})

// ACT
payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)

// ASSERT
Expect(err).NotTo(HaveOccurred())
Expect(payout.IsZero()).To(Equal(BeTrue()))

fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(0))

fundingAlice, foundAlice := s.App().FundersKeeper.GetFunding(s.Ctx(), i.ALICE, 0)
Expect(foundAlice).To(BeTrue())
Expect(fundingAlice.Amount).To(Equal(0 * i.KYVE))
Expect(fundingAlice.TotalFunded).To(Equal(0 * i.KYVE))
Expect(fundingAlice.Amounts.IsZero()).To(BeTrue())
Expect(fundingAlice.TotalFunded.IsZero()).To(BeTrue())

fundingBob, foundBob := s.App().FundersKeeper.GetFunding(s.Ctx(), i.BOB, 0)
Expect(foundBob).To(BeTrue())
Expect(fundingBob.Amount).To(Equal(0 * i.KYVE))
Expect(fundingBob.TotalFunded).To(Equal(0 * i.KYVE))
Expect(fundingBob.Amounts.IsZero()).To(BeTrue())
Expect(fundingBob.TotalFunded.IsZero()).To(BeTrue())

Expect(err).NotTo(HaveOccurred())
Expect(payout).To(Equal(0 * i.KYVE))
fundersBalance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAcc, globaltypes.Denom).Amount.Uint64()
poolBalance := s.App().BankKeeper.GetBalance(s.Ctx(), poolModuleAcc, globaltypes.Denom).Amount.Uint64()
Expect(fundersBalance).To(Equal(0 * i.KYVE))
Expect(poolBalance).To(Equal(0 * i.KYVE))
fundersBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), fundersModuleAcc)
poolBalance := s.App().BankKeeper.GetAllBalances(s.Ctx(), poolModuleAcc)
Expect(fundersBalance.IsZero()).To(BeTrue())
Expect(poolBalance.IsZero()).To(BeTrue())
})

It("Check if the lowest funding is returned correctly", func() {
whitelist := []*funderstypes.WhitelistCoinEntry{
{
CoinDenom: i.A_DENOM,
CoinWeight: math.LegacyNewDec(1),
},
{
CoinDenom: i.B_DENOM,
CoinWeight: math.LegacyNewDec(2),
},
{
CoinDenom: i.C_DENOM,
CoinWeight: math.LegacyNewDec(3),
},
}

fundings := []funderstypes.Funding{
{
FunderAddress: i.DUMMY[0],
PoolId: 0,
Amount: 1000 * i.KYVE,
AmountPerBundle: 1 * i.KYVE,
FunderAddress: i.DUMMY[0],
PoolId: 0,
Amounts: sdk.NewCoins(i.ACoin(1000*i.T_KYVE), i.BCoin(500*i.T_KYVE), i.CCoin(100)),
AmountsPerBundle: sdk.NewCoins(i.ACoin(1*i.T_KYVE), i.BCoin(1*i.T_KYVE), i.CCoin(1)),
},
{
FunderAddress: i.DUMMY[1],
PoolId: 0,
Amount: 900 * i.KYVE,
AmountPerBundle: 1 * i.KYVE,
FunderAddress: i.DUMMY[1],
PoolId: 0,
Amounts: sdk.NewCoins(i.ACoin(1100*i.T_KYVE), i.BCoin(600*i.T_KYVE), i.CCoin(5)),
AmountsPerBundle: sdk.NewCoins(i.ACoin(1*i.T_KYVE), i.BCoin(1*i.T_KYVE), i.CCoin(1)),
},
{
FunderAddress: i.DUMMY[2],
PoolId: 0,
Amount: 1100 * i.KYVE,
AmountPerBundle: 1 * i.KYVE,
FunderAddress: i.DUMMY[2],
PoolId: 0,
Amounts: sdk.NewCoins(i.ACoin(500*i.T_KYVE), i.CCoin(700)),
AmountsPerBundle: sdk.NewCoins(i.ACoin(1*i.T_KYVE), i.CCoin(1)),
},
}

getLowestFunding, err := s.App().FundersKeeper.GetLowestFunding(fundings)
getLowestFunding, err := s.App().FundersKeeper.GetLowestFunding(fundings, whitelist)
Expect(err).NotTo(HaveOccurred())
Expect(getLowestFunding.Amount).To(Equal(900 * i.KYVE))
Expect(getLowestFunding.FunderAddress).To(Equal(i.DUMMY[1]))
})
})
Loading

0 comments on commit 89b071a

Please sign in to comment.