Skip to content

Commit a17d1df

Browse files
committed
feat: add init_versioned_contract_with_db fn
1 parent 1dee9cb commit a17d1df

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

clarity/src/vm/contexts.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,51 @@ impl<'a> OwnedEnvironment<'a> {
735735
)
736736
}
737737

738+
/// Initializes a versioned Clarity smart contract with a custom analysis database within a transaction context.
739+
///
740+
/// This function should only be used for testing.
741+
///
742+
/// This function creates a complete transaction environment to initialize a Clarity smart contract
743+
/// using a provided memory-backed database for analysis data. It executes the contract initialization
744+
/// within a proper execution context with a specific Clarity version.
745+
///
746+
/// # Arguments
747+
///
748+
/// * `contract_identifier` - Unique identifier for the contract (principal + contract name)
749+
/// * `version` - The Clarity version to use for this contract
750+
/// * `contract_content` - The raw Clarity source code as a string
751+
/// * `sponsor` - Optional sponsor principal for transaction fees (if `None`, sender pays)
752+
/// * `ast_rules` - Parsing rules to apply during AST construction (e.g., `ASTRules::PrecheckSize`)
753+
/// * `analysis_db` - Mutable reference to a database for analysis data
754+
///
755+
#[cfg(any(test, feature = "testing"))]
756+
pub fn initialize_versioned_contract_with_db(
757+
&mut self,
758+
contract_identifier: QualifiedContractIdentifier,
759+
version: ClarityVersion,
760+
contract_content: &str,
761+
sponsor: Option<PrincipalData>,
762+
ast_rules: ASTRules,
763+
analysis_db: &mut AnalysisDatabase,
764+
) -> Result<((), AssetMap, Vec<StacksTransactionEvent>)> {
765+
self.execute_in_env(
766+
contract_identifier.issuer.clone().into(),
767+
sponsor,
768+
Some(ContractContext::new(
769+
QualifiedContractIdentifier::transient(),
770+
version,
771+
)),
772+
|exec_env| {
773+
exec_env.initialize_contract_with_db(
774+
contract_identifier,
775+
contract_content,
776+
ast_rules,
777+
analysis_db,
778+
)
779+
},
780+
)
781+
}
782+
738783
pub fn initialize_contract_from_ast(
739784
&mut self,
740785
contract_identifier: QualifiedContractIdentifier,

0 commit comments

Comments
 (0)