Skip to content

Commit

Permalink
bank supply offsets (#923)
Browse files Browse the repository at this point in the history
* update cosmos sdk version to branch with supply offsets

* add supply offset changes to mint and burn functions in superfluid

* add tests for supply changes

* go mod update
  • Loading branch information
sunnya97 authored Feb 21, 2022
1 parent cd53acb commit 09f20ce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ require (
replace (
github.com/CosmWasm/wasmd => github.com/osmosis-labs/wasmd v0.22.0-osmo-alpha-0
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk v0.45.0x-osmo-v7
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220220082240-1b4ae82744ea
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220221115656-75545ea8cb2d

github.com/cosmos/iavl => github.com/osmosis-labs/iavl v0.17.3-osmo-v3
// Use osmosis fork of ibc-go
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnh
github.com/ory/dockertest/v3 v3.6.2/go.mod h1:EFLcVUOl8qCwp9NyDAcCDtq/QviLtYswW/VbWzUnTNE=
github.com/osmosis-labs/bech32-ibc v0.2.0-rc2 h1:7xy1pLtNiF2KaRSkolayZf4z3OfCJsO3eqBtEAXg2VA=
github.com/osmosis-labs/bech32-ibc v0.2.0-rc2/go.mod h1:0JCaioRNOVUiw7c3MngmKACnumaQ2sjPenXCnwxCttI=
github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220220082240-1b4ae82744ea h1:blDrOjZ2V46JEYWYr6Ikffc8euk/8uvL61hKC8zwAvQ=
github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220220082240-1b4ae82744ea/go.mod h1:HsD/JxengDcevHFRA+wlWgeM78Og3ZwYM+cya3yOa7c=
github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220221115656-75545ea8cb2d h1:Y0JpKFb4n04ViTMUGm7SO4OobWKr6dBB/l3cvuBe6EY=
github.com/osmosis-labs/cosmos-sdk v0.45.1-0.20220221115656-75545ea8cb2d/go.mod h1:HsD/JxengDcevHFRA+wlWgeM78Og3ZwYM+cya3yOa7c=
github.com/osmosis-labs/iavl v0.17.3-osmo-v3 h1:q2Qv3+DK52w5b68I96VApHI05jBu7HeQK/YDttKh/jY=
github.com/osmosis-labs/iavl v0.17.3-osmo-v3/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/ibc-go/v2 v2.0.2-osmo h1:XyYyDTjPIu7qX2nhQp9mboj7Pa9FEnjg1RXw73Ctv5U=
Expand Down
3 changes: 3 additions & 0 deletions x/superfluid/keeper/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func (k Keeper) mintOsmoTokensAndDelegate(ctx sdk.Context, osmoAmount sdk.Int, i
if err != nil {
return err
}
k.bk.AddSupplyOffset(ctx, bondDenom, osmoAmount.Neg())
err = k.bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, intermediaryAccount.GetAccAddress(), coins)
if err != nil {
return err
Expand Down Expand Up @@ -318,6 +319,8 @@ func (k Keeper) forceUndelegateAndBurnOsmoTokens(ctx sdk.Context,
return err
}
err = k.bk.BurnCoins(ctx, types.ModuleName, undelegatedCoins)
bondDenom := k.sk.BondDenom(ctx)
k.bk.AddSupplyOffset(ctx, bondDenom, undelegatedCoins.AmountOf(bondDenom))
return err
}

Expand Down
37 changes: 36 additions & 1 deletion x/superfluid/keeper/stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,29 @@ func (suite *KeeperTestSuite) TestSuperfluidDelegate() {

poolId := suite.createGammPool([]string{appparams.BaseCoinUnit, "foo"})
suite.Require().Equal(poolId, uint64(1))
bondDenom := suite.app.StakingKeeper.BondDenom(suite.ctx)

// Generate delegator addresses
delAddrs := CreateRandomAccounts(1)

// setup validators
valAddrs := suite.SetupValidators(tc.validatorStats)

// 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)

// 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))

unbondingDuration := suite.app.StakingKeeper.GetParams(suite.ctx).UnbondingTime

// setup superfluid delegations
for index, del := range tc.superDelegations {
lock := locks[index]
valAddr := valAddrs[del.valIndex]
Expand Down Expand Up @@ -359,6 +372,7 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() {

poolId := suite.createGammPool([]string{appparams.BaseCoinUnit, "foo"})
suite.Require().Equal(poolId, uint64(1))
bondDenom := suite.app.StakingKeeper.BondDenom(suite.ctx)

// Generate delegator addresses
delAddrs := CreateRandomAccounts(1)
Expand Down Expand Up @@ -393,6 +407,10 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() {
lock = &lockuptypes.PeriodLock{}
}

// get pre-superfluid delgations osmo supply and supplyWithOffset
presupply := suite.app.BankKeeper.GetSupply(suite.ctx, bondDenom)
presupplyWithOffset := suite.app.BankKeeper.GetSupplyWithOffset(suite.ctx, bondDenom)

// superfluid undelegate
err = suite.app.SuperfluidKeeper.SuperfluidUndelegate(suite.ctx, lock.Owner, lockId)
if tc.expSuperUnbondingErr[index] {
Expand All @@ -401,6 +419,12 @@ func (suite *KeeperTestSuite) TestSuperfluidUndelegate() {
}
suite.Require().NoError(err)

// 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))

// check lockId and intermediary account connection deletion
addr := suite.app.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.ctx, lockId)
suite.Require().Equal(addr.String(), "")
Expand Down Expand Up @@ -689,6 +713,7 @@ func (suite *KeeperTestSuite) TestRefreshIntermediaryDelegationAmounts() {
params := suite.app.SuperfluidKeeper.GetParams(suite.ctx)
poolId := suite.createGammPool([]string{appparams.BaseCoinUnit, "foo"})
suite.Require().Equal(poolId, uint64(1))
bondDenom := suite.app.StakingKeeper.BondDenom(suite.ctx)

// Generate delegator addresses
delAddrs := CreateRandomAccounts(1)
Expand Down Expand Up @@ -723,11 +748,21 @@ func (suite *KeeperTestSuite) TestRefreshIntermediaryDelegationAmounts() {
CurrentEpoch: 2,
})

// get pre-superfluid delgations osmo supply and supplyWithOffset
presupply := suite.app.BankKeeper.GetSupply(suite.ctx, bondDenom)
presupplyWithOffset := suite.app.BankKeeper.GetSupplyWithOffset(suite.ctx, bondDenom)

// refresh intermediary account delegations
suite.NotPanics(func() {
suite.app.SuperfluidKeeper.RefreshIntermediaryDelegationAmounts(suite.ctx)
})

// 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))

originTwap := sdk.NewDec(20)
targetDelegations := []sdk.Dec{}
targetAmounts := []sdk.Int{}
Expand Down
3 changes: 3 additions & 0 deletions x/superfluid/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type BankKeeper interface {
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error
GetSupplyOffset(ctx sdk.Context, denom string) sdk.Int
AddSupplyOffset(ctx sdk.Context, denom string, offsetAmount sdk.Int)
GetSupplyWithOffset(ctx sdk.Context, denom string) sdk.Coin
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
}
Expand Down

0 comments on commit 09f20ce

Please sign in to comment.