Skip to content

Commit

Permalink
refactor: remove provider from ScriptCallHandler (#1391)
Browse files Browse the repository at this point in the history
Removed `provider` from `ScriptCallHandler`. The `provider` can be
accessed from the `account` the same way we do in
`ContractCallHandlers`.

Another benefit is that we will not panic when initializing a new script
instance if the provider is not set up.
  • Loading branch information
hal3e authored May 28, 2024
1 parent 068543c commit b54cf54
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,10 @@ fn expand_fn(fn_abi: &FullABIFunction) -> Result<TokenStream> {
let arg_tokens = generator.tokenized_args();
let body = quote! {
let encoded_args = ::fuels::core::codec::ABIEncoder::new(self.encoder_config).encode(&#arg_tokens);
let provider = ::fuels::accounts::ViewOnlyAccount::try_provider(&self.account).expect("Provider not set up")
.clone();
::fuels::programs::script_calls::ScriptCallHandler::new(
self.binary.clone(),
encoded_args,
self.account.clone(),
provider,
self.log_decoder.clone()
)
};
Expand Down Expand Up @@ -195,13 +192,10 @@ mod tests {
pub fn main(&self, bimbam: ::core::primitive::bool) -> ::fuels::programs::script_calls::ScriptCallHandler<T, ()> {
let encoded_args=::fuels::core::codec::ABIEncoder::new(self.encoder_config)
.encode(&[::fuels::core::traits::Tokenizable::into_token(bimbam)]);
let provider = ::fuels::accounts::ViewOnlyAccount::try_provider(&self.account)
.expect("Provider not set up").clone();
::fuels::programs::script_calls::ScriptCallHandler::new(
self.binary.clone(),
encoded_args,
self.account.clone(),
provider,
self.log_decoder.clone()
)
}
Expand Down
6 changes: 2 additions & 4 deletions packages/fuels-programs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,8 @@ where

/// Create a [`FuelCallResponse`] from call receipts
pub fn get_response(&self, receipts: Vec<Receipt>) -> Result<FuelCallResponse<D>> {
let token = ReceiptParser::new(&receipts, self.decoder_config).parse_call(
&self.contract_call.contract_id,
&self.contract_call.output_param,
)?;
let token = ReceiptParser::new(&receipts, self.decoder_config)
.parse_call(&self.contract_call.contract_id, &D::param_type())?;

Ok(FuelCallResponse::new(
D::from_token(token)?,
Expand Down
22 changes: 10 additions & 12 deletions packages/fuels-programs/src/script_calls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::{collections::HashSet, fmt::Debug, marker::PhantomData};

use fuel_tx::{Bytes32, ContractId, Output, Receipt};
use fuels_accounts::{
provider::{Provider, TransactionCost},
Account,
};
use fuels_accounts::{provider::TransactionCost, Account};
use fuels_core::{
codec::{DecoderConfig, LogDecoder},
error,
Expand Down Expand Up @@ -82,7 +79,6 @@ pub struct ScriptCallHandler<T: Account, D> {
cached_tx_id: Option<Bytes32>,
decoder_config: DecoderConfig,
pub account: T,
pub provider: Provider,
pub datatype: PhantomData<D>,
pub log_decoder: LogDecoder,
}
Expand All @@ -95,7 +91,6 @@ where
script_binary: Vec<u8>,
encoded_args: Result<Vec<u8>>,
account: T,
provider: Provider,
log_decoder: LogDecoder,
) -> Self {
let script_call = ScriptCall {
Expand All @@ -111,7 +106,6 @@ where
tx_policies: TxPolicies::default(),
cached_tx_id: None,
account,
provider,
datatype: PhantomData,
log_decoder,
decoder_config: DecoderConfig::default(),
Expand Down Expand Up @@ -224,13 +218,14 @@ where
/// The other field of [`FuelCallResponse`], `receipts`, contains the receipts of the transaction.
async fn call_or_simulate(&mut self, simulate: bool) -> Result<FuelCallResponse<D>> {
let tx = self.build_tx().await?;
let provider = self.account.try_provider()?;

self.cached_tx_id = Some(tx.id(self.provider.chain_id()));
self.cached_tx_id = Some(tx.id(provider.chain_id()));

let tx_status = if simulate {
self.provider.dry_run(tx).await?
provider.dry_run(tx).await?
} else {
self.provider.send_transaction_and_await_commit(tx).await?
provider.send_transaction_and_await_commit(tx).await?
};
let receipts = tx_status.take_receipts_checked(Some(&self.log_decoder))?;

Expand All @@ -244,7 +239,9 @@ where

pub async fn submit(mut self) -> Result<SubmitResponse<T, D>> {
let tx = self.build_tx().await?;
let tx_id = self.provider.send_transaction(tx).await?;
let provider = self.account.try_provider()?;

let tx_id = provider.send_transaction(tx).await?;
self.cached_tx_id = Some(tx_id);

Ok(SubmitResponse::new(tx_id, self))
Expand All @@ -268,7 +265,8 @@ where
let tx = self.build_tx().await?;

let transaction_cost = self
.provider
.account
.try_provider()?
.estimate_transaction_cost(tx, tolerance, block_horizon)
.await?;

Expand Down
1 change: 1 addition & 0 deletions packages/fuels-programs/src/submit_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct SubmitResponse<T: Account, D> {
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum CallHandler<T: Account, D> {
Contract(ContractCallHandler<T, D>),
Script(ScriptCallHandler<T, D>),
Expand Down

0 comments on commit b54cf54

Please sign in to comment.