scrypto_test/sdk/
package.rs

1use std::path::Path;
2
3use crate::prelude::*;
4
5pub struct PackageFactory;
6
7impl PackageFactory {
8    pub fn publish<D>(
9        code: Vec<u8>,
10        package_definition: PackageDefinition,
11        metadata: MetadataInit,
12        env: &mut TestEnvironment<D>,
13    ) -> Result<(PackageAddress, Bucket), RuntimeError>
14    where
15        D: SubstateDatabase + CommittableSubstateDatabase + 'static,
16    {
17        env.with_auth_module_disabled(|env| {
18            env.call_function_typed::<PackagePublishWasmInput, PackagePublishWasmOutput>(
19                PACKAGE_PACKAGE,
20                PACKAGE_BLUEPRINT,
21                PACKAGE_PUBLISH_WASM_IDENT,
22                &PackagePublishWasmInput {
23                    code,
24                    definition: package_definition,
25                    metadata,
26                },
27            )
28        })
29    }
30
31    pub fn publish_advanced<D>(
32        owner_role: OwnerRole,
33        definition: PackageDefinition,
34        code: Vec<u8>,
35        metadata: MetadataInit,
36        package_address: Option<GlobalAddressReservation>,
37        env: &mut TestEnvironment<D>,
38    ) -> Result<PackageAddress, RuntimeError>
39    where
40        D: SubstateDatabase + CommittableSubstateDatabase + 'static,
41    {
42        env.with_auth_module_disabled(|env| {
43            env.call_function_typed::<PackagePublishWasmAdvancedInput, PackagePublishWasmAdvancedOutput>(
44                PACKAGE_PACKAGE,
45                PACKAGE_BLUEPRINT,
46                PACKAGE_PUBLISH_WASM_ADVANCED_IDENT,
47                &PackagePublishWasmAdvancedInput {
48                    owner_role,
49                    definition,
50                    code,
51                    metadata,
52                    package_address
53                },
54            )
55        })
56    }
57
58    pub fn compile_and_publish<P, D>(
59        path: P,
60        env: &mut TestEnvironment<D>,
61        compile_profile: CompileProfile,
62    ) -> Result<PackageAddress, RuntimeError>
63    where
64        P: AsRef<Path>,
65        D: SubstateDatabase + CommittableSubstateDatabase + 'static,
66    {
67        let (wasm, package_definition) = Self::compile(path, compile_profile);
68        Self::publish_advanced(
69            OwnerRole::None,
70            package_definition,
71            wasm,
72            Default::default(),
73            Default::default(),
74            env,
75        )
76    }
77
78    pub fn compile<P>(path: P, compile_profile: CompileProfile) -> (Vec<u8>, PackageDefinition)
79    where
80        P: AsRef<Path>,
81    {
82        Compile::compile_with_env_vars(path, BTreeMap::new(), compile_profile, false)
83    }
84}