Skip to content

Commit

Permalink
refactor: separate incentives liquidity into a separate module address (
Browse files Browse the repository at this point in the history
#4796)

* refactor: separate incentives liquidity into a separate module address

* update go mod

* lint

* Update x/concentrated-liquidity/incentives.go

Co-authored-by: Adam Tucker <[email protected]>

---------

Co-authored-by: Adam Tucker <[email protected]>
  • Loading branch information
p0mvn and czarcas7ic authored Mar 31, 2023
1 parent a71a52a commit 85ab280
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 111 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/ory/dockertest/v3 v3.9.1
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230328024000-175ec88e4304
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230328175100-184629d4f758
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230330162120-3f56e1ab713b
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230328024000-175ec88e4304
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230328024000-175ec88e430
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230328024000-175ec88e4304/go.mod h1:/h3CZIo25kMrM4Ojm7qBgMxKofTVwOycVWSa4rhEsaM=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230328175100-184629d4f758 h1:rBHpiUpFdNbqFGKijltJ+1TMvowKTDgoY/vSc+ukQYU=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230328175100-184629d4f758/go.mod h1:YFYyDzcapRxYoOGVSyBKk8xPiz6D5bw1p1g3Q1IUofc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230330162120-3f56e1ab713b h1:b+KE2kSEFgnv+4UmTeMPJ3yT978JW8QJ7T60fI3L21Y=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230330162120-3f56e1ab713b/go.mod h1:psyK1Qz149N3KGG3EamOlVSZipmgg8g8OvTDmwhh5cI=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304 h1:RIrWLzIiZN5Xd2JOfSOtGZaf6V3qEQYg6EaDTAkMnCo=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304/go.mod h1:yPWoJTj5RKrXKUChAicp+G/4Ni/uVEpp27mi/FF/L9c=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.0-20230328024000-175ec88e4304 h1:Sa4RJpuP4tEzUoxpzOm1GZ798PXOhHK0WgV11/sHh0o=
Expand Down
12 changes: 12 additions & 0 deletions osmoutils/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package osmoutils

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)

