-
Notifications
You must be signed in to change notification settings - Fork 845
/
evm.go
240 lines (206 loc) · 6.45 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package keeper
import (
"encoding/json"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/evmos/ethermint/server/config"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/evmos/evmos/v6/contracts"
"github.com/evmos/evmos/v6/x/erc20/types"
)
// DeployERC20Contract creates and deploys an ERC20 contract on the EVM with the
// erc20 module account as owner.
func (k Keeper) DeployERC20Contract(
ctx sdk.Context,
coinMetadata banktypes.Metadata,
) (common.Address, error) {
decimals := uint8(0)
if len(coinMetadata.DenomUnits) > 0 {
decimalsIdx := len(coinMetadata.DenomUnits) - 1
decimals = uint8(coinMetadata.DenomUnits[decimalsIdx].Exponent)
}
ctorArgs, err := contracts.ERC20MinterBurnerDecimalsContract.ABI.Pack(
"",
coinMetadata.Name,
coinMetadata.Symbol,
decimals,
)
if err != nil {
return common.Address{}, sdkerrors.Wrapf(types.ErrABIPack, "coin metadata is invalid %s: %s", coinMetadata.Name, err.Error())
}
data := make([]byte, len(contracts.ERC20MinterBurnerDecimalsContract.Bin)+len(ctorArgs))
copy(data[:len(contracts.ERC20MinterBurnerDecimalsContract.Bin)], contracts.ERC20MinterBurnerDecimalsContract.Bin)
copy(data[len(contracts.ERC20MinterBurnerDecimalsContract.Bin):], ctorArgs)
nonce, err := k.accountKeeper.GetSequence(ctx, types.ModuleAddress.Bytes())
if err != nil {
return common.Address{}, err
}
contractAddr := crypto.CreateAddress(types.ModuleAddress, nonce)
_, err = k.CallEVMWithData(ctx, types.ModuleAddress, nil, data, true)
if err != nil {
return common.Address{}, sdkerrors.Wrapf(err, "failed to deploy contract for %s", coinMetadata.Name)
}
return contractAddr, nil
}
// QueryERC20 returns the data of a deployed ERC20 contract
func (k Keeper) QueryERC20(
ctx sdk.Context,
contract common.Address,
) (types.ERC20Data, error) {
var (
nameRes types.ERC20StringResponse
symbolRes types.ERC20StringResponse
decimalRes types.ERC20Uint8Response
)
erc20 := contracts.ERC20MinterBurnerDecimalsContract.ABI
// Name
res, err := k.CallEVM(ctx, erc20, types.ModuleAddress, contract, false, "name")
if err != nil {
return types.ERC20Data{}, err
}
if err := erc20.UnpackIntoInterface(&nameRes, "name", res.Ret); err != nil {
return types.ERC20Data{}, sdkerrors.Wrapf(
types.ErrABIUnpack, "failed to unpack name: %s", err.Error(),
)
}
// Symbol
res, err = k.CallEVM(ctx, erc20, types.ModuleAddress, contract, false, "symbol")
if err != nil {
return types.ERC20Data{}, err
}
if err := erc20.UnpackIntoInterface(&symbolRes, "symbol", res.Ret); err != nil {
return types.ERC20Data{}, sdkerrors.Wrapf(
types.ErrABIUnpack, "failed to unpack symbol: %s", err.Error(),
)
}
// Decimals
res, err = k.CallEVM(ctx, erc20, types.ModuleAddress, contract, false, "decimals")
if err != nil {
return types.ERC20Data{}, err
}
if err := erc20.UnpackIntoInterface(&decimalRes, "decimals", res.Ret); err != nil {
return types.ERC20Data{}, sdkerrors.Wrapf(
types.ErrABIUnpack, "failed to unpack decimals: %s", err.Error(),
)
}
return types.NewERC20Data(nameRes.Value, symbolRes.Value, decimalRes.Value), nil
}
// BalanceOf queries an account's balance for a given ERC20 contract
func (k Keeper) BalanceOf(
ctx sdk.Context,
abi abi.ABI,
contract, account common.Address,
) *big.Int {
res, err := k.CallEVM(ctx, abi, types.ModuleAddress, contract, false, "balanceOf", account)
if err != nil {
return nil
}
unpacked, err := abi.Unpack("balanceOf", res.Ret)
if err != nil || len(unpacked) == 0 {
return nil
}
balance, ok := unpacked[0].(*big.Int)
if !ok {
return nil
}
return balance
}
// CallEVM performs a smart contract method call using given args
func (k Keeper) CallEVM(
ctx sdk.Context,
abi abi.ABI,
from, contract common.Address,
commit bool,
method string,
args ...interface{},
) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, args...)
if err != nil {
return nil, sdkerrors.Wrap(
types.ErrABIPack,
sdkerrors.Wrap(err, "failed to create transaction data").Error(),
)
}
resp, err := k.CallEVMWithData(ctx, from, &contract, data, commit)
if err != nil {
return nil, sdkerrors.Wrapf(err, "contract call failed: method '%s', contract '%s'", method, contract)
}
return resp, nil
}
// CallEVMWithData performs a smart contract method call using contract data
func (k Keeper) CallEVMWithData(
ctx sdk.Context,
from common.Address,
contract *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: contract,
Data: (*hexutil.Bytes)(&data),
})
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrJSONMarshal, "failed to marshal tx args: %s", err.Error())
}
gasRes, err := k.evmKeeper.EstimateGas(sdk.WrapSDKContext(ctx), &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
})
if err != nil {
return nil, err
}
gasCap = gasRes.Gas
}
msg := ethtypes.NewMessage(
from,
contract,
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, sdkerrors.Wrap(evmtypes.ErrVMExecution, res.VmError)
}
return res, nil
}
// monitorApprovalEvent returns an error if the given transactions logs include
// an unexpected `Approval` event
func (k Keeper) monitorApprovalEvent(res *evmtypes.MsgEthereumTxResponse) error {
if res == nil || len(res.Logs) == 0 {
return nil
}
logApprovalSig := []byte("Approval(address,address,uint256)")
logApprovalSigHash := crypto.Keccak256Hash(logApprovalSig)
for _, log := range res.Logs {
if log.Topics[0] == logApprovalSigHash.Hex() {
return sdkerrors.Wrapf(
types.ErrUnexpectedEvent, "unexpected Approval event",
)
}
}
return nil
}