1use crate::{CallDecoder, Error, EthCall, Result};
2use alloy_consensus::SignableTransaction;
3use alloy_dyn_abi::{DynSolValue, JsonAbiExt};
4use alloy_json_abi::Function;
5use alloy_network::{
6 eip2718::Encodable2718, Ethereum, IntoWallet, Network, NetworkTransactionBuilder,
7 TransactionBuilder, TransactionBuilder4844, TransactionBuilder7702, TransactionBuilderError,
8 TxSigner,
9};
10use alloy_network_primitives::ReceiptResponse;
11use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, U256};
12use alloy_provider::{PendingTransactionBuilder, Provider};
13use alloy_rpc_types_eth::{
14 state::StateOverride, AccessList, BlobTransactionSidecar, BlobTransactionSidecarEip7594,
15 BlockId, SignedAuthorization,
16};
17use alloy_sol_types::SolCall;
18use std::marker::PhantomData;
19
20pub type SolCallBuilder<P, C, N = Ethereum> = CallBuilder<P, PhantomData<C>, N>;
26
27pub type DynCallBuilder<P, N = Ethereum> = CallBuilder<P, Function, N>;
29
30pub type RawCallBuilder<P, N = Ethereum> = CallBuilder<P, (), N>;
32
33#[derive(Clone)]
129#[must_use = "call builders do nothing unless you `.call`, `.send`, or `.await` them"]
130pub struct CallBuilder<P, D, N: Network = Ethereum> {
131 pub(crate) request: N::TransactionRequest,
132 block: BlockId,
133 state: Option<StateOverride>,
134 pub provider: P,
137 decoder: D,
138}
139
140impl<P, D, N: Network> CallBuilder<P, D, N> {
141 pub fn into_transaction_request(self) -> N::TransactionRequest {
143 self.request
144 }
145
146 pub fn build_unsigned_raw_transaction(self) -> Result<Vec<u8>, TransactionBuilderError<N>>
179 where
180 N::UnsignedTx: SignableTransaction<Signature>,
181 {
182 let tx = self.request.build_unsigned().map_err(|e| e.error)?;
183 Ok(tx.encoded_for_signing())
184 }
185
186 pub async fn build_raw_transaction<S>(
222 self,
223 signer: S,
224 ) -> Result<Vec<u8>, TransactionBuilderError<N>>
225 where
226 S: TxSigner<Signature> + IntoWallet<N>,
227 {
228 let tx = self.request.build(&signer.into_wallet()).await?;
229 Ok(tx.encoded_2718())
230 }
231}
232
233impl<P, D, N: Network> AsRef<N::TransactionRequest> for CallBuilder<P, D, N> {
234 fn as_ref(&self) -> &N::TransactionRequest {
235 &self.request
236 }
237}
238
239impl<P: Provider<N>, N: Network> DynCallBuilder<P, N> {
241 pub(crate) fn new_dyn(
242 provider: P,
243 address: &Address,
244 function: &Function,
245 args: &[DynSolValue],
246 ) -> Result<Self> {
247 Ok(Self::new_inner_call(
248 provider,
249 function.abi_encode_input(args)?.into(),
250 function.clone(),
251 )
252 .to(*address))
253 }
254}
255
256#[doc(hidden)]
257impl<'a, P: Provider<N>, C: SolCall, N: Network> SolCallBuilder<&'a P, C, N> {
258 pub fn new_sol(provider: &'a P, address: &Address, call: &C) -> Self {
261 Self::new_inner_call(provider, call.abi_encode().into(), PhantomData::<C>).to(*address)
262 }
263}
264
265impl<P: Provider<N>, D, N: Network> CallBuilder<P, D, N> {
266 #[inline]
268 pub fn clear_decoder(self) -> RawCallBuilder<P, N> {
269 RawCallBuilder {
270 request: self.request,
271 block: self.block,
272 state: self.state,
273 provider: self.provider,
274 decoder: (),
275 }
276 }
277}
278
279impl<P: Provider<N>, N: Network> RawCallBuilder<P, N> {
280 #[inline]
332 pub fn with_sol_decoder<C: SolCall>(self) -> SolCallBuilder<P, C, N> {
333 SolCallBuilder {
334 request: self.request,
335 block: self.block,
336 state: self.state,
337 provider: self.provider,
338 decoder: PhantomData::<C>,
339 }
340 }
341}
342
343impl<P: Provider<N>, N: Network> RawCallBuilder<P, N> {
344 #[inline]
349 pub fn new_raw(provider: P, input: Bytes) -> Self {
350 Self::new_inner_call(provider, input, ())
351 }
352
353 pub fn new_raw_deploy(provider: P, input: Bytes) -> Self {
359 Self::new_inner_deploy(provider, input, ())
360 }
361}
362
363impl<P: Provider<N>, D: CallDecoder, N: Network> CallBuilder<P, D, N> {
364 fn new_inner_deploy(provider: P, input: Bytes, decoder: D) -> Self {
365 Self {
366 request: <N::TransactionRequest>::default().with_deploy_code(input),
367 decoder,
368 provider,
369 block: BlockId::default(),
370 state: None,
371 }
372 }
373
374 fn new_inner_call(provider: P, input: Bytes, decoder: D) -> Self {
375 Self {
376 request: <N::TransactionRequest>::default().with_input(input),
377 decoder,
378 provider,
379 block: BlockId::default(),
380 state: None,
381 }
382 }
383
384 pub fn chain_id(mut self, chain_id: ChainId) -> Self {
386 self.request.set_chain_id(chain_id);
387 self
388 }
389
390 pub fn from(mut self, from: Address) -> Self {
392 self.request.set_from(from);
393 self
394 }
395
396 pub fn kind(mut self, to: TxKind) -> Self {
398 self.request.set_kind(to);
399 self
400 }
401
402 pub fn to(mut self, to: Address) -> Self {
404 self.request.set_to(to);
405 self
406 }
407
408 pub fn sidecar(mut self, blob_sidecar: BlobTransactionSidecar) -> Self
410 where
411 N::TransactionRequest: TransactionBuilder4844,
412 {
413 self.request.set_blob_sidecar_4844(blob_sidecar);
414 self
415 }
416
417 pub fn sidecar_7594(mut self, sidecar: BlobTransactionSidecarEip7594) -> Self
419 where
420 N::TransactionRequest: TransactionBuilder4844,
421 {
422 self.request.set_blob_sidecar_7594(sidecar);
423 self
424 }
425
426 pub fn gas(mut self, gas: u64) -> Self {
428 self.request.set_gas_limit(gas);
429 self
430 }
431
432 pub fn gas_price(mut self, gas_price: u128) -> Self {
436 self.request.set_gas_price(gas_price);
437 self
438 }
439
440 pub fn max_fee_per_gas(mut self, max_fee_per_gas: u128) -> Self {
442 self.request.set_max_fee_per_gas(max_fee_per_gas);
443 self
444 }
445
446 pub fn max_priority_fee_per_gas(mut self, max_priority_fee_per_gas: u128) -> Self {
448 self.request.set_max_priority_fee_per_gas(max_priority_fee_per_gas);
449 self
450 }
451
452 pub fn max_fee_per_blob_gas(mut self, max_fee_per_blob_gas: u128) -> Self
454 where
455 N::TransactionRequest: TransactionBuilder4844,
456 {
457 self.request.set_max_fee_per_blob_gas(max_fee_per_blob_gas);
458 self
459 }
460
461 pub fn access_list(mut self, access_list: AccessList) -> Self {
463 self.request.set_access_list(access_list);
464 self
465 }
466
467 pub fn authorization_list(mut self, authorization_list: Vec<SignedAuthorization>) -> Self
469 where
470 N::TransactionRequest: TransactionBuilder7702,
471 {
472 self.request.set_authorization_list(authorization_list);
473 self
474 }
475
476 pub fn value(mut self, value: U256) -> Self {
478 self.request.set_value(value);
479 self
480 }
481
482 pub fn nonce(mut self, nonce: u64) -> Self {
484 self.request.set_nonce(nonce);
485 self
486 }
487
488 pub fn map<F>(mut self, f: F) -> Self
490 where
491 F: FnOnce(N::TransactionRequest) -> N::TransactionRequest,
492 {
493 self.request = f(self.request);
494 self
495 }
496
497 pub const fn block(mut self, block: BlockId) -> Self {
499 self.block = block;
500 self
501 }
502
503 pub fn state(mut self, state: impl Into<StateOverride>) -> Self {
509 self.state = Some(state.into());
510 self
511 }
512
513 pub fn calldata(&self) -> &Bytes {
515 self.request.input().expect("set in the constructor")
516 }
517
518 pub async fn estimate_gas(&self) -> Result<u64> {
521 let mut estimate = self.provider.estimate_gas(self.request.clone());
522 if let Some(state) = self.state.clone() {
523 estimate = estimate.overrides(state);
524 }
525 estimate.block(self.block).await.map_err(Into::into)
526 }
527
528 #[doc(alias = "eth_call")]
534 #[doc(alias = "call_with_overrides")]
535 pub fn call(&self) -> EthCall<'_, D, N> {
536 self.call_raw().with_decoder(&self.decoder)
537 }
538
539 pub fn call_raw(&self) -> EthCall<'_, (), N> {
546 let call = self.provider.call(self.request.clone()).block(self.block);
547 let call = match self.state.clone() {
548 Some(state) => call.overrides(state),
549 None => call,
550 };
551 call.into()
552 }
553
554 #[inline]
556 pub fn decode_output(&self, data: Bytes) -> Result<D::CallOutput> {
557 self.decoder.abi_decode_output(data)
558 }
559
560 pub async fn deploy(&self) -> Result<Address> {
571 if !self.request.kind().is_some_and(|to| to.is_create()) {
572 return Err(Error::NotADeploymentTransaction);
573 }
574 let pending_tx = self.send().await?;
575 let receipt = pending_tx.get_receipt().await?;
576 if !receipt.status() {
577 return Err(Error::ContractNotDeployed);
578 }
579 receipt.contract_address().ok_or(Error::ContractNotDeployed)
580 }
581
582 pub async fn deploy_sync(&self) -> Result<Address> {
602 if !self.request.kind().is_some_and(|to| to.is_create()) {
603 return Err(Error::NotADeploymentTransaction);
604 }
605 let receipt = self.send_sync().await?;
606 if !receipt.status() {
607 return Err(Error::ContractNotDeployed);
608 }
609 receipt.contract_address().ok_or(Error::ContractNotDeployed)
610 }
611
612 pub async fn send(&self) -> Result<PendingTransactionBuilder<N>> {
617 Ok(self.provider.send_transaction(self.request.clone()).await?)
618 }
619
620 pub async fn send_sync(&self) -> Result<N::ReceiptResponse> {
634 Ok(self.provider.send_transaction_sync(self.request.clone()).await?)
635 }
636
637 pub fn calculate_create_address(&self) -> Option<Address> {
642 self.request.calculate_create_address()
643 }
644}
645
646impl<P: Clone, D, N: Network> CallBuilder<&P, D, N> {
647 pub fn with_cloned_provider(self) -> CallBuilder<P, D, N> {
649 CallBuilder {
650 request: self.request,
651 block: self.block,
652 state: self.state,
653 provider: self.provider.clone(),
654 decoder: self.decoder,
655 }
656 }
657}
658
659impl<P, D: CallDecoder, N: Network> std::fmt::Debug for CallBuilder<P, D, N> {
660 #[inline]
661 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
662 f.debug_struct("CallBuilder")
663 .field("request", &self.request)
664 .field("block", &self.block)
665 .field("state", &self.state)
666 .field("decoder", &self.decoder.as_debug_field())
667 .finish()
668 }
669}
670
671#[cfg(test)]
672mod tests {
673 use super::*;
674 use alloy_consensus::Transaction;
675 use alloy_network::EthereumWallet;
676 use alloy_node_bindings::Anvil;
677 use alloy_primitives::{address, b256, bytes, hex, utils::parse_units, B256};
678 use alloy_provider::{Provider, ProviderBuilder, WalletProvider};
679 use alloy_rpc_types_eth::{AccessListItem, Authorization};
680 use alloy_signer_local::PrivateKeySigner;
681 use alloy_sol_types::sol;
682 use futures::Future;
683
684 #[test]
685 fn empty_constructor() {
686 sol! {
687 #[sol(rpc, bytecode = "6942")]
688 contract EmptyConstructor {
689 constructor();
690 }
691 }
692
693 let provider = ProviderBuilder::new().connect_anvil();
694 let call_builder = EmptyConstructor::deploy_builder(&provider);
695 assert_eq!(*call_builder.calldata(), bytes!("6942"));
696 }
697
698 sol! {
699 #[sol(rpc, bytecode = "60803461006357601f61014838819003918201601f19168301916001600160401b038311848410176100675780849260209460405283398101031261006357518015158091036100635760ff80195f54169116175f5560405160cc908161007c8239f35b5f80fd5b634e487b7160e01b5f52604160045260245ffdfe60808060405260043610156011575f80fd5b5f3560e01c9081638bf1799f14607a575063b09a261614602f575f80fd5b346076576040366003190112607657602435801515810360765715606f57604060015b81516004356001600160a01b0316815260ff919091166020820152f35b60405f6052565b5f80fd5b346076575f36600319011260765760209060ff5f541615158152f3fea264697066735822122043709781c9bdc30c530978abf5db25a4b4ccfebf989baafd2ba404519a7f7e8264736f6c63430008180033")]
702 contract MyContract {
703 bool public myState;
704
705 constructor(bool myState_) {
706 myState = myState_;
707 }
708
709 function doStuff(uint a, bool b) external pure returns(address c, bytes32 d) {
710 return (address(uint160(a)), bytes32(uint256(b ? 1 : 0)));
711 }
712 }
713 }
714
715 sol! {
716 #[sol(rpc, bytecode = "608080604052346100155760d4908161001a8239f35b5f80fdfe60808060405260043610156011575f80fd5b5f3560e01c90816361bc221a14607e575063d09de08a14602f575f80fd5b34607a575f366003190112607a575f546001600160801b038082166001018181116066576001600160801b03199092169116175f55005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b34607a575f366003190112607a575f546001600160801b03168152602090f3fea26469706673582212208b360e442c4bb2a4bbdec007ee24588c7a88e0aa52ac39efac748e5e23eff69064736f6c63430008180033")]
719 contract Counter {
720 uint128 public counter;
721
722 function increment() external {
723 counter += 1;
724 }
725 }
726 }
727
728 fn build_call_builder() -> CallBuilder<impl Provider, PhantomData<MyContract::doStuffCall>> {
730 let provider = ProviderBuilder::new().connect_anvil();
731 let contract = MyContract::new(Address::ZERO, provider);
732 let call_builder = contract.doStuff(U256::ZERO, true).with_cloned_provider();
733 call_builder
734 }
735
736 #[test]
737 fn change_chain_id() {
738 let call_builder = build_call_builder().chain_id(1337);
739 assert_eq!(
740 call_builder.request.chain_id.expect("chain_id should be set"),
741 1337,
742 "chain_id of request should be '1337'"
743 );
744 }
745
746 #[test]
747 fn change_max_fee_per_gas() {
748 let call_builder = build_call_builder().max_fee_per_gas(42);
749 assert_eq!(
750 call_builder.request.max_fee_per_gas.expect("max_fee_per_gas should be set"),
751 42,
752 "max_fee_per_gas of request should be '42'"
753 );
754 }
755
756 #[test]
757 fn change_max_priority_fee_per_gas() {
758 let call_builder = build_call_builder().max_priority_fee_per_gas(45);
759 assert_eq!(
760 call_builder
761 .request
762 .max_priority_fee_per_gas
763 .expect("max_priority_fee_per_gas should be set"),
764 45,
765 "max_priority_fee_per_gas of request should be '45'"
766 );
767 }
768
769 #[test]
770 fn change_max_fee_per_blob_gas() {
771 let call_builder = build_call_builder().max_fee_per_blob_gas(50);
772 assert_eq!(
773 call_builder.request.max_fee_per_blob_gas.expect("max_fee_per_blob_gas should be set"),
774 50,
775 "max_fee_per_blob_gas of request should be '50'"
776 );
777 }
778
779 #[test]
780 fn change_authorization_list() {
781 let authorization_list = vec![SignedAuthorization::new_unchecked(
782 Authorization { chain_id: U256::from(1337), address: Address::ZERO, nonce: 0 },
783 0,
784 U256::ZERO,
785 U256::ZERO,
786 )];
787 let call_builder = build_call_builder().authorization_list(authorization_list.clone());
788 assert_eq!(
789 call_builder.request.authorization_list.expect("authorization_list should be set"),
790 authorization_list,
791 "Authorization list of the transaction should have been set to our authorization list"
792 );
793 }
794
795 #[test]
796 fn change_access_list() {
797 let access_list = AccessList::from(vec![AccessListItem {
798 address: Address::ZERO,
799 storage_keys: vec![B256::ZERO],
800 }]);
801 let call_builder = build_call_builder().access_list(access_list.clone());
802 assert_eq!(
803 call_builder.request.access_list.expect("access_list should be set"),
804 access_list,
805 "Access list of the transaction should have been set to our access list"
806 )
807 }
808
809 #[test]
810 fn call_encoding() {
811 let provider = ProviderBuilder::new().connect_anvil();
812 let contract = MyContract::new(Address::ZERO, &&provider).with_cloned_provider();
813 let call_builder = contract.doStuff(U256::ZERO, true).with_cloned_provider();
814 assert_eq!(
815 *call_builder.calldata(),
816 bytes!(
817 "b09a2616"
818 "0000000000000000000000000000000000000000000000000000000000000000"
819 "0000000000000000000000000000000000000000000000000000000000000001"
820 ),
821 );
822 let _future: Box<dyn Future<Output = Result<MyContract::doStuffReturn>> + Send> =
824 Box::new(async move { call_builder.call().await });
825 }
826
827 #[test]
828 fn deploy_encoding() {
829 let provider = ProviderBuilder::new().connect_anvil();
830 let bytecode = &MyContract::BYTECODE[..];
831 let call_builder = MyContract::deploy_builder(&provider, false);
832 assert_eq!(
833 call_builder.calldata()[..],
834 [
835 bytecode,
836 &hex!("0000000000000000000000000000000000000000000000000000000000000000")[..]
837 ]
838 .concat(),
839 );
840 let call_builder = MyContract::deploy_builder(&provider, true);
841 assert_eq!(
842 call_builder.calldata()[..],
843 [
844 bytecode,
845 &hex!("0000000000000000000000000000000000000000000000000000000000000001")[..]
846 ]
847 .concat(),
848 );
849 }
850
851 #[tokio::test(flavor = "multi_thread")]
852 async fn deploy_and_call() {
853 let provider = ProviderBuilder::new().connect_anvil_with_wallet();
854
855 let expected_address = provider.default_signer_address().create(0);
856 let my_contract = MyContract::deploy(provider, true).await.unwrap();
857 assert_eq!(*my_contract.address(), expected_address);
858
859 let my_state_builder = my_contract.myState();
860 assert_eq!(my_state_builder.calldata()[..], MyContract::myStateCall {}.abi_encode(),);
861 let my_state = my_state_builder.call().await.unwrap();
862 assert!(my_state);
863
864 let do_stuff_builder = my_contract.doStuff(U256::from(0x69), true);
865 assert_eq!(
866 do_stuff_builder.calldata()[..],
867 MyContract::doStuffCall { a: U256::from(0x69), b: true }.abi_encode(),
868 );
869 let result: MyContract::doStuffReturn = do_stuff_builder.call().await.unwrap();
870 assert_eq!(result.c, address!("0000000000000000000000000000000000000069"));
871 assert_eq!(
872 result.d,
873 b256!("0000000000000000000000000000000000000000000000000000000000000001"),
874 );
875 }
876
877 #[tokio::test(flavor = "multi_thread")]
878 async fn deploy_and_call_with_priority() {
879 let provider = ProviderBuilder::new().connect_anvil_with_wallet();
880 let counter_contract = Counter::deploy(provider.clone()).await.unwrap();
881 let max_fee_per_gas: U256 = parse_units("50", "gwei").unwrap().into();
882 let max_priority_fee_per_gas: U256 = parse_units("0.1", "gwei").unwrap().into();
883 let receipt = counter_contract
884 .increment()
885 .max_fee_per_gas(max_fee_per_gas.to())
886 .max_priority_fee_per_gas(max_priority_fee_per_gas.to())
887 .send()
888 .await
889 .expect("Could not send transaction")
890 .get_receipt()
891 .await
892 .expect("Could not get the receipt");
893 let transaction_hash = receipt.transaction_hash;
894 let transaction = provider
895 .get_transaction_by_hash(transaction_hash)
896 .await
897 .expect("failed to fetch tx")
898 .expect("tx not included");
899 assert_eq!(
900 transaction.max_fee_per_gas(),
901 max_fee_per_gas.to::<u128>(),
902 "max_fee_per_gas of the transaction should be set to the right value"
903 );
904 assert_eq!(
905 transaction
906 .max_priority_fee_per_gas()
907 .expect("max_priority_fee_per_gas of the transaction should be set"),
908 max_priority_fee_per_gas.to::<u128>(),
909 "max_priority_fee_per_gas of the transaction should be set to the right value"
910 )
911 }
912
913 #[tokio::test(flavor = "multi_thread")]
914 async fn deploy_and_call_with_priority_sync() {
915 let provider = ProviderBuilder::new().connect_anvil_with_wallet();
916 let counter_address =
917 Counter::deploy_builder(provider.clone()).deploy_sync().await.unwrap();
918 let counter_contract = Counter::new(counter_address, provider.clone());
919 let max_fee_per_gas: U256 = parse_units("50", "gwei").unwrap().into();
920 let max_priority_fee_per_gas: U256 = parse_units("0.1", "gwei").unwrap().into();
921 let receipt = counter_contract
922 .increment()
923 .max_fee_per_gas(max_fee_per_gas.to())
924 .max_priority_fee_per_gas(max_priority_fee_per_gas.to())
925 .send_sync()
926 .await
927 .expect("Could not send transaction");
928 let transaction_hash = receipt.transaction_hash;
929 let transaction = provider
930 .get_transaction_by_hash(transaction_hash)
931 .await
932 .expect("failed to fetch tx")
933 .expect("tx not included");
934 assert_eq!(
935 transaction.max_fee_per_gas(),
936 max_fee_per_gas.to::<u128>(),
937 "max_fee_per_gas of the transaction should be set to the right value"
938 );
939 assert_eq!(
940 transaction
941 .max_priority_fee_per_gas()
942 .expect("max_priority_fee_per_gas of the transaction should be set"),
943 max_priority_fee_per_gas.to::<u128>(),
944 "max_priority_fee_per_gas of the transaction should be set to the right value"
945 )
946 }
947
948 sol! {
949 #[sol(rpc, bytecode = "6080604052348015600e575f80fd5b506101448061001c5f395ff3fe60806040526004361061001d575f3560e01c8063785d04f514610021575b5f80fd5b61003461002f3660046100d5565b610036565b005b5f816001600160a01b0316836040515f6040518083038185875af1925050503d805f811461007f576040519150601f19603f3d011682016040523d82523d5f602084013e610084565b606091505b50509050806100d05760405162461bcd60e51b81526020600482015260146024820152734661696c656420746f2073656e64206d6f6e657960601b604482015260640160405180910390fd5b505050565b5f80604083850312156100e6575f80fd5b8235915060208301356001600160a01b0381168114610103575f80fd5b80915050925092905056fea2646970667358221220188e65dcedbc4bd68fdebc795292d5a9bf643385f138383969a28f796ff8858664736f6c63430008190033")]
950 contract SendMoney {
951 function send(uint256 amount, address target) external payable {
952 (bool success, ) = target.call{value: amount}("");
953 require(success, "Failed to send money");
954 }
955 }
956 }
957
958 #[tokio::test]
960 async fn fill_eth_call() {
961 let anvil = Anvil::new().spawn();
962 let pk: PrivateKeySigner =
963 "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse().unwrap();
964
965 let wallet = EthereumWallet::new(pk);
966
967 let wallet_provider =
968 ProviderBuilder::new().wallet(wallet).connect_http(anvil.endpoint_url());
969
970 let contract = SendMoney::deploy(wallet_provider.clone()).await.unwrap();
971
972 let tx = contract
973 .send(U256::from(1000000), Address::with_last_byte(1))
974 .into_transaction_request()
975 .value(U256::from(1000000));
976
977 assert!(tx.from.is_none());
978
979 let std_provider = ProviderBuilder::new().connect_http(anvil.endpoint_url());
980 let should_fail = std_provider.estimate_gas(tx.clone()).await.is_err();
981
982 assert!(should_fail);
983
984 let gas = wallet_provider.estimate_gas(tx).await.unwrap();
985
986 assert_eq!(gas, 56555);
987 }
988
989 #[test]
990 fn change_sidecar_7594() {
991 use alloy_consensus::Blob;
992
993 let sidecar =
994 BlobTransactionSidecarEip7594::new(vec![Blob::repeat_byte(0xAB)], vec![], vec![]);
995 let call_builder = build_call_builder().sidecar_7594(sidecar.clone());
996
997 let set_sidecar = call_builder
998 .request
999 .sidecar
1000 .expect("sidecar should be set")
1001 .into_eip7594()
1002 .expect("sidecar should be EIP-7594 variant");
1003
1004 assert_eq!(set_sidecar, sidecar, "EIP-7594 sidecar should match the one we set");
1005 }
1006
1007 #[tokio::test]
1008 async fn decode_eth_call_ret_bytes() {
1009 sol! {
1010 #[derive(Debug, PartialEq)]
1011 #[sol(rpc, bytecode = "0x6080604052348015600e575f5ffd5b506101578061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c80630d1d2c641461002d575b5f5ffd5b61003561004b565b6040516100429190610108565b60405180910390f35b61005361007b565b6040518060400160405280602a67ffffffffffffffff16815260200160011515815250905090565b60405180604001604052805f67ffffffffffffffff1681526020015f151581525090565b5f67ffffffffffffffff82169050919050565b6100bb8161009f565b82525050565b5f8115159050919050565b6100d5816100c1565b82525050565b604082015f8201516100ef5f8501826100b2565b50602082015161010260208501826100cc565b50505050565b5f60408201905061011b5f8301846100db565b9291505056fea264697066735822122039acc87c027f3bddf6806ff9914411d4245bdc708bca36a07138a37b1b98573464736f6c634300081c0033")]
1012 contract RetStruct {
1013 struct MyStruct {
1014 uint64 a;
1015 bool b;
1016 }
1017
1018 function retStruct() external pure returns (MyStruct memory) {
1019 return MyStruct(42, true);
1020 }
1021 }
1022 }
1023
1024 let provider = ProviderBuilder::new().connect_anvil_with_wallet();
1025
1026 let contract = RetStruct::deploy(provider.clone()).await.unwrap();
1027
1028 let tx = contract.retStruct().into_transaction_request();
1029
1030 let result =
1031 provider.call(tx).decode_resp::<RetStruct::retStructCall>().await.unwrap().unwrap();
1032
1033 assert_eq!(result, RetStruct::MyStruct { a: 42, b: true });
1034 }
1035}