Skip to content

Commit

Permalink
Send charged rent from failed contracts to fee collector (sei-protoco…
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Jun 5, 2023
1 parent a9d94c7 commit 52cf567
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions x/dex/contract/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions x/dex/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 52cf567

Please sign in to comment.