@@ -189,14 +189,14 @@ impl PartialOrd for BlockInfo {
189189/// Individual address balance change
190190#[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
191191pub struct AddressDelta {
192- /// Address
193192 pub address : Address ,
193+ pub tx_identifier : TxIdentifier ,
194194
195- /// UTxO causing address delta
196- pub utxo : UTxOIdentifier ,
195+ pub spent_utxos : Vec < UTxOIdentifier > ,
196+ pub created_utxos : Vec < UTxOIdentifier > ,
197197
198- /// Balance change
199- pub value : ValueDelta ,
198+ pub sent : ValueDelta ,
199+ pub received : Value ,
200200}
201201
202202/// Stake balance change
@@ -341,7 +341,7 @@ pub enum ReferenceScript {
341341}
342342
343343/// Value (lovelace + multiasset)
344- #[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
344+ #[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize , Default ) ]
345345pub struct Value {
346346 pub lovelace : u64 ,
347347 pub assets : NativeAssets ,
@@ -462,6 +462,70 @@ impl From<ValueDeltaMap> for ValueDelta {
462462 }
463463}
464464
465+ impl ValueMap {
466+ pub fn add_value ( & mut self , other : & Value ) {
467+ // Handle lovelace
468+ self . lovelace = self . lovelace . saturating_add ( other. lovelace ) ;
469+
470+ // Handle multi-assets
471+ for ( policy, assets) in & other. assets {
472+ let policy_entry = self . assets . entry ( * policy) . or_default ( ) ;
473+ for asset in assets {
474+ * policy_entry. entry ( asset. name ) . or_default ( ) = policy_entry
475+ . get ( & asset. name )
476+ . copied ( )
477+ . unwrap_or ( 0 )
478+ . saturating_add ( asset. amount ) ;
479+ }
480+ }
481+ }
482+ }
483+
484+ impl From < ValueMap > for Value {
485+ fn from ( map : ValueMap ) -> Self {
486+ Self {
487+ lovelace : map. lovelace ,
488+ assets : map
489+ . assets
490+ . into_iter ( )
491+ . map ( |( policy, assets) | {
492+ (
493+ policy,
494+ assets
495+ . into_iter ( )
496+ . map ( |( name, amount) | NativeAsset { name, amount } )
497+ . collect ( ) ,
498+ )
499+ } )
500+ . collect ( ) ,
501+ }
502+ }
503+ }
504+
505+ impl From < ValueMap > for ValueDelta {
506+ fn from ( map : ValueMap ) -> Self {
507+ Self {
508+ lovelace : map. lovelace as i64 ,
509+ assets : map
510+ . assets
511+ . into_iter ( )
512+ . map ( |( policy, assets) | {
513+ (
514+ policy,
515+ assets
516+ . into_iter ( )
517+ . map ( |( name, amount) | NativeAssetDelta {
518+ name,
519+ amount : amount as i64 ,
520+ } )
521+ . collect ( ) ,
522+ )
523+ } )
524+ . collect ( ) ,
525+ }
526+ }
527+ }
528+
465529#[ derive( Debug , Default , Clone , serde:: Serialize , serde:: Deserialize ) ]
466530pub struct ValueDelta {
467531 pub lovelace : i64 ,
@@ -555,24 +619,20 @@ pub struct TxOutput {
555619 pub reference_script : Option < ReferenceScript > ,
556620}
557621
558- /// Transaction input (UTXO reference)
559- #[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
560- pub struct TxInput {
561- /// Identifer of the referenced UTxO
562- pub utxo_identifier : UTxOIdentifier ,
563- }
564-
565- /// Option of either TxOutput or TxInput
566622#[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
567- pub enum UTXODelta {
568- None ( ( ) ) ,
569- Output ( TxOutput ) ,
570- Input ( TxInput ) ,
623+ pub struct TxUTxODeltas {
624+ pub tx_identifier : TxIdentifier ,
625+ pub inputs : Vec < UTxOIdentifier > ,
626+ pub outputs : Vec < TxOutput > ,
571627}
572628
573- impl Default for UTXODelta {
574- fn default ( ) -> Self {
575- Self :: None ( ( ) )
629+ impl TxUTxODeltas {
630+ pub fn new ( tx_identifier : TxIdentifier ) -> Self {
631+ Self {
632+ tx_identifier,
633+ inputs : Vec :: new ( ) ,
634+ outputs : Vec :: new ( ) ,
635+ }
576636 }
577637}
578638
@@ -2137,6 +2197,12 @@ pub struct AddressTotals {
21372197 pub tx_count : u64 ,
21382198}
21392199
2200+ #[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
2201+ pub struct TxTotals {
2202+ pub sent : ValueDelta ,
2203+ pub received : Value ,
2204+ }
2205+
21402206impl AddAssign for AddressTotals {
21412207 fn add_assign ( & mut self , other : Self ) {
21422208 self . sent += other. sent ;
@@ -2146,25 +2212,24 @@ impl AddAssign for AddressTotals {
21462212}
21472213
21482214impl AddressTotals {
2149- pub fn apply_delta ( & mut self , delta : & ValueDelta ) {
2150- if delta. lovelace > 0 {
2151- self . received . lovelace += delta. lovelace as u64 ;
2152- } else if delta. lovelace < 0 {
2153- self . sent . lovelace += ( -delta. lovelace ) as u64 ;
2215+ pub fn apply_delta ( & mut self , delta : & TxTotals ) {
2216+ self . received . lovelace += delta. received . lovelace ;
2217+ self . sent . lovelace += delta. sent . lovelace as u64 ;
2218+
2219+ for ( policy, assets) in & delta. received . assets {
2220+ for asset in assets {
2221+ Self :: apply_asset ( & mut self . received . assets , * policy, asset. name , asset. amount ) ;
2222+ }
21542223 }
21552224
2156- for ( policy, assets) in & delta. assets {
2157- for a in assets {
2158- if a. amount > 0 {
2159- Self :: apply_asset ( & mut self . received . assets , * policy, a. name , a. amount as u64 ) ;
2160- } else if a. amount < 0 {
2161- Self :: apply_asset (
2162- & mut self . sent . assets ,
2163- * policy,
2164- a. name ,
2165- a. amount . unsigned_abs ( ) ,
2166- ) ;
2167- }
2225+ for ( policy, assets) in & delta. sent . assets {
2226+ for asset in assets {
2227+ Self :: apply_asset (
2228+ & mut self . sent . assets ,
2229+ * policy,
2230+ asset. name ,
2231+ asset. amount . unsigned_abs ( ) ,
2232+ ) ;
21682233 }
21692234 }
21702235
0 commit comments