-
Notifications
You must be signed in to change notification settings - Fork 0
/
evm.go
105 lines (92 loc) · 2.59 KB
/
evm.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
package keeper
import (
"encoding/json"
"math/big"
"github.com/irisnet/erc721-bridge/x/converter/types"
"github.com/ethereum/go-ethereum/accounts/abi"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/evmos/ethermint/server/config"
evmtypes "github.com/evmos/ethermint/x/evm/types"
)
// CallEVM performs a smart contract method call using given args
func (k Keeper) CallEVM(
ctx sdk.Context,
contractABI abi.ABI,
from, to common.Address,
commit bool,
method string,
args ...interface{},
) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := contractABI.Pack(method, args...)
if err != nil {
return nil, errorsmod.Wrap(
types.ErrABIPack,
errorsmod.Wrap(err, "failed to create transaction data").Error(),
)
}
resp, err := k.CallEVMWithData(ctx, from, &to, data, commit)
if err != nil {
return nil, errorsmod.Wrapf(err, "contract call failed: method '%s', contract '%s'", method, to)
}
return resp, nil
}
// CallEVMWithData performs a smart contract method call using contract data
func (k Keeper) CallEVMWithData(
ctx sdk.Context,
from common.Address,
contractAddress *common.Address,
data []byte,
commit bool,
) (*evmtypes.MsgEthereumTxResponse, error) {
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
gasCap := config.DefaultGasCap
if commit {
args, err := json.Marshal(evmtypes.TransactionArgs{
From: &from,
To: contractAddress,
Data: (*hexutil.Bytes)(&data),
})
if err != nil {
return nil, errorsmod.Wrapf(errortypes.ErrJSONMarshal, "failed to marshal tx args: %s", err.Error())
}
//k.evmKeeper.
gasRes, err := k.evmKeeper.EstimateGas(sdk.WrapSDKContext(ctx), &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
ChainId: k.evmKeeper.ChainID().Int64(),
})
if err != nil {
return nil, err
}
gasCap = gasRes.Gas
}
msg := ethtypes.NewMessage(
from,
contractAddress,
nonce,
big.NewInt(0), // amount
gasCap, // gasLimit
big.NewInt(0), // gasFeeCap
big.NewInt(0), // gasTipCap
big.NewInt(0), // gasPrice
data,
ethtypes.AccessList{}, // AccessList
!commit, // isFake
)
res, err := k.evmKeeper.ApplyMessage(ctx, msg, evmtypes.NewNoOpTracer(), commit)
if err != nil {
return nil, err
}
if res.Failed() {
return nil, errorsmod.Wrap(evmtypes.ErrVMExecution, res.VmError)
}
return res, nil
}