diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index cba57258..6e91ce51 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -97,11 +97,23 @@ func (k *Keeper) SetBalance(ctx sdk.Context, addr common.Address, amount *big.In delta := new(big.Int).Sub(amount, balance) switch delta.Sign() { case 1: - coins := sdk.NewCoins(sdk.NewCoin(evmDenom, sdkmath.NewIntFromBigInt(delta))) - return k.AddBalance(ctx, cosmosAddr, coins) + // mint + coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdkmath.NewIntFromBigInt(delta))) + if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil { + return err + } + if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, cosmosAddr, coins); err != nil { + return err + } case -1: - coins := sdk.NewCoins(sdk.NewCoin(evmDenom, sdkmath.NewIntFromBigInt(new(big.Int).Abs(delta)))) - return k.SubBalance(ctx, cosmosAddr, coins) + // burn + coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdkmath.NewIntFromBigInt(new(big.Int).Neg(delta)))) + if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, cosmosAddr, types.ModuleName, coins); err != nil { + return err + } + if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins); err != nil { + return err + } default: // not changed } diff --git a/x/evm/statedb/statedb.go b/x/evm/statedb/statedb.go index c44c7d95..220a3de4 100644 --- a/x/evm/statedb/statedb.go +++ b/x/evm/statedb/statedb.go @@ -17,11 +17,9 @@ package statedb import ( "fmt" - "math/big" "sort" errorsmod "cosmossdk.io/errors" - sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" @@ -375,36 +373,18 @@ func (s *StateDB) CacheContext() sdk.Context { */ // AddBalance adds amount to the account associated with addr. -func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { - if amount.Sign() <= 0 { - return - } - coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))} - if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { - return s.keeper.AddBalance(ctx, sdk.AccAddress(addr.Bytes()), coins) - }); err != nil { - s.err = err +func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int) { + stateObject := s.getOrNewStateObject(addr) + if stateObject != nil { + stateObject.AddBalance(amount) } } // SubBalance subtracts amount from the account associated with addr. -func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { - if amount.Sign() <= 0 { - return - } - coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))} - if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { - return s.keeper.SubBalance(ctx, sdk.AccAddress(addr.Bytes()), coins) - }); err != nil { - s.err = err - } -} - -func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { - if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { - return s.keeper.SetBalance(ctx, addr, amount, s.evmDenom) - }); err != nil { - s.err = err +func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int) { + stateObject := s.getOrNewStateObject(addr) + if stateObject != nil { + stateObject.SubBalance(amount) } }