-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth.go
172 lines (150 loc) · 5.33 KB
/
eth.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
// Copyright 2022 Serv Foundation
// This file is part of the Serv Network packages.
//
// Serv is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Serv packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Serv packages. If not, see https://github.com/twobitedd/serv/blob/main/LICENSE
package tx
import (
"encoding/json"
"math/big"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/twobitedd/serv/v12/app"
"github.com/twobitedd/serv/v12/server/config"
"github.com/twobitedd/serv/v12/utils"
evmtypes "github.com/twobitedd/serv/v12/x/evm/types"
)
// PrepareEthTx creates an ethereum tx and signs it with the provided messages and private key.
// It returns the signed transaction and an error
func PrepareEthTx(
txCfg client.TxConfig,
appServ *app.Serv,
priv cryptotypes.PrivKey,
msgs ...sdk.Msg,
) (authsigning.Tx, error) {
txBuilder := txCfg.NewTxBuilder()
signer := ethtypes.LatestSignerForChainID(appServ.EvmKeeper.ChainID())
txFee := sdk.Coins{}
txGasLimit := uint64(0)
// Sign messages and compute gas/fees.
for _, m := range msgs {
msg, ok := m.(*evmtypes.MsgEthereumTx)
if !ok {
return nil, errorsmod.Wrapf(errorsmod.Error{}, "cannot mix Ethereum and Cosmos messages in one Tx")
}
if priv != nil {
err := msg.Sign(signer, NewSigner(priv))
if err != nil {
return nil, err
}
}
msg.From = ""
txGasLimit += msg.GetGas()
txFee = txFee.Add(sdk.Coin{Denom: utils.BaseDenom, Amount: sdkmath.NewIntFromBigInt(msg.GetFee())})
}
if err := txBuilder.SetMsgs(msgs...); err != nil {
return nil, err
}
// Set the extension
var option *codectypes.Any
option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
if err != nil {
return nil, err
}
builder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder)
if !ok {
return nil, errorsmod.Wrapf(errorsmod.Error{}, "could not set extensions for Ethereum tx")
}
builder.SetExtensionOptions(option)
txBuilder.SetGasLimit(txGasLimit)
txBuilder.SetFeeAmount(txFee)
return txBuilder.GetTx(), nil
}
// CreateEthTx is a helper function to create and sign an Ethereum transaction.
//
// If the given private key is not nil, it will be used to sign the transaction.
//
// It offers the ability to increment the nonce by a given amount in case one wants to set up
// multiple transactions that are supposed to be executed one after another.
// Should this not be the case, just pass in zero.
func CreateEthTx(
ctx sdk.Context,
appServ *app.Serv,
privKey cryptotypes.PrivKey,
from sdk.AccAddress,
dest sdk.AccAddress,
amount *big.Int,
nonceIncrement int,
) (*evmtypes.MsgEthereumTx, error) {
toAddr := common.BytesToAddress(dest.Bytes())
fromAddr := common.BytesToAddress(from.Bytes())
chainID := appServ.EvmKeeper.ChainID()
// When we send multiple Ethereum Tx's in one Cosmos Tx, we need to increment the nonce for each one.
nonce := appServ.EvmKeeper.GetNonce(ctx, fromAddr) + uint64(nonceIncrement)
evmTxParams := &evmtypes.EvmTxArgs{
ChainID: chainID,
Nonce: nonce,
To: &toAddr,
Amount: amount,
GasLimit: 100000,
GasFeeCap: appServ.FeeMarketKeeper.GetBaseFee(ctx),
GasTipCap: big.NewInt(1),
Accesses: ðtypes.AccessList{},
}
msgEthereumTx := evmtypes.NewTx(evmTxParams)
msgEthereumTx.From = fromAddr.String()
// If we are creating multiple eth Tx's with different senders, we need to sign here rather than later.
if privKey != nil {
signer := ethtypes.LatestSignerForChainID(appServ.EvmKeeper.ChainID())
err := msgEthereumTx.Sign(signer, NewSigner(privKey))
if err != nil {
return nil, err
}
}
return msgEthereumTx, nil
}
// GasLimit estimates the gas limit for the provided parameters. To achieve
// this, need to provide the corresponding QueryClient to call the
// `eth_estimateGas` rpc method. If not provided, returns a default value
func GasLimit(ctx sdk.Context, from common.Address, data evmtypes.HexString, queryClientEvm evmtypes.QueryClient) (uint64, error) {
// default gas limit (used if no queryClientEvm is provided)
gas := uint64(100000000000)
if queryClientEvm != nil {
args, err := json.Marshal(&evmtypes.TransactionArgs{
From: &from,
Data: (*hexutil.Bytes)(&data),
})
if err != nil {
return gas, err
}
goCtx := sdk.WrapSDKContext(ctx)
res, err := queryClientEvm.EstimateGas(goCtx, &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
})
if err != nil {
return gas, err
}
gas = res.Gas
}
return gas, nil
}