// NewModuleAddressWithPrefix returns a new module address with the given prefix and identifier.
func NewModuleAddressWithPrefix(moduleName, prefix string, identifier []byte) sdk.AccAddress {
key := append([]byte(prefix), identifier...)
return address.Module(moduleName, key)
}
26 changes: 16 additions & 10 deletions proto/osmosis/concentrated-liquidity/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,54 @@ message Pool {
option (gogoproto.goproto_stringer) = false;
option (cosmos_proto.implements_interface) = "PoolI";

// pool's address holding all liquidity tokens.
string address = 1 [ (gogoproto.moretags) = "yaml:\"address\"" ];
uint64 id = 2;

// address holding the incentives liquidity.
string incentives_address = 2
[ (gogoproto.moretags) = "yaml:\"incentives_address\"" ];

uint64 id = 3;

// Amount of total liquidity
string current_tick_liquidity = 3 [
string current_tick_liquidity = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"current_tick_liquidity\"",
(gogoproto.nullable) = false
];

string token0 = 4;
string token1 = 5;
string token0 = 5;
string token1 = 6;

string current_sqrt_price = 6 [
string current_sqrt_price = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"spot_price\"",
(gogoproto.nullable) = false
];
string current_tick = 7 [
string current_tick = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"current_tick\"",
(gogoproto.nullable) = false
];
// tick_spacing must be one of the authorized_tick_spacing values set in the
// concentrated-liquidity parameters
uint64 tick_spacing = 8 [ (gogoproto.moretags) = "yaml:\"tick_spacing\"" ];
string exponent_at_price_one = 9 [
uint64 tick_spacing = 9 [ (gogoproto.moretags) = "yaml:\"tick_spacing\"" ];
string exponent_at_price_one = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"exponent_at_price_one\"",
(gogoproto.nullable) = false
];

// swap_fee is the ratio that is charged on the amount of token in.
string swap_fee = 10 [
string swap_fee = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"swap_fee\"",
(gogoproto.nullable) = false
];

// last_liquidity_update is the last time either the pool liquidity or the
// active tick changed
google.protobuf.Timestamp last_liquidity_update = 11 [
google.protobuf.Timestamp last_liquidity_update = 12 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true,
(gogoproto.moretags) = "yaml:\"last_liquidity_update\""
Expand Down
6 changes: 3 additions & 3 deletions x/concentrated-liquidity/incentives.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func (k Keeper) collectIncentives(ctx sdk.Context, owner sdk.AccAddress, positio
}

// Send the collected incentives to the position's owner from the pool's address.
if err := k.bankKeeper.SendCoins(ctx, pool.GetAddress(), owner, collectedIncentivesForPosition); err != nil {
if err := k.bankKeeper.SendCoins(ctx, pool.GetIncentivesAddress(), owner, collectedIncentivesForPosition); err != nil {
return sdk.Coins{}, err
}

Expand Down Expand Up @@ -695,8 +695,8 @@ func (k Keeper) createIncentive(ctx sdk.Context, poolId uint64, sender sdk.AccAd
return types.IncentiveRecord{}, err
}

// Transfer tokens from sender to pool balance
if err := k.bankKeeper.SendCoins(ctx, sender, pool.GetAddress(), sdk.NewCoins(incentiveCoin)); err != nil {
// Transfer tokens from sender to the pool's incentive address
if err := k.bankKeeper.SendCoins(ctx, sender, pool.GetIncentivesAddress(), sdk.NewCoins(incentiveCoin)); err != nil {
return types.IncentiveRecord{}, err
}

Expand Down
11 changes: 8 additions & 3 deletions x/concentrated-liquidity/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ func (s *KeeperTestSuite) TestCollectIncentives() {
validPool := s.PrepareConcentratedPool()
validPoolId := validPool.GetId()

s.FundAcc(validPool.GetAddress(), tc.expectedIncentivesClaimed)
s.FundAcc(validPool.GetIncentivesAddress(), tc.expectedIncentivesClaimed)

clKeeper := s.App.ConcentratedLiquidityKeeper
ctx := s.Ctx
Expand Down Expand Up @@ -2664,6 +2664,7 @@ func (s *KeeperTestSuite) TestCollectIncentives() {

// Checkpoint starting balance to compare against later
poolBalanceBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, validPool.GetAddress())
incentivesBalanceBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, validPool.GetIncentivesAddress())
ownerBalancerBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, ownerWithValidPosition)

// Set up invalid pool ID for error-catching case(s)
Expand All @@ -2678,15 +2679,19 @@ func (s *KeeperTestSuite) TestCollectIncentives() {
// Assertions

poolBalanceAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, validPool.GetAddress())
incentivesBalanceAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, validPool.GetIncentivesAddress())
ownerBalancerAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, ownerWithValidPosition)

// Ensure pool balances are unchanged independent of error.
s.Require().Equal(poolBalanceBeforeCollect, poolBalanceAfterCollect)

if tc.expectedError != nil {
s.Require().Error(err)
s.Require().ErrorContains(err, tc.expectedError.Error())
s.Require().Equal(tc.expectedIncentivesClaimed, actualIncentivesClaimed)

// Ensure balances are unchanged
s.Require().Equal(poolBalanceBeforeCollect, poolBalanceAfterCollect)
s.Require().Equal(incentivesBalanceBeforeCollect, incentivesBalanceAfterCollect)
s.Require().Equal(ownerBalancerAfterCollect, ownerBalancerBeforeCollect)
return
}
Expand All @@ -2696,7 +2701,7 @@ func (s *KeeperTestSuite) TestCollectIncentives() {
s.Require().Equal(tc.expectedIncentivesClaimed.String(), actualIncentivesClaimed.String())

// Ensure balances are updated by the correct amounts
s.Require().Equal(tc.expectedIncentivesClaimed.String(), (poolBalanceBeforeCollect.Sub(poolBalanceAfterCollect)).String())
s.Require().Equal(tc.expectedIncentivesClaimed.String(), (incentivesBalanceBeforeCollect.Sub(incentivesBalanceAfterCollect)).String())
s.Require().Equal(tc.expectedIncentivesClaimed.String(), (ownerBalancerAfterCollect.Sub(ownerBalancerBeforeCollect)).String())
})
}
Expand Down
11 changes: 7 additions & 4 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,14 @@ func (s *KeeperTestSuite) TestWithdrawPosition() {

// Set expected incentives and fund pool with appropriate amount
expectedIncentivesClaimed = expectedIncentivesFromUptimeGrowth(defaultUptimeGrowth, liquidityCreated, tc.timeElapsed, sdk.OneInt())
s.FundAcc(pool.GetAddress(), expectedIncentivesClaimed)
s.FundAcc(pool.GetIncentivesAddress(), expectedIncentivesClaimed)

// Note the pool and owner balances before collecting fees.
poolBalanceBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, pool.GetAddress())
incentivesBalanceBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, pool.GetIncentivesAddress())
ownerBalancerBeforeCollect := s.App.BankKeeper.GetAllBalances(ctx, owner)

