/
accounts.go
187 lines (170 loc) · 4.57 KB
/
accounts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package storagereader
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/spacemeshos/explorer-backend/model"
)
// CountAccounts returns the number of accounts matching the query.
func (s *Reader) CountAccounts(ctx context.Context, query *bson.D, opts ...*options.CountOptions) (int64, error) {
return s.db.Collection("accounts").CountDocuments(ctx, query, opts...)
}
// GetAccounts returns the accounts matching the query.
func (s *Reader) GetAccounts(ctx context.Context, query *bson.D, opts ...*options.FindOptions) ([]*model.Account, error) {
skip := int64(0)
if opts[0].Skip != nil {
skip = *opts[0].Skip
}
pipeline := bson.A{
bson.D{
{"$lookup",
bson.D{
{"from", "txs"},
{"let", bson.D{{"addr", "$address"}}},
{"pipeline",
bson.A{
bson.D{
{"$match",
bson.D{
{"$or",
bson.A{
bson.D{
{"$expr",
bson.D{
{"$eq",
bson.A{
"$sender",
"$$addr",
},
},
},
},
},
bson.D{
{"$expr",
bson.D{
{"$eq",
bson.A{
"$receiver",
"$$addr",
},
},
},
},
},
},
},
},
},
},
bson.D{{"$sort", bson.D{{"layer", 1}}}},
bson.D{{"$limit", 1}},
bson.D{
{"$project",
bson.D{
{"_id", 0},
{"layer", 1},
},
},
},
},
},
{"as", "createdLayerRst"},
},
},
},
bson.D{
{"$addFields",
bson.D{
{"createdLayer",
bson.D{
{"$arrayElemAt",
bson.A{
"$createdLayerRst.layer",
0,
},
},
},
},
},
},
},
bson.D{{"$project", bson.D{{"createdLayerRst", 0}}}},
bson.D{{Key: "$sort", Value: bson.D{{Key: "createdLayer", Value: -1}}}},
bson.D{{Key: "$skip", Value: skip}},
bson.D{{Key: "$limit", Value: *opts[0].Limit}},
}
if query != nil {
pipeline = append(bson.A{
bson.D{{Key: "$match", Value: *query}},
}, pipeline...)
}
cursor, err := s.db.Collection("accounts").Aggregate(ctx, pipeline)
if err != nil {
return nil, fmt.Errorf("failed to get accounts: %w", err)
}
var docs []*model.Account
if err = cursor.All(ctx, &docs); err != nil {
return nil, fmt.Errorf("error decode accounts: %w", err)
}
for _, doc := range docs {
summary, err := s.GetAccountSummary(ctx, doc.Address)
if err != nil {
return nil, fmt.Errorf("failed to get account summary: %w", err)
}
if summary == nil {
continue
}
doc.Sent = summary.Sent
doc.Received = summary.Received
doc.Awards = summary.Awards
doc.Fees = summary.Fees
doc.LastActivity = summary.LastActivity
}
return docs, nil
}
// GetAccountSummary returns the summary of the accounts matching the query. Not all accounts from api have filled this data.
func (s *Reader) GetAccountSummary(ctx context.Context, address string) (*model.AccountSummary, error) {
var accSummary model.AccountSummary
totalRewards, _, err := s.CountCoinbaseRewards(ctx, address)
if err != nil {
return nil, fmt.Errorf("error occured while getting sum of rewards: %w", err)
}
accSummary.Awards = uint64(totalRewards)
received, _, err := s.CountReceivedTransactions(ctx, address)
if err != nil {
if err != nil {
return nil, fmt.Errorf("error occured while getting sum of received txs: %w", err)
}
}
accSummary.Received = uint64(received)
sent, fees, _, err := s.CountSentTransactions(ctx, address)
if err != nil {
if err != nil {
return nil, fmt.Errorf("error occured while getting sum of sent txs: %w", err)
}
}
accSummary.Sent = uint64(sent)
accSummary.Fees = uint64(fees)
latestTx, err := s.GetLatestTransaction(ctx, address)
if err != nil {
return nil, fmt.Errorf("error occured while getting latest sent txs: %w", err)
}
latestReward, err := s.GetLatestReward(ctx, address)
if err != nil {
return nil, fmt.Errorf("error occured while getting latest reawrd: %w", err)
}
if latestTx != nil {
if latestReward != nil && latestReward.Layer > latestTx.Layer {
accSummary.LastActivity = int32(s.GetLayerTimestamp(latestReward.Layer))
} else {
accSummary.LastActivity = int32(s.GetLayerTimestamp(latestTx.Layer))
}
} else {
if latestReward != nil {
accSummary.LastActivity = int32(s.GetLayerTimestamp(latestReward.Layer))
}
}
return &accSummary, nil
}