Skip to content

Commit

Permalink
revert experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Oct 31, 2024
1 parent 70b6e5e commit 53e8c59
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 78 deletions.
12 changes: 1 addition & 11 deletions x/evm/keeper/bank_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var (

type NibiruBankKeeper struct {
bankkeeper.BaseKeeper
StateDB *statedb.StateDB
TxStateDB *statedb.StateDB
StateDB *statedb.StateDB
}

func (evmKeeper *Keeper) NewStateDB(
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
73 changes: 9 additions & 64 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -163,21 +163,18 @@ 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,
evm.ModuleName,
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,
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions x/evm/precompile/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -66,6 +72,7 @@ func (p precompileWasm) Run(
}

type precompileWasm struct {
*evmkeeper.Keeper
Wasm Wasm
}

Expand All @@ -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,
Expand Down

0 comments on commit 53e8c59

Please sign in to comment.