@@ -62,52 +62,88 @@ impl OutputCollection {
62
62
{ "metadata.spent_metadata.spent.milestone_index" : milestone_index } ,
63
63
]
64
64
} } ,
65
- doc! { "$set" : { "kind" : {
66
- "$cond" : [
67
- { "$eq" : [ "$metadata.spent_metadata.spent.milestone_index" , milestone_index ] } ,
68
- "consumed_output" ,
69
- "created_output"
70
- ]
71
- } } } ,
72
- // Re-assemble the inputs and outputs per transaction.
65
+ doc! { "$set" : {
66
+ "new_booked_output" : {
67
+ "$cond" : [ { "$eq" : [ "$metadata.booked.milestone_index" , milestone_index ] } , true , false ]
68
+ } ,
69
+ "new_spent_output" : {
70
+ "$cond" : [ { "$eq" : [ "$metadata.spent_metadata.spent.milestone_index" , milestone_index ] } , true , false ]
71
+ }
72
+ } } ,
73
+
74
+ doc! { "$facet" : {
75
+ "booked_outputs" : [
76
+ { "$redact" : {
77
+ "$cond" : {
78
+ "if" : { "$eq" : [ "$new_booked_output" , true ] } ,
79
+ "then" : "$$KEEP" ,
80
+ "else" : "$$PRUNE"
81
+ } } } ,
82
+ { "$project" : {
83
+ "_id" : 1 ,
84
+ "tx" : "$_id.transaction_id" ,
85
+ "address" : "$details.address.ed25519" ,
86
+ "amount" : { "$toDecimal" : "$output.amount" } ,
87
+ } } ,
88
+ ] ,
89
+ "consumed_outputs" : [
90
+ { "$redact" : {
91
+ "$cond" : {
92
+ "if" : { "$eq" : [ "$new_spent_output" , true ] } ,
93
+ "then" : "$$KEEP" ,
94
+ "else" : "$$PRUNE"
95
+ } } } ,
96
+ { "$project" : {
97
+ "_id" : 1 ,
98
+ "tx" : "$metadata.spent_metadata.transaction_id" ,
99
+ "address" : "$details.address.ed25519" ,
100
+ "amount" : { "$toDecimal" : "$output.amount" } ,
101
+ } } ,
102
+ { "$group" : {
103
+ "_id" : {
104
+ "tx" : "$tx" ,
105
+ "address" : "$address"
106
+ } ,
107
+ "amount" : { "$sum" : "$amount" } ,
108
+ } }
109
+ ] ,
110
+ } } ,
111
+
112
+ doc! { "$unwind" : {
113
+ "path" : "$booked_outputs" ,
114
+ } } ,
115
+
73
116
doc! { "$project" : {
74
- "_id" : { "$cond" : [
75
- { "$eq" : [ "$kind" , "created_output" ] } ,
76
- "$_id.transaction_id" ,
77
- "$metadata.spent_metadata.transaction_id"
78
- ] } ,
79
- "address" : "$details.address" ,
80
- "amount" : { "$toDecimal" : "$output.amount" } ,
81
- "kind" : 1 ,
117
+ "booked_outputs" : 1 ,
118
+ "sent_back_addr" : { "$first" : {
119
+ "$filter" : {
120
+ "input" : "$spent_outputs" ,
121
+ "as" : "item" ,
122
+ "cond" : { "$and" : [
123
+ { "$eq" : [ "$$item._id.tx" , "$booked_outputs.tx" ] } ,
124
+ { "$eq" : [ "$$item._id.address" , "$booked_outputs.address" ] } ,
125
+ ] }
126
+ }
127
+ } }
82
128
} } ,
83
- // Note: we sum input amounts and subtract output amounts per transaction and per address.
84
- // This way we make sure that amounts that were sent back to an input address within the
85
- // same transaction get subtracted and are not falsely counted as a token transfer.
86
- doc! {
87
- "$group" : {
88
- "_id" : {
89
- "tx_id" : "$_id" ,
90
- "address" : "$address"
91
- } ,
92
- "booked_value" : { "$sum" : {
93
- "$cond" : [ { "$eq" : [ "$kind" , "consumed_output" ] } , "$amount" , 0 ] } } ,
94
- "transferred_value" : { "$sum" : {
95
- "$cond" : [ { "$eq" : [ "$kind" , "consumed_output" ] } , "$amount" , { "$subtract" : [ 0 , "$amount" ] } ]
96
- } }
97
- }
98
- } ,
99
- doc! {
100
- "$group" : {
101
- "_id" : null,
102
- "booked_value" : { "$sum" : "$booked_value" } ,
103
- "transferred_value" : { "$sum" : {
104
- "$cond" : [ { "$gt" : [ "$transferred_value" , 0 ] } , "$transferred_value" , 0 ]
105
- } }
106
- }
107
- } ,
129
+
130
+ doc! { "$project" : {
131
+ "booked_amount" : "$booked_outputs.amount" ,
132
+ "spent_amount" : { "$ifNull" : [ "$sent_back_addr.amount" , 0 ] } ,
133
+ } } ,
134
+
135
+ doc! { "$group" : {
136
+ "_id" : null,
137
+ "booked_amount" : { "$sum" : "$booked_amount" } ,
138
+ "transferred_amount" : { "$sum" : {
139
+ "$cond" : [ { "$gt" : [ "$booked_amount" , "$spent_amount" ] } , { "$subtract" : [ "$booked_amount" , "$spent_amount" ] } , 0
140
+ ]
141
+ } }
142
+ } } ,
143
+
108
144
doc! { "$project" : {
109
- "booked_value" : { "$toString" : "$booked_value " } ,
110
- "transferred_value" : { "$toString" : "$transferred_value " } ,
145
+ "booked_value" : { "$toString" : "$booked_amount " } ,
146
+ "transferred_value" : { "$toString" : "$transferred_amount " } ,
111
147
} } ,
112
148
] ,
113
149
None ,
0 commit comments