Skip to content

Commit 52cf567

Browse files
authored
Send charged rent from failed contracts to fee collector (#828)
1 parent a9d94c7 commit 52cf567

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

x/dex/contract/abci.go

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo
8383
}
8484

8585
failedContractsToReasons := map[string]string{}
86+
failedContractsPreRents := map[string]uint64{}
87+
failedContractsPostRents := map[string]uint64{}
8688
// persistent contract rent charges for failed contracts and discard everything else
8789
env.failedContractAddressesToErrors.Range(func(failedContractAddress string, failedReason error) bool {
8890
cachedContract, err := keeper.GetContract(cachedCtx, failedContractAddress)
@@ -96,6 +98,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo
9698
return true
9799
}
98100
contract.RentBalance = cachedContract.RentBalance
101+
failedContractsPreRents[failedContractAddress] = preRunRents[failedContractAddress]
102+
failedContractsPostRents[failedContractAddress] = contract.RentBalance
99103
err = keeper.SetContract(ctx, &contract)
100104
if err != nil {
101105
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
104108
failedContractsToReasons[failedContractAddress] = dexutils.GetTruncatedErrors(failedReason)
105109
return true
106110
})
111+
TransferRentFromDexToCollector(ctx, keeper.BankKeeper, failedContractsPreRents, failedContractsPostRents)
107112

108113
// restore keeper in-memory state
109114
newGoContext := context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, memStateCopy)

x/dex/module_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212
"github.com/cosmos/cosmos-sdk/utils/tracing"
13+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
1314
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
1415
dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
1516
"github.com/sei-protocol/sei-chain/x/dex/contract"
@@ -652,6 +653,11 @@ func TestEndBlockRollbackWithRentCharge(t *testing.T) {
652653
require.Nil(t, err)
653654
require.True(t, c.Suspended) // bad contract is suspended not because of out-of-rent but because of execution error
654655
require.Equal(t, uint64(0), c.RentBalance) // rent balance should be drained
656+
require.Equal(t, int64(1), dexkeeper.BankKeeper.GetBalance(
657+
ctx,
658+
dexkeeper.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName),
659+
"usei",
660+
).Amount.Int64()) // bad contract rent should be sent to fee collector
655661
creatorBalanceAfter := bankkeeper.GetBalance(ctx, testAccount, "usei")
656662
require.Equal(t, creatorBalanceBefore, creatorBalanceAfter)
657663
}

0 commit comments

Comments
 (0)