diff --git a/x/dex/contract/abci.go b/x/dex/contract/abci.go index c94759d065..7d955ff785 100644 --- a/x/dex/contract/abci.go +++ b/x/dex/contract/abci.go @@ -83,6 +83,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo } failedContractsToReasons := map[string]string{} + failedContractsPreRents := map[string]uint64{} + failedContractsPostRents := map[string]uint64{} // persistent contract rent charges for failed contracts and discard everything else env.failedContractAddressesToErrors.Range(func(failedContractAddress string, failedReason error) bool { cachedContract, err := keeper.GetContract(cachedCtx, failedContractAddress) @@ -96,6 +98,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo return true } contract.RentBalance = cachedContract.RentBalance + failedContractsPreRents[failedContractAddress] = preRunRents[failedContractAddress] + failedContractsPostRents[failedContractAddress] = contract.RentBalance err = keeper.SetContract(ctx, &contract) if err != nil { ctx.Logger().Error(fmt.Sprintf("error %s when persisting contract %s's rent balance", err, failedContractAddress)) @@ -104,6 +108,7 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo failedContractsToReasons[failedContractAddress] = dexutils.GetTruncatedErrors(failedReason) return true }) + TransferRentFromDexToCollector(ctx, keeper.BankKeeper, failedContractsPreRents, failedContractsPostRents) // restore keeper in-memory state newGoContext := context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, memStateCopy) diff --git a/x/dex/module_test.go b/x/dex/module_test.go index 7426c300a9..d04f7c0c88 100644 --- a/x/dex/module_test.go +++ b/x/dex/module_test.go @@ -10,6 +10,7 @@ import ( wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/utils/tracing" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" keepertest "github.com/sei-protocol/sei-chain/testutil/keeper" dexcache "github.com/sei-protocol/sei-chain/x/dex/cache" "github.com/sei-protocol/sei-chain/x/dex/contract" @@ -652,6 +653,11 @@ func TestEndBlockRollbackWithRentCharge(t *testing.T) { require.Nil(t, err) require.True(t, c.Suspended) // bad contract is suspended not because of out-of-rent but because of execution error require.Equal(t, uint64(0), c.RentBalance) // rent balance should be drained + require.Equal(t, int64(1), dexkeeper.BankKeeper.GetBalance( + ctx, + dexkeeper.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), + "usei", + ).Amount.Int64()) // bad contract rent should be sent to fee collector creatorBalanceAfter := bankkeeper.GetBalance(ctx, testAccount, "usei") require.Equal(t, creatorBalanceBefore, creatorBalanceAfter) }