-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (74 loc) · 2.4 KB
/
main.go
File metadata and controls
87 lines (74 loc) · 2.4 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
package main
import (
"context"
"flag"
"fmt"
"strings"
"wallet-info/client"
"wallet-info/config"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
log "github.com/charmbracelet/log"
)
func main() {
wallet := flag.String("wallet", "", "Wallet address (takes priority over value in config file if provided)")
filter := flag.String("filter", "", "Filter by blockchain name")
configPath := flag.String("config", "", "Path to configuration file (default: $HOME/wallet-info-config.yaml)")
flag.Parse()
cfg, err := config.LoadConfig(*configPath)
if err != nil {
log.Error("Error loading config:", "err", err)
return
}
if *wallet == "" {
*wallet = cfg.Wallet
}
if *wallet == "" {
log.Error("Wallet must be provided")
return
}
rows := buildRows(cfg, *wallet, *filter)
t := createStyledTable().Headers("Wallet", "Chain", "Token", "Amount").Rows(rows...)
fmt.Println(t)
}
func buildRows(cfg *config.Config, wallet, filter string) [][]string {
var rows [][]string
for _, blockchain := range cfg.Blockchains {
if filter == "" || strings.Contains(filter, blockchain.Name) {
rows = append(rows, fetchBlockchainData(blockchain, wallet)...)
}
}
return rows
}
func fetchBlockchainData(blockchain config.Blockchain, wallet string) [][]string {
var rows [][]string
ctx := context.Background()
evmClient := client.NewEvmChainClient(blockchain.RPC, false)
mainBalance := evmClient.GetBalance(ctx, wallet, blockchain.Decimals)
rows = append(rows, []string{wallet, blockchain.Name, blockchain.MainTokenLabel, mainBalance})
for _, token := range blockchain.Tokens {
tokenBalance := evmClient.GetBalanceForToken(ctx, wallet, token.Address, token.Decimals)
rows = append(rows, []string{wallet, blockchain.Name, token.Label, tokenBalance})
}
return rows
}
func createStyledTable() *table.Table {
return table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("#1565c0"))).
StyleFunc(func(row, col int) lipgloss.Style {
width := map[int]int{0: 45, 1: 20, 2: 20}[col]
if width == 0 {
width = 40
}
style := lipgloss.NewStyle().Width(width)
switch {
case row == 0:
return style.Foreground(lipgloss.Color("#f5f5f5")).Background(lipgloss.Color("#1565c0"))
case row%2 == 0:
return style.Foreground(lipgloss.Color("#303f9f"))
default:
return style.Foreground(lipgloss.Color("#388e3c"))
}
})
}