Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[CL]: separate fees into different module account #5230

Merged
merged 4 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions proto/osmosis/concentrated-liquidity/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,43 @@ message Pool {
string incentives_address = 2
[ (gogoproto.moretags) = "yaml:\"incentives_address\"" ];

uint64 id = 3;
// address holding fees from swaps.
string fees_address = 3 [ (gogoproto.moretags) = "yaml:\"fees_address\"" ];

uint64 id = 4;

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

string token0 = 5;
string token1 = 6;
string token0 = 6;
string token1 = 7;

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

// swap_fee is the ratio that is charged on the amount of token in.
string swap_fee = 11 [
string swap_fee = 12 [
(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 = 12 [
google.protobuf.Timestamp last_liquidity_update = 13 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true,
(gogoproto.moretags) = "yaml:\"last_liquidity_update\""
Expand Down
8 changes: 4 additions & 4 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (k Keeper) ComputeOutAmtGivenIn(
swapFee sdk.Dec,
priceLimit sdk.Dec,

) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, err error) {
) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, totalFees sdk.Dec, err error) {
return k.computeOutAmtGivenIn(ctx, poolId, tokenInMin, tokenOutDenom, swapFee, priceLimit)
}

Expand All @@ -93,7 +93,7 @@ func (k Keeper) ComputeInAmtGivenOut(
priceLimit sdk.Dec,
poolId uint64,

) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, err error) {
) (calcTokenIn, calcTokenOut sdk.Coin, currentTick int64, liquidity, sqrtPrice sdk.Dec, totalFees sdk.Dec, err error) {
return k.computeInAmtGivenOut(ctx, desiredTokenOut, tokenInDenom, swapFee, priceLimit, poolId)
}

Expand Down Expand Up @@ -300,8 +300,8 @@ func (k Keeper) GetAllPositions(ctx sdk.Context) ([]model.Position, error) {
return k.getAllPositions(ctx)
}

func (k Keeper) UpdatePoolForSwap(ctx sdk.Context, pool types.ConcentratedPoolExtension, sender sdk.AccAddress, tokenIn sdk.Coin, tokenOut sdk.Coin, newCurrentTick int64, newLiquidity sdk.Dec, newSqrtPrice sdk.Dec) error {
return k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOut, newCurrentTick, newLiquidity, newSqrtPrice)
func (k Keeper) UpdatePoolForSwap(ctx sdk.Context, pool types.ConcentratedPoolExtension, sender sdk.AccAddress, tokenIn sdk.Coin, tokenOut sdk.Coin, newCurrentTick int64, newLiquidity sdk.Dec, newSqrtPrice sdk.Dec, totalFees sdk.Dec) error {
return k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOut, newCurrentTick, newLiquidity, newSqrtPrice, totalFees)
}

func (k Keeper) PrepareBalancerPoolAsFullRange(ctx sdk.Context, clPoolId uint64) (uint64, sdk.Dec, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (k Keeper) collectFees(ctx sdk.Context, sender sdk.AccAddress, positionId u
if err != nil {
return sdk.Coins{}, err
}
if err := k.bankKeeper.SendCoins(ctx, pool.GetAddress(), sender, feesClaimed); err != nil {
if err := k.bankKeeper.SendCoins(ctx, pool.GetFeesAddress(), sender, feesClaimed); err != nil {
return sdk.Coins{}, err
}

Expand Down
10 changes: 5 additions & 5 deletions x/concentrated-liquidity/fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ func (s *KeeperTestSuite) TestQueryAndCollectFees() {
validPool := s.PrepareConcentratedPool()
validPoolId := validPool.GetId()

s.FundAcc(validPool.GetAddress(), tc.expectedFeesClaimed)
s.FundAcc(validPool.GetFeesAddress(), tc.expectedFeesClaimed)

clKeeper := s.App.ConcentratedLiquidityKeeper
ctx := s.Ctx
Expand All @@ -923,7 +923,7 @@ func (s *KeeperTestSuite) TestQueryAndCollectFees() {
err = clKeeper.ChargeFee(ctx, validPoolId, tc.globalFeeGrowth[0])
s.Require().NoError(err)

poolBalanceBeforeCollect := s.App.BankKeeper.GetBalance(ctx, validPool.GetAddress(), ETH)
poolFeeBalanceBeforeCollect := s.App.BankKeeper.GetBalance(ctx, validPool.GetFeesAddress(), ETH)
ownerBalancerBeforeCollect := s.App.BankKeeper.GetBalance(ctx, tc.owner, ETH)

var preQueryPosition accum.Record
Expand All @@ -949,7 +949,7 @@ func (s *KeeperTestSuite) TestQueryAndCollectFees() {

// Assertions.

poolBalanceAfterCollect := s.App.BankKeeper.GetBalance(ctx, validPool.GetAddress(), ETH)
poolFeeBalanceAfterCollect := s.App.BankKeeper.GetBalance(ctx, validPool.GetFeesAddress(), ETH)
ownerBalancerAfterCollect := s.App.BankKeeper.GetBalance(ctx, tc.owner, ETH)

if tc.expectedError != nil {
Expand All @@ -958,7 +958,7 @@ func (s *KeeperTestSuite) TestQueryAndCollectFees() {
s.Require().Equal(sdk.Coins{}, actualFeesClaimed)

// balances are unchanged
s.Require().Equal(poolBalanceBeforeCollect, poolBalanceAfterCollect)
s.Require().Equal(poolFeeBalanceAfterCollect, poolFeeBalanceBeforeCollect)
s.Require().Equal(ownerBalancerAfterCollect, ownerBalancerBeforeCollect)
return
}
Expand All @@ -969,7 +969,7 @@ func (s *KeeperTestSuite) TestQueryAndCollectFees() {
s.Require().Equal(feeQueryAmount.String(), actualFeesClaimed.String())

expectedETHAmount := tc.expectedFeesClaimed.AmountOf(ETH)
s.Require().Equal(expectedETHAmount.String(), poolBalanceBeforeCollect.Sub(poolBalanceAfterCollect).Amount.String())
s.Require().Equal(expectedETHAmount.String(), poolFeeBalanceBeforeCollect.Sub(poolFeeBalanceAfterCollect).Amount.String())
s.Require().Equal(expectedETHAmount.String(), ownerBalancerAfterCollect.Sub(ownerBalancerBeforeCollect).Amount.String())
})
}
Expand Down
10 changes: 7 additions & 3 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (s *KeeperTestSuite) TestWithdrawPosition() {
// Fund the pool account with the expected fees claimed.
if expectedRemainingLiquidity.IsZero() {
expectedFeesClaimed = expectedFeesClaimed.Add(sdk.NewCoin(ETH, liquidityCreated.TruncateInt()))
s.FundAcc(pool.GetAddress(), expectedFeesClaimed)
s.FundAcc(pool.GetFeesAddress(), expectedFeesClaimed)
}

communityPoolBalanceBefore := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distributiontypes.ModuleName))
Expand All @@ -581,10 +581,11 @@ func (s *KeeperTestSuite) TestWithdrawPosition() {

// Note the pool and owner balances before withdrawal of the position.
poolBalanceBeforeWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetAddress())
poolFeeBalanceBeforeWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetFeesAddress())
incentivesBalanceBeforeWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetIncentivesAddress())
ownerBalancerBeforeWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, owner)

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

var withdrawAccount sdk.AccAddress
if tc.withdrawWithNonOwner {
Expand All @@ -610,12 +611,13 @@ 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.
poolBalanceAfterWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetAddress())
poolFeeBalanceAfterWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetFeesAddress())
incentivesBalanceAfterWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetIncentivesAddress())
ownerBalancerAfterWithdraw := s.App.BankKeeper.GetAllBalances(s.Ctx, owner)
communityPoolBalanceAfter := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distributiontypes.ModuleName))

