-
Notifications
You must be signed in to change notification settings - Fork 127
/
token.go
110 lines (93 loc) · 2.92 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package wrapper
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)
)
// NewEVMKeeper 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 NewEVMKeeper(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 {
// NOTICE: when the account has not executed native transactions, the pubkey is empty.
if pubKey == nil {
return true
}
_, ok := pubKey.(*ethsecp256k1.PubKey)
return ok
}
// NewICS20Keeper wraps the given ibctransferkeeper.Keeper into an ics20Keeper.
//
// Parameters:
// - ik: the ibctransferkeeper.Keeper to be wrapped.
//
// Return:
// - *ics20Keeper: the wrapped ics20Keeper.
func NewICS20Keeper(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
}