Skip to content

Commit

Permalink
State from state file load
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayanski committed Oct 22, 2024
1 parent 9c7ef7d commit fd05d92
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cw-orch-daemon/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ pub trait DeployedChains<Chain: CwEnv>: cw_orch_core::contract::Deploy<Chain> {
/// Set the default contract state for a contract, so that users can retrieve it in their application when importing the library
/// If a state is provided, it is used for all contracts, otherwise, the state is loaded from the crate's state file.
fn set_contracts_state(&mut self, custom_state: Option<Value>) {
let mut is_loading_from_file = false;

Check warning on line 360 in cw-orch-daemon/src/state.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/state.rs#L360

Added line #L360 was not covered by tests
let Some(maybe_old_state) = custom_state.or_else(|| {
is_loading_from_file = true;

Check warning on line 362 in cw-orch-daemon/src/state.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/state.rs#L362

Added line #L362 was not covered by tests
Self::deployed_state_file_path()
.and_then(|state_file| crate::json_lock::read(&state_file).ok())
}) else {
Expand All @@ -368,6 +370,11 @@ pub trait DeployedChains<Chain: CwEnv>: cw_orch_core::contract::Deploy<Chain> {
let all_contracts = self.get_contracts_mut();

for contract in all_contracts {
// If we're loading the state from file and the environment doesn't allow that, we don't load addresses and code-ids
if is_loading_from_file && !contract.environment().can_load_state_from_state_file() {
return;
}

Check warning on line 377 in cw-orch-daemon/src/state.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/state.rs#L374-L377

Added lines #L374 - L377 were not covered by tests
// We set the code_id and/or address of the contract in question if they are not present already
let env_info = contract.environment().env_info();
// We load the file
Expand Down
4 changes: 4 additions & 0 deletions cw-orch-daemon/src/sync/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ impl<Sender> ChainState for DaemonBase<Sender> {
fn state(&self) -> Self::Out {
self.daemon.state.clone()
}

fn can_load_state_from_state_file(&self) -> bool {
true
}

Check warning on line 149 in cw-orch-daemon/src/sync/core.rs

View check run for this annotation

Codecov / codecov/patch

cw-orch-daemon/src/sync/core.rs#L147-L149

Added lines #L147 - L149 were not covered by tests
}

// Execute on the real chain, returns tx response
Expand Down
4 changes: 4 additions & 0 deletions packages/clone-testing/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ impl<S: StateInterface> ChainState for CloneTesting<S> {
fn state(&self) -> Self::Out {
self.state.clone()
}

fn can_load_state_from_state_file(&self) -> bool {
true
}

Check warning on line 277 in packages/clone-testing/src/core.rs

View check run for this annotation

Codecov / codecov/patch

packages/clone-testing/src/core.rs#L275-L277

Added lines #L275 - L277 were not covered by tests
}

// Execute on the test chain, returns test response type
Expand Down
6 changes: 6 additions & 0 deletions packages/cw-orch-core/src/environment/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub trait ChainState {
type Out: StateInterface;
/// Get the underlying state.
fn state(&self) -> Self::Out;
/// Returns wether this environment can load state from the state file.
///
/// This can be used within the Deploy trait to load the contracts from file if necessary
fn can_load_state_from_state_file(&self) -> bool {
false
}

Check warning on line 19 in packages/cw-orch-core/src/environment/state.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/state.rs#L17-L19

Added lines #L17 - L19 were not covered by tests
}

/// This Interface allows for managing the local state of a deployment on any CosmWasm-supported environment.
Expand Down

0 comments on commit fd05d92

Please sign in to comment.