// owner should only have tokens equivilent to the delta balance of the pool
expectedOwnerBalanceDelta := expectedPoolBalanceDelta.Add(expectedIncentivesClaimed...)
expectedOwnerBalanceDelta := expectedPoolBalanceDelta.Add(expectedIncentivesClaimed...).Add(expectedFeesClaimed...)
actualOwnerBalancerDelta := ownerBalancerAfterWithdraw.Sub(ownerBalancerBeforeWithdraw)

communityPoolBalanceDelta := communityPoolBalanceAfter.Sub(communityPoolBalanceBefore)
Expand All @@ -638,6 +640,8 @@ func (s *KeeperTestSuite) TestWithdrawPosition() {
s.Require().True(expected.Equal(actual))
}

s.Require().Equal(poolFeeBalanceBeforeWithdraw.Sub(poolFeeBalanceAfterWithdraw).String(), expectedFeesClaimed.String())

// if the position's expected remaining liquidity is equal to zero, we check if all state
// have been correctly deleted.
if expectedRemainingLiquidity.IsZero() {
Expand Down
12 changes: 11 additions & 1 deletion x/concentrated-liquidity/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
incentivesAddressPrefix = "incentives"
feesAddressPrefix = "fees"
)

var (
Expand All @@ -39,6 +40,7 @@ func NewConcentratedLiquidityPool(poolId uint64, denom0, denom1 string, tickSpac
pool := Pool{
Address: poolmanagertypes.NewPoolAddress(poolId).String(),
IncentivesAddress: osmoutils.NewModuleAddressWithPrefix(types.ModuleName, incentivesAddressPrefix, sdk.Uint64ToBigEndian(poolId)).String(),
FeesAddress: osmoutils.NewModuleAddressWithPrefix(types.ModuleName, feesAddressPrefix, sdk.Uint64ToBigEndian(poolId)).String(),
Id: poolId,
CurrentSqrtPrice: sdk.ZeroDec(),
CurrentTick: 0,
Expand All @@ -65,7 +67,15 @@ func (p Pool) GetAddress() sdk.AccAddress {
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()))
panic(fmt.Sprintf("could not bech32 decode incentive address of pool with id: %d", p.GetId()))
}
return addr
}

func (p Pool) GetFeesAddress() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(p.FeesAddress)
if err != nil {
panic(fmt.Sprintf("could not bech32 decode fee address of pool with id: %d", p.GetId()))
}
return addr
}
Expand Down
Loading