1use crate::errors::Error;
2use forc::test::{forc_build, BuildCommand};
3use forc::util::constants;
4use forc::util::helpers::read_manifest;
5use fuel_gql_client::client::FuelClient;
6use fuel_tx::{Receipt, Transaction};
7use std::path::PathBuf;
8use sway_utils::find_manifest_dir;
9
10pub struct Script {
13 pub tx: Transaction,
14}
15
16#[derive(Debug, Clone)]
17pub struct CompiledScript {
18 pub raw: Vec<u8>,
19 pub target_network_url: String,
20}
21
22impl Script {
23 pub fn new(tx: Transaction) -> Self {
24 Self { tx }
25 }
26
27 pub async fn call(self, fuel_client: &FuelClient) -> Result<Vec<Receipt>, Error> {
28 let tx_id = fuel_client.submit(&self.tx).await.unwrap();
29
30 let receipts = fuel_client.receipts(&tx_id.0.to_string()).await.unwrap();
31
32 Ok(receipts)
33 }
34
35 pub fn compile_sway_script(project_path: &str) -> Result<CompiledScript, Error> {
37 let build_command = BuildCommand {
38 path: Some(project_path.into()),
39 print_finalized_asm: false,
40 print_intermediate_asm: false,
41 binary_outfile: None,
42 offline_mode: false,
43 silent_mode: true,
44 use_ir: false,
45 print_ir: false,
46 };
47
48 let raw =
49 forc_build::build(build_command).map_err(|message| Error::CompilationError(message))?;
50
51 let manifest_dir = find_manifest_dir(&PathBuf::from(project_path)).unwrap();
52 let manifest = read_manifest(&manifest_dir).map_err(|e| {
53 Error::CompilationError(format!("Failed to find manifest for contract: {}", e))
54 })?;
55
56 let node_url = match &manifest.network {
57 Some(network) => &network.url,
58 _ => constants::DEFAULT_NODE_URL,
59 };
60
61 Ok(CompiledScript {
62 raw,
63 target_network_url: node_url.to_string(),
64 })
65 }
66}