Skip to content

Commit 8c0ac2f

Browse files
committed
bp: add RgbWallet::wallet_state method
1 parent c1b4c87 commit 8c0ac2f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/contracts.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ pub struct ContractStateName {
5959
pub state_name: StateName,
6060
}
6161

62-
#[derive(Clone, Eq, PartialEq, Debug, Default)]
62+
impl ContractStateName {
63+
pub fn new(contract_id: ContractId, state_name: StateName) -> Self {
64+
ContractStateName { contract_id, state_name }
65+
}
66+
}
67+
68+
#[derive(Clone, Eq, PartialEq, Debug)]
6369
#[cfg_attr(
6470
feature = "serde",
6571
derive(Serialize, Deserialize),
@@ -74,6 +80,10 @@ pub struct WalletState<Seal> {
7480
pub aggregated: BTreeMap<ContractStateName, StrictVal>,
7581
}
7682

83+
impl<Seal> Default for WalletState<Seal> {
84+
fn default() -> Self { Self { immutable: bmap! {}, owned: bmap! {}, aggregated: bmap! {} } }
85+
}
86+
7787
/// Collection of RGB smart contracts and contract issuers, which can be cached in memory.
7888
///
7989
/// # Generics

src/popls/bp.rs

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

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

6263
/// Trait abstracting a specific implementation of a bitcoin wallet.
@@ -466,6 +467,29 @@ where
466467
WitnessOut::new(address.payload, nonce)
467468
}
468469

470+
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
491+
}
492+
469493
pub fn wallet_contract_state(&self, contract_id: ContractId) -> ContractState<Outpoint> {
470494
self.contracts
471495
.contract_state(contract_id)

0 commit comments

Comments
 (0)