This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gui.go
140 lines (118 loc) · 3.73 KB
/
gui.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
package main
import (
"encoding/json"
"fmt"
astilectron "github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
"github.com/champii/crypto-dht/blockchain"
)
var (
AppName string
BuiltAt string
window *astilectron.Window
app *astilectron.Astilectron
bc *blockchain.Blockchain
)
type MinerInfo struct {
Hashrate int `json:"hashrate"`
Running bool `json:"running"`
WaitingTransactions int `json:"waitingTransactions"`
ProcessingTransactions int `json:"processingTransactions"`
}
type BaseInfo struct {
MinerInfo MinerInfo `json:"minerInfo"`
Wallets []WalletClient `json:"wallets"`
NodesNb int `json:"nodesNb"`
Synced bool `json:"synced"`
BlocksHeight int64 `json:"blocksHeight"`
Difficulty int64 `json:"difficulty"`
NextDifficulty int64 `json:"nextDifficulty"`
TimeSinceLastBlock int64 `json:"timeSinceLastBlock"`
StoredKeys int `json:"storedKeys"`
History []blockchain.HistoryTx `json:"history"`
OwnWaitingTx []blockchain.HistoryTx `json:"ownWaitingTx"`
}
type WalletClient struct {
Name string `json:"name"`
Address string `json:"address"`
Amount int `json:"amount"`
}
func GetBaseInfos() BaseInfo {
wallets := bc.Wallets()
var walletsRes []WalletClient
for _, wallet := range wallets {
walletsRes = append(walletsRes, WalletClient{
Name: wallet.Name(),
Address: blockchain.SanitizePubKey(wallet.Pub()),
Amount: bc.GetAvailableFunds(wallet.Pub()),
})
}
stats := bc.Stats()
hashRate := 0
if len(stats.HashesPerSec) > 0 {
hashRate = stats.HashesPerSec[len(stats.HashesPerSec)-1]
}
return BaseInfo{
Wallets: walletsRes,
NodesNb: bc.GetConnectedNodesNb(),
Synced: bc.Synced(),
BlocksHeight: bc.BlocksHeight(),
Difficulty: bc.Difficulty(),
NextDifficulty: bc.NextDifficulty(),
StoredKeys: bc.StoredKeys(),
TimeSinceLastBlock: bc.TimeSinceLastBlock(),
History: bc.GetOwnHistory(),
OwnWaitingTx: bc.GetOwnWaitingTx(),
MinerInfo: MinerInfo{
Hashrate: hashRate,
Running: bc.Running(),
WaitingTransactions: bc.WaitingTransactionCount(),
ProcessingTransactions: bc.ProcessingTransactionCount(),
},
}
}
func handleMessages(w *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "getInfos":
payload = GetBaseInfos()
case "send":
var r string
json.Unmarshal(m.Payload, &r)
err := bc.SendTo(r)
payload = ""
if err != nil {
payload = err.Error()
}
}
return
}
func gui(bc_ *blockchain.Blockchain) {
err := bootstrap.Run(bootstrap.Options{
Asset: Asset,
RestoreAssets: RestoreAssets,
Homepage: "index.html",
MessageHandler: handleMessages,
MenuOptions: []*astilectron.MenuItemOptions{},
OnWait: func(a *astilectron.Astilectron, w *astilectron.Window, _ *astilectron.Menu, t *astilectron.Tray, _ *astilectron.Menu) error {
window = w
app = a
bc = bc_
// w.OpenDevTools()
return nil
},
WindowOptions: &astilectron.WindowOptions{
BackgroundColor: astilectron.PtrStr("#333"),
Center: astilectron.PtrBool(true),
Height: astilectron.PtrInt(380),
Width: astilectron.PtrInt(1050),
Resizable: astilectron.PtrBool(false),
Frame: astilectron.PtrBool(false),
HasShadow: astilectron.PtrBool(true),
Transparent: astilectron.PtrBool(true),
},
})
if err != nil {
fmt.Println(err.Error())
return
}
}