Skip to content

Commit c47aabe

Browse files
authored
[Bug]: move taker fees to begin block (#1245)
* move taker fees to begin block * comment * remove * update test
1 parent be80f59 commit c47aabe

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

x/masterchef/keeper/abci.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
stabletypes "github.com/elys-network/elys/x/stablestake/types"
1616
)
1717

18+
// BeginBlocker of amm module
19+
func (k Keeper) BeginBlocker(ctx sdk.Context) error {
20+
// convert balances in taker address to elys and burn them
21+
k.ProcessTakerFee(ctx)
22+
return nil
23+
}
24+
1825
// EndBlocker of amm module
1926
func (k Keeper) EndBlocker(ctx sdk.Context) error {
2027

@@ -28,8 +35,6 @@ func (k Keeper) EndBlocker(ctx sdk.Context) error {
2835
// distribute external rewards
2936
k.ProcessExternalRewardsDistribution(ctx)
3037

31-
// convert balances in taker address to elys and burn them
32-
k.ProcessTakerFee(ctx)
3338
return nil
3439
}
3540

@@ -737,7 +742,8 @@ func (k Keeper) ProcessTakerFee(ctx sdk.Context) {
737742

738743
balances := k.bankKeeper.GetAllBalances(ctx, collectionAddress)
739744
for _, balance := range balances {
740-
if balance.Denom == ptypes.Elys {
745+
// need atleast certain amount to swap
746+
if balance.Denom == ptypes.Elys || balance.Amount.LT(sdkmath.NewInt(1000000)) {
741747
continue
742748
}
743749
_, err := k.amm.SwapByDenom(ctx, &ammtypes.MsgSwapByDenom{

x/masterchef/keeper/abci_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,22 +390,22 @@ func (suite *MasterchefKeeperTestSuite) TestProcessTakerFees() {
390390
suite.ResetSuite(true)
391391

392392
// Generate 1 random account with 1000stake balanced
393-
addr := simapp.AddTestAddrs(suite.app, suite.ctx, 1, sdkmath.NewInt(1000000))
393+
addr := simapp.AddTestAddrs(suite.app, suite.ctx, 1, sdkmath.NewInt(100000000000))
394394

395395
// mint some tokens in taker address
396396
takerAddress := suite.app.ParameterKeeper.GetParams(suite.ctx).TakerFeeCollectionAddress
397-
suite.MintTokenToAddress(sdk.MustAccAddressFromBech32(takerAddress), sdkmath.NewInt(1000), ptypes.BaseCurrency)
398-
suite.MintTokenToAddress(addr[0], sdkmath.NewInt(100000), ptypes.BaseCurrency)
397+
suite.MintTokenToAddress(sdk.MustAccAddressFromBech32(takerAddress), sdkmath.NewInt(1000000), ptypes.BaseCurrency)
398+
suite.MintTokenToAddress(addr[0], sdkmath.NewInt(100000000), ptypes.BaseCurrency)
399399

400400
// Pool with 1000 ELYS and 1000 USDC
401401
poolAssets := []ammtypes.PoolAsset{
402402
{
403403
Weight: sdkmath.NewInt(50),
404-
Token: sdk.NewCoin(ptypes.Elys, sdkmath.NewInt(100000)),
404+
Token: sdk.NewCoin(ptypes.Elys, sdkmath.NewInt(1000000000)),
405405
},
406406
{
407407
Weight: sdkmath.NewInt(50),
408-
Token: sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(10000)),
408+
Token: sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000000)),
409409
},
410410
}
411411

@@ -423,7 +423,7 @@ func (suite *MasterchefKeeperTestSuite) TestProcessTakerFees() {
423423
suite.Require().Equal(len(pools), 1)
424424

425425
elysSupplyBefore := suite.app.BankKeeper.GetSupply(suite.ctx, ptypes.Elys)
426-
suite.Require().Equal(elysSupplyBefore.Amount.String(), "100000002000000")
426+
suite.Require().Equal(elysSupplyBefore.Amount.String(), "100100001000000")
427427

428428
// Process taker fees
429429
suite.app.MasterchefKeeper.ProcessTakerFee(suite.ctx)
@@ -436,5 +436,5 @@ func (suite *MasterchefKeeperTestSuite) TestProcessTakerFees() {
436436

437437
// Check elys supply is reduced
438438
elysSupplyAfter := suite.app.BankKeeper.GetSupply(suite.ctx, ptypes.Elys)
439-
suite.Require().Equal(elysSupplyAfter.Amount.String(), "100000001990992")
439+
suite.Require().Equal(elysSupplyAfter.Amount.String(), "100099991197050")
440440
}

x/masterchef/module.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
158158
func (AppModule) ConsensusVersion() uint64 { return 6 }
159159

160160
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
161-
func (am AppModule) BeginBlock(_ context.Context) error {
162-
return nil
161+
func (am AppModule) BeginBlock(goCtx context.Context) error {
162+
ctx := sdk.UnwrapSDKContext(goCtx)
163+
return am.keeper.BeginBlocker(ctx)
163164
}
164165

165166
// EndBlock contains the logic that is automatically triggered at the end of each block

0 commit comments

Comments
 (0)