Skip to content

Commit 146ae25

Browse files
facundomedicamergify[bot]
authored andcommitted
fix(x/protocolpool)!: do not incur into extra writes if there are no continuous funds (#21356)
(cherry picked from commit c286e6e)
1 parent 3285de7 commit 146ae25

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

x/protocolpool/keeper/keeper.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ func (k Keeper) SetToDistribute(ctx context.Context) error {
177177
// Calculate the amount to be distributed
178178
amountToDistribute := distributionBalance.Sub(lastBalance)
179179

180+
// Check if there are any recipients to distribute to, if not, send straight to the community pool and avoid
181+
// setting the distributions
182+
hasContinuousFunds := false
183+
err = k.ContinuousFund.Walk(ctx, nil, func(_ sdk.AccAddress, _ types.ContinuousFund) (bool, error) {
184+
hasContinuousFunds = true
185+
return true, nil
186+
})
187+
if err != nil {
188+
return err
189+
}
190+
191+
// if there are no continuous funds, send all the funds to the community pool and reset the last balance
192+
if !hasContinuousFunds {
193+
poolCoins := sdk.NewCoins(sdk.NewCoin(denom, amountToDistribute))
194+
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ProtocolPoolDistrAccount, types.ModuleName, poolCoins); err != nil {
195+
return err
196+
}
197+
198+
if !lastBalance.IsZero() { // only reset if the last balance is not zero (so we leave it at zero/nil)
199+
return k.LastBalance.Set(ctx, math.ZeroInt())
200+
}
201+
202+
return nil
203+
}
204+
180205
if err = k.Distributions.Set(ctx, k.HeaderService.HeaderInfo(ctx).Time, amountToDistribute); err != nil {
181206
return fmt.Errorf("error while setting Distributions: %w", err)
182207
}

x/protocolpool/keeper/keeper_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,32 @@ func (suite *KeeperTestSuite) TestSetToDistribute() {
186186
distrBal := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1000000)))
187187
suite.bankKeeper.EXPECT().GetAllBalances(suite.ctx, poolDistrAcc.GetAddress()).Return(distrBal).AnyTimes()
188188

189+
// because there are no continuous funds, all are going to the community pool
190+
suite.bankKeeper.EXPECT().SendCoinsFromModuleToModule(suite.ctx, poolDistrAcc.GetName(), poolAcc.GetName(), distrBal)
191+
189192
err := suite.poolKeeper.SetToDistribute(suite.ctx)
190193
suite.Require().NoError(err)
191194

195+
// Verify that LastBalance was not set (zero balance)
196+
_, err = suite.poolKeeper.LastBalance.Get(suite.ctx)
197+
suite.Require().ErrorContains(err, "not found")
198+
199+
// create new continuous fund and distribute again
200+
addrCdc := address.NewBech32Codec("cosmos")
201+
addrStr := "cosmos1qypq2q2l8z4wz2z2l8z4wz2z2l8z4wz2srklj6"
202+
addrBz, err := addrCdc.StringToBytes(addrStr)
203+
suite.Require().NoError(err)
204+
205+
err = suite.poolKeeper.ContinuousFund.Set(suite.ctx, addrBz, types.ContinuousFund{
206+
Recipient: addrStr,
207+
Percentage: math.LegacyMustNewDecFromStr("0.3"),
208+
Expiry: nil,
209+
})
210+
suite.Require().NoError(err)
211+
212+
err = suite.poolKeeper.SetToDistribute(suite.ctx)
213+
suite.Require().NoError(err)
214+
192215
// Verify that LastBalance was set correctly
193216
lastBalance, err := suite.poolKeeper.LastBalance.Get(suite.ctx)
194217
suite.Require().NoError(err)

0 commit comments

Comments
 (0)