-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
110 lines (100 loc) · 3.07 KB
/
process.go
File metadata and controls
110 lines (100 loc) · 3.07 KB
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
package main
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
)
// Simplified transaction data structure
// This struct represents a cleaned-up version of the raw GraphQL data
type Transaction struct {
Hash string
BlockHeight float64
Amount float64 // in ugnot
From string
To string
}
// GraphQL response typed model (only required fields)
type gqlResponse struct {
Data struct {
GetTransactions []struct {
Hash string `json:"hash"`
BlockHeight float64 `json:"block_height"`
Messages []struct {
Value struct {
FromAddress string `json:"from_address"`
ToAddress string `json:"to_address"`
Amount string `json:"amount"` // e.g. 15000000ugnot
} `json:"value"`
} `json:"messages"`
} `json:"getTransactions"`
} `json:"data"`
}
// Step 1 - Parse JSON from GraphQL into our Transaction structs
// This function takes raw JSON from the indexer and converts it to Go structs
func parseTransactions(jsonData []byte) ([]Transaction, error) {
var resp gqlResponse
if err := json.Unmarshal(jsonData, &resp); err != nil {
return nil, fmt.Errorf("decode graphql response: %w", err)
}
if len(resp.Data.GetTransactions) == 0 {
return nil, nil
}
out := make([]Transaction, 0, len(resp.Data.GetTransactions))
for _, tx := range resp.Data.GetTransactions {
if len(tx.Messages) == 0 {
continue
}
msg := tx.Messages[0].Value
amtStr := strings.TrimSuffix(msg.Amount, "ugnot")
amt, _ := strconv.ParseFloat(amtStr, 64) // ignore parse error -> 0
out = append(out, Transaction{
Hash: tx.Hash,
BlockHeight: tx.BlockHeight,
Amount: amt,
From: msg.FromAddress,
To: msg.ToAddress,
})
}
return out, nil
}
// Step 2 - Sort transactions by amount (biggest first)
// This helps us identify the largest transfers on the network
func sortTransactions(txs []Transaction) []Transaction {
sort.Slice(txs, func(i, j int) bool { return txs[i].Amount > txs[j].Amount })
return txs
}
// Step 3 - Show the transactions in a nice format
// Convert raw data into human-readable output
func displayTransactions(txs []Transaction) {
fmt.Println("Top GNOT Transactions:")
for i, tx := range txs {
if i >= 5 {
break
} // Limit to top 5
gnotAmount := tx.Amount
fmt.Printf("%d. %.2f uGNOT from %s to %s (block %.0f)\n",
i+1, gnotAmount, tx.From, tx.To, tx.BlockHeight)
}
}
func RunProcessExample() {
// This would be your actual JSON from the GraphQL query
// In a real app, you'd get this from an HTTP request to the indexer
jsonData := []byte(`{"data": {"getTransactions": []}}`)
// Process the data in 3 steps:
transactions, err := parseTransactions(jsonData)
if err != nil || len(transactions) == 0 {
return
}
if len(transactions) == 0 {
fmt.Println("no transactions decoded")
return
}
sorted := sortTransactions(transactions) // 2. Sort by amount (largest first)
displayTransactions(sorted) // 3. Display results nicely
// At this point, you have clean, sorted transaction data ready for:
// - Saving to a database
// - Serving via an API
// - ...
}