-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
69 lines (60 loc) · 1.38 KB
/
operations.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
package main
import (
"fmt"
"io"
"github.com/stellar/go/ingest"
)
func getOperationCategory(opType int) string {
// map for operation types
operationTypeMap := map[int]string{
0: "account_creation",
9: "account creation",
1: "payments",
2: "payments",
13: "payments",
3: "offers_and_AMMs",
4: "offers_and_AMMs",
12: "offers_and_AMMs",
22: "offers_and_AMMs",
23: "offers_and_AMMs",
6: "trust",
7: "trust",
21: "trust",
14: "claimable_balances",
15: "claimable_balances",
20: "claimable_balances",
16: "sponsorship",
17: "sponsorship",
18: "sponsorship",
}
if opType > 23 {
return "soroban"
}
// default to 'Other'
if val, ok := operationTypeMap[opType]; ok {
return val
}
return "other"
}
type Operations struct {
Total int `json:"total"`
Categories map[string]int `json:"categories"`
}
func getOperationsStat(transactionReader *ingest.LedgerTransactionReader) (Operations, error) {
operations := Operations{0, make(map[string]int)}
for {
tx, err := transactionReader.Read()
if err == io.EOF {
break
}
if err != nil {
return operations, fmt.Errorf("could not read transaction %w", err)
}
for _, op := range tx.Envelope.Operations() {
catType := getOperationCategory(int(op.Body.Type))
operations.Categories[catType]++
}
operations.Total += len(tx.Envelope.Operations())
}
return operations, nil
}