Skip to content

Commit

Permalink
added all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stackman27 committed Aug 16, 2023
1 parent 74759bb commit 18c5cd3
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 22 deletions.
86 changes: 69 additions & 17 deletions x/incentives/keeper/distribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,40 @@ func (s *KeeperTestSuite) IncentivizeInternalGauge(poolIds []uint64, epochDurati
)
s.Require().NoError(err)
}
func (s *KeeperTestSuite) TestAllocateAcrossGauges() {
s.SetupTest()
expectedInternalGaugeCoins := sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(33_333_333)))
s.FundAcc(s.TestAccs[1], sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))) // 1,000 osmo
clPool := s.PrepareConcentratedPool() // gaugeid = 1

// create 3 internal Gauge
var internalGauges []uint64
for i := 0; i <= 2; i++ {
internalGauge := s.CreateNoLockExternalGauges(clPool.GetId(), sdk.NewCoins(), s.TestAccs[1], uint64(1)) // gauge id = 2,3,4
internalGauges = append(internalGauges, internalGauge)
}

// create group gauge
groupGauge, err := s.App.IncentivesKeeper.CreateGroupGauge(s.Ctx, sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000))), uint64(1), s.TestAccs[1], internalGauges) // gauge id = 3
s.Require().NoError(err)

// Call AllocateAcrossGauges
// Allocate 100 osmo across 3 internal gauges evenly
s.App.IncentivesKeeper.AllocateAcrossGauges(s.Ctx)

groupGaugePostAllocate, err := s.App.IncentivesKeeper.GetGaugeByID(s.Ctx, groupGauge)
s.Require().NoError(err)

s.Require().Equal(groupGaugePostAllocate.Coins.Sub(groupGaugePostAllocate.DistributedCoins), sdk.Coins(nil))

for _, gauge := range internalGauges {
internalGauge, err := s.App.IncentivesKeeper.GetGaugeByID(s.Ctx, gauge)
s.Require().NoError(err)

s.Require().Equal(internalGauge.Coins.Sub(internalGauge.DistributedCoins), expectedInternalGaugeCoins)
}

}

