Skip to content

Commit

Permalink
refactor token module
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Apr 17, 2024
1 parent 04bc996 commit 094b4a8
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 20 deletions.
39 changes: 22 additions & 17 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
105 changes: 105 additions & 0 deletions modules/token/wrapper.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 094b4a8

Please sign in to comment.