expectedBalanceDelta := expectedFeesClaimed.Add(expectedIncentivesClaimed...).Add(sdk.NewCoin(ETH, config.amount0Expected.Abs())).Add(sdk.NewCoin(USDC, config.amount1Expected.Abs()))
expectedPoolBalanceDelta := expectedFeesClaimed.Add(sdk.NewCoin(ETH, config.amount0Expected.Abs())).Add(sdk.NewCoin(USDC, config.amount1Expected.Abs()))

// System under test.
amtDenom0, amtDenom1, err := concentratedLiquidityKeeper.WithdrawPosition(ctx, owner, config.positionId, config.liquidityAmount)
Expand All @@ -457,10 +458,12 @@ func (s *KeeperTestSuite) TestWithdrawPosition() {
// If the remaining liquidity is zero, all fees and incentives should be collected and the position should be deleted.
// Check if all fees and incentives were collected.
poolBalanceAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, pool.GetAddress())
incentivesBalanceAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, pool.GetIncentivesAddress())
ownerBalancerAfterCollect := s.App.BankKeeper.GetAllBalances(ctx, owner)

s.Require().Equal(expectedBalanceDelta.String(), poolBalanceBeforeCollect.Sub(poolBalanceAfterCollect).String())
s.Require().Equal(expectedBalanceDelta.String(), ownerBalancerAfterCollect.Sub(ownerBalancerBeforeCollect).String())
s.Require().Equal(expectedPoolBalanceDelta.String(), poolBalanceBeforeCollect.Sub(poolBalanceAfterCollect).String())
s.Require().Equal(expectedPoolBalanceDelta.Add(expectedIncentivesClaimed...).String(), ownerBalancerAfterCollect.Sub(ownerBalancerBeforeCollect).String())
s.Require().Equal(expectedIncentivesClaimed.String(), incentivesBalanceBeforeCollect.Sub(incentivesBalanceAfterCollect).String())

if expectedRemainingLiquidity.IsZero() {
// Check that the positionLiquidity was deleted.
Expand Down
19 changes: 16 additions & 3 deletions x/concentrated-liquidity/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/internal/math"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
gammtypes "github.com/osmosis-labs/osmosis/v15/x/gamm/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
)

const (
incentivesAddressPrefix = "incentives"
)

var (
_ types.ConcentratedPoolExtension = &Pool{}
one = sdk.OneDec()
Expand All @@ -38,8 +42,8 @@ func NewConcentratedLiquidityPool(poolId uint64, denom0, denom1 string, tickSpac

// Create a new pool struct with the specified parameters
pool := Pool{
// TODO: move gammtypes.NewPoolAddress(poolId) to poolmanagertypes
Address: gammtypes.NewPoolAddress(poolId).String(),
Address: poolmanagertypes.NewPoolAddress(poolId).String(),
IncentivesAddress: osmoutils.NewModuleAddressWithPrefix(types.ModuleName, incentivesAddressPrefix, sdk.Uint64ToBigEndian(poolId)).String(),
Id: poolId,
CurrentSqrtPrice: sdk.ZeroDec(),
CurrentTick: sdk.ZeroInt(),
Expand All @@ -63,6 +67,15 @@ func (p Pool) GetAddress() sdk.AccAddress {
return addr
}

// GetIncentivesAddress returns the address storing incentives of the concentrated liquidity pool.
func (p Pool) GetIncentivesAddress() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(p.IncentivesAddress)
if err != nil {
panic(fmt.Sprintf("could not bech32 decode address of pool with id: %d", p.GetId()))
}
return addr
}

// GetId returns the id of the concentrated liquidity pool
func (p Pool) GetId() uint64 {
return p.Id
Expand Down
Loading

0 comments on commit 85ab280

Please sign in to comment.