/*
Test Structure
Expand Down Expand Up @@ -1159,6 +1193,7 @@ Let epoch 2 pass, we expect:
decrement GroupGauge coins from 66_666_667 - 33_333_333 = 33_333_334
Hence, total 5 incentiveRecord for CL pools
Step 5:
Let epoch 3 pass, we expect:
internalGauge1 to recieve 11_111_111uosmo, internalGauge2 to recieve 11_111_111uosmo, internalGauge3 to recieve 11_111_111uosmo
Expand All @@ -1183,7 +1218,6 @@ func (s *KeeperTestSuite) TestCreateGroupGaugeAndDistribute() {

groupGaugeCoinAfterEpoch1 = sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(66_666_667)))
groupGaugeCoinAfterEpoch2 = sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(33_333_334)))
// groupGaugeCoinAfterEpoch3 = sdk.Coins{}
)

s.FundAcc(s.TestAccs[1], sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(1_000_000_000)))) // 1,000 osmo
Expand Down Expand Up @@ -1231,18 +1265,21 @@ func (s *KeeperTestSuite) TestCreateGroupGaugeAndDistribute() {

s.ValidateDistributedGauge(internalGauge1, 1, expectedCoinsDistributed)
s.ValidateDistributedGauge(internalGauge2, 1, expectedCoinsDistributed)
//s.ValidateDistributedGauge(internalGauge3, 0, expectedCoinsDistributed) // this needs to be added with actual denom, but i've made sure the coin is in gauge

// Validate that we created incentive record after distributing gaugeId 2,3,5
clPoolIncentiveRecordsAtEpoch1, err := s.App.ConcentratedLiquidityKeeper.GetAllIncentiveRecordsForPool(s.Ctx, clPool.GetId())
s.Require().NoError(err)

s.Require().Equal(3, len(clPoolIncentiveRecordsAtEpoch1))

s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch1[0])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch1[1])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch1[2])
for i := 0; i < len(clPoolIncentiveRecordsAtEpoch1); i++ {
if i == 2 {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch1[i])
} else {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch1[i])
}

}
// Step 4
//let epoch 2 pass
// ******************** EPOCH 2 *********************
Expand All @@ -1264,11 +1301,14 @@ func (s *KeeperTestSuite) TestCreateGroupGaugeAndDistribute() {

s.Require().Equal(5, len(clPoolIncentiveRecordsAtEpoch2))

s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch2[0])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch2[1])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch2[2])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch2[3])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch2[4])
for i := 0; i < len(clPoolIncentiveRecordsAtEpoch2); i++ {
if i == 2 {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch2[i])
} else {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch2[i])
}

}

// Step 4
//let epoch 3 pass
Expand All @@ -1291,12 +1331,24 @@ func (s *KeeperTestSuite) TestCreateGroupGaugeAndDistribute() {

s.Require().Equal(7, len(clPoolIncentiveRecordsAtEpoch3))

s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[0])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[1])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch3[2])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[3])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[4])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[5])
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[6])
for i := 0; i < len(clPoolIncentiveRecordsAtEpoch3); i++ {
if i == 2 {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedRegularGauge[0], newEmissionRateForClPool, clPoolIncentiveRecordsAtEpoch3[i])
} else {
s.ValidateIncentiveRecord(clPool.GetId(), expectedCoinsDistributedDecCoins[0], emissionRateForPoolClPool, clPoolIncentiveRecordsAtEpoch3[i])
}

}

// Step 5
// let epoch 4 pass
// There should not be any distribution since all the tokens have been distributed
// ******************** EPOCH 4 *********************
s.Ctx = s.Ctx.WithBlockTime(s.Ctx.BlockTime().Add(epochInfo.Duration))
s.App.EpochsKeeper.AfterEpochEnd(s.Ctx, epochInfo.GetIdentifier(), 4)

clPoolIncentiveRecordsAtEpoch4, err := s.App.ConcentratedLiquidityKeeper.GetAllIncentiveRecordsForPool(s.Ctx, clPool.GetId())
s.Require().NoError(err)

s.Require().Equal(7, len(clPoolIncentiveRecordsAtEpoch4))
}
11 changes: 7 additions & 4 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func (k Keeper) CreateGroupGauge(ctx sdk.Context, coins sdk.Coins, numEpochPaidO
isPerp = true
}

// check that all the internalGaugeIds exist
if _, err := k.GetGaugeFromIDs(ctx, internalGaugeIds); err != nil {
return 0, fmt.Errorf("Invalid internalGaugeIds, please make sure all the internalGauge have been created.")
}

nextGaugeId := k.GetLastGaugeID(ctx) + 1

gauge := types.Gauge{
Expand All @@ -234,8 +239,7 @@ func (k Keeper) CreateGroupGauge(ctx sdk.Context, coins sdk.Coins, numEpochPaidO
return 0, err
}

err := k.setGauge(ctx, &gauge)
if err != nil {
if err := k.setGauge(ctx, &gauge); err != nil {
return 0, err
}

Expand All @@ -250,8 +254,7 @@ func (k Keeper) CreateGroupGauge(ctx sdk.Context, coins sdk.Coins, numEpochPaidO
combinedKeys := combineKeys(types.KeyPrefixUpcomingGauges, getTimeKey(gauge.StartTime))
activeOrUpcomingGauge := true

err = k.CreateGaugeRefKeys(ctx, &gauge, combinedKeys, activeOrUpcomingGauge)
if err != nil {
if err := k.CreateGaugeRefKeys(ctx, &gauge, combinedKeys, activeOrUpcomingGauge); err != nil {
return 0, err
}
k.hooks.AfterCreateGauge(ctx, gauge.Id)
Expand Down
140 changes: 140 additions & 0 deletions x/incentives/keeper/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,143 @@ func (s *KeeperTestSuite) TestCreateGauge_NoLockGauges() {
})
}
}

func (s *KeeperTestSuite) TestCreateGroupGauge() {
coinsToAdd := sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))
tests := []struct {
name string
coins sdk.Coins
numEpochPaidOver uint64
internalGaugeIds []uint64
expectErr bool
}{
{
name: "Happy case: created valid gauge",
coins: coinsToAdd,
numEpochPaidOver: 1,
internalGaugeIds: []uint64{2, 3, 4},
expectErr: false,
},

{
name: "Error: Invalid InternalGauge Id",
coins: coinsToAdd,
numEpochPaidOver: 1,
internalGaugeIds: []uint64{2, 3, 4, 5},
expectErr: true,
},
{
name: "Error: owner doesnot have enough funds",
coins: sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(200_000_000))),
numEpochPaidOver: 1,
internalGaugeIds: []uint64{2, 3, 4},
expectErr: true,
},
}

for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
s.FundAcc(s.TestAccs[1], sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))) // 1,000 osmo
clPool := s.PrepareConcentratedPool() // gaugeid = 1

// create 3 internal Gauge
for i := 0; i <= 2; i++ {
s.CreateNoLockExternalGauges(clPool.GetId(), sdk.NewCoins(), s.TestAccs[1], uint64(1)) // gauge id = 2,3,4
}

groupGaugeId, err := s.App.IncentivesKeeper.CreateGroupGauge(s.Ctx, tc.coins, tc.numEpochPaidOver, s.TestAccs[1], tc.internalGaugeIds) // gauge id = 5
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)

// check that the gauge has been create with right value
groupGauge, err := s.App.IncentivesKeeper.GetGaugeByID(s.Ctx, groupGaugeId)
s.Require().NoError(err)

s.Require().Equal(groupGauge.Coins, tc.coins)
s.Require().Equal(groupGauge.NumEpochsPaidOver, tc.numEpochPaidOver)
s.Require().Equal(groupGauge.IsPerpetual, true)
s.Require().Equal(groupGauge.DistributeTo.LockQueryType, lockuptypes.ByGroup)

// check that GroupGauge has been added to state
groupGaugeObj, err := s.App.IncentivesKeeper.GetGroupGaugeById(s.Ctx, groupGaugeId)
s.Require().NoError(err)

s.Require().Equal(groupGaugeObj.InternalIds, tc.internalGaugeIds)
}

})
}
}

func (s *KeeperTestSuite) TestAddToGaugeRewardsFromGauge() {
coinsToTransfer := sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))
tests := []struct {
name string
groupGaugeId uint64
internalGaugeId uint64
coinsToTransfer sdk.Coins
expectErr bool
}{
{
name: "Happy case: Valid gaugeId with valid Internal GaugeId",
groupGaugeId: 3,
internalGaugeId: 2,
coinsToTransfer: coinsToTransfer,
expectErr: false,
},
{
name: "Error: InternalGauge is not present in groupGauge",
groupGaugeId: 3,
internalGaugeId: 1,
coinsToTransfer: coinsToTransfer,
expectErr: true,
},
{
name: "Error: Not enough tokens to transfer",
groupGaugeId: 3,
internalGaugeId: 2,
coinsToTransfer: sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(200_000_000))),
expectErr: true,
},
{
name: "Error: GroupGaugeId doesnot exist",
groupGaugeId: 5,
internalGaugeId: 2,
coinsToTransfer: coinsToTransfer,
expectErr: true,
},
}

for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
s.FundAcc(s.TestAccs[1], sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))) // 1,000 osmo
clPool := s.PrepareConcentratedPool() // gaugeid = 1

// create internal Gauge
internalGauge1 := s.CreateNoLockExternalGauges(clPool.GetId(), sdk.NewCoins(), s.TestAccs[1], uint64(1)) // gauge id = 2

// create group gauge
_, err := s.App.IncentivesKeeper.CreateGroupGauge(s.Ctx, sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000))), uint64(1), s.TestAccs[1], []uint64{internalGauge1}) // gauge id = 3
s.Require().NoError(err)

err = s.App.IncentivesKeeper.AddToGaugeRewardsFromGauge(s.Ctx, tc.groupGaugeId, tc.coinsToTransfer, tc.internalGaugeId)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)

// check that the coins have been transferred
gauge, err := s.App.IncentivesKeeper.GetGaugeByID(s.Ctx, tc.groupGaugeId)
s.Require().NoError(err)

s.Require().Equal(gauge.Coins, tc.coinsToTransfer)

}
})
}

}
49 changes: 48 additions & 1 deletion x/incentives/keeper/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package keeper_test

import "github.com/stretchr/testify/suite"
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v17/x/incentives/types"
)

var _ = suite.TestingSuite(nil)

Expand Down Expand Up @@ -33,3 +38,45 @@ func (s *KeeperTestSuite) TestGaugeReferencesManagement() {
gaugeRefs3 := s.App.IncentivesKeeper.GetGaugeRefs(s.Ctx, key2)
s.Require().Equal(len(gaugeRefs3), 2)
}

func (s *KeeperTestSuite) TestGetGroupGaugeById() {
tests := map[string]struct {
groupGaugeId uint64
expectedRecord types.GroupGauge
}{
"Valid record": {
groupGaugeId: uint64(5),
expectedRecord: types.GroupGauge{
GroupGaugeId: uint64(5),
InternalIds: []uint64{2, 3, 4},
},
},

"InValid record": {
groupGaugeId: uint64(6),
expectedRecord: types.GroupGauge{},
},
}

for name, test := range tests {
s.Run(name, func() {
s.FundAcc(s.TestAccs[1], sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000)))) // 1,000 osmo
clPool := s.PrepareConcentratedPool() // gaugeid = 1

// create 3 internal Gauge
var internalGauges []uint64
for i := 0; i <= 2; i++ {
internalGauge := s.CreateNoLockExternalGauges(clPool.GetId(), sdk.NewCoins(), s.TestAccs[1], uint64(1)) // gauge id = 2,3,4
internalGauges = append(internalGauges, internalGauge)
}

_, err := s.App.IncentivesKeeper.CreateGroupGauge(s.Ctx, sdk.NewCoins(sdk.NewCoin("uosmo", sdk.NewInt(100_000_000))), 1, s.TestAccs[1], internalGauges) // gauge id = 5
s.Require().NoError(err)

record, err := s.App.IncentivesKeeper.GetGroupGaugeById(s.Ctx, test.groupGaugeId)
s.Require().NoError(err)

s.Require().Equal(test.expectedRecord, record)
})
}
}

0 comments on commit 18c5cd3

Please sign in to comment.