forked from wtfutil/wtf
/
widget.go
121 lines (102 loc) · 3.5 KB
/
widget.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
package blockfolio
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
app *tview.Application
device_token string
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Blockfolio ", "blockfolio", false),
device_token: wtf.Config.UString("wtf.mods.blockfolio.device_token"),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.View.SetTitle(" Blockfolio ")
positions, err := Fetch(widget.device_token)
if err != nil {
return
}
widget.View.SetText(contentFrom(positions))
}
/* -------------------- Unexported Functions -------------------- */
func contentFrom(positions *AllPositionsResponse) string {
res := ""
colorName := wtf.Config.UString("wtf.mods.blockfolio.colors.name")
colorGrows := wtf.Config.UString("wtf.mods.blockfolio.colors.grows")
colorDrop := wtf.Config.UString("wtf.mods.blockfolio.colors.drop")
displayHoldings := wtf.Config.UBool("wtf.mods.blockfolio.displayHoldings")
var totalFiat float32
totalFiat = 0.0
for i := 0; i < len(positions.PositionList); i++ {
colorForChange := colorGrows
if positions.PositionList[i].TwentyFourHourPercentChangeFiat <= 0 {
colorForChange = colorDrop
}
totalFiat += positions.PositionList[i].HoldingValueFiat
if displayHoldings {
res = res + fmt.Sprintf("[%s]%-6s - %5.2f ([%s]%.3fk [%s]%.2f%s)\n", colorName, positions.PositionList[i].Coin, positions.PositionList[i].Quantity, colorForChange, positions.PositionList[i].HoldingValueFiat/1000, colorForChange, positions.PositionList[i].TwentyFourHourPercentChangeFiat, "%")
} else {
res = res + fmt.Sprintf("[%s]%-6s - %5.2f ([%s]%.2f%s)\n", colorName, positions.PositionList[i].Coin, positions.PositionList[i].Quantity, colorForChange, positions.PositionList[i].TwentyFourHourPercentChangeFiat, "%")
}
}
if displayHoldings {
res = res + fmt.Sprintf("\n[%s]Total value: $%.3fk", "green", totalFiat/1000)
}
return res
}
//always the same
const magic = "edtopjhgn2345piuty89whqejfiobh89-2q453"
type Position struct {
Coin string `json:coin`
LastPriceFiat float32 `json:lastPriceFiat`
TwentyFourHourPercentChangeFiat float32 `json:twentyFourHourPercentChangeFiat`
Quantity float32 `json:quantity`
HoldingValueFiat float32 `json:holdingValueFiat`
}
type AllPositionsResponse struct {
PositionList []Position `json:positionList`
}
func MakeApiRequest(token string, method string) ([]byte, error) {
client := &http.Client{}
url := "https://api-v0.blockfolio.com/rest/" + method + "/" + token + "?use_alias=true&fiat_currency=USD"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("magic", magic)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func GetAllPositions(token string) (*AllPositionsResponse, error) {
jsn, err := MakeApiRequest(token, "get_all_positions")
var parsed AllPositionsResponse
err = json.Unmarshal(jsn, &parsed)
if err != nil {
log.Fatalf("Failed to parse json %v", err)
return nil, err
}
return &parsed, err
}
func Fetch(token string) (*AllPositionsResponse, error) {
return GetAllPositions(token)
}