diff --git a/x/evm/keeper/bank_extension.go b/x/evm/keeper/bank_extension.go index 79bafa8b3..9272ff590 100644 --- a/x/evm/keeper/bank_extension.go +++ b/x/evm/keeper/bank_extension.go @@ -17,8 +17,7 @@ var ( type NibiruBankKeeper struct { bankkeeper.BaseKeeper - StateDB *statedb.StateDB - TxStateDB *statedb.StateDB + StateDB *statedb.StateDB } func (evmKeeper *Keeper) NewStateDB( @@ -29,15 +28,6 @@ func (evmKeeper *Keeper) NewStateDB( return stateDB } -func (evmKeeper *Keeper) NewTxStateDB( - ctx sdk.Context, txConfig statedb.TxConfig, -) *statedb.StateDB { - stateDB := statedb.New(ctx, evmKeeper, txConfig) - evmKeeper.Bank.StateDB = stateDB - evmKeeper.Bank.TxStateDB = stateDB - return stateDB -} - func (bk NibiruBankKeeper) MintCoins( ctx sdk.Context, moduleName string, diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index 57f3cc54a..dbc1e8f74 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -202,9 +202,10 @@ func (k Keeper) GetHashFn(ctx sdk.Context) vm.GetHashFunc { } } -// ApplyEvmMsg computes the new state by applying the given message against the existing state. -// If the message fails, the VM execution error with the reason will be returned to the client -// and the transaction won't be committed to the store. +// ApplyEvmMsg computes the new state by applying the given message against the +// existing state. If the message fails, the VM execution error with the reason +// will be returned to the client and the transaction won't be committed to the +// store. // // # Reverted state // diff --git a/x/evm/precompile/funtoken.go b/x/evm/precompile/funtoken.go index 92b6701ac..ced70e9ad 100644 --- a/x/evm/precompile/funtoken.go +++ b/x/evm/precompile/funtoken.go @@ -6,17 +6,14 @@ import ( "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - auth "github.com/cosmos/cosmos-sdk/x/auth/types" gethabi "github.com/ethereum/go-ethereum/accounts/abi" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" "github.com/NibiruChain/nibiru/v2/app/keepers" - "github.com/NibiruChain/nibiru/v2/eth" "github.com/NibiruChain/nibiru/v2/x/evm" "github.com/NibiruChain/nibiru/v2/x/evm/embeds" evmkeeper "github.com/NibiruChain/nibiru/v2/x/evm/keeper" - "github.com/NibiruChain/nibiru/v2/x/evm/statedb" ) var _ vm.PrecompiledContract = (*precompileFunToken)(nil) @@ -152,9 +149,12 @@ func (p precompileFunToken) bankSend( return } } else { + // NOTE: The NibiruBankKeeper needs to reference the current [vm.StateDB] before + // any operation that has the potential to use Bank send methods. This will + // guarantee that [evmkeeper.Keeper.SetAccBalance] journal changes are + // recorded if wei (NIBI) is transferred. p.evmKeeper.Bank.StateDB = start.StateDB err = p.evmKeeper.Bank.MintCoins(ctx, evm.ModuleName, sdk.NewCoins(coinToSend)) - // err = SafeMintCoins(ctx, evm.ModuleName, coinToSend, p.evmKeeper, start.StateDB) if err != nil { return nil, fmt.Errorf("mint failed for module \"%s\" (%s): contract caller %s: %w", evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err, @@ -163,6 +163,11 @@ func (p precompileFunToken) bankSend( } // Transfer the bank coin + // + // NOTE: The NibiruBankKeeper needs to reference the current [vm.StateDB] before + // any operation that has the potential to use Bank send methods. This will + // guarantee that [evmkeeper.Keeper.SetAccBalance] journal changes are + // recorded if wei (NIBI) is transferred. p.evmKeeper.Bank.StateDB = start.StateDB err = p.evmKeeper.Bank.SendCoinsFromModuleToAccount( ctx, @@ -170,14 +175,6 @@ func (p precompileFunToken) bankSend( toAddr, sdk.NewCoins(coinToSend), ) - // err = SafeSendCoinFromModuleToAccount( - // ctx, - // evm.ModuleName, - // toAddr, - // coinToSend, - // p.evmKeeper, - // start.StateDB, - // ) if err != nil { return nil, fmt.Errorf("send failed for module \"%s\" (%s): contract caller %s: %w", evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err, @@ -220,55 +217,3 @@ func (p precompileFunToken) decomposeBankSendArgs(args []any) ( return } -func SafeMintCoins( - ctx sdk.Context, - moduleName string, - amt sdk.Coin, - ek *evmkeeper.Keeper, - db *statedb.StateDB, -) error { - bk := ek.Bank - err := bk.MintCoins(ctx, evm.ModuleName, sdk.NewCoins(amt)) - if err != nil { - return err - } - if amt.Denom == evm.EVMBankDenom { - evmBech32Addr := auth.NewModuleAddress(evm.ModuleName) - balAfter := bk.GetBalance(ctx, evmBech32Addr, amt.Denom).Amount.BigInt() - db.SetBalanceWei( - evm.EVM_MODULE_ADDRESS, - evm.NativeToWei(balAfter), - ) - } - - return nil -} -func SafeSendCoinFromModuleToAccount( - ctx sdk.Context, - senderModule string, - recipientAddr sdk.AccAddress, - amt sdk.Coin, - ek *evmkeeper.Keeper, - db *statedb.StateDB, -) error { - bk := ek.Bank - err := bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(amt)) - if err != nil { - return err - } - if amt.Denom == evm.EVMBankDenom { - evmBech32Addr := auth.NewModuleAddress(evm.ModuleName) - balAfterFrom := bk.GetBalance(ctx, evmBech32Addr, amt.Denom).Amount.BigInt() - db.SetBalanceWei( - evm.EVM_MODULE_ADDRESS, - evm.NativeToWei(balAfterFrom), - ) - - balAfterTo := bk.GetBalance(ctx, recipientAddr, amt.Denom).Amount.BigInt() - db.SetBalanceWei( - eth.NibiruAddrToEthAddr(recipientAddr), - evm.NativeToWei(balAfterTo), - ) - } - return nil -} diff --git a/x/evm/precompile/wasm.go b/x/evm/precompile/wasm.go index a7b21684c..7f18fb244 100644 --- a/x/evm/precompile/wasm.go +++ b/x/evm/precompile/wasm.go @@ -8,6 +8,7 @@ import ( "github.com/NibiruChain/nibiru/v2/app/keepers" "github.com/NibiruChain/nibiru/v2/eth" "github.com/NibiruChain/nibiru/v2/x/evm/embeds" + evmkeeper "github.com/NibiruChain/nibiru/v2/x/evm/keeper" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" gethabi "github.com/ethereum/go-ethereum/accounts/abi" @@ -42,6 +43,11 @@ func (p precompileWasm) Run( } method := start.Method + // The NibiruBankKeeper needs to reference the current [vm.StateDB] before + // any operation that has the potential to use Bank send methods. This will + // guarantee that [evmkeeper.Keeper.SetAccBalance] journal changes are + // recorded if wei (NIBI) is transferred. + p.Bank.StateDB = start.StateDB switch PrecompileMethod(method.Name) { case WasmMethod_execute: bz, err = p.execute(start, contract.CallerAddress, readonly) @@ -66,6 +72,7 @@ func (p precompileWasm) Run( } type precompileWasm struct { + *evmkeeper.Keeper Wasm Wasm } @@ -91,6 +98,7 @@ type Wasm struct { func PrecompileWasm(keepers keepers.PublicKeepers) vm.PrecompiledContract { return precompileWasm{ + Keeper: keepers.EvmKeeper, Wasm: Wasm{ wasmkeeper.NewDefaultPermissionKeeper(keepers.WasmKeeper), keepers.WasmKeeper,