forked from meshplus/eth-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptor.go
124 lines (117 loc) · 3.6 KB
/
adaptor.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
package adaptor
import (
"errors"
"math/big"
"github.com/axiomesh/axiom-kit/types"
vm "github.com/axiomesh/eth-kit/evm"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func CallArgsToMessage(args *types.CallArgs, globalGasCap uint64, baseFee *big.Int) (*vm.Message, error) {
// Reject invalid combinations of pre- and post-1559 fee styles
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
}
// Set sender address or use zero address if none specified.
addr := args.GetFrom()
// Set default gas & gas price if none were set
gas := globalGasCap
if gas == 0 {
gas = uint64(math.MaxUint64 / 2)
}
if args.Gas != nil {
gas = uint64(*args.Gas)
}
if globalGasCap != 0 && globalGasCap < gas {
// log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
gas = globalGasCap
}
var (
gasPrice *big.Int
gasFeeCap *big.Int
gasTipCap *big.Int
)
if baseFee == nil {
// If there's no basefee, then it must be a non-1559 execution
gasPrice = new(big.Int)
if args.GasPrice != nil {
gasPrice = args.GasPrice.ToInt()
}
gasFeeCap, gasTipCap = gasPrice, gasPrice
} else {
// A basefee is provided, necessitating 1559-type execution
if args.GasPrice != nil {
// User specified the legacy gas field, convert to 1559 gas typing
gasPrice = args.GasPrice.ToInt()
gasFeeCap, gasTipCap = gasPrice, gasPrice
} else {
// User specified 1559 gas fields (or none), use those
gasFeeCap = new(big.Int)
if args.MaxFeePerGas != nil {
gasFeeCap = args.MaxFeePerGas.ToInt()
}
gasTipCap = new(big.Int)
if args.MaxPriorityFeePerGas != nil {
gasTipCap = args.MaxPriorityFeePerGas.ToInt()
}
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
gasPrice = new(big.Int)
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
}
}
}
value := new(big.Int)
if args.Value != nil {
value = args.Value.ToInt()
}
data := args.GetData()
var accessList ethtypes.AccessList
if args.AccessList != nil {
accessList = *args.AccessList
}
msg := &vm.Message{
From: addr,
To: args.To,
Value: value,
GasLimit: gas,
GasPrice: gasPrice,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Data: data,
AccessList: accessList,
SkipAccountChecks: true,
}
return msg, nil
}
func TransactionToMessage(tx *types.Transaction) *vm.Message {
from := common.BytesToAddress(tx.GetFrom().Bytes())
var to *common.Address
if tx.GetTo() != nil {
toAddr := common.BytesToAddress(tx.GetTo().Bytes())
to = &toAddr
}
isFake := false
if v, _, _ := tx.GetRawSignature(); v == nil {
isFake = true
}
msg := &vm.Message{
Nonce: tx.GetNonce(),
GasLimit: tx.GetGas(),
GasPrice: new(big.Int).Set(tx.GetGasPrice()),
GasFeeCap: new(big.Int).Set(tx.GetGasFeeCap()),
GasTipCap: new(big.Int).Set(tx.GetGasTipCap()),
From: from,
To: to,
Value: tx.GetValue(),
Data: tx.GetPayload(),
AccessList: tx.GetInner().GetAccessList(),
SkipAccountChecks: isFake,
}
// If baseFee provided, set gasPrice to effectiveGasPrice.
// if baseFee != nil {
// msg.GasPrice = cmath.BigMin(msg.GasPrice.Add(msg.GasTipCap, baseFee), msg.GasFeeCap)
// }
return msg
}