Skip to content

Commit 9605a0e

Browse files
authored
Merge pull request #325 from RGB-WG/node
New APIs required by RGB Node
2 parents 38d0693 + 3154b79 commit 9605a0e

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

src/contracts.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,77 @@ use rgb::RgbSeal;
3939
use strict_encoding::{
4040
ReadRaw, StrictDecode, StrictDumb, StrictEncode, StrictReader, StrictWriter, WriteRaw,
4141
};
42+
use strict_types::StrictVal;
4243

4344
use crate::{
4445
parse_consignment, Articles, Consensus, Consignment, ConsumeError, Contract, ContractRef,
45-
ContractState, CreateParams, Identity, Issuer, Operation, Pile, SigBlob, Stockpile,
46-
WitnessStatus,
46+
ContractState, CreateParams, Identity, ImmutableState, Issuer, Operation, OwnedState, Pile,
47+
SigBlob, StateName, Stockpile, WitnessStatus,
4748
};
4849

4950
pub const CONSIGN_VERSION: u16 = 0;
5051
#[cfg(feature = "binfile")]
5152
pub use _fs::CONSIGN_MAGIC_NUMBER;
5253

54+
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
55+
#[display("{contract_id}/{state_name}")]
56+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
57+
pub struct ContractStateName {
58+
pub contract_id: ContractId,
59+
pub state_name: StateName,
60+
}
61+
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)]
69+
#[cfg_attr(
70+
feature = "serde",
71+
derive(Serialize, Deserialize),
72+
serde(
73+
rename_all = "camelCase",
74+
bound = "Seal: serde::Serialize + for<'d> serde::Deserialize<'d>"
75+
)
76+
)]
77+
pub struct WalletState<Seal> {
78+
pub immutable: BTreeMap<ContractStateName, Vec<ImmutableState>>,
79+
pub owned: BTreeMap<ContractStateName, Vec<OwnedState<Seal>>>,
80+
pub aggregated: BTreeMap<ContractStateName, StrictVal>,
81+
}
82+
83+
impl<Seal> Default for WalletState<Seal> {
84+
fn default() -> Self { Self { immutable: bmap! {}, owned: bmap! {}, aggregated: bmap! {} } }
85+
}
86+
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+
53113
/// Collection of RGB smart contracts and contract issuers, which can be cached in memory.
54114
///
55115
/// # Generics

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ pub use contract::{
7777
};
7878
#[cfg(feature = "binfile")]
7979
pub use contracts::CONSIGN_MAGIC_NUMBER;
80-
pub use contracts::{Contracts, IssuerError, SyncError, CONSIGN_VERSION};
80+
pub use contracts::{
81+
ContractStateName, Contracts, IssuerError, SyncError, WalletState, CONSIGN_VERSION,
82+
};
8183
pub use hypersonic::*;
8284
pub use pile::{OpRels, Pile, Witness, WitnessStatus};
8385
pub use rgb::*;

src/popls/bp.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use alloc::collections::btree_map::Entry;
2929
use alloc::collections::{btree_set, BTreeMap, BTreeSet};
3030
use alloc::vec;
31+
use core::mem;
3132
use std::collections::HashMap;
3233

3334
use amplify::confinement::{
@@ -55,7 +56,8 @@ use strict_types::StrictVal;
5556
use crate::contracts::SyncError;
5657
use crate::{
5758
Assignment, CodexId, Consensus, ConsumeError, Contract, ContractState, Contracts, CreateParams,
58-
EitherSeal, Identity, Issuer, IssuerError, OwnedState, Pile, SigBlob, Stockpile, WitnessStatus,
59+
EitherSeal, Identity, Issuer, IssuerError, OwnedState, Pile, SigBlob, Stockpile, WalletState,
60+
WitnessStatus,
5961
};
6062

6163
/// Trait abstracting a specific implementation of a bitcoin wallet.
@@ -438,6 +440,8 @@ where
438440

439441
pub fn into_components(self) -> (W, Contracts<Sp, S, C>) { (self.wallet, self.contracts) }
440442

443+
pub fn switch_wallet(&mut self, new: W) -> W { mem::replace(&mut self.wallet, new) }
444+
441445
pub fn issue(
442446
&mut self,
443447
params: CreateParams<Outpoint>,
@@ -463,7 +467,15 @@ where
463467
WitnessOut::new(address.payload, nonce)
464468
}
465469

466-
pub fn state_own(&self, contract_id: ContractId) -> ContractState<Outpoint> {
470+
pub fn wallet_state(&self) -> WalletState<TxoSeal> {
471+
let iter = self
472+
.contracts
473+
.contract_ids()
474+
.map(|id| (id, self.contracts.contract_state(id)));
475+
WalletState::from_contracts_state(iter)
476+
}
477+
478+
pub fn wallet_contract_state(&self, contract_id: ContractId) -> ContractState<Outpoint> {
467479
self.contracts
468480
.contract_state(contract_id)
469481
.clone()
@@ -478,7 +490,10 @@ where
478490
)
479491
}
480492

481-
pub fn state_all(&self, contract_id: ContractId) -> ContractState<<Sp::Pile as Pile>::Seal> {
493+
pub fn contract_state_full(
494+
&self,
495+
contract_id: ContractId,
496+
) -> ContractState<<Sp::Pile as Pile>::Seal> {
482497
self.contracts.contract_state(contract_id)
483498
}
484499

@@ -512,7 +527,7 @@ where
512527
let value = invoice.data.as_ref().ok_or(FulfillError::ValueMissed)?;
513528

514529
// Do coinselection
515-
let state = self.state_own(contract_id);
530+
let state = self.wallet_contract_state(contract_id);
516531
let state = state
517532
.owned
518533
.get(&state_name)

0 commit comments

Comments
 (0)