From 855d1bd99f0a359bc8489c7d59c6cdf8b26f753a Mon Sep 17 00:00:00 2001 From: antstalepresh Date: Fri, 4 Mar 2022 03:20:37 +1000 Subject: [PATCH 01/10] add utility for SetupGammPoolsAndSuperfluidAssets --- x/superfluid/keeper/keeper_test.go | 55 ++++++++++++++++++++++++++++++ x/superfluid/keeper/slash_test.go | 4 +++ 2 files changed, 59 insertions(+) diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index 8b17d7b8f7a..c5eb56116d2 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -157,6 +157,61 @@ func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondSt return valAddrs } +func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) []string { + suite.app.GAMMKeeper.SetParams(suite.ctx, gammtypes.Params{ + PoolCreationFee: sdk.Coins{}, + }) + + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + denoms := []string{} + + for index, multiplier := range multipliers { + token := fmt.Sprintf("token%d", index) + + params := suite.app.SuperfluidKeeper.GetParams(suite.ctx) + uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).Quo(sdk.OneDec().Sub(params.MinimumRiskFactor)).RoundInt() + + err := simapp.FundAccount(suite.app.BankKeeper, suite.ctx, acc1, sdk.NewCoins( + sdk.NewCoin("uosmo", uosmoAmount.Mul(sdk.NewInt(10))), + sdk.NewInt64Coin(token, 100000), + )) + suite.NoError(err) + + var ( + defaultFutureGovernor = "" + + // pool assets + defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin("uosmo", uosmoAmount), + } + defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin(token, sdk.NewInt(10000)), + } + poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} + ) + + poolId, err := suite.app.GAMMKeeper.CreateBalancerPool(suite.ctx, acc1, balancer.PoolParams{ + SwapFee: sdk.NewDecWithPrec(1, 2), + ExitFee: sdk.NewDecWithPrec(1, 2), + }, poolAssets, defaultFutureGovernor) + suite.Require().NoError(err) + + pool, err := suite.app.GAMMKeeper.GetPool(suite.ctx, poolId) + suite.Require().NoError(err) + + denom := pool.GetTotalShares().Denom + suite.app.SuperfluidKeeper.AddNewSuperfluidAsset(suite.ctx, types.SuperfluidAsset{ + Denom: denom, + AssetType: types.SuperfluidAssetTypeLPShare, + }) + + denoms = append(denoms, denom) + } + return denoms +} + func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } diff --git a/x/superfluid/keeper/slash_test.go b/x/superfluid/keeper/slash_test.go index 55f0654bdcc..88ec37f9b44 100644 --- a/x/superfluid/keeper/slash_test.go +++ b/x/superfluid/keeper/slash_test.go @@ -61,6 +61,10 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + // TODO: utilize returned denoms + denoms := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + _ = denoms + locks := []lockuptypes.PeriodLock{} slashFactor := sdk.NewDecWithPrec(5, 2) From 7022ac14b70f2a5c2ec8ae1bee397a5a631f9783 Mon Sep 17 00:00:00 2001 From: antstalepresh Date: Mon, 7 Mar 2022 22:53:06 +1000 Subject: [PATCH 02/10] remove hardcoded gamm pool denoms --- x/superfluid/keeper/distribution_test.go | 35 +++++++------ x/superfluid/keeper/grpc_query_test.go | 42 +++++++-------- x/superfluid/keeper/hooks_test.go | 41 +++++++++------ x/superfluid/keeper/keeper_test.go | 21 +++++--- x/superfluid/keeper/slash_test.go | 29 +++++----- x/superfluid/keeper/stake.go | 3 ++ x/superfluid/keeper/stake_test.go | 67 +++++++++++------------- 7 files changed, 125 insertions(+), 113 deletions(-) diff --git a/x/superfluid/keeper/distribution_test.go b/x/superfluid/keeper/distribution_test.go index 2662b921635..8d8a6c0bd3d 100644 --- a/x/superfluid/keeper/distribution_test.go +++ b/x/superfluid/keeper/distribution_test.go @@ -27,10 +27,10 @@ func (suite *KeeperTestSuite) allocateRewardsToValidator(valAddr sdk.ValAddress) func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { type gaugeChecker struct { - gaugeId uint64 - valIndex int64 - lpDenom string - rewarded bool + intermediaryAccIndex uint64 + valIndex int64 + lpIndex int64 + rewarded bool } testCases := []struct { name string @@ -44,33 +44,33 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { "happy path with single validator and delegator", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []int64{0}, - []gaugeChecker{{4, 0, "gamm/pool/1", true}}, + []gaugeChecker{{0, 0, 0, true}}, }, { "two LP tokens delegation to a single validator", []stakingtypes.BondStatus{stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 0, "gamm/pool/2", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {0, 0, 1, 1000000}}, []int64{0}, - []gaugeChecker{{4, 0, "gamm/pool/1", true}, {5, 0, "gamm/pool/2", true}}, + []gaugeChecker{{0, 0, 0, true}, {1, 0, 1, true}}, }, { "one LP token with two locks to a single validator", []stakingtypes.BondStatus{stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, []int64{0}, - []gaugeChecker{{4, 0, "gamm/pool/1", true}}, + []gaugeChecker{{0, 0, 0, true}}, }, { "add unbonded validator case", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Unbonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []int64{0}, - []gaugeChecker{{4, 0, "gamm/pool/1", true}, {5, 1, "gamm/pool/1", false}}, + []gaugeChecker{{0, 0, 0, true}, {1, 1, 0, false}}, }, } @@ -86,8 +86,10 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // setup superfluid delegations - suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime // allocate rewards to first validator @@ -100,13 +102,14 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { // check gauge balance for _, gaugeCheck := range tc.gaugeChecks { - gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeCheck.gaugeId) + gaugeId := intermediaryAccs[gaugeCheck.intermediaryAccIndex].GaugeId + gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeId) suite.Require().NoError(err) - suite.Require().Equal(gauge.Id, gaugeCheck.gaugeId) + suite.Require().Equal(gauge.Id, gaugeId) suite.Require().Equal(gauge.IsPerpetual, true) suite.Require().Equal(lockuptypes.QueryCondition{ LockQueryType: lockuptypes.ByDuration, - Denom: keeper.StakingSyntheticDenom(gaugeCheck.lpDenom, valAddrs[gaugeCheck.valIndex].String()), + Denom: keeper.StakingSyntheticDenom(denoms[gaugeCheck.lpIndex], valAddrs[gaugeCheck.valIndex].String()), Duration: unbondingDuration, }, gauge.DistributeTo) if gaugeCheck.rewarded { diff --git a/x/superfluid/keeper/grpc_query_test.go b/x/superfluid/keeper/grpc_query_test.go index 56dd151b787..cfdad362424 100644 --- a/x/superfluid/keeper/grpc_query_test.go +++ b/x/superfluid/keeper/grpc_query_test.go @@ -47,31 +47,29 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() { // setup 2 validators valAddrs := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}) - denoms := []string{"gamm/pool/1", "gamm/pool/2"} + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) // create a delegation of 1000000 for every combination of 2 delegations, 2 validators, and 2 superfluid denoms superfluidDelegations := []superfluidDelegation{ - {0, 0, denoms[0], 1000000}, - - {0, 1, denoms[1], 1000000}, - - {1, 0, denoms[1], 1000000}, - - {1, 1, denoms[0], 1000000}, + {0, 0, 0, 1000000}, + {0, 1, 1, 1000000}, + {1, 0, 1, 1000000}, + {1, 1, 0, 1000000}, } // setup superfluid delegations - suite.SetupSuperfluidDelegations(delAddrs, valAddrs, superfluidDelegations) + suite.SetupSuperfluidDelegations(delAddrs, valAddrs, superfluidDelegations, denoms) // for each superfluid delegation, query the amount and make sure it is 1000000 for _, delegation := range superfluidDelegations { + lpDenom := denoms[delegation.lpIndex] res, err := suite.queryClient.SuperfluidDelegationAmount(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationAmountRequest{ DelegatorAddress: delAddrs[delegation.delIndex].String(), ValidatorAddress: valAddrs[delegation.valIndex].String(), - Denom: delegation.lpDenom, + Denom: lpDenom, }) suite.Require().NoError(err) - suite.Require().Equal(res.Amount.AmountOf(delegation.lpDenom).Int64(), delegation.lpAmount) + suite.Require().Equal(res.Amount.AmountOf(lpDenom).Int64(), delegation.lpAmount) } // for each delegator, query all their superfluid delegations and make sure they have 2 delegations @@ -82,8 +80,8 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() { suite.Require().NoError(err) suite.Require().Len(res.SuperfluidDelegationRecords, 2) suite.Require().True(res.TotalDelegatedCoins.IsEqual(sdk.NewCoins( - sdk.NewInt64Coin("gamm/pool/1", 1000000), - sdk.NewInt64Coin("gamm/pool/2", 1000000), + sdk.NewInt64Coin(denoms[0], 1000000), + sdk.NewInt64Coin(denoms[1], 1000000), ))) } @@ -110,7 +108,6 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() { totalSuperfluidDelegationsRes, err := suite.queryClient.TotalSuperfluidDelegations(sdk.WrapSDKContext(suite.Ctx), &types.TotalSuperfluidDelegationsRequest{}) suite.Require().NoError(err) suite.Require().Equal(sdk.NewInt(40000000), totalSuperfluidDelegationsRes.TotalDelegations) - } func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbonding() { @@ -122,21 +119,18 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon // setup 2 validators valAddrs := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}) - denoms := []string{"gamm/pool/1", "gamm/pool/2"} + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) // create a delegation of 1000000 for every combination of 2 delegations, 2 validators, and 2 superfluid denoms superfluidDelegations := []superfluidDelegation{ - {0, 0, denoms[0], 1000000}, - - {0, 1, denoms[1], 1000000}, - - {1, 0, denoms[1], 1000000}, - - {1, 1, denoms[0], 1000000}, + {0, 0, 0, 1000000}, + {0, 1, 1, 1000000}, + {1, 0, 1, 1000000}, + {1, 1, 0, 1000000}, } // setup superfluid delegations - _, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, superfluidDelegations) + _, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, superfluidDelegations, denoms) // start unbonding the superfluid delegations of denom0 from delegator0 to validator0 err := suite.App.SuperfluidKeeper.SuperfluidUndelegate(suite.Ctx, locks[0].Owner, locks[0].ID) @@ -158,7 +152,7 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegationsDontIncludeUnbon suite.Require().NoError(err) suite.Require().Len(res2.SuperfluidDelegationRecords, 1) suite.Require().Equal(sdk.NewCoins( - sdk.NewInt64Coin("gamm/pool/2", 1000000)), res2.TotalDelegatedCoins) + sdk.NewInt64Coin(denoms[1], 1000000)), res2.TotalDelegatedCoins) // query to make sure that the unbonding delegation is not included in the validator denom pair query amountRes, err := suite.queryClient.EstimateSuperfluidDelegatedAmountByValidatorDenom(sdk.WrapSDKContext(suite.Ctx), &types.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest{ diff --git a/x/superfluid/keeper/hooks_test.go b/x/superfluid/keeper/hooks_test.go index db79912b39c..0789c62aa2a 100644 --- a/x/superfluid/keeper/hooks_test.go +++ b/x/superfluid/keeper/hooks_test.go @@ -17,7 +17,7 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { { "happy path with single validator and delegator", []stakingtypes.BondStatus{stakingtypes.Bonded}, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, sdk.Coins{}, }, } @@ -26,29 +26,35 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { suite.Run(tc.name, func() { suite.SetupTest() valAddrs := suite.SetupValidators(tc.validatorStats) - bondDenom := suite.App.StakingKeeper.BondDenom(suite.Ctx) + + denoms, poolIds := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) // Generate delegator addresses delAddrs := CreateRandomAccounts(1) - intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) // gamm swap operation before refresh - suite.App.SuperfluidKeeper.SetOsmoEquivalentMultiplier(suite.Ctx, 2, "gamm/pool/1", sdk.NewDec(10)) acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + pool, err := suite.App.GAMMKeeper.GetPool(suite.Ctx, poolIds[0]) + suite.Require().NoError(err) + poolAssets := pool.GetAllPoolAssets() - coins := sdk.Coins{sdk.NewInt64Coin("foo", 100000000000000)} - err := simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, acc1, coins) + coins := sdk.Coins{sdk.NewInt64Coin(poolAssets[1].Token.Denom, 100000000000000)} + err = simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, acc1, coins) suite.Require().NoError(err) - _, _, err = suite.App.GAMMKeeper.SwapExactAmountOut(suite.Ctx, acc1, 1, "foo", sdk.NewInt(100000000000000), sdk.NewInt64Coin(bondDenom, 250000000000)) + _, _, err = suite.App.GAMMKeeper.SwapExactAmountOut( + suite.Ctx, acc1, + poolIds[0], poolAssets[1].Token.Denom, poolAssets[1].Token.Amount, + sdk.NewCoin(poolAssets[0].Token.Denom, poolAssets[0].Token.Amount.Quo(sdk.NewInt(4)))) suite.Require().NoError(err) // run epoch actions suite.BeginNewBlock(true) // check lptoken twap value set - newEpochTwap := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, "gamm/pool/1") - suite.Require().Equal(newEpochTwap.String(), "0.009999997500000000") + newEpochTwap := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0]) + suite.Require().Equal(newEpochTwap.String(), "15.000000000000000000") // check delegation changes for _, acc := range intermediaryAccs { @@ -56,7 +62,7 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { suite.Require().NoError(err) delegation, found := suite.App.StakingKeeper.GetDelegation(suite.Ctx, acc.GetAccAddress(), valAddr) suite.Require().True(found) - suite.Require().Equal(sdk.NewDec(5000), delegation.Shares) + suite.Require().Equal(sdk.NewDec(7500000), delegation.Shares) // TODO: Check reward distribution // suite.Require().NotEqual(sdk.Coins{}, ) } @@ -187,7 +193,7 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() { "happy path with single validator and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []uint64{1}, []int64{0}, []uint64{1}, @@ -197,7 +203,7 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() { "with single validator and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, []uint64{1, 2}, []int64{0}, []uint64{1, 2}, @@ -207,7 +213,7 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() { "with multiple validators and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []uint64{1, 2}, []int64{0}, []uint64{1}, @@ -228,8 +234,11 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // setup superfluid delegations - intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) for _, lockId := range tc.superUnbondingLockIds { @@ -258,14 +267,14 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() { for _, lockId := range tc.expSlashedLockIds { gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId) suite.Require().NoError(err) - suite.Require().Equal(sdk.NewInt(950000).String(), gotLock.Coins.AmountOf("gamm/pool/1").String()) + suite.Require().Equal(sdk.NewInt(950000).String(), gotLock.Coins.AmountOf(denoms[0]).String()) } // check unslashed lockups for _, lockId := range tc.expUnslashedLockIds { gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId) suite.Require().NoError(err) - suite.Require().Equal(sdk.NewInt(1000000).String(), gotLock.Coins.AmountOf("gamm/pool/1").String()) + suite.Require().Equal(sdk.NewInt(1000000).String(), gotLock.Coins.AmountOf(denoms[0]).String()) } }) } diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index c5eb56116d2..7330ec1233d 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -157,22 +157,24 @@ func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondSt return valAddrs } -func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) []string { +func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) ([]string, []uint64) { suite.app.GAMMKeeper.SetParams(suite.ctx, gammtypes.Params{ PoolCreationFee: sdk.Coins{}, }) + bondDenom := suite.app.StakingKeeper.BondDenom(suite.ctx) + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) denoms := []string{} + poolIds := []uint64{} for index, multiplier := range multipliers { token := fmt.Sprintf("token%d", index) - params := suite.app.SuperfluidKeeper.GetParams(suite.ctx) - uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).Quo(sdk.OneDec().Sub(params.MinimumRiskFactor)).RoundInt() + uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() err := simapp.FundAccount(suite.app.BankKeeper, suite.ctx, acc1, sdk.NewCoins( - sdk.NewCoin("uosmo", uosmoAmount.Mul(sdk.NewInt(10))), + sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), sdk.NewInt64Coin(token, 100000), )) suite.NoError(err) @@ -183,7 +185,7 @@ func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sd // pool assets defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ Weight: sdk.NewInt(100), - Token: sdk.NewCoin("uosmo", uosmoAmount), + Token: sdk.NewCoin(bondDenom, uosmoAmount), } defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ Weight: sdk.NewInt(100), @@ -207,9 +209,16 @@ func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sd AssetType: types.SuperfluidAssetTypeLPShare, }) + // register a LP token as a superfluid asset + suite.app.SuperfluidKeeper.AddNewSuperfluidAsset(suite.ctx, types.SuperfluidAsset{ + Denom: denom, + AssetType: types.SuperfluidAssetTypeLPShare, + }) + + poolIds = append(poolIds, poolId) denoms = append(denoms, denom) } - return denoms + return denoms, poolIds } func TestKeeperTestSuite(t *testing.T) { diff --git a/x/superfluid/keeper/slash_test.go b/x/superfluid/keeper/slash_test.go index 88ec37f9b44..f9ff527de45 100644 --- a/x/superfluid/keeper/slash_test.go +++ b/x/superfluid/keeper/slash_test.go @@ -19,7 +19,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { "with single validator and single superfluid delegation", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []int64{0}, []int64{0}, }, @@ -27,7 +27,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { "with single validator and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, []int64{0}, []int64{0, 1}, }, @@ -35,7 +35,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { "with multiple validators and multiple superfluid delegations with single validator slash", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []int64{0}, []int64{0}, }, @@ -43,7 +43,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { "with multiple validators and multiple superfluid delegations with two validators slash", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []int64{0, 1}, []int64{0, 1}, }, @@ -61,9 +61,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) - // TODO: utilize returned denoms - denoms := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) - _ = denoms + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) locks := []lockuptypes.PeriodLock{} slashFactor := sdk.NewDecWithPrec(5, 2) @@ -72,7 +70,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { for _, del := range tc.superDelegations { valAddr := valAddrs[del.valIndex] delAddr := delAddrs[del.delIndex] - lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, del.lpDenom, del.lpAmount) + lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, denoms[del.lpIndex], del.lpAmount) // save accounts and locks for future use locks = append(locks, lock) @@ -96,7 +94,7 @@ func (suite *KeeperTestSuite) TestBeforeValidatorSlashed() { gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, locks[lockIndex].ID) suite.Require().NoError(err) suite.Require().Equal( - gotLock.Coins.AmountOf("gamm/pool/1").String(), + gotLock.Coins.AmountOf(denoms[0]).String(), sdk.NewDec(1000000).Mul(sdk.OneDec().Sub(slashFactor)).TruncateInt().String(), ) } @@ -116,21 +114,21 @@ func (suite *KeeperTestSuite) TestSlashLockupsForUnbondingDelegationSlash() { "happy path with single validator and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []uint64{1}, }, { "with single validator and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}}, []uint64{1, 2}, }, { "with multiple validators and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []uint64{1, 2}, }, } @@ -146,8 +144,11 @@ func (suite *KeeperTestSuite) TestSlashLockupsForUnbondingDelegationSlash() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // setup superfluid delegations - intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) for _, lockId := range tc.superUnbondingLockIds { @@ -172,7 +173,7 @@ func (suite *KeeperTestSuite) TestSlashLockupsForUnbondingDelegationSlash() { for _, lockId := range tc.superUnbondingLockIds { gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId) suite.Require().NoError(err) - suite.Require().Equal(gotLock.Coins.AmountOf("gamm/pool/1").String(), sdk.NewInt(950000).String()) + suite.Require().Equal(gotLock.Coins[0].Amount.String(), sdk.NewInt(950000).String()) } }) } diff --git a/x/superfluid/keeper/stake.go b/x/superfluid/keeper/stake.go index b49fa8f8087..8303d13a68f 100644 --- a/x/superfluid/keeper/stake.go +++ b/x/superfluid/keeper/stake.go @@ -327,6 +327,9 @@ func (k Keeper) forceUndelegateAndBurnOsmoTokens(ctx sdk.Context, return err } err = k.bk.BurnCoins(cacheCtx, types.ModuleName, undelegatedCoins) + if err != nil { + return err + } bondDenom := k.sk.BondDenom(cacheCtx) k.bk.AddSupplyOffset(cacheCtx, bondDenom, undelegatedCoins.AmountOf(bondDenom)) diff --git a/x/superfluid/keeper/stake_test.go b/x/superfluid/keeper/stake_test.go index 41af5640fbb..baf2763bb6c 100644 --- a/x/superfluid/keeper/stake_test.go +++ b/x/superfluid/keeper/stake_test.go @@ -5,7 +5,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - epochstypes "github.com/osmosis-labs/osmosis/v7/x/epochs/types" lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" minttypes "github.com/osmosis-labs/osmosis/v7/x/mint/types" "github.com/osmosis-labs/osmosis/v7/x/superfluid/keeper" @@ -15,7 +14,7 @@ import ( type superfluidDelegation struct { delIndex int64 valIndex int64 - lpDenom string + lpIndex int64 lpAmount int64 } type superfluidRedelegation struct { @@ -34,7 +33,7 @@ type osmoEquivalentMultiplier struct { price sdk.Dec } -func (suite *KeeperTestSuite) SetupSuperfluidDelegations(delAddrs []sdk.AccAddress, valAddrs []sdk.ValAddress, superDelegations []superfluidDelegation) ([]types.SuperfluidIntermediaryAccount, []lockuptypes.PeriodLock) { +func (suite *KeeperTestSuite) SetupSuperfluidDelegations(delAddrs []sdk.AccAddress, valAddrs []sdk.ValAddress, superDelegations []superfluidDelegation, denoms []string) ([]types.SuperfluidIntermediaryAccount, []lockuptypes.PeriodLock) { flagIntermediaryAcc := make(map[string]bool) intermediaryAccs := []types.SuperfluidIntermediaryAccount{} locks := []lockuptypes.PeriodLock{} @@ -47,13 +46,14 @@ func (suite *KeeperTestSuite) SetupSuperfluidDelegations(delAddrs []sdk.AccAddre for _, del := range superDelegations { delAddr := delAddrs[del.delIndex] valAddr := valAddrs[del.valIndex] - lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, del.lpDenom, del.lpAmount) + lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, denoms[del.lpIndex], del.lpAmount) expAcc := types.NewSuperfluidIntermediaryAccount(lock.Coins[0].Denom, valAddr.String(), 0) + gotAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.ctx, expAcc.GetAccAddress()) // save accounts for future use - if flagIntermediaryAcc[expAcc.String()] == false { - flagIntermediaryAcc[expAcc.String()] = true - intermediaryAccs = append(intermediaryAccs, expAcc) + if flagIntermediaryAcc[gotAcc.String()] == false { + flagIntermediaryAcc[gotAcc.String()] = true + intermediaryAccs = append(intermediaryAccs, gotAcc) } // save locks for future use locks = append(locks, lock) @@ -82,20 +82,6 @@ func (suite *KeeperTestSuite) checkIntermediaryAccountDelegations(intermediaryAc func (suite *KeeperTestSuite) SetupSuperfluidDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, denom string, amount int64) lockuptypes.PeriodLock { unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime - // register a LP token as a superfluid asset - suite.App.SuperfluidKeeper.SetSuperfluidAsset(suite.Ctx, types.SuperfluidAsset{ - Denom: denom, - AssetType: types.SuperfluidAssetTypeLPShare, - }) - - // set OSMO TWAP price for LP token - suite.App.SuperfluidKeeper.SetOsmoEquivalentMultiplier(suite.Ctx, 1, denom, sdk.NewDec(20)) - epochIdentifier := suite.App.SuperfluidKeeper.GetEpochIdentifier(suite.Ctx) - suite.App.EpochsKeeper.SetEpochInfo(suite.Ctx, epochstypes.EpochInfo{ - Identifier: epochIdentifier, - CurrentEpoch: 2, - }) - // create lockup of LP token coins := sdk.Coins{sdk.NewInt64Coin(denom, amount)} lastLockID := suite.App.LockupKeeper.GetLastLockID(suite.Ctx) @@ -141,21 +127,21 @@ func (suite *KeeperTestSuite) TestSuperfluidDelegate() { "with single validator and single superfluid delegation", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []sdk.Dec{sdk.NewDec(10000000)}, // 50% x 20 x 1000000 }, { "with single validator and additional superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {0, 0, 0, 1000000}}, []sdk.Dec{sdk.NewDec(20000000)}, // 50% x 20 x 1000000 x 2 }, { - "with multiples validator and multiple superfluid delegations", + "with multiple validators and multiple superfluid delegations", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []sdk.Dec{sdk.NewDec(10000000), sdk.NewDec(10000000)}, // 50% x 20 x 1000000 }, } @@ -172,18 +158,21 @@ func (suite *KeeperTestSuite) TestSuperfluidDelegate() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // get pre-superfluid delgations osmo supply and supplyWithOffset presupply := suite.App.BankKeeper.GetSupply(suite.Ctx, bondDenom) presupplyWithOffset := suite.App.BankKeeper.GetSupplyWithOffset(suite.Ctx, bondDenom) // setup superfluid delegations - intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + _, _, _ = delAddrs, valAddrs, denoms + intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) // ensure post-superfluid delegations osmo supplywithoffset is the same while supply is not postsupply := suite.App.BankKeeper.GetSupply(suite.Ctx, bondDenom) postsupplyWithOffset := suite.App.BankKeeper.GetSupplyWithOffset(suite.Ctx, bondDenom) suite.Require().False(postsupply.IsEqual(presupply), "presupply: %s postsupply: %s", presupply, postsupply) - suite.Require().True(postsupplyWithOffset.IsEqual(presupplyWithOffset)) + suite.Require().Equal(postsupplyWithOffset.String(), presupplyWithOffset.String()) unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime @@ -262,7 +251,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { "with single validator and single superfluid delegation and single undelegation", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []uint64{}, []uint64{1}, []bool{false}, @@ -271,7 +260,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { // { // "with single validator, single superfluid delegation, add more tokens to the lock, and single undelegation", // []stakingtypes.BondStatus{stakingtypes.Bonded}, - // []superfluidDelegation{{0, "gamm/pool/1", 1000000}}, + // []superfluidDelegation{{0, 0, 1000000}}, // []uint64{1}, // []uint64{1}, // []bool{false}, @@ -281,7 +270,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { "with single validator and additional superfluid delegations and single undelegation", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {0, 0, 0, 1000000}}, []uint64{}, []uint64{1}, []bool{false}, @@ -291,7 +280,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { "with multiple validators and multiple superfluid delegations and multiple undelegations", []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, 2, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {1, 1, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, []uint64{}, []uint64{1, 2}, []bool{false, false}, @@ -301,17 +290,17 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { "undelegating not available lock id", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []uint64{}, []uint64{2}, []bool{true}, - []sdk.Dec{sdk.NewDec(10000000)}, + []sdk.Dec{}, }, { "try undelegating twice for same lock id", []stakingtypes.BondStatus{stakingtypes.Bonded}, 1, - []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}, + []superfluidDelegation{{0, 0, 0, 1000000}}, []uint64{}, []uint64{1, 1}, []bool{false, true}, @@ -332,8 +321,10 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() { // setup validators valAddrs := suite.SetupValidators(tc.validatorStats) + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // setup superfluid delegations - intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations) + intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) for _, lockId := range tc.addMoreTokensLockIds { @@ -438,8 +429,10 @@ func (suite *KeeperTestSuite) TestSuperfluidUnbondLock() { // setup validators valAddrs := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded}) + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + // setup superfluid delegations - intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}}) + intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, []superfluidDelegation{{0, 0, 0, 1000000}}, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) for _, lock := range locks { @@ -503,7 +496,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUnbondLock() { // check if finished unlocking succesfully increased balance balances = suite.App.BankKeeper.GetAllBalances(suite.Ctx, lock.OwnerAddress()) suite.Require().Equal(1, balances.Len()) - suite.Require().Equal("gamm/pool/1", balances[0].Denom) + suite.Require().Equal(denoms[0], balances[0].Denom) suite.Require().Equal(sdk.NewInt(1000000), balances[0].Amount) } From a594d4e41dd8594a20bee381ae649f3e19e86527 Mon Sep 17 00:00:00 2001 From: antstalepresh Date: Tue, 8 Mar 2022 03:29:17 +1000 Subject: [PATCH 03/10] fix for main branch ctx => Ctx, app => App in tests --- x/superfluid/keeper/hooks_test.go | 16 ++++++++-------- x/superfluid/keeper/keeper_test.go | 14 +++++++------- x/superfluid/keeper/stake_test.go | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/x/superfluid/keeper/hooks_test.go b/x/superfluid/keeper/hooks_test.go index 0789c62aa2a..8016836d8e2 100644 --- a/x/superfluid/keeper/hooks_test.go +++ b/x/superfluid/keeper/hooks_test.go @@ -142,14 +142,14 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { // for index, lockId := range tc.unbondingLockIds { // // get intermediary account -// accAddr := suite.app.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId) -// intermediaryAcc := suite.app.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, accAddr) +// accAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId) +// intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, accAddr) // valAddr := intermediaryAcc.ValAddr // // unlock native lockup -// lock, err := suite.app.LockupKeeper.GetLockByID(suite.Ctx, lockId) +// lock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId) // if err == nil { -// err = suite.app.LockupKeeper.BeginUnlock(suite.Ctx, *lock, nil) +// err = suite.App.LockupKeeper.BeginUnlock(suite.Ctx, *lock, nil) // } // if tc.expUnbondingErr[index] { @@ -159,16 +159,16 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { // suite.Require().NoError(err) // // check lockId and intermediary account connection deletion -// addr := suite.app.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId) +// addr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId) // suite.Require().Equal(addr.String(), "") // // check bonding synthetic lockup deletion -// _, err = suite.app.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.StakingSyntheticDenom(lock.Coins[0].Denom, valAddr)) +// _, err = suite.App.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.StakingSyntheticDenom(lock.Coins[0].Denom, valAddr)) // suite.Require().Error(err) // // check unbonding synthetic lockup creation -// unbondingDuration := suite.app.StakingKeeper.GetParams(suite.Ctx).UnbondingTime -// synthLock, err := suite.app.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr)) +// unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime +// synthLock, err := suite.App.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr)) // suite.Require().NoError(err) // suite.Require().Equal(synthLock.UnderlyingLockId, lockId) // suite.Require().Equal(synthLock.SynthDenom, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr)) diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index 7330ec1233d..0e6b055ec85 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -158,11 +158,11 @@ func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondSt } func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) ([]string, []uint64) { - suite.app.GAMMKeeper.SetParams(suite.ctx, gammtypes.Params{ + suite.App.GAMMKeeper.SetParams(suite.Ctx, gammtypes.Params{ PoolCreationFee: sdk.Coins{}, }) - bondDenom := suite.app.StakingKeeper.BondDenom(suite.ctx) + bondDenom := suite.App.StakingKeeper.BondDenom(suite.Ctx) acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) denoms := []string{} @@ -173,7 +173,7 @@ func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sd uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() - err := simapp.FundAccount(suite.app.BankKeeper, suite.ctx, acc1, sdk.NewCoins( + err := simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, acc1, sdk.NewCoins( sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), sdk.NewInt64Coin(token, 100000), )) @@ -194,23 +194,23 @@ func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sd poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} ) - poolId, err := suite.app.GAMMKeeper.CreateBalancerPool(suite.ctx, acc1, balancer.PoolParams{ + poolId, err := suite.App.GAMMKeeper.CreateBalancerPool(suite.Ctx, acc1, balancer.PoolParams{ SwapFee: sdk.NewDecWithPrec(1, 2), ExitFee: sdk.NewDecWithPrec(1, 2), }, poolAssets, defaultFutureGovernor) suite.Require().NoError(err) - pool, err := suite.app.GAMMKeeper.GetPool(suite.ctx, poolId) + pool, err := suite.App.GAMMKeeper.GetPool(suite.Ctx, poolId) suite.Require().NoError(err) denom := pool.GetTotalShares().Denom - suite.app.SuperfluidKeeper.AddNewSuperfluidAsset(suite.ctx, types.SuperfluidAsset{ + suite.App.SuperfluidKeeper.AddNewSuperfluidAsset(suite.Ctx, types.SuperfluidAsset{ Denom: denom, AssetType: types.SuperfluidAssetTypeLPShare, }) // register a LP token as a superfluid asset - suite.app.SuperfluidKeeper.AddNewSuperfluidAsset(suite.ctx, types.SuperfluidAsset{ + suite.App.SuperfluidKeeper.AddNewSuperfluidAsset(suite.Ctx, types.SuperfluidAsset{ Denom: denom, AssetType: types.SuperfluidAssetTypeLPShare, }) diff --git a/x/superfluid/keeper/stake_test.go b/x/superfluid/keeper/stake_test.go index baf2763bb6c..c1aa564b9bd 100644 --- a/x/superfluid/keeper/stake_test.go +++ b/x/superfluid/keeper/stake_test.go @@ -48,7 +48,7 @@ func (suite *KeeperTestSuite) SetupSuperfluidDelegations(delAddrs []sdk.AccAddre valAddr := valAddrs[del.valIndex] lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, denoms[del.lpIndex], del.lpAmount) expAcc := types.NewSuperfluidIntermediaryAccount(lock.Coins[0].Denom, valAddr.String(), 0) - gotAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.ctx, expAcc.GetAccAddress()) + gotAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, expAcc.GetAccAddress()) // save accounts for future use if flagIntermediaryAcc[gotAcc.String()] == false { From 5cf707486477d3ec29be65cade040e3d5138ac5a Mon Sep 17 00:00:00 2001 From: mattverse Date: Thu, 10 Mar 2022 16:42:14 +0900 Subject: [PATCH 04/10] Test Changes --- app/apptesting/test_suite.go | 16 +++++ proto/osmosis/incentives/gauge.proto | 1 + x/incentives/keeper/distribute.go | 3 + x/incentives/types/gauge.pb.go | 5 +- x/superfluid/keeper/distribution_test.go | 20 +----- x/superfluid/keeper/hooks_test.go | 24 +++++-- .../keeper/intermediary_account_test.go | 67 +++++++++++++++++++ x/superfluid/keeper/keeper_test.go | 2 - 8 files changed, 110 insertions(+), 28 deletions(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index 261822fc258..73945090054 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -11,6 +11,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -99,3 +100,18 @@ func (keeperTestHelper *KeeperTestHelper) EndBlock() { reqEndBlock := abci.RequestEndBlock{Height: keeperTestHelper.Ctx.BlockHeight()} keeperTestHelper.App.EndBlocker(keeperTestHelper.Ctx, reqEndBlock) } + +func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk.ValAddress) { + validator, found := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddr) + keeperTestHelper.Require().True(found) + + // allocate reward tokens to distribution module + coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20000))} + simapp.FundModuleAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, distrtypes.ModuleName, coins) + + // allocate rewards to validator + keeperTestHelper.Ctx = keeperTestHelper.Ctx.WithBlockHeight(keeperTestHelper.Ctx.BlockHeight() + 1) + decTokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(20000)}} + keeperTestHelper.App.DistrKeeper.AllocateTokensToValidator(keeperTestHelper.Ctx, validator, decTokens) + keeperTestHelper.App.DistrKeeper.IncrementValidatorPeriod(keeperTestHelper.Ctx, validator) +} diff --git a/proto/osmosis/incentives/gauge.proto b/proto/osmosis/incentives/gauge.proto index 6a78f6cf9f0..8622c870be7 100644 --- a/proto/osmosis/incentives/gauge.proto +++ b/proto/osmosis/incentives/gauge.proto @@ -17,6 +17,7 @@ message Gauge { // these queries osmosis.lockup.QueryCondition distribute_to = 3 [ (gogoproto.nullable) = false ]; + // total amount of Coins that has been in the gauge. repeated cosmos.base.v1beta1.Coin coins = 4 [ (gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins" diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index 973038cad7d..5d1d590b981 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -368,6 +368,9 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, er locksByDenomCache := make(map[string][]lockuptypes.PeriodLock) totalDistributedCoins := sdk.Coins{} for _, gauge := range gauges { + if gauge.Id == uint64(10){ + fmt.Println("aaa") + } filteredLocks := k.getDistributeToBaseLocks(ctx, gauge, locksByDenomCache) // send based on synthetic lockup coins if it's distributing to synthetic lockups var gaugeDistributedCoins sdk.Coins diff --git a/x/incentives/types/gauge.pb.go b/x/incentives/types/gauge.pb.go index 4d22ccfff8d..f15ed43e558 100644 --- a/x/incentives/types/gauge.pb.go +++ b/x/incentives/types/gauge.pb.go @@ -37,8 +37,9 @@ type Gauge struct { // distribution incentives by third party // Rewards are distributed to lockups that are are returned by at least one of // these queries - DistributeTo types.QueryCondition `protobuf:"bytes,3,opt,name=distribute_to,json=distributeTo,proto3" json:"distribute_to"` - Coins github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,rep,name=coins,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"coins"` + DistributeTo types.QueryCondition `protobuf:"bytes,3,opt,name=distribute_to,json=distributeTo,proto3" json:"distribute_to"` + // total amount of Coins that has been in the gauge. + Coins github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,rep,name=coins,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"coins"` // distribution start time StartTime time.Time `protobuf:"bytes,5,opt,name=start_time,json=startTime,proto3,stdtime" json:"start_time" yaml:"start_time"` NumEpochsPaidOver uint64 `protobuf:"varint,6,opt,name=num_epochs_paid_over,json=numEpochsPaidOver,proto3" json:"num_epochs_paid_over,omitempty"` diff --git a/x/superfluid/keeper/distribution_test.go b/x/superfluid/keeper/distribution_test.go index 8d8a6c0bd3d..ff73e540cb7 100644 --- a/x/superfluid/keeper/distribution_test.go +++ b/x/superfluid/keeper/distribution_test.go @@ -2,29 +2,11 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" - distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" - minttypes "github.com/osmosis-labs/osmosis/v7/x/mint/types" "github.com/osmosis-labs/osmosis/v7/x/superfluid/keeper" ) -func (suite *KeeperTestSuite) allocateRewardsToValidator(valAddr sdk.ValAddress) { - validator, found := suite.App.StakingKeeper.GetValidator(suite.Ctx, valAddr) - suite.Require().True(found) - - // allocate reward tokens to distribution module - coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20000))} - suite.App.BankKeeper.MintCoins(suite.Ctx, minttypes.ModuleName, coins) - suite.App.BankKeeper.SendCoinsFromModuleToModule(suite.Ctx, minttypes.ModuleName, distrtypes.ModuleName, coins) - - // allocate rewards to validator - suite.Ctx = suite.Ctx.WithBlockHeight(suite.Ctx.BlockHeight() + 1) - decTokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(20000)}} - suite.App.DistrKeeper.AllocateTokensToValidator(suite.Ctx, validator, decTokens) - suite.App.DistrKeeper.IncrementValidatorPeriod(suite.Ctx, validator) -} - func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { type gaugeChecker struct { intermediaryAccIndex uint64 @@ -94,7 +76,7 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { // allocate rewards to first validator for _, valIndex := range tc.rewardedVals { - suite.allocateRewardsToValidator(valAddrs[valIndex]) + suite.AllocateRewardsToValidator(valAddrs[valIndex]) } // move intermediary account delegation rewards to gauges diff --git a/x/superfluid/keeper/hooks_test.go b/x/superfluid/keeper/hooks_test.go index 8016836d8e2..9e427887ef4 100644 --- a/x/superfluid/keeper/hooks_test.go +++ b/x/superfluid/keeper/hooks_test.go @@ -18,7 +18,7 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { "happy path with single validator and delegator", []stakingtypes.BondStatus{stakingtypes.Bonded}, []superfluidDelegation{{0, 0, 0, 1000000}}, - sdk.Coins{}, + sdk.Coins{{Amount: sdk.NewInt(999990), Denom: "stake"}}, }, } @@ -27,11 +27,12 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { suite.SetupTest() valAddrs := suite.SetupValidators(tc.validatorStats) + // we create two additional pools: total three pools, 10 gauges denoms, poolIds := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) // Generate delegator addresses delAddrs := CreateRandomAccounts(1) - intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) + intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) // gamm swap operation before refresh @@ -54,7 +55,20 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { // check lptoken twap value set newEpochTwap := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0]) - suite.Require().Equal(newEpochTwap.String(), "15.000000000000000000") + suite.Require().Equal(newEpochTwap, sdk.NewDec(15)) + + // check gauge creation in new block + intermediaryAccAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, locks[0].ID) + intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, intermediaryAccAddr) + gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, intermediaryAcc.GaugeId) + + suite.Require().NoError(err) + suite.Require().Equal(gauge.Id, intermediaryAcc.GaugeId) + suite.Require().Equal(gauge.IsPerpetual, true) + suite.Require().Equal(gauge.Coins, tc.expRewards) + suite.Require().Equal(gauge.NumEpochsPaidOver, uint64(1)) + suite.Require().Equal(gauge.FilledEpochs, uint64(1)) + suite.Require().Equal(gauge.DistributedCoins, tc.expRewards) // check delegation changes for _, acc := range intermediaryAccs { @@ -63,9 +77,9 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { delegation, found := suite.App.StakingKeeper.GetDelegation(suite.Ctx, acc.GetAccAddress(), valAddr) suite.Require().True(found) suite.Require().Equal(sdk.NewDec(7500000), delegation.Shares) - // TODO: Check reward distribution - // suite.Require().NotEqual(sdk.Coins{}, ) } + balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, delAddrs[0]) + suite.Require().Equal(tc.expRewards, balance) }) } } diff --git a/x/superfluid/keeper/intermediary_account_test.go b/x/superfluid/keeper/intermediary_account_test.go index 9eef13bfc87..d99991b0afe 100644 --- a/x/superfluid/keeper/intermediary_account_test.go +++ b/x/superfluid/keeper/intermediary_account_test.go @@ -2,9 +2,76 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/osmosis-labs/osmosis/v7/x/superfluid/types" ) +func (suite *KeeperTestSuite) TestIntermediaryAccountCreation() { + testCases := []struct { + name string + validatorStats []stakingtypes.BondStatus + delegatorNumber int64 + superDelegations []superfluidDelegation + }{ + { + "test intermediary account with single superfluid delegation", + []stakingtypes.BondStatus{stakingtypes.Bonded}, + 1, + []superfluidDelegation{{0, 0, 0, 1000000}}, + }, + { + "test multiple intermediary accounts with multiple superfluid delegation", + []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded}, + 2, + []superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}}, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() + valAddrs := suite.SetupValidators(tc.validatorStats) + delAddrs := CreateRandomAccounts(int(tc.delegatorNumber)) + + // we create two additional pools: total three pools, 10 gauges + denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)}) + + var interAccs []types.SuperfluidIntermediaryAccount + + for _, superDelegation := range tc.superDelegations { + delAddr := delAddrs[superDelegation.delIndex] + valAddr := valAddrs[superDelegation.valIndex] + denom := denoms[superDelegation.lpIndex] + + // check intermediary Account prior to superfluid delegation, should have nil Intermediary Account + expAcc := types.NewSuperfluidIntermediaryAccount(denom, valAddr.String(), 0) + interAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, expAcc.GetAccAddress()) + suite.Require().NotEqual(expAcc.GetAccAddress(), interAcc.GetAccAddress()) + suite.Require().Equal("", interAcc.Denom) + suite.Require().Equal(uint64(0), interAcc.GaugeId) + suite.Require().Equal("", interAcc.ValAddr) + + lock := suite.SetupSuperfluidDelegate(delAddr, valAddr, denom, superDelegation.lpAmount) + + // check that intermediary Account connection is established + interAccConnection := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lock.ID) + suite.Require().Equal(expAcc.GetAccAddress(), interAccConnection) + + interAcc = suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, interAccConnection) + suite.Require().Equal(expAcc.GetAccAddress(), interAcc.GetAccAddress()) + + // check on interAcc that has been created + suite.Require().Equal(denom, interAcc.Denom) + suite.Require().Equal(valAddr.String(), interAcc.ValAddr) + + interAccs = append(interAccs, interAcc) + } + suite.checkIntermediaryAccountDelegations(interAccs) + }) + } +} + func (suite *KeeperTestSuite) TestIntermediaryAccountsSetGetDeleteFlow() { suite.SetupTest() diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index 0e6b055ec85..74b50298c87 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -50,8 +50,6 @@ func (suite *KeeperTestSuite) SetupTest() { startTime := time.Unix(1645580000, 0) suite.Ctx = suite.App.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: startTime.UTC()}) - a := suite.App.StakingKeeper.GetParams(suite.Ctx).BondDenom - fmt.Println(a) queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) types.RegisterQueryServer(queryHelper, suite.App.SuperfluidKeeper) suite.queryClient = types.NewQueryClient(queryHelper) From 97de47c6e15de9191986df1c1fce7f80bf1e25ee Mon Sep 17 00:00:00 2001 From: mattverse Date: Thu, 10 Mar 2022 22:28:07 +0900 Subject: [PATCH 05/10] Add testing packages --- app/apptesting/balancertesting/test_suite.go | 88 ++++++++++++++++++++ app/apptesting/lockuptesting/test_suite.go | 25 ++++++ x/superfluid/keeper/epoch.go | 3 + x/superfluid/keeper/hooks_test.go | 19 +---- x/superfluid/keeper/keeper_test.go | 83 ++++-------------- x/superfluid/keeper/stake_test.go | 2 +- 6 files changed, 135 insertions(+), 85 deletions(-) create mode 100644 app/apptesting/balancertesting/test_suite.go create mode 100644 app/apptesting/lockuptesting/test_suite.go diff --git a/app/apptesting/balancertesting/test_suite.go b/app/apptesting/balancertesting/test_suite.go new file mode 100644 index 00000000000..58216584d3c --- /dev/null +++ b/app/apptesting/balancertesting/test_suite.go @@ -0,0 +1,88 @@ +package balancertesting + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/osmosis-labs/osmosis/v7/app/apptesting" + "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" + gammtypes "github.com/osmosis-labs/osmosis/v7/x/gamm/types" + "github.com/tendermint/tendermint/crypto/ed25519" +) + +type BalancerTestHelper struct { + *apptesting.KeeperTestHelper +} + +func (balancerTestHelper *BalancerTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []sdk.Dec) []gammtypes.PoolI { + balancerTestHelper.App.GAMMKeeper.SetParams(balancerTestHelper.Ctx, gammtypes.Params{ + PoolCreationFee: sdk.Coins{}, + }) + + bondDenom := balancerTestHelper.App.StakingKeeper.BondDenom(balancerTestHelper.Ctx) + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + + pools := []gammtypes.PoolI{} + + for index, multiplier := range multipliers { + token := fmt.Sprintf("token%d", index) + + uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() + + err := simapp.FundAccount(balancerTestHelper.App.BankKeeper, balancerTestHelper.Ctx, acc1, sdk.NewCoins( + sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), + sdk.NewInt64Coin(token, 100000), + )) + balancerTestHelper.NoError(err) + + var ( + defaultFutureGovernor = "" + + // pool assets + defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin(bondDenom, uosmoAmount), + } + defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin(token, sdk.NewInt(10000)), + } + poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} + ) + + poolId, err := balancerTestHelper.App.GAMMKeeper.CreateBalancerPool(balancerTestHelper.Ctx, acc1, balancer.PoolParams{ + SwapFee: sdk.NewDecWithPrec(1, 2), + ExitFee: sdk.NewDecWithPrec(1, 2), + }, poolAssets, defaultFutureGovernor) + balancerTestHelper.Require().NoError(err) + + pool, err := balancerTestHelper.App.GAMMKeeper.GetPool(balancerTestHelper.Ctx, poolId) + balancerTestHelper.Require().NoError(err) + + pools = append(pools, pool) + } + return pools +} + +// SwapAndSetSpotPrice runs a swap to set Spot price of a pool using arbitrary values +// returns spot price after the arbitrary swap +func (balancerTestHelper *BalancerTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset gammtypes.PoolAsset, toAsset gammtypes.PoolAsset) sdk.Dec { + // create a dummy account + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + + // fund dummy account with tokens to swap + coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Token.Denom, 100000000000000)} + err := simapp.FundAccount(balancerTestHelper.App.BankKeeper, balancerTestHelper.Ctx, acc1, coins) + balancerTestHelper.Require().NoError(err) + + _, _, err = balancerTestHelper.App.GAMMKeeper.SwapExactAmountOut( + balancerTestHelper.Ctx, acc1, + poolId, fromAsset.Token.Denom, fromAsset.Token.Amount, + sdk.NewCoin(toAsset.Token.Denom, toAsset.Token.Amount.Quo(sdk.NewInt(4)))) + balancerTestHelper.Require().NoError(err) + + spotPrice, err := balancerTestHelper.App.GAMMKeeper.CalculateSpotPrice(balancerTestHelper.Ctx, poolId, toAsset.Token.Denom, fromAsset.Token.Denom) + balancerTestHelper.Require().NoError(err) + return spotPrice +} diff --git a/app/apptesting/lockuptesting/test_suite.go b/app/apptesting/lockuptesting/test_suite.go new file mode 100644 index 00000000000..4ea8e45aa62 --- /dev/null +++ b/app/apptesting/lockuptesting/test_suite.go @@ -0,0 +1,25 @@ +package lockuptesting + +import ( + "time" + + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/osmosis-labs/osmosis/v7/app/apptesting" + + lockupkeeper "github.com/osmosis-labs/osmosis/v7/x/lockup/keeper" + lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" +) + +type LockupTestHelper struct { + *apptesting.KeeperTestHelper +} + +func (lockupTestHelper *LockupTestHelper) LockTokens(addr sdk.AccAddress, coins sdk.Coins, duration time.Duration) (lockID uint64) { + msgServer := lockupkeeper.NewMsgServerImpl(lockupTestHelper.App.LockupKeeper) + err := simapp.FundAccount(lockupTestHelper.App.BankKeeper, lockupTestHelper.Ctx, addr, coins) + lockupTestHelper.Require().NoError(err) + msgResponse, err := msgServer.LockTokens(sdk.WrapSDKContext(lockupTestHelper.Ctx), lockuptypes.NewMsgLockTokens(addr, duration, coins)) + lockupTestHelper.Require().NoError(err) + return msgResponse.ID +} diff --git a/x/superfluid/keeper/epoch.go b/x/superfluid/keeper/epoch.go index 00425675c8c..57dcbba407e 100644 --- a/x/superfluid/keeper/epoch.go +++ b/x/superfluid/keeper/epoch.go @@ -2,6 +2,7 @@ package keeper import ( "errors" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v7/osmoutils" @@ -118,6 +119,8 @@ func (k Keeper) UpdateOsmoEquivalentMultipliers(ctx sdk.Context, asset types.Sup } twap := k.calculateOsmoBackingPerShare(pool, osmoPoolAsset) + fmt.Println(poolId) + fmt.Println(twap) k.SetOsmoEquivalentMultiplier(ctx, newEpochNumber, asset.Denom, twap) } else if asset.AssetType == types.SuperfluidAssetTypeNative { // TODO: Consider deleting superfluid asset type native diff --git a/x/superfluid/keeper/hooks_test.go b/x/superfluid/keeper/hooks_test.go index 9e427887ef4..f707ca61d58 100644 --- a/x/superfluid/keeper/hooks_test.go +++ b/x/superfluid/keeper/hooks_test.go @@ -1,10 +1,8 @@ package keeper_test import ( - "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/tendermint/tendermint/crypto/ed25519" ) func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { @@ -35,27 +33,18 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { intermediaryAccs, locks := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations, denoms) suite.checkIntermediaryAccountDelegations(intermediaryAccs) - // gamm swap operation before refresh - acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + // run swap and set spot price pool, err := suite.App.GAMMKeeper.GetPool(suite.Ctx, poolIds[0]) suite.Require().NoError(err) poolAssets := pool.GetAllPoolAssets() - - coins := sdk.Coins{sdk.NewInt64Coin(poolAssets[1].Token.Denom, 100000000000000)} - err = simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, acc1, coins) - suite.Require().NoError(err) - _, _, err = suite.App.GAMMKeeper.SwapExactAmountOut( - suite.Ctx, acc1, - poolIds[0], poolAssets[1].Token.Denom, poolAssets[1].Token.Amount, - sdk.NewCoin(poolAssets[0].Token.Denom, poolAssets[0].Token.Amount.Quo(sdk.NewInt(4)))) - suite.Require().NoError(err) + suite.balancerTestHelper.SwapAndSetSpotPrice(poolIds[0], poolAssets[1], poolAssets[0]) // run epoch actions suite.BeginNewBlock(true) // check lptoken twap value set - newEpochTwap := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0]) - suite.Require().Equal(newEpochTwap, sdk.NewDec(15)) + newEpochMultiplier := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0]) + suite.Require().Equal(newEpochMultiplier, sdk.NewDec(15)) // check gauge creation in new block intermediaryAccAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, locks[0].ID) diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index 74b50298c87..ad020daea1b 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "testing" "time" @@ -11,6 +10,8 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/osmosis-labs/osmosis/v7/app" "github.com/osmosis-labs/osmosis/v7/app/apptesting" + "github.com/osmosis-labs/osmosis/v7/app/apptesting/balancertesting" + "github.com/osmosis-labs/osmosis/v7/app/apptesting/lockuptesting" "github.com/osmosis-labs/osmosis/v7/x/superfluid/types" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -18,36 +19,27 @@ import ( "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" "github.com/tendermint/tendermint/crypto/ed25519" - lockupkeeper "github.com/osmosis-labs/osmosis/v7/x/lockup/keeper" - epochtypes "github.com/osmosis-labs/osmosis/v7/x/epochs/types" gammtypes "github.com/osmosis-labs/osmosis/v7/x/gamm/types" - lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" minttypes "github.com/osmosis-labs/osmosis/v7/x/mint/types" ) type KeeperTestSuite struct { apptesting.KeeperTestHelper - queryClient types.QueryClient -} + balancerTestHelper balancertesting.BalancerTestHelper + lockupTestHelper lockuptesting.LockupTestHelper -func (suite *KeeperTestSuite) GetSuite() *suite.Suite { - return &suite.Suite -} -func (suite *KeeperTestSuite) GetCtx() sdk.Context { - return suite.Ctx -} -func (suite *KeeperTestSuite) GetApp() *app.OsmosisApp { - return suite.App -} -func (suite *KeeperTestSuite) SetCtx(Ctx sdk.Context) { - suite.Ctx = Ctx + queryClient types.QueryClient } func (suite *KeeperTestSuite) SetupTest() { suite.App = app.Setup(false) + // make reference to keepertesthelpers + suite.balancerTestHelper.KeeperTestHelper = &suite.KeeperTestHelper + suite.lockupTestHelper.KeeperTestHelper = &suite.KeeperTestHelper + startTime := time.Unix(1645580000, 0) suite.Ctx = suite.App.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: startTime.UTC()}) queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) @@ -137,15 +129,6 @@ func (suite *KeeperTestSuite) createGammPool(denoms []string) uint64 { return poolId } -func (suite *KeeperTestSuite) LockTokens(addr sdk.AccAddress, coins sdk.Coins, duration time.Duration) (lockID uint64) { - msgServer := lockupkeeper.NewMsgServerImpl(suite.App.LockupKeeper) - err := simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, addr, coins) - suite.Require().NoError(err) - msgResponse, err := msgServer.LockTokens(sdk.WrapSDKContext(suite.Ctx), lockuptypes.NewMsgLockTokens(addr, duration, coins)) - suite.Require().NoError(err) - return msgResponse.ID -} - func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondStatus) []sdk.ValAddress { valAddrs := []sdk.ValAddress{} for _, status := range bondStatuses { @@ -156,52 +139,13 @@ func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondSt } func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) ([]string, []uint64) { - suite.App.GAMMKeeper.SetParams(suite.Ctx, gammtypes.Params{ - PoolCreationFee: sdk.Coins{}, - }) - - bondDenom := suite.App.StakingKeeper.BondDenom(suite.Ctx) + pools := suite.balancerTestHelper.SetupGammPoolsWithBondDenomMultiplier(multipliers) - acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) denoms := []string{} poolIds := []uint64{} - - for index, multiplier := range multipliers { - token := fmt.Sprintf("token%d", index) - - uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() - - err := simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, acc1, sdk.NewCoins( - sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), - sdk.NewInt64Coin(token, 100000), - )) - suite.NoError(err) - - var ( - defaultFutureGovernor = "" - - // pool assets - defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ - Weight: sdk.NewInt(100), - Token: sdk.NewCoin(bondDenom, uosmoAmount), - } - defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ - Weight: sdk.NewInt(100), - Token: sdk.NewCoin(token, sdk.NewInt(10000)), - } - poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} - ) - - poolId, err := suite.App.GAMMKeeper.CreateBalancerPool(suite.Ctx, acc1, balancer.PoolParams{ - SwapFee: sdk.NewDecWithPrec(1, 2), - ExitFee: sdk.NewDecWithPrec(1, 2), - }, poolAssets, defaultFutureGovernor) - suite.Require().NoError(err) - - pool, err := suite.App.GAMMKeeper.GetPool(suite.Ctx, poolId) - suite.Require().NoError(err) - + for _, pool := range pools { denom := pool.GetTotalShares().Denom + suite.App.SuperfluidKeeper.AddNewSuperfluidAsset(suite.Ctx, types.SuperfluidAsset{ Denom: denom, AssetType: types.SuperfluidAssetTypeLPShare, @@ -213,9 +157,10 @@ func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sd AssetType: types.SuperfluidAssetTypeLPShare, }) - poolIds = append(poolIds, poolId) denoms = append(denoms, denom) + poolIds = append(poolIds, pool.GetId()) } + return denoms, poolIds } diff --git a/x/superfluid/keeper/stake_test.go b/x/superfluid/keeper/stake_test.go index c1aa564b9bd..cb32b5b15d8 100644 --- a/x/superfluid/keeper/stake_test.go +++ b/x/superfluid/keeper/stake_test.go @@ -86,7 +86,7 @@ func (suite *KeeperTestSuite) SetupSuperfluidDelegate(delAddr sdk.AccAddress, va coins := sdk.Coins{sdk.NewInt64Coin(denom, amount)} lastLockID := suite.App.LockupKeeper.GetLastLockID(suite.Ctx) - lockID := suite.LockTokens(delAddr, coins, unbondingDuration) + lockID := suite.lockupTestHelper.LockTokens(delAddr, coins, unbondingDuration) lock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockID) suite.Require().NoError(err) From 7d050216389229fe45feaa8a87c942e09deda9f3 Mon Sep 17 00:00:00 2001 From: mattverse Date: Thu, 10 Mar 2022 22:45:00 +0900 Subject: [PATCH 06/10] Delete fmt --- x/incentives/keeper/distribute.go | 3 --- x/superfluid/keeper/epoch.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index 5d1d590b981..973038cad7d 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -368,9 +368,6 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, er locksByDenomCache := make(map[string][]lockuptypes.PeriodLock) totalDistributedCoins := sdk.Coins{} for _, gauge := range gauges { - if gauge.Id == uint64(10){ - fmt.Println("aaa") - } filteredLocks := k.getDistributeToBaseLocks(ctx, gauge, locksByDenomCache) // send based on synthetic lockup coins if it's distributing to synthetic lockups var gaugeDistributedCoins sdk.Coins diff --git a/x/superfluid/keeper/epoch.go b/x/superfluid/keeper/epoch.go index 57dcbba407e..00425675c8c 100644 --- a/x/superfluid/keeper/epoch.go +++ b/x/superfluid/keeper/epoch.go @@ -2,7 +2,6 @@ package keeper import ( "errors" - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v7/osmoutils" @@ -119,8 +118,6 @@ func (k Keeper) UpdateOsmoEquivalentMultipliers(ctx sdk.Context, asset types.Sup } twap := k.calculateOsmoBackingPerShare(pool, osmoPoolAsset) - fmt.Println(poolId) - fmt.Println(twap) k.SetOsmoEquivalentMultiplier(ctx, newEpochNumber, asset.Denom, twap) } else if asset.AssetType == types.SuperfluidAssetTypeNative { // TODO: Consider deleting superfluid asset type native From 7b278b90d93322dfb0258f8f8653fcf5c01a0f2f Mon Sep 17 00:00:00 2001 From: mattverse Date: Thu, 10 Mar 2022 22:54:04 +0900 Subject: [PATCH 07/10] Fix lint --- app/apptesting/test_suite.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index 73945090054..d95b4d2eab7 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -107,7 +107,8 @@ func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk // allocate reward tokens to distribution module coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20000))} - simapp.FundModuleAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, distrtypes.ModuleName, coins) + err := simapp.FundModuleAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, distrtypes.ModuleName, coins) + keeperTestHelper.Require().NoError(err) // allocate rewards to validator keeperTestHelper.Ctx = keeperTestHelper.Ctx.WithBlockHeight(keeperTestHelper.Ctx.BlockHeight() + 1) From 505dd3cceb107f129c8d4b01d7109437bc9e9eae Mon Sep 17 00:00:00 2001 From: mattverse Date: Fri, 11 Mar 2022 01:59:08 +0900 Subject: [PATCH 08/10] Remove multiple pacakges for apptesting --- app/apptesting/balancertesting/test_suite.go | 88 -------------------- app/apptesting/lockuptesting/test_suite.go | 25 ------ app/apptesting/test_suite.go | 87 +++++++++++++++++++ x/superfluid/keeper/hooks_test.go | 2 +- x/superfluid/keeper/keeper_test.go | 11 +-- x/superfluid/keeper/stake_test.go | 2 +- 6 files changed, 90 insertions(+), 125 deletions(-) delete mode 100644 app/apptesting/balancertesting/test_suite.go delete mode 100644 app/apptesting/lockuptesting/test_suite.go diff --git a/app/apptesting/balancertesting/test_suite.go b/app/apptesting/balancertesting/test_suite.go deleted file mode 100644 index 58216584d3c..00000000000 --- a/app/apptesting/balancertesting/test_suite.go +++ /dev/null @@ -1,88 +0,0 @@ -package balancertesting - -import ( - "fmt" - - "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/osmosis-labs/osmosis/v7/app/apptesting" - "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" - gammtypes "github.com/osmosis-labs/osmosis/v7/x/gamm/types" - "github.com/tendermint/tendermint/crypto/ed25519" -) - -type BalancerTestHelper struct { - *apptesting.KeeperTestHelper -} - -func (balancerTestHelper *BalancerTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []sdk.Dec) []gammtypes.PoolI { - balancerTestHelper.App.GAMMKeeper.SetParams(balancerTestHelper.Ctx, gammtypes.Params{ - PoolCreationFee: sdk.Coins{}, - }) - - bondDenom := balancerTestHelper.App.StakingKeeper.BondDenom(balancerTestHelper.Ctx) - acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) - - pools := []gammtypes.PoolI{} - - for index, multiplier := range multipliers { - token := fmt.Sprintf("token%d", index) - - uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() - - err := simapp.FundAccount(balancerTestHelper.App.BankKeeper, balancerTestHelper.Ctx, acc1, sdk.NewCoins( - sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), - sdk.NewInt64Coin(token, 100000), - )) - balancerTestHelper.NoError(err) - - var ( - defaultFutureGovernor = "" - - // pool assets - defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ - Weight: sdk.NewInt(100), - Token: sdk.NewCoin(bondDenom, uosmoAmount), - } - defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ - Weight: sdk.NewInt(100), - Token: sdk.NewCoin(token, sdk.NewInt(10000)), - } - poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} - ) - - poolId, err := balancerTestHelper.App.GAMMKeeper.CreateBalancerPool(balancerTestHelper.Ctx, acc1, balancer.PoolParams{ - SwapFee: sdk.NewDecWithPrec(1, 2), - ExitFee: sdk.NewDecWithPrec(1, 2), - }, poolAssets, defaultFutureGovernor) - balancerTestHelper.Require().NoError(err) - - pool, err := balancerTestHelper.App.GAMMKeeper.GetPool(balancerTestHelper.Ctx, poolId) - balancerTestHelper.Require().NoError(err) - - pools = append(pools, pool) - } - return pools -} - -// SwapAndSetSpotPrice runs a swap to set Spot price of a pool using arbitrary values -// returns spot price after the arbitrary swap -func (balancerTestHelper *BalancerTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset gammtypes.PoolAsset, toAsset gammtypes.PoolAsset) sdk.Dec { - // create a dummy account - acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) - - // fund dummy account with tokens to swap - coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Token.Denom, 100000000000000)} - err := simapp.FundAccount(balancerTestHelper.App.BankKeeper, balancerTestHelper.Ctx, acc1, coins) - balancerTestHelper.Require().NoError(err) - - _, _, err = balancerTestHelper.App.GAMMKeeper.SwapExactAmountOut( - balancerTestHelper.Ctx, acc1, - poolId, fromAsset.Token.Denom, fromAsset.Token.Amount, - sdk.NewCoin(toAsset.Token.Denom, toAsset.Token.Amount.Quo(sdk.NewInt(4)))) - balancerTestHelper.Require().NoError(err) - - spotPrice, err := balancerTestHelper.App.GAMMKeeper.CalculateSpotPrice(balancerTestHelper.Ctx, poolId, toAsset.Token.Denom, fromAsset.Token.Denom) - balancerTestHelper.Require().NoError(err) - return spotPrice -} diff --git a/app/apptesting/lockuptesting/test_suite.go b/app/apptesting/lockuptesting/test_suite.go deleted file mode 100644 index 4ea8e45aa62..00000000000 --- a/app/apptesting/lockuptesting/test_suite.go +++ /dev/null @@ -1,25 +0,0 @@ -package lockuptesting - -import ( - "time" - - "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/osmosis-labs/osmosis/v7/app/apptesting" - - lockupkeeper "github.com/osmosis-labs/osmosis/v7/x/lockup/keeper" - lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" -) - -type LockupTestHelper struct { - *apptesting.KeeperTestHelper -} - -func (lockupTestHelper *LockupTestHelper) LockTokens(addr sdk.AccAddress, coins sdk.Coins, duration time.Duration) (lockID uint64) { - msgServer := lockupkeeper.NewMsgServerImpl(lockupTestHelper.App.LockupKeeper) - err := simapp.FundAccount(lockupTestHelper.App.BankKeeper, lockupTestHelper.Ctx, addr, coins) - lockupTestHelper.Require().NoError(err) - msgResponse, err := msgServer.LockTokens(sdk.WrapSDKContext(lockupTestHelper.Ctx), lockuptypes.NewMsgLockTokens(addr, duration, coins)) - lockupTestHelper.Require().NoError(err) - return msgResponse.ID -} diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index d95b4d2eab7..ca885490724 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -17,7 +17,13 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/osmosis-labs/osmosis/v7/app" + "github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer" + gammtypes "github.com/osmosis-labs/osmosis/v7/x/gamm/types" + lockupkeeper "github.com/osmosis-labs/osmosis/v7/x/lockup/keeper" + lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/crypto/ed25519" ) type KeeperTestHelper struct { @@ -116,3 +122,84 @@ func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk keeperTestHelper.App.DistrKeeper.AllocateTokensToValidator(keeperTestHelper.Ctx, validator, decTokens) keeperTestHelper.App.DistrKeeper.IncrementValidatorPeriod(keeperTestHelper.Ctx, validator) } + +func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []sdk.Dec) []gammtypes.PoolI { + keeperTestHelper.App.GAMMKeeper.SetParams(keeperTestHelper.Ctx, gammtypes.Params{ + PoolCreationFee: sdk.Coins{}, + }) + + bondDenom := keeperTestHelper.App.StakingKeeper.BondDenom(keeperTestHelper.Ctx) + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + + pools := []gammtypes.PoolI{} + + for index, multiplier := range multipliers { + token := fmt.Sprintf("token%d", index) + + uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt() + + err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, sdk.NewCoins( + sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))), + sdk.NewInt64Coin(token, 100000), + )) + keeperTestHelper.NoError(err) + + var ( + defaultFutureGovernor = "" + + // pool assets + defaultFooAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin(bondDenom, uosmoAmount), + } + defaultBarAsset gammtypes.PoolAsset = gammtypes.PoolAsset{ + Weight: sdk.NewInt(100), + Token: sdk.NewCoin(token, sdk.NewInt(10000)), + } + poolAssets []gammtypes.PoolAsset = []gammtypes.PoolAsset{defaultFooAsset, defaultBarAsset} + ) + + poolId, err := keeperTestHelper.App.GAMMKeeper.CreateBalancerPool(keeperTestHelper.Ctx, acc1, balancer.PoolParams{ + SwapFee: sdk.NewDecWithPrec(1, 2), + ExitFee: sdk.NewDecWithPrec(1, 2), + }, poolAssets, defaultFutureGovernor) + keeperTestHelper.Require().NoError(err) + + pool, err := keeperTestHelper.App.GAMMKeeper.GetPool(keeperTestHelper.Ctx, poolId) + keeperTestHelper.Require().NoError(err) + + pools = append(pools, pool) + } + return pools +} + +// SwapAndSetSpotPrice runs a swap to set Spot price of a pool using arbitrary values +// returns spot price after the arbitrary swap +func (keeperTestHelper *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fromAsset gammtypes.PoolAsset, toAsset gammtypes.PoolAsset) sdk.Dec { + // create a dummy account + acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + + // fund dummy account with tokens to swap + coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Token.Denom, 100000000000000)} + err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, coins) + keeperTestHelper.Require().NoError(err) + + _, _, err = keeperTestHelper.App.GAMMKeeper.SwapExactAmountOut( + keeperTestHelper.Ctx, acc1, + poolId, fromAsset.Token.Denom, fromAsset.Token.Amount, + sdk.NewCoin(toAsset.Token.Denom, toAsset.Token.Amount.Quo(sdk.NewInt(4)))) + keeperTestHelper.Require().NoError(err) + + spotPrice, err := keeperTestHelper.App.GAMMKeeper.CalculateSpotPrice(keeperTestHelper.Ctx, poolId, toAsset.Token.Denom, fromAsset.Token.Denom) + keeperTestHelper.Require().NoError(err) + return spotPrice +} + +func (keeperTestHelper *KeeperTestHelper) LockTokens(addr sdk.AccAddress, coins sdk.Coins, duration time.Duration) (lockID uint64) { + msgServer := lockupkeeper.NewMsgServerImpl(keeperTestHelper.App.LockupKeeper) + err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, addr, coins) + keeperTestHelper.Require().NoError(err) + msgResponse, err := msgServer.LockTokens(sdk.WrapSDKContext(keeperTestHelper.Ctx), lockuptypes.NewMsgLockTokens(addr, duration, coins)) + keeperTestHelper.Require().NoError(err) + return msgResponse.ID +} diff --git a/x/superfluid/keeper/hooks_test.go b/x/superfluid/keeper/hooks_test.go index f707ca61d58..0753ed5cc13 100644 --- a/x/superfluid/keeper/hooks_test.go +++ b/x/superfluid/keeper/hooks_test.go @@ -37,7 +37,7 @@ func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() { pool, err := suite.App.GAMMKeeper.GetPool(suite.Ctx, poolIds[0]) suite.Require().NoError(err) poolAssets := pool.GetAllPoolAssets() - suite.balancerTestHelper.SwapAndSetSpotPrice(poolIds[0], poolAssets[1], poolAssets[0]) + suite.SwapAndSetSpotPrice(poolIds[0], poolAssets[1], poolAssets[0]) // run epoch actions suite.BeginNewBlock(true) diff --git a/x/superfluid/keeper/keeper_test.go b/x/superfluid/keeper/keeper_test.go index ad020daea1b..54b0444f83b 100644 --- a/x/superfluid/keeper/keeper_test.go +++ b/x/superfluid/keeper/keeper_test.go @@ -10,8 +10,6 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/osmosis-labs/osmosis/v7/app" "github.com/osmosis-labs/osmosis/v7/app/apptesting" - "github.com/osmosis-labs/osmosis/v7/app/apptesting/balancertesting" - "github.com/osmosis-labs/osmosis/v7/app/apptesting/lockuptesting" "github.com/osmosis-labs/osmosis/v7/x/superfluid/types" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -27,19 +25,12 @@ import ( type KeeperTestSuite struct { apptesting.KeeperTestHelper - balancerTestHelper balancertesting.BalancerTestHelper - lockupTestHelper lockuptesting.LockupTestHelper - queryClient types.QueryClient } func (suite *KeeperTestSuite) SetupTest() { suite.App = app.Setup(false) - // make reference to keepertesthelpers - suite.balancerTestHelper.KeeperTestHelper = &suite.KeeperTestHelper - suite.lockupTestHelper.KeeperTestHelper = &suite.KeeperTestHelper - startTime := time.Unix(1645580000, 0) suite.Ctx = suite.App.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: startTime.UTC()}) queryHelper := baseapp.NewQueryServerTestHelper(suite.Ctx, suite.App.InterfaceRegistry()) @@ -139,7 +130,7 @@ func (suite *KeeperTestSuite) SetupValidators(bondStatuses []stakingtypes.BondSt } func (suite *KeeperTestSuite) SetupGammPoolsAndSuperfluidAssets(multipliers []sdk.Dec) ([]string, []uint64) { - pools := suite.balancerTestHelper.SetupGammPoolsWithBondDenomMultiplier(multipliers) + pools := suite.SetupGammPoolsWithBondDenomMultiplier(multipliers) denoms := []string{} poolIds := []uint64{} diff --git a/x/superfluid/keeper/stake_test.go b/x/superfluid/keeper/stake_test.go index d1a34284309..bbe3808378b 100644 --- a/x/superfluid/keeper/stake_test.go +++ b/x/superfluid/keeper/stake_test.go @@ -86,7 +86,7 @@ func (suite *KeeperTestSuite) SetupSuperfluidDelegate(delAddr sdk.AccAddress, va coins := sdk.Coins{sdk.NewInt64Coin(denom, amount)} lastLockID := suite.App.LockupKeeper.GetLastLockID(suite.Ctx) - lockID := suite.lockupTestHelper.LockTokens(delAddr, coins, unbondingDuration) + lockID := suite.LockTokens(delAddr, coins, unbondingDuration) lock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockID) suite.Require().NoError(err) From be3a65eafd8ebc2809b5d3d6c21fd6445233d480 Mon Sep 17 00:00:00 2001 From: mattverse Date: Fri, 11 Mar 2022 11:40:36 +0900 Subject: [PATCH 09/10] Add parameter for reward allocation --- app/apptesting/test_suite.go | 4 ++-- x/superfluid/keeper/distribution_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index ca885490724..c8f9709ce91 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -107,12 +107,12 @@ func (keeperTestHelper *KeeperTestHelper) EndBlock() { keeperTestHelper.App.EndBlocker(keeperTestHelper.Ctx, reqEndBlock) } -func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk.ValAddress) { +func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk.ValAddress, rewardAmt sdk.Int) { validator, found := keeperTestHelper.App.StakingKeeper.GetValidator(keeperTestHelper.Ctx, valAddr) keeperTestHelper.Require().True(found) // allocate reward tokens to distribution module - coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20000))} + coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, rewardAmt)} err := simapp.FundModuleAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, distrtypes.ModuleName, coins) keeperTestHelper.Require().NoError(err) diff --git a/x/superfluid/keeper/distribution_test.go b/x/superfluid/keeper/distribution_test.go index ff73e540cb7..50086902853 100644 --- a/x/superfluid/keeper/distribution_test.go +++ b/x/superfluid/keeper/distribution_test.go @@ -76,7 +76,7 @@ func (suite *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() { // allocate rewards to first validator for _, valIndex := range tc.rewardedVals { - suite.AllocateRewardsToValidator(valAddrs[valIndex]) + suite.AllocateRewardsToValidator(valAddrs[valIndex], sdk.NewInt(20000)) } // move intermediary account delegation rewards to gauges From 2390648865225b2fa622e8c24f818849fe48ca56 Mon Sep 17 00:00:00 2001 From: mattverse Date: Mon, 14 Mar 2022 13:03:55 +0900 Subject: [PATCH 10/10] Improvements on test --- app/apptesting/test_suite.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/apptesting/test_suite.go b/app/apptesting/test_suite.go index c8f9709ce91..ff46f5a2714 100644 --- a/app/apptesting/test_suite.go +++ b/app/apptesting/test_suite.go @@ -120,17 +120,23 @@ func (keeperTestHelper *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk keeperTestHelper.Ctx = keeperTestHelper.Ctx.WithBlockHeight(keeperTestHelper.Ctx.BlockHeight() + 1) decTokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(20000)}} keeperTestHelper.App.DistrKeeper.AllocateTokensToValidator(keeperTestHelper.Ctx, validator, decTokens) - keeperTestHelper.App.DistrKeeper.IncrementValidatorPeriod(keeperTestHelper.Ctx, validator) } +// SetupGammPoolsWithBondDenomMultiplier uses given multipliers to set initial pool supply of bond denom. func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(multipliers []sdk.Dec) []gammtypes.PoolI { keeperTestHelper.App.GAMMKeeper.SetParams(keeperTestHelper.Ctx, gammtypes.Params{ PoolCreationFee: sdk.Coins{}, }) bondDenom := keeperTestHelper.App.StakingKeeper.BondDenom(keeperTestHelper.Ctx) + //TODO: use sdk crypto instead of tendermint to generate address acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) + //fund account with pool creation fee + poolCreationFee := keeperTestHelper.App.GAMMKeeper.GetParams(keeperTestHelper.Ctx) + err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, poolCreationFee.PoolCreationFee) + keeperTestHelper.Require().NoError(err) + pools := []gammtypes.PoolI{} for index, multiplier := range multipliers {