Skip to content

Commit

Permalink
Ensure gauges can only be created for assets that exist on-chain (#855)
Browse files Browse the repository at this point in the history
* ensure gauges can only be created for onchain denoms + comments

* added and fixed tests + cleaned up comments

* added support for superfluid synthetic denoms

* fixed tests to accommodate new checks
  • Loading branch information
AlpinYukseloglu authored Feb 18, 2022
1 parent 3cea329 commit 9eafd1a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions x/incentives/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func TestPerpetualGaugeNotExpireAfterDistribution(t *testing.T) {
Denom: "lptoken",
Duration: time.Second,
}

// mints coins so supply exists on chain
mintLPtokens := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err = simapp.FundAccount(app.BankKeeper, ctx, addr, mintLPtokens)
require.NoError(t, err)

_, err = app.IncentivesKeeper.CreateGauge(ctx, true, addr, coins, distrTo, time.Now(), 1)
require.NoError(t, err)

Expand Down Expand Up @@ -62,6 +68,12 @@ func TestNonPerpetualGaugeExpireAfterDistribution(t *testing.T) {
Denom: "lptoken",
Duration: time.Second,
}

// mints coins so supply exists on chain
mintLPtokens := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err = simapp.FundAccount(app.BankKeeper, ctx, addr, mintLPtokens)
require.NoError(t, err)

_, err = app.IncentivesKeeper.CreateGauge(ctx, false, addr, coins, distrTo, time.Now(), 1)
require.NoError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions x/incentives/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func TestIncentivesExportGenesis(t *testing.T) {
startTime := time.Now()
err := simapp.FundAccount(app.BankKeeper, ctx, addr, coins)
require.NoError(t, err)

// mints coins so supply exists on chain
mintLPtokens := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err = simapp.FundAccount(app.BankKeeper, ctx, addr, mintLPtokens)
require.NoError(t, err)

gaugeID, err := app.IncentivesKeeper.CreateGauge(ctx, true, addr, coins, distrTo, startTime, 1)
require.NoError(t, err)

Expand Down
8 changes: 8 additions & 0 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/json"
"fmt"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -100,6 +101,8 @@ func (k Keeper) SetGaugeWithRefKey(ctx sdk.Context, gauge *types.Gauge) error {

// CreateGauge create a gauge and send coins to the gauge
func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddress, coins sdk.Coins, distrTo lockuptypes.QueryCondition, startTime time.Time, numEpochsPaidOver uint64) (uint64, error) {

// Ensure that this gauge's duration is one of the allowed durations on chain
durations := k.GetLockableDurations(ctx)
if distrTo.LockQueryType == lockuptypes.ByDuration {
durationOk := false
Expand All @@ -114,6 +117,11 @@ func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddr
}
}

// Ensure that the denom this gauge pays out to exists on-chain
if !k.bk.HasSupply(ctx, distrTo.Denom) && !strings.Contains(distrTo.Denom, "osmovaloper") {
return 0, fmt.Errorf("denom does not exist: %s", distrTo.Denom)
}

gauge := types.Gauge{
Id: k.GetLastGaugeID(ctx) + 1,
IsPerpetual: isPerpetual,
Expand Down
17 changes: 17 additions & 0 deletions x/incentives/keeper/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ func (suite *KeeperTestSuite) TestInvalidDurationGaugeCreationValidation() {
suite.Require().NoError(err)
}

func (suite *KeeperTestSuite) TestNonExistentDenomGaugeCreation() {
suite.SetupTest()

addrNoSupply := sdk.AccAddress([]byte("Gauge_Creation_Addr_"))
addrs := suite.SetupManyLocks(1, defaultLiquidTokens, defaultLPTokens, defaultLockDuration)
distrTo := lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: defaultLPDenom,
Duration: defaultLockDuration,
}
_, err := suite.app.IncentivesKeeper.CreateGauge(suite.ctx, false, addrNoSupply, defaultLiquidTokens, distrTo, time.Time{}, 1)
suite.Require().Error(err)

_, err = suite.app.IncentivesKeeper.CreateGauge(suite.ctx, false, addrs[0], defaultLiquidTokens, distrTo, time.Time{}, 1)
suite.Require().NoError(err)
}

// TODO: Make this test table driven
// OR if it needs to be script based,
// remove lots of boilerplate so this can actually be followed
Expand Down
6 changes: 6 additions & 0 deletions x/incentives/keeper/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func (suite *KeeperTestSuite) setupNewGaugeWithDuration(isPerpetual bool, coins
Denom: "lptoken",
Duration: duration,
}

// mints coins so supply exists on chain
mintCoins := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err := simapp.FundAccount(suite.app.BankKeeper, suite.ctx, addr, mintCoins)
suite.Require().NoError(err)

numEpochsPaidOver := uint64(2)
if isPerpetual {
numEpochsPaidOver = uint64(1)
Expand Down
2 changes: 2 additions & 0 deletions x/incentives/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type BankKeeper interface {
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins

HasSupply(ctx sdk.Context, denom string) bool

SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToManyAccounts(
ctx sdk.Context, senderModule string, recipientAddrs []sdk.AccAddress, amts []sdk.Coins,
Expand Down
6 changes: 6 additions & 0 deletions x/mint/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ func setupGaugeForLPIncentives(t *testing.T, app *osmoapp.OsmosisApp, ctx sdk.Co
Denom: "lptoken",
Duration: time.Second,
}

// mints coins so supply exists on chain
mintLPtokens := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err = simapp.FundAccount(app.BankKeeper, ctx, addr, mintLPtokens)
require.NoError(t, err)

_, err = app.IncentivesKeeper.CreateGauge(ctx, true, addr, coins, distrTo, time.Now(), 1)
require.NoError(t, err)
}
6 changes: 6 additions & 0 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func (suite *KeeperTestSuite) TestDistrAssetToDeveloperRewardsAddrWhenNotEmpty()
Denom: "lptoken",
Duration: time.Second,
}

// mints coins so supply exists on chain
mintLPtokens := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
err = simapp.FundAccount(suite.app.BankKeeper, suite.ctx, gaugeCreator, mintLPtokens)
suite.Require().NoError(err)

gaugeId, err := suite.app.IncentivesKeeper.CreateGauge(suite.ctx, true, gaugeCreator, coins, distrTo, time.Now(), 1)
suite.NoError(err)
err = suite.app.PoolIncentivesKeeper.UpdateDistrRecords(suite.ctx, poolincentivestypes.DistrRecord{
Expand Down

0 comments on commit 9eafd1a

Please sign in to comment.