-
Notifications
You must be signed in to change notification settings - Fork 0
/
evm.go
183 lines (156 loc) · 5 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
package keeper
import (
"encoding/json"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"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/tharsis/ethermint/server/config"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
"github.com/fanfury-sports/blackfury/x/erc20/types"
"github.com/tharsis/evmos/v4/contracts"
)
// 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.ERC20BurnableContract.ABI
// Name
res, err := k.CallEVM(ctx, erc20, types.ModuleAddress, contract, "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, "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, "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
}
func (k Keeper) totalSupply(ctx sdk.Context, contract common.Address) (*big.Int, error) {
erc20 := contracts.ERC20BurnableContract.ABI
// Name
res, err := k.CallEVM(ctx, erc20, types.ModuleAddress, contract, "totalSupply")
if err != nil {
return nil, err
}
unpacked, err := erc20.Unpack("totalSupply", res.Ret)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrABIUnpack, "failed to unpack totalSupply: %s", err)
}
if len(unpacked) > 0 {
supply, ok := unpacked[0].(*big.Int)
if ok {
return supply, nil
}
}
return nil, sdkerrors.Wrapf(types.ErrABIUnpack, "failed to unpack totalSupply")
}
func (k Keeper) balanceOf(ctx sdk.Context, contract, account common.Address) (*big.Int, error) {
erc20 := contracts.ERC20BurnableContract.ABI
// Name
res, err := k.CallEVM(ctx, erc20, types.ModuleAddress, contract, "balanceOf", account)
if err != nil {
return nil, err
}
unpacked, err := erc20.Unpack("balanceOf", res.Ret)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrABIUnpack, "failed to unpack balanceOf: %s", err)
}
if len(unpacked) > 0 {
balance, ok := unpacked[0].(*big.Int)
if ok {
return balance, nil
}
}
return nil, sdkerrors.Wrapf(types.ErrABIUnpack, "failed to unpack balanceOf")
}
// CallEVM performs a smart contract method call using given args
func (k Keeper) CallEVM(
ctx sdk.Context,
abi abi.ABI,
from, contract common.Address,
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)
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,
) (*evmtypes.MsgEthereumTxResponse, error) {
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
args, err := json.Marshal(evmtypes.TransactionArgs{
From: &from,
To: contract,
Data: (*hexutil.Bytes)(&data),
})
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrJSONMarshal, "failled 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
}
msg := ethtypes.NewMessage(
from,
contract,
nonce,
big.NewInt(0), // amount
gasRes.Gas, // gasLimit
big.NewInt(0), // gasFeeCap
big.NewInt(0), // gasTipCap
big.NewInt(0), // gasPrice
data,
ethtypes.AccessList{}, // AccessList
true, // checkNonce
)
res, err := k.evmKeeper.ApplyMessage(ctx, msg, evmtypes.NewNoOpTracer(), true)
if err != nil {
return nil, err
}
if res.Failed() {
return nil, sdkerrors.Wrap(evmtypes.ErrVMExecution, res.VmError)
}
return res, nil
}