@@ -39,17 +39,77 @@ use rgb::RgbSeal;
3939use strict_encoding:: {
4040 ReadRaw , StrictDecode , StrictDumb , StrictEncode , StrictReader , StrictWriter , WriteRaw ,
4141} ;
42+ use strict_types:: StrictVal ;
4243
4344use 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
4950pub const CONSIGN_VERSION : u16 = 0 ;
5051#[ cfg( feature = "binfile" ) ]
5152pub 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
0 commit comments