-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathEip1559TransactionLib.sol
More file actions
181 lines (158 loc) · 6.61 KB
/
Eip1559TransactionLib.sol
File metadata and controls
181 lines (158 loc) · 6.61 KB
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
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.13 <0.9.0;
import {VmRlp} from "../StdVm.sol";
import {TxRlp} from "./TxRlp.sol";
import {AccessListItem} from "./AccessListTypes.sol";
/// @notice An EIP-1559 (type 2) Ethereum transaction.
struct Eip1559Transaction {
uint64 chainId;
uint64 nonce;
uint256 maxPriorityFeePerGas;
uint256 maxFeePerGas;
uint64 gasLimit;
address to;
uint256 value;
bytes data;
AccessListItem[] accessList;
}
/// @title Builder and RLP encoder for EIP-1559 transactions (type 0x02).
library Eip1559TransactionLib {
using Eip1559TransactionLib for Eip1559Transaction;
/// @notice EIP-1559 transaction type prefix.
uint8 internal constant TX_TYPE = 0x02;
/// @notice Creates a new EIP-1559 transaction with default values.
function create() internal view returns (Eip1559Transaction memory tx_) {
tx_.chainId = uint64(block.chainid);
tx_.gasLimit = 21000;
}
/// @notice Sets the chain ID.
function withChainId(Eip1559Transaction memory self, uint64 chainId)
internal
pure
returns (Eip1559Transaction memory)
{
self.chainId = chainId;
return self;
}
/// @notice Sets the nonce.
function withNonce(Eip1559Transaction memory self, uint64 nonce) internal pure returns (Eip1559Transaction memory) {
self.nonce = nonce;
return self;
}
/// @notice Sets the max priority fee per gas.
function withMaxPriorityFeePerGas(Eip1559Transaction memory self, uint256 fee)
internal
pure
returns (Eip1559Transaction memory)
{
self.maxPriorityFeePerGas = fee;
return self;
}
/// @notice Sets the max fee per gas.
function withMaxFeePerGas(Eip1559Transaction memory self, uint256 fee)
internal
pure
returns (Eip1559Transaction memory)
{
self.maxFeePerGas = fee;
return self;
}
/// @notice Sets the gas limit.
function withGasLimit(Eip1559Transaction memory self, uint64 gasLimit)
internal
pure
returns (Eip1559Transaction memory)
{
self.gasLimit = gasLimit;
return self;
}
/// @notice Sets the recipient address.
function withTo(Eip1559Transaction memory self, address to) internal pure returns (Eip1559Transaction memory) {
self.to = to;
return self;
}
/// @notice Sets the value to send.
function withValue(Eip1559Transaction memory self, uint256 value)
internal
pure
returns (Eip1559Transaction memory)
{
self.value = value;
return self;
}
/// @notice Sets the calldata.
function withData(Eip1559Transaction memory self, bytes memory data)
internal
pure
returns (Eip1559Transaction memory)
{
self.data = data;
return self;
}
/// @notice Sets the access list.
function withAccessList(Eip1559Transaction memory self, AccessListItem[] memory accessList)
internal
pure
returns (Eip1559Transaction memory)
{
self.accessList = accessList;
return self;
}
/// @notice RLP encodes the unsigned transaction with type prefix 0x02.
/// @dev Format: 0x02 || RLP([chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList])
function encode(Eip1559Transaction memory self, VmRlp) internal pure returns (bytes memory) {
bytes[] memory fields = new bytes[](9);
fields[0] = TxRlp.encodeString(TxRlp.encodeUint(self.chainId));
fields[1] = TxRlp.encodeString(TxRlp.encodeUint(self.nonce));
fields[2] = TxRlp.encodeString(TxRlp.encodeUint(self.maxPriorityFeePerGas));
fields[3] = TxRlp.encodeString(TxRlp.encodeUint(self.maxFeePerGas));
fields[4] = TxRlp.encodeString(TxRlp.encodeUint(self.gasLimit));
fields[5] = TxRlp.encodeString(self.to == address(0) ? TxRlp.encodeNone() : TxRlp.encodeAddress(self.to));
fields[6] = TxRlp.encodeString(TxRlp.encodeUint(self.value));
fields[7] = TxRlp.encodeString(self.data);
fields[8] = _encodeAccessList(self.accessList);
bytes memory rlpPayload = TxRlp.encodeRawList(fields);
return abi.encodePacked(TX_TYPE, rlpPayload);
}
/// @notice RLP encodes the signed transaction with type prefix 0x02.
/// @dev Format: 0x02 || RLP([chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, yParity, r, s])
function encodeWithSignature(Eip1559Transaction memory self, VmRlp, uint8 v, bytes32 r, bytes32 s)
internal
pure
returns (bytes memory)
{
bytes[] memory fields = new bytes[](12);
fields[0] = TxRlp.encodeString(TxRlp.encodeUint(self.chainId));
fields[1] = TxRlp.encodeString(TxRlp.encodeUint(self.nonce));
fields[2] = TxRlp.encodeString(TxRlp.encodeUint(self.maxPriorityFeePerGas));
fields[3] = TxRlp.encodeString(TxRlp.encodeUint(self.maxFeePerGas));
fields[4] = TxRlp.encodeString(TxRlp.encodeUint(self.gasLimit));
fields[5] = TxRlp.encodeString(self.to == address(0) ? TxRlp.encodeNone() : TxRlp.encodeAddress(self.to));
fields[6] = TxRlp.encodeString(TxRlp.encodeUint(self.value));
fields[7] = TxRlp.encodeString(self.data);
fields[8] = _encodeAccessList(self.accessList);
// yParity: 0 or 1 (v - 27)
uint8 yParity = v >= 27 ? v - 27 : v;
fields[9] = TxRlp.encodeString(TxRlp.encodeUint(yParity));
fields[10] = TxRlp.encodeString(TxRlp.encodeBytes32(r));
fields[11] = TxRlp.encodeString(TxRlp.encodeBytes32(s));
bytes memory rlpPayload = TxRlp.encodeRawList(fields);
return abi.encodePacked(TX_TYPE, rlpPayload);
}
/// @notice Encodes the access list as an RLP list.
function _encodeAccessList(AccessListItem[] memory list) private pure returns (bytes memory) {
bytes[] memory encodedItems = new bytes[](list.length);
for (uint256 i = 0; i < list.length; i++) {
bytes[] memory keys = new bytes[](list[i].storageKeys.length);
for (uint256 j = 0; j < list[i].storageKeys.length; j++) {
keys[j] = TxRlp.encodeString(TxRlp.encodeBytes32Full(list[i].storageKeys[j]));
}
bytes memory keysList = TxRlp.encodeRawList(keys);
bytes[] memory itemFields = new bytes[](2);
itemFields[0] = TxRlp.encodeString(TxRlp.encodeAddress(list[i].target));
itemFields[1] = keysList;
encodedItems[i] = TxRlp.encodeRawList(itemFields);
}
return TxRlp.encodeRawList(encodedItems);
}
}