Skip to content

Commit 1c5d500

Browse files
authored
Added possibility to send transactions with access list (#727)
* added possibility to send transactions with access list
1 parent 4c171d2 commit 1c5d500

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

ethcontract/src/contract/method.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::transaction::{Account, GasPrice, TransactionBuilder, TransactionResul
66
use crate::{batch::CallBatch, errors::MethodError, tokens::Tokenize};
77
use ethcontract_common::abi::{Function, Token};
88
use std::marker::PhantomData;
9-
use web3::types::{Address, BlockId, Bytes, CallRequest, U256};
9+
use web3::types::{AccessList, Address, BlockId, Bytes, CallRequest, U256};
1010
use web3::Transport;
1111
use web3::{api::Web3, BatchTransport};
1212

@@ -114,6 +114,12 @@ impl<T: Transport, R: Tokenize> MethodBuilder<T, R> {
114114
self
115115
}
116116

117+
/// Specify the access list for the transaction, if not specified no access list will be used.
118+
pub fn access_list(mut self, value: AccessList) -> Self {
119+
self.tx = self.tx.access_list(value);
120+
self
121+
}
122+
117123
/// Extract inner `TransactionBuilder` from this `SendBuilder`. This exposes
118124
/// `TransactionBuilder` only APIs.
119125
pub fn into_inner(self) -> TransactionBuilder<T> {
@@ -203,6 +209,12 @@ impl<T: Transport, R: Tokenize> ViewMethodBuilder<T, R> {
203209
self
204210
}
205211

212+
/// Specify the access list for the transaction, if not specified no access list will be used.
213+
pub fn access_list(mut self, value: AccessList) -> Self {
214+
self.m = self.m.access_list(value);
215+
self
216+
}
217+
206218
/// Specify the block height for the call, if not specified then latest
207219
/// mined block will be used.
208220
pub fn block(mut self, value: BlockId) -> Self {
@@ -250,7 +262,7 @@ impl<T: Transport, R: Tokenize> ViewMethodBuilder<T, R> {
250262
value: self.m.tx.value,
251263
data: self.m.tx.data,
252264
transaction_type: resolved_gas_price.transaction_type,
253-
access_list: None,
265+
access_list: self.m.tx.access_list,
254266
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
255267
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
256268
},
@@ -289,6 +301,7 @@ mod tests {
289301
use super::*;
290302
use crate::test::prelude::*;
291303
use ethcontract_common::abi::{Param, ParamType};
304+
use web3::types::AccessListItem;
292305

293306
fn test_abi_function() -> (Function, Bytes) {
294307
#[allow(deprecated)]
@@ -324,6 +337,7 @@ mod tests {
324337
.gas_price(2.0.into())
325338
.value(28.into())
326339
.nonce(42.into())
340+
.access_list(vec![AccessListItem::default()])
327341
.into_inner();
328342

329343
assert_eq!(tx.from.map(|a| a.address()), Some(from));
@@ -333,6 +347,7 @@ mod tests {
333347
assert_eq!(tx.value, Some(28.into()));
334348
assert_eq!(tx.data, Some(data));
335349
assert_eq!(tx.nonce, Some(42.into()));
350+
assert_eq!(tx.access_list, Some(vec![AccessListItem::default()]));
336351
transport.assert_no_more_requests();
337352
}
338353

ethcontract/src/transaction.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::send::TransactionResult;
1313
use crate::errors::ExecutionError;
1414
use crate::secret::{Password, PrivateKey};
1515
use web3::api::Web3;
16-
use web3::types::{Address, Bytes, CallRequest, TransactionCondition, U256};
16+
use web3::types::{AccessList, Address, Bytes, CallRequest, TransactionCondition, U256};
1717
use web3::Transport;
1818

1919
/// The account type used for signing the transaction.
@@ -88,6 +88,8 @@ pub struct TransactionBuilder<T: Transport> {
8888
/// Optional resolve conditions. Defaults to waiting the transaction to be
8989
/// mined without any extra confirmation blocks.
9090
pub resolve: Option<ResolveCondition>,
91+
/// Access list
92+
pub access_list: Option<AccessList>,
9193
}
9294

9395
impl<T: Transport> TransactionBuilder<T> {
@@ -103,6 +105,7 @@ impl<T: Transport> TransactionBuilder<T> {
103105
data: None,
104106
nonce: None,
105107
resolve: None,
108+
access_list: None,
106109
}
107110
}
108111

@@ -162,6 +165,12 @@ impl<T: Transport> TransactionBuilder<T> {
162165
self
163166
}
164167

168+
/// Specify the access list for the transaction, if not specified no access list will be used.
169+
pub fn access_list(mut self, value: AccessList) -> Self {
170+
self.access_list = Some(value);
171+
self
172+
}
173+
165174
/// Specify the number of confirmations to use for the confirmation options.
166175
/// This is a utility method for specifying the resolve condition.
167176
pub fn confirmations(mut self, value: usize) -> Self {
@@ -197,7 +206,7 @@ impl<T: Transport> TransactionBuilder<T> {
197206
value: self.value,
198207
data: self.data.clone(),
199208
transaction_type: resolved_gas_price.transaction_type,
200-
access_list: None,
209+
access_list: self.access_list,
201210
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
202211
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
203212
},
@@ -214,7 +223,7 @@ mod tests {
214223
use crate::errors::ExecutionError;
215224
use crate::test::prelude::*;
216225
use hex_literal::hex;
217-
use web3::types::{H2048, H256};
226+
use web3::types::{AccessListItem, H2048, H256};
218227

219228
#[test]
220229
fn tx_builder_estimate_gas() {
@@ -260,6 +269,7 @@ mod tests {
260269
.value(28.into())
261270
.data(Bytes(vec![0x13, 0x37]))
262271
.nonce(42.into())
272+
.access_list(vec![AccessListItem::default()])
263273
.resolve(ResolveCondition::Pending)
264274
.send()
265275
.immediate()
@@ -277,6 +287,10 @@ mod tests {
277287
"value": "0x1c",
278288
"data": "0x1337",
279289
"nonce": "0x2a",
290+
"accessList": [{
291+
"address": "0x0000000000000000000000000000000000000000",
292+
"storageKeys": [],
293+
}],
280294
"condition": { "block": 100 },
281295
})],
282296
);

ethcontract/src/transaction/build.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::transaction::gas_price::GasPrice;
1010
use crate::transaction::{Account, TransactionBuilder};
1111
use web3::api::Web3;
1212
use web3::types::{
13-
Address, Bytes, CallRequest, RawTransaction, SignedTransaction, TransactionCondition,
14-
TransactionParameters, TransactionRequest, H256, U256,
13+
AccessList, Address, Bytes, CallRequest, RawTransaction, SignedTransaction,
14+
TransactionCondition, TransactionParameters, TransactionRequest, H256, U256,
1515
};
1616
use web3::Transport;
1717

@@ -29,6 +29,7 @@ impl<T: Transport> TransactionBuilder<T> {
2929
value: self.value,
3030
data: self.data,
3131
nonce: self.nonce,
32+
access_list: self.access_list,
3233
};
3334

3435
let tx = match self.from {
@@ -127,6 +128,8 @@ struct TransactionOptions {
127128
pub data: Option<Bytes>,
128129
/// The transaction nonce.
129130
pub nonce: Option<U256>,
131+
/// The access list
132+
pub access_list: Option<AccessList>,
130133
}
131134

132135
/// Transaction options specific to `TransactionRequests` since they may also
@@ -154,7 +157,7 @@ impl TransactionRequestOptions {
154157
nonce: self.0.nonce,
155158
condition: self.1,
156159
transaction_type: resolved_gas_price.transaction_type,
157-
access_list: None,
160+
access_list: self.0.access_list,
158161
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
159162
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
160163
}
@@ -224,7 +227,7 @@ async fn build_offline_signed_transaction<T: Transport>(
224227
data: options.data.unwrap_or_default(),
225228
chain_id,
226229
transaction_type: resolved_gas_price.transaction_type,
227-
access_list: None,
230+
access_list: options.access_list,
228231
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
229232
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
230233
},
@@ -257,7 +260,7 @@ async fn resolve_gas_limit<T: Transport>(
257260
value: options.value,
258261
data: options.data.clone(),
259262
transaction_type: resolved_gas_price.transaction_type,
260-
access_list: None,
263+
access_list: options.access_list.clone(),
261264
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
262265
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
263266
},

0 commit comments

Comments
 (0)