Skip to content

Commit

Permalink
Handles dev fee payment without storing in kvstore
Browse files Browse the repository at this point in the history
- removes functions no longer necessary
- Still needs upgrade migration work
- Still needs a way to pay current dev fee payment to us during the upgrade
- still needs to get tests to pass
  • Loading branch information
NotJeremyLiu committed Apr 5, 2023
1 parent 5bacd9e commit 1f2f3dc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 131 deletions.
43 changes: 10 additions & 33 deletions x/protorev/keeper/developer_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,33 @@ import (
)

// SendDeveloperFeesToDeveloperAccount sends the developer fees from the module account to the developer account
func (k Keeper) SendDeveloperFeesToDeveloperAccount(ctx sdk.Context) error {
func (k Keeper) SendDeveloperFeesToDeveloperAccount(ctx sdk.Context, profitDenom string, profitAmount sdk.Int) error {
// Developer account must be set in order to be able to withdraw developer fees
developerAccount, err := k.GetDeveloperAccount(ctx)
if err != nil {
return err
}

coins, err := k.GetAllDeveloperFees(ctx)
if err != nil {
return err
}

for _, coin := range coins {
// Send the coins to the developer account
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, developerAccount, sdk.NewCoins(coin)); err != nil {
return err
}

// Reset the developer fees for the coin
k.DeleteDeveloperFees(ctx, coin.Denom)
}

return nil
}

// UpdateDeveloperFees updates the fees that developers can withdraw from the module account
func (k Keeper) UpdateDeveloperFees(ctx sdk.Context, denom string, profit sdk.Int) error {
// Get the days since genesis
daysSinceGenesis, err := k.GetDaysSinceModuleGenesis(ctx)
if err != nil {
return err
}

// Initialize the developer profit to 0
devProfit := sdk.NewCoin(profitDenom, sdk.ZeroInt())

// Calculate the developer fee
if daysSinceGenesis < types.Phase1Length {
profit = profit.MulRaw(types.ProfitSplitPhase1).QuoRaw(100)
devProfit.Amount = profitAmount.MulRaw(types.ProfitSplitPhase1).QuoRaw(100)
} else if daysSinceGenesis < types.Phase2Length {
profit = profit.MulRaw(types.ProfitSplitPhase2).QuoRaw(100)
devProfit.Amount = profitAmount.MulRaw(types.ProfitSplitPhase2).QuoRaw(100)
} else {
profit = profit.MulRaw(types.ProfitSplitPhase3).QuoRaw(100)
}

// Get the developer fees for the denom, if not there then set it to 0 and initialize it
currentDeveloperFee, err := k.GetDeveloperFees(ctx, denom)
if err != nil {
currentDeveloperFee = sdk.NewCoin(denom, sdk.ZeroInt())
devProfit.Amount = profitAmount.MulRaw(types.ProfitSplitPhase3).QuoRaw(100)
}
currentDeveloperFee.Amount = currentDeveloperFee.Amount.Add(profit)

// Set the developer fees for the denom
if err = k.SetDeveloperFees(ctx, currentDeveloperFee); err != nil {
// Send the developer profit to the developer account
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, developerAccount, sdk.NewCoins(devProfit)); err != nil {
return err
}

Expand Down
7 changes: 0 additions & 7 deletions x/protorev/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
k.SetDeveloperAccount(ctx, account)
}

// Set the developer fees that have been collected.
for _, fee := range genState.DeveloperFees {
if err := k.SetDeveloperFees(ctx, fee); err != nil {
panic(err)
}
}

// Set the number of days since the module genesis.
k.SetDaysSinceModuleGenesis(ctx, genState.DaysSinceModuleGenesis)

Expand Down
22 changes: 0 additions & 22 deletions x/protorev/keeper/protorev.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,6 @@ func (k Keeper) GetAllDeveloperFees(ctx sdk.Context) ([]sdk.Coin, error) {
return fees, nil
}

// SetDeveloperFees sets the fees the developers can withdraw from the module account
func (k Keeper) SetDeveloperFees(ctx sdk.Context, developerFees sdk.Coin) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeveloperFees)
key := types.GetKeyPrefixDeveloperFees(developerFees.Denom)

bz, err := developerFees.Marshal()
if err != nil {
return err
}

store.Set(key, bz)

return nil
}

// DeleteDeveloperFees deletes the developer fees given a denom
func (k Keeper) DeleteDeveloperFees(ctx sdk.Context, denom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeveloperFees)
key := types.GetKeyPrefixDeveloperFees(denom)
store.Delete(key)
}

// GetProtoRevEnabled returns whether protorev is enabled
func (k Keeper) GetProtoRevEnabled(ctx sdk.Context) bool {
params := k.GetParams(ctx)
Expand Down
63 changes: 0 additions & 63 deletions x/protorev/keeper/protorev_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper_test

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

"github.com/osmosis-labs/osmosis/v15/x/protorev/types"
)

Expand Down Expand Up @@ -127,67 +125,6 @@ func (suite *KeeperTestSuite) TestGetDaysSinceModuleGenesis() {
suite.Require().Equal(uint64(1), daysSinceGenesis)
}

// TestGetDeveloperFees tests the GetDeveloperFees, SetDeveloperFees, and GetAllDeveloperFees functions.
func (suite *KeeperTestSuite) TestGetDeveloperFees() {
// Should be initialized to [] on genesis
fees, err := suite.App.ProtoRevKeeper.GetAllDeveloperFees(suite.Ctx)
suite.Require().NoError(err)
suite.Require().Equal(0, len(fees))

// Should be no osmo fees on genesis
osmoFees, err := suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, types.OsmosisDenomination)
suite.Require().Error(err)
suite.Require().Equal(sdk.Coin{}, osmoFees)

// Should be no atom fees on genesis
atomFees, err := suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, "Atom")
suite.Require().Error(err)
suite.Require().Equal(sdk.Coin{}, atomFees)

// Should be able to set the fees
err = suite.App.ProtoRevKeeper.SetDeveloperFees(suite.Ctx, sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(100)))
suite.Require().NoError(err)
err = suite.App.ProtoRevKeeper.SetDeveloperFees(suite.Ctx, sdk.NewCoin("Atom", sdk.NewInt(100)))
suite.Require().NoError(err)
err = suite.App.ProtoRevKeeper.SetDeveloperFees(suite.Ctx, sdk.NewCoin("weth", sdk.NewInt(100)))

// Should be able to get the fees
osmoFees, err = suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, types.OsmosisDenomination)
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(100)), osmoFees)
atomFees, err = suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, "Atom")
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoin("Atom", sdk.NewInt(100)), atomFees)
wethFees, err := suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, "weth")
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoin("weth", sdk.NewInt(100)), wethFees)

fees, err = suite.App.ProtoRevKeeper.GetAllDeveloperFees(suite.Ctx)
suite.Require().NoError(err)
suite.Require().Equal(3, len(fees))
suite.Require().Contains(fees, osmoFees)
suite.Require().Contains(fees, atomFees)
}

// TestDeleteDeveloperFees tests the DeleteDeveloperFees function.
func (suite *KeeperTestSuite) TestDeleteDeveloperFees() {
err := suite.App.ProtoRevKeeper.SetDeveloperFees(suite.Ctx, sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(100)))
suite.Require().NoError(err)

// Should be able to get the fees
osmoFees, err := suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, types.OsmosisDenomination)
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(100)), osmoFees)

// Should be able to delete the fees
suite.App.ProtoRevKeeper.DeleteDeveloperFees(suite.Ctx, types.OsmosisDenomination)

// Should be no osmo fees after deletion
osmoFees, err = suite.App.ProtoRevKeeper.GetDeveloperFees(suite.Ctx, types.OsmosisDenomination)
suite.Require().Error(err)
suite.Require().Equal(sdk.Coin{}, osmoFees)
}

// TestGetProtoRevEnabled tests the GetProtoRevEnabled and SetProtoRevEnabled functions.
func (suite *KeeperTestSuite) TestGetProtoRevEnabled() {
// Should be initialized to true on genesis
Expand Down
7 changes: 1 addition & 6 deletions x/protorev/keeper/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,8 @@ func (k Keeper) ExecuteTrade(ctx sdk.Context, route poolmanagertypes.SwapAmountI
return err
}

// Update the developer fees
if err = k.UpdateDeveloperFees(ctx, inputCoin.Denom, profit); err != nil {
return err
}

// Send the developer fees to the developer
if err = k.SendDeveloperFeesToDeveloperAccount(ctx); err != nil {
if err = k.SendDeveloperFeesToDeveloperAccount(ctx, inputCoin.Denom, profit); err != nil {
return err
}

Expand Down

0 comments on commit 1f2f3dc

Please sign in to comment.