-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d144bc4
commit 84d990f
Showing
7 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package statedb_modifier | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/ethereum/go-ethereum/params" | ||
evm "github.com/evmos/ethermint/x/evm/vm" | ||
"github.com/evmos/ethermint/x/evm/vm/geth" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
ethermint_statedb "github.com/evmos/ethermint/x/evm/statedb" | ||
) | ||
|
||
var ErrUnsupportedStateDB = errors.New("unsupported statedb") | ||
|
||
type IBCTransferKeeper interface { | ||
Transfer(goCtx context.Context, msg *ibctransfertypes.MsgTransfer) (*ibctransfertypes.MsgTransferResponse, error) | ||
} | ||
|
||
type EvmutilKeeper interface { | ||
ConvertERC20ToCoin( | ||
goCtx context.Context, | ||
initiator string, | ||
receiver string, | ||
kavaERC20Address string, | ||
amount sdk.Int, | ||
) error | ||
} | ||
|
||
type ModifiedStateDB struct { | ||
vm.StateDB | ||
ibcTransferKeeper IBCTransferKeeper | ||
evmutilKeeper EvmutilKeeper | ||
} | ||
|
||
func NewModifiedStateDB(stateDB vm.StateDB, ibcTransferKeeper IBCTransferKeeper, evmutilKeeper EvmutilKeeper) *ModifiedStateDB { | ||
return &ModifiedStateDB{ | ||
StateDB: stateDB, | ||
ibcTransferKeeper: ibcTransferKeeper, | ||
evmutilKeeper: evmutilKeeper, | ||
} | ||
} | ||
|
||
func GetEVMConstructor( | ||
ibcTransferKeeper IBCTransferKeeper, | ||
evmutilKeeper EvmutilKeeper, | ||
) evm.Constructor { | ||
return func( | ||
blockCtx vm.BlockContext, | ||
txCtx vm.TxContext, | ||
stateDB vm.StateDB, | ||
chainConfig *params.ChainConfig, | ||
config vm.Config, | ||
customPrecompiles evm.PrecompiledContracts, | ||
) evm.EVM { | ||
customStateDB := NewModifiedStateDB(stateDB, ibcTransferKeeper, evmutilKeeper) | ||
return geth.NewEVM(blockCtx, txCtx, customStateDB, chainConfig, config, customPrecompiles) | ||
} | ||
} | ||
|
||
func (s *ModifiedStateDB) Context() (context.Context, error) { | ||
stateDB, ok := s.StateDB.(*ethermint_statedb.StateDB) | ||
if !ok { | ||
return nil, ErrUnsupportedStateDB | ||
} | ||
|
||
return stateDB.Context(), nil | ||
} | ||
|
||
func (s *ModifiedStateDB) IBCTransfer(goCtx context.Context, msg *ibctransfertypes.MsgTransfer) (*ibctransfertypes.MsgTransferResponse, error) { | ||
resp, err := s.ibcTransferKeeper.Transfer(goCtx, msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (s *ModifiedStateDB) ConvertERC20ToCoin( | ||
goCtx context.Context, | ||
initiator string, | ||
receiver string, | ||
kavaERC20Address string, | ||
amount sdk.Int, | ||
) error { | ||
return s.evmutilKeeper.ConvertERC20ToCoin(goCtx, initiator, receiver, kavaERC20Address, amount) | ||
} |