-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (128 loc) · 4.39 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/Fornaxian/config"
"gitlab.com/NebulousLabs/Sia/node/api/client"
)
type Config struct {
SiaAPIPassword string `toml:"sia_api_password"`
// These prices are per host, without redundancy. Multiply the storage and
// collateral values by the Redundancy float to get the prices which the
// renter ends up paying
MaxStoragePriceTBMonth float64 `toml:"max_storage_price_tb_month"`
MaxDownloadPriceTB float64 `toml:"max_download_price_tb"`
MaxUploadPriceTB float64 `toml:"max_upload_price_tb"`
MaxContractFormationPrice float64 `toml:"max_contract_formation_price"`
MaxCollateralTBMonth float64 `toml:"max_collateral_tb_month"`
Redundancy float64 `toml:"redundancy"`
Hosts uint64 `toml:"hosts"`
ContractLength uint64 `toml:"contract_length"`
RenewWindow uint64 `toml:"renew_window"`
}
const defaultConf = `# Allowance manager configuration
# Password for accessing the Sia API. Can be found in ~/.sia/apipassword
sia_api_password = ""
# Max storage price in euros per month per TB per host (without redundancy)
max_storage_price_tb_month = 1.80
# Max download price in euros per TB
max_download_price_tb = 2.50
# Max upload price in euros per TB
max_upload_price_tb = 2.00
# Max contract formation fee in euros per contract
max_contract_formation_price = 0.10
# Max collateral price in euros per month per TB per host (without redundancy)
max_collateral_tb_month = 8.00
# Data redundancy value to use in calculations. When setting the allowance the
# max_storage_price_tb_month and max_collateral_tb_month will be multiplied by
# this value in order to calculate the real price you will end up paying
redundancy = 3.00
# Number of hosts to use when creating contracts
hosts = 50
# Contract length and renew window in blocks. The defaults are 3 months and one
# month, respectively. Assuming a month is exactly 30 days, which it isn't. But
# it's close enough
contract_length = 12960
renew_window = 4320
`
func main() {
var err error
var conf Config
if _, err = config.New(defaultConf, "", "sia_allowance_manager.toml", &conf, true); err != nil {
panic(err)
}
sia := client.New(client.Options{
Address: "127.0.0.1:9980",
Password: conf.SiaAPIPassword,
})
scPrice, err := getKrakenPrice()
if err != nil {
panic(err)
}
if err = adjustAllowance(sia, conf, scPrice); err != nil {
panic(err)
}
}
// Doc https://www.kraken.com/features/api#get-ticker-info
type krakenResponse struct {
Error []string `json:"error"`
Result struct {
SCEUR struct {
Ask []string `json:"a"` // price, whole lot volume, lot volume
Bid []string `json:"b"` // price, whole lot volume, lot volume
Closed []string `json:"c"` // price, lot volume
Volume []string `json:"v"` // today, 24h
WeightedAveragePrice []string `json:"p"` // today, 24h
TotalTrades []float64 `json:"t"` // today, 24h
Low []string `json:"l"` // today, 24h
High []string `json:"h"` // today, 24h
Opening string `json:"o"`
} `json:"SCEUR"`
} `json:"result"`
}
func getKrakenPrice() (price float64, err error) {
resp, err := http.Get("https://api.kraken.com/0/public/Ticker?pair=SCEUR")
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return 0, fmt.Errorf("status: %d", resp.StatusCode)
}
var kr krakenResponse
if err = json.NewDecoder(resp.Body).Decode(&kr); err != nil {
return 0, err
}
// Return the 24h average price
return strconv.ParseFloat(kr.Result.SCEUR.Low[1], 64)
}
// FormatData prints an amount if bytes in a readable rounded amount
func FormatData(size int64) string {
var fmtSize = func(n float64, u string) string {
var f string
if n >= 100 {
f = "%.1f"
} else if n >= 10 {
f = "%.2f"
} else {
f = "%.3f"
}
return fmt.Sprintf(f+" "+u, n)
}
if size >= 1e18 {
return fmtSize(float64(size)/1e12, "EB")
} else if size >= 1e15 {
return fmtSize(float64(size)/1e12, "PB")
} else if size >= 1e12 {
return fmtSize(float64(size)/1e12, "TB")
} else if size >= 1e9 {
return fmtSize(float64(size)/1e9, "GB")
} else if size >= 1e6 {
return fmtSize(float64(size)/1e6, "MB")
} else if size >= 1e3 {
return fmtSize(float64(size)/1e3, "kB")
}
return fmt.Sprintf("%d B", size)
}