Skip to content

Commit 3154b79

Browse files
committed
contracts: move WalletState constructor to a dedicated method
1 parent 8c0ac2f commit 3154b79

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/contracts.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ impl<Seal> Default for WalletState<Seal> {
8484
fn default() -> Self { Self { immutable: bmap! {}, owned: bmap! {}, aggregated: bmap! {} } }
8585
}
8686

87+
impl<Seal> WalletState<Seal> {
88+
pub fn from_contracts_state(
89+
contracts: impl IntoIterator<Item = (ContractId, ContractState<Seal>)>,
90+
) -> Self {
91+
let mut wallet_state = WalletState::default();
92+
for (contract_id, contract_state) in contracts {
93+
for (state_name, state) in contract_state.immutable {
94+
wallet_state
95+
.immutable
96+
.insert(ContractStateName::new(contract_id, state_name), state);
97+
}
98+
for (state_name, state) in contract_state.owned {
99+
wallet_state
100+
.owned
101+
.insert(ContractStateName::new(contract_id, state_name), state);
102+
}
103+
for (state_name, state) in contract_state.aggregated {
104+
wallet_state
105+
.aggregated
106+
.insert(ContractStateName::new(contract_id, state_name), state);
107+
}
108+
}
109+
wallet_state
110+
}
111+
}
112+
87113
/// Collection of RGB smart contracts and contract issuers, which can be cached in memory.
88114
///
89115
/// # Generics

src/popls/bp.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ use strict_types::StrictVal;
5555

5656
use crate::contracts::SyncError;
5757
use crate::{
58-
Assignment, CodexId, Consensus, ConsumeError, Contract, ContractState, ContractStateName,
59-
Contracts, CreateParams, EitherSeal, Identity, Issuer, IssuerError, OwnedState, Pile, SigBlob,
60-
Stockpile, WalletState, WitnessStatus,
58+
Assignment, CodexId, Consensus, ConsumeError, Contract, ContractState, Contracts, CreateParams,
59+
EitherSeal, Identity, Issuer, IssuerError, OwnedState, Pile, SigBlob, Stockpile, WalletState,
60+
WitnessStatus,
6161
};
6262

6363
/// Trait abstracting a specific implementation of a bitcoin wallet.
@@ -468,26 +468,11 @@ where
468468
}
469469

470470
pub fn wallet_state(&self) -> WalletState<TxoSeal> {
471-
let mut wallet_state = WalletState::default();
472-
for contract_id in self.contracts.contract_ids() {
473-
let contract_state = self.contracts.contract_state(contract_id);
474-
for (state_name, state) in contract_state.immutable {
475-
wallet_state
476-
.immutable
477-
.insert(ContractStateName::new(contract_id, state_name), state);
478-
}
479-
for (state_name, state) in contract_state.owned {
480-
wallet_state
481-
.owned
482-
.insert(ContractStateName::new(contract_id, state_name), state);
483-
}
484-
for (state_name, state) in contract_state.aggregated {
485-
wallet_state
486-
.aggregated
487-
.insert(ContractStateName::new(contract_id, state_name), state);
488-
}
489-
}
490-
wallet_state
471+
let iter = self
472+
.contracts
473+
.contract_ids()
474+
.map(|id| (id, self.contracts.contract_state(id)));
475+
WalletState::from_contracts_state(iter)
491476
}
492477

493478
pub fn wallet_contract_state(&self, contract_id: ContractId) -> ContractState<Outpoint> {

0 commit comments

Comments
 (0)