From 46fda09023843d927b276899dfb2e5822654d808 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 3 Apr 2023 21:53:42 -0700 Subject: [PATCH 1/6] added gas cost for add reward to gauge --- x/incentives/keeper/gauge.go | 4 ++ x/incentives/keeper/gauge_test.go | 77 +++++++++++++++++++++++++++++++ x/incentives/types/constants.go | 5 ++ 3 files changed, 86 insertions(+) create mode 100644 x/incentives/types/constants.go diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index c69727b1405..f66d8634015 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -152,6 +152,10 @@ func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins s if gauge.IsFinishedGauge(ctx.BlockTime()) { return errors.New("gauge is already completed") } + + // Fixed gas consumption adding reward to gauges based on the number of coins to add + ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForAddRewardToGauge*len(coins)), "add to gauge reward cost") + if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil { return err } diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index bc8a0c4f25b..e8672d41a10 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -332,3 +332,80 @@ func (suite *KeeperTestSuite) TestChargeFeeIfSufficientFeeDenomBalance() { }) } } + +func (suite *KeeperTestSuite) TestAddToGaugeRewards() { + testCases := []struct { + name string + owner sdk.AccAddress + coinsToAdd sdk.Coins + gaugeId uint64 + minimumGasConsumed uint64 + + expectErr bool + }{ + { + name: "valid case: valid gauge", + owner: suite.TestAccs[0], + coinsToAdd: sdk.NewCoins( + sdk.NewCoin("uosmo", sdk.NewInt(100000)), + sdk.NewCoin("atom", sdk.NewInt(99999)), + ), + gaugeId: 1, + minimumGasConsumed: uint64(2 * types.BaseGasFeeForAddRewardToGauge), + + expectErr: false, + }, + { + name: "invalid case: gauge Id is not valid", + owner: suite.TestAccs[0], + coinsToAdd: sdk.NewCoins( + sdk.NewCoin("uosmo", sdk.NewInt(100000)), + sdk.NewCoin("atom", sdk.NewInt(99999)), + ), + gaugeId: 0, + minimumGasConsumed: uint64(0), + + expectErr: true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() + _, _, existingGaugeCoins, _ := suite.SetupNewGauge(true, sdk.Coins{sdk.NewInt64Coin("stake", 12)}) + + suite.FundAcc(tc.owner, tc.coinsToAdd) + + existingGasConsumed := suite.Ctx.GasMeter().GasConsumed() + + err := suite.App.IncentivesKeeper.AddToGaugeRewards(suite.Ctx, tc.owner, tc.coinsToAdd, tc.gaugeId) + if tc.expectErr { + suite.Require().Error(err) + + // balance shouldn't change in the module + balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, suite.App.AccountKeeper.GetModuleAddress(types.ModuleName)) + suite.Require().Equal(existingGaugeCoins, balance) + + } else { + suite.Require().NoError(err) + + // Ensure that at least the minimum amount of gas was charged (based on number of additional gauge coins) + gasConsumed := suite.Ctx.GasMeter().GasConsumed() - existingGasConsumed + suite.Require().True(gasConsumed >= tc.minimumGasConsumed) + + // existing coins gets added to the module when we create gauge and add to gauge + expectedCoins := existingGaugeCoins.Add(tc.coinsToAdd...) + + // check module account balance, should go up + balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, suite.App.AccountKeeper.GetModuleAddress(types.ModuleName)) + suite.Require().Equal(expectedCoins, balance) + + // check gauge coins should go up + gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, tc.gaugeId) + suite.Require().NoError(err) + + suite.Require().Equal(expectedCoins, gauge.Coins) + } + }) + } +} diff --git a/x/incentives/types/constants.go b/x/incentives/types/constants.go new file mode 100644 index 00000000000..eab79b004b9 --- /dev/null +++ b/x/incentives/types/constants.go @@ -0,0 +1,5 @@ +package types + +var ( + BaseGasFeeForAddRewardToGauge = 10_000 +) From d52a66a99e3b77c7b3e25e1d9dc1d6a31c4d5567 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 3 Apr 2023 22:44:24 -0700 Subject: [PATCH 2/6] added more test --- x/incentives/keeper/gauge.go | 2 +- x/incentives/keeper/gauge_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index f66d8634015..4361dc2b94c 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -154,7 +154,7 @@ func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins s } // Fixed gas consumption adding reward to gauges based on the number of coins to add - ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForAddRewardToGauge*len(coins)), "add to gauge reward cost") + ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForAddRewardToGauge*len(coins)), "scaling gas cost for adding to gauge rewards") if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil { return err diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index e8672d41a10..d1784e3170a 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -355,6 +355,24 @@ func (suite *KeeperTestSuite) TestAddToGaugeRewards() { expectErr: false, }, + { + name: "valid case: valid gauge with >4 denoms", + owner: suite.TestAccs[0], + coinsToAdd: sdk.NewCoins( + sdk.NewCoin("uosmo", sdk.NewInt(100000)), + sdk.NewCoin("atom", sdk.NewInt(99999)), + sdk.NewCoin("mars", sdk.NewInt(88888)), + sdk.NewCoin("akash", sdk.NewInt(77777)), + sdk.NewCoin("eth", sdk.NewInt(6666)), + sdk.NewCoin("usdc", sdk.NewInt(555)), + sdk.NewCoin("dai", sdk.NewInt(4444)), + sdk.NewCoin("ust", sdk.NewInt(3333)), + ), + gaugeId: 1, + minimumGasConsumed: uint64(4 * types.BaseGasFeeForAddRewardToGauge), + + expectErr: false, + }, { name: "invalid case: gauge Id is not valid", owner: suite.TestAccs[0], From b582f2ddfa40c43d85475164c005ee6e5736e032 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 3 Apr 2023 22:46:19 -0700 Subject: [PATCH 3/6] nit --- x/incentives/keeper/gauge_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index d1784e3170a..ef672054e85 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" "time" "github.com/stretchr/testify/suite" @@ -369,7 +370,7 @@ func (suite *KeeperTestSuite) TestAddToGaugeRewards() { sdk.NewCoin("ust", sdk.NewInt(3333)), ), gaugeId: 1, - minimumGasConsumed: uint64(4 * types.BaseGasFeeForAddRewardToGauge), + minimumGasConsumed: uint64(8 * types.BaseGasFeeForAddRewardToGauge), expectErr: false, }, @@ -409,6 +410,7 @@ func (suite *KeeperTestSuite) TestAddToGaugeRewards() { // Ensure that at least the minimum amount of gas was charged (based on number of additional gauge coins) gasConsumed := suite.Ctx.GasMeter().GasConsumed() - existingGasConsumed + fmt.Println(gasConsumed, tc.minimumGasConsumed) suite.Require().True(gasConsumed >= tc.minimumGasConsumed) // existing coins gets added to the module when we create gauge and add to gauge From 3d33969904d4d7411f78f1b207f4606211fd2dec Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 3 Apr 2023 22:46:33 -0700 Subject: [PATCH 4/6] more nit --- x/incentives/keeper/gauge_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index ef672054e85..f961ac4ae1f 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "time" "github.com/stretchr/testify/suite" @@ -410,7 +409,6 @@ func (suite *KeeperTestSuite) TestAddToGaugeRewards() { // Ensure that at least the minimum amount of gas was charged (based on number of additional gauge coins) gasConsumed := suite.Ctx.GasMeter().GasConsumed() - existingGasConsumed - fmt.Println(gasConsumed, tc.minimumGasConsumed) suite.Require().True(gasConsumed >= tc.minimumGasConsumed) // existing coins gets added to the module when we create gauge and add to gauge From 813843616a404f952d48b0abe4b6b09123c0e04b Mon Sep 17 00:00:00 2001 From: stackman27 Date: Mon, 3 Apr 2023 23:00:17 -0700 Subject: [PATCH 5/6] fixed calc --- x/incentives/keeper/gauge.go | 2 +- x/incentives/keeper/gauge_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index 4361dc2b94c..4df9b666feb 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -154,7 +154,7 @@ func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins s } // Fixed gas consumption adding reward to gauges based on the number of coins to add - ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForAddRewardToGauge*len(coins)), "scaling gas cost for adding to gauge rewards") + ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForAddRewardToGauge*(len(coins)+len(gauge.Coins))), "scaling gas cost for adding to gauge rewards") if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil { return err diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index f961ac4ae1f..ef672054e85 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" "time" "github.com/stretchr/testify/suite" @@ -409,6 +410,7 @@ func (suite *KeeperTestSuite) TestAddToGaugeRewards() { // Ensure that at least the minimum amount of gas was charged (based on number of additional gauge coins) gasConsumed := suite.Ctx.GasMeter().GasConsumed() - existingGasConsumed + fmt.Println(gasConsumed, tc.minimumGasConsumed) suite.Require().True(gasConsumed >= tc.minimumGasConsumed) // existing coins gets added to the module when we create gauge and add to gauge From 97837cbdfe70c153decb7593a91ae5c833a4eb41 Mon Sep 17 00:00:00 2001 From: stackman27 Date: Tue, 11 Apr 2023 12:11:14 -0700 Subject: [PATCH 6/6] added changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa6001fa312..5e0e1f123f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#4659](https://github.com/osmosis-labs/osmosis/pull/4659) implement AllPools query in x/poolmanager. * [#4783](https://github.com/osmosis-labs/osmosis/pull/4783) Update wasmd to 0.31.0 + * [#4830](https://github.com/osmosis-labs/osmosis/pull/4830) Add gas cost when we AddToGaugeRewards, linearly increase with coins to add ### Misc Improvements