From 094b4a87feda4a80382063f9d26e1d91f1cdd93b Mon Sep 17 00:00:00 2001 From: dreamer Date: Wed, 17 Apr 2024 18:53:18 +0800 Subject: [PATCH] refactor token module --- app/keepers/keepers.go | 39 ++++++++------- go.mod | 2 +- go.sum | 4 +- modules/token/wrapper.go | 105 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 modules/token/wrapper.go diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index 6b65ec441..0597f78b2 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -110,6 +110,7 @@ import ( "github.com/irisnet/irishub/v3/modules/internft" mintkeeper "github.com/irisnet/irishub/v3/modules/mint/keeper" minttypes "github.com/irisnet/irishub/v3/modules/mint/types" + iristoken "github.com/irisnet/irishub/v3/modules/token" iristypes "github.com/irisnet/irishub/v3/types" ) @@ -440,23 +441,6 @@ func New( appKeepers.keys[guardiantypes.StoreKey], ) - appKeepers.TokenKeeper = tokenkeeper.NewKeeper( - appCodec, - appKeepers.keys[tokentypes.StoreKey], - appKeepers.BankKeeper, - authtypes.FeeCollectorName, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), - ).WithSwapRegistry(tokenv1.SwapRegistry{ - iristypes.NativeToken.MinUnit: tokenv1.SwapParams{ - MinUnit: iristypes.EvmToken.MinUnit, - Ratio: sdk.OneDec(), - }, - iristypes.EvmToken.MinUnit: tokenv1.SwapParams{ - MinUnit: iristypes.NativeToken.MinUnit, - Ratio: sdk.OneDec(), - }, - }) - appKeepers.RecordKeeper = recordkeeper.NewKeeper( appCodec, appKeepers.keys[recordtypes.StoreKey], @@ -561,6 +545,27 @@ func New( cast.ToString(appOpts.Get(srvflags.EVMTracer)), appKeepers.GetSubspace(evmtypes.ModuleName), ) + + appKeepers.TokenKeeper = tokenkeeper.NewKeeper( + appCodec, + appKeepers.keys[tokentypes.StoreKey], + appKeepers.BankKeeper, + appKeepers.AccountKeeper, + iristoken.WrapEVMKeeper(appKeepers.EvmKeeper), + iristoken.WrapICS20Keeper(appKeepers.IBCTransferKeeper), + authtypes.FeeCollectorName, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), + ).WithSwapRegistry(tokenv1.SwapRegistry{ + iristypes.NativeToken.MinUnit: tokenv1.SwapParams{ + MinUnit: iristypes.EvmToken.MinUnit, + Ratio: sdk.OneDec(), + }, + iristypes.EvmToken.MinUnit: tokenv1.SwapParams{ + MinUnit: iristypes.NativeToken.MinUnit, + Ratio: sdk.OneDec(), + }, + }) + appKeepers.EvmKeeper = appKeepers.EvmKeeper.SetHooks(appKeepers.TokenKeeper.Hooks()) return appKeepers } diff --git a/go.mod b/go.mod index e703ec7ac..dec033d74 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/cosmos/iavl v0.20.1 // indirect github.com/cosmos/ibc-go/v7 v7.3.0 github.com/evmos/ethermint v0.22.0 - github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674 //release/v1.8.0-lsm + github.com/irisnet/irismod v1.8.1-0.20240416092250-4018d9c74e78 //release/v1.8.0-lsm ) require ( diff --git a/go.sum b/go.sum index 37f0bf16d..cdd261627 100644 --- a/go.sum +++ b/go.sum @@ -846,8 +846,8 @@ github.com/influxdata/promql/v2 v2.12.0/go.mod h1:fxOPu+DY0bqCTCECchSRtWfc+0X19y github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bSgUQ7q5ZLSO+bKBGqJiCBGAl+9DxyW63zLTujjUlOE= github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0= github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po= -github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674 h1:7fBGecB0olPrkoeY0Y7wn202BQ4Q2zF8o+KwYFzNYQs= -github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674/go.mod h1:kvPd2HckP0Mr4BZRtUa2RjDNH1EfOP3xHESHRIXayOY= +github.com/irisnet/irismod v1.8.1-0.20240416092250-4018d9c74e78 h1:o2P1Ve1XO72zrGjFo8lGIaDvgWw4YJkUIQW7pEgNJ94= +github.com/irisnet/irismod v1.8.1-0.20240416092250-4018d9c74e78/go.mod h1:yR/g0kuqqdrUdoXJsRHyajGZxogAJZjRbj8OWEfK9cA= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e/go.mod h1:G1CVv03EnqU1wYL2dFwXxW2An0az9JTl/ZsqXQeBlkU= diff --git a/modules/token/wrapper.go b/modules/token/wrapper.go new file mode 100644 index 000000000..8d330c6f3 --- /dev/null +++ b/modules/token/wrapper.go @@ -0,0 +1,105 @@ +package token + +import ( + "context" + "math/big" + "strings" + + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/vm" + + "github.com/evmos/ethermint/crypto/ethsecp256k1" + evmkeeper "github.com/evmos/ethermint/x/evm/keeper" + evmtypes "github.com/evmos/ethermint/x/evm/types" + + ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper" + ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" + + tokentypes "github.com/irisnet/irismod/modules/token/types" + irismodtypes "github.com/irisnet/irismod/types" +) + +var ( + _ tokentypes.EVMKeeper = (*evmKeeper)(nil) + _ tokentypes.ICS20Keeper = (*ics20Keeper)(nil) +) + +// WrapEVMKeeper wraps the given evmkeeper.Keeper and returns a new evmKeeper. +// +// ek: The evmkeeper.Keeper to be wrapped. +// Returns a pointer to the wrapped evmKeeper. +func WrapEVMKeeper(ek *evmkeeper.Keeper) tokentypes.EVMKeeper { + return &evmKeeper{ek: ek} +} + +type evmKeeper struct { + ek *evmkeeper.Keeper +} + +// ApplyMessage implements types.EVMKeeper. +func (e *evmKeeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*irismodtypes.Result, error) { + res, err := e.ek.ApplyMessage(ctx, msg, tracer, commit) + if err != nil { + return nil, err + } + return &irismodtypes.Result{ + Hash: res.Hash, + Logs: evmtypes.LogsToEthereum(res.Logs), + Ret: res.Ret, + VMError: res.VmError, + GasUsed: res.GasUsed, + }, nil +} + +// ChainID implements types.EVMKeeper. +func (e *evmKeeper) ChainID() *big.Int { + return e.ek.ChainID() +} + +// EstimateGas implements types.EVMKeeper. +func (e *evmKeeper) EstimateGas(ctx context.Context, req *irismodtypes.EthCallRequest) (uint64, error) { + res, err := e.ek.EstimateGas(ctx, &evmtypes.EthCallRequest{ + Args: req.Args, + GasCap: req.GasCap, + ProposerAddress: req.ProposerAddress, + ChainId: req.ChainID, + }) + if err != nil { + return 0, err + } + return res.Gas, nil +} + +// SupportedKey implements types.EVMKeeper. +func (e *evmKeeper) SupportedKey(pubKey cryptotypes.PubKey) bool { + _, ok := pubKey.(*ethsecp256k1.PubKey) + return ok +} + +// WrapICS20Keeper wraps the given ibctransferkeeper.Keeper into an ics20Keeper. +// +// Parameters: +// - ik: the ibctransferkeeper.Keeper to be wrapped. +// +// Return: +// - *ics20Keeper: the wrapped ics20Keeper. +func WrapICS20Keeper(ik ibctransferkeeper.Keeper) tokentypes.ICS20Keeper { + return &ics20Keeper{ik: ik} +} + +type ics20Keeper struct { + ik ibctransferkeeper.Keeper +} + +// HasTrace implements types.ICS20Keeper. +func (i *ics20Keeper) HasTrace(ctx sdk.Context, denom string) bool { + hash, err := ibctransfertypes.ParseHexHash(strings.TrimPrefix(denom, "ibc/")) + if err != nil { + return false + } + _, has := i.ik.GetDenomTrace(ctx, hash) + return has +}