forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio.go
519 lines (461 loc) · 14.1 KB
/
portfolio.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package portfolio
import (
"errors"
"fmt"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
cryptoIDAPIURL = "https://chainz.cryptoid.info"
xrpScanAPIURL = "https://api.xrpscan.com/api/v1/account/"
ethplorerAPIURL = "https://api.ethplorer.io"
ethplorerAddressInfo = "getAddressInfo"
// PortfolioAddressExchange is a label for an exchange address
PortfolioAddressExchange = "Exchange"
// PortfolioAddressPersonal is a label for a personal/offline address
PortfolioAddressPersonal = "Personal"
)
// Portfolio is variable store holding an array of portfolioAddress
var Portfolio Base
// Verbose allows for debug output when sending an http request
var Verbose bool
// GetEthereumBalance single or multiple address information as
// EtherchainBalanceResponse
func GetEthereumBalance(address string) (EthplorerResponse, error) {
valid, _ := common.IsValidCryptoAddress(address, "eth")
if !valid {
return EthplorerResponse{}, errors.New("not an Ethereum address")
}
urlPath := fmt.Sprintf(
"%s/%s/%s?apiKey=freekey", ethplorerAPIURL, ethplorerAddressInfo, address,
)
result := EthplorerResponse{}
return result, common.SendHTTPGetRequest(urlPath, true, Verbose, &result)
}
// GetCryptoIDAddress queries CryptoID for an address balance for a
// specified cryptocurrency
func GetCryptoIDAddress(address string, coinType currency.Code) (float64, error) {
ok, err := common.IsValidCryptoAddress(address, coinType.String())
if !ok || err != nil {
return 0, errors.New("invalid address")
}
var result interface{}
url := fmt.Sprintf("%s/%s/api.dws?q=getbalance&a=%s",
cryptoIDAPIURL,
coinType.Lower(),
address)
err = common.SendHTTPGetRequest(url, true, Verbose, &result)
if err != nil {
return 0, err
}
return result.(float64), nil
}
// GetRippleBalance returns the value for a ripple address
func GetRippleBalance(address string) (float64, error) {
var result XRPScanAccount
err := common.SendHTTPGetRequest(xrpScanAPIURL+address, true, Verbose, &result)
if err != nil {
return 0, err
}
if (result == XRPScanAccount{}) {
return 0, errors.New("no balance info returned")
}
return result.XRPBalance, nil
}
// GetAddressBalance acceses the portfolio base and returns the balance by passed
// in address, coin type and description
func (p *Base) GetAddressBalance(address, description string, coinType currency.Code) (float64, bool) {
for x := range p.Addresses {
if p.Addresses[x].Address == address &&
p.Addresses[x].Description == description &&
p.Addresses[x].CoinType == coinType {
return p.Addresses[x].Balance, true
}
}
return 0, false
}
// ExchangeExists checks to see if an exchange exists in the portfolio base
func (p *Base) ExchangeExists(exchangeName string) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName {
return true
}
}
return false
}
// AddressExists checks to see if there is an address associated with the
// portfolio base
func (p *Base) AddressExists(address string) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == address {
return true
}
}
return false
}
// ExchangeAddressExists checks to see if there is an exchange address
// associated with the portfolio base
func (p *Base) ExchangeAddressExists(exchangeName string, coinType currency.Code) bool {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
return true
}
}
return false
}
// AddExchangeAddress adds an exchange address to the portfolio base
func (p *Base) AddExchangeAddress(exchangeName string, coinType currency.Code, balance float64) {
if p.ExchangeAddressExists(exchangeName, coinType) {
p.UpdateExchangeAddressBalance(exchangeName, coinType, balance)
} else {
p.Addresses = append(
p.Addresses, Address{Address: exchangeName, CoinType: coinType,
Balance: balance, Description: PortfolioAddressExchange},
)
}
}
// UpdateAddressBalance updates the portfolio base balance
func (p *Base) UpdateAddressBalance(address string, amount float64) {
for x := range p.Addresses {
if p.Addresses[x].Address == address {
p.Addresses[x].Balance = amount
}
}
}
// RemoveExchangeAddress removes an exchange address from the portfolio.
func (p *Base) RemoveExchangeAddress(exchangeName string, coinType currency.Code) {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
p.Addresses = append(p.Addresses[:x], p.Addresses[x+1:]...)
return
}
}
}
// UpdateExchangeAddressBalance updates the portfolio balance when checked
// against correct exchangeName and coinType.
func (p *Base) UpdateExchangeAddressBalance(exchangeName string, coinType currency.Code, balance float64) {
for x := range p.Addresses {
if p.Addresses[x].Address == exchangeName && p.Addresses[x].CoinType == coinType {
p.Addresses[x].Balance = balance
}
}
}
// AddAddress adds an address to the portfolio base
func (p *Base) AddAddress(address, description string, coinType currency.Code, balance float64) error {
if address == "" {
return errors.New("address is empty")
}
if coinType.String() == "" {
return errors.New("coin type is empty")
}
if description == PortfolioAddressExchange {
p.AddExchangeAddress(address, coinType, balance)
}
if !p.AddressExists(address) {
p.Addresses = append(
p.Addresses, Address{Address: address, CoinType: coinType,
Balance: balance, Description: description},
)
} else {
if balance <= 0 {
p.RemoveAddress(address, description, coinType)
} else {
p.UpdateAddressBalance(address, balance)
}
}
return nil
}
// RemoveAddress removes an address when checked against the correct address and
// coinType
func (p *Base) RemoveAddress(address, description string, coinType currency.Code) error {
if address == "" {
return errors.New("address is empty")
}
if coinType.String() == "" {
return errors.New("coin type is empty")
}
for x := range p.Addresses {
if p.Addresses[x].Address == address &&
p.Addresses[x].CoinType == coinType &&
p.Addresses[x].Description == description {
p.Addresses = append(p.Addresses[:x], p.Addresses[x+1:]...)
return nil
}
}
return errors.New("portfolio item does not exist")
}
// UpdatePortfolio adds to the portfolio addresses by coin type
func (p *Base) UpdatePortfolio(addresses []string, coinType currency.Code) error {
if strings.Contains(strings.Join(addresses, ","), PortfolioAddressExchange) ||
strings.Contains(strings.Join(addresses, ","), PortfolioAddressPersonal) {
return nil
}
switch coinType {
case currency.ETH:
for x := range addresses {
result, err := GetEthereumBalance(addresses[x])
if err != nil {
return err
}
if result.Error.Message != "" {
return errors.New(result.Error.Message)
}
err = p.AddAddress(addresses[x],
PortfolioAddressPersonal,
coinType,
result.ETH.Balance)
if err != nil {
return err
}
}
case currency.XRP:
for x := range addresses {
result, err := GetRippleBalance(addresses[x])
if err != nil {
return err
}
err = p.AddAddress(addresses[x],
PortfolioAddressPersonal,
coinType,
result)
if err != nil {
return err
}
}
default:
for x := range addresses {
result, err := GetCryptoIDAddress(addresses[x], coinType)
if err != nil {
return err
}
err = p.AddAddress(addresses[x],
PortfolioAddressPersonal,
coinType,
result)
if err != nil {
return err
}
}
}
return nil
}
// GetPortfolioByExchange returns currency portfolio amount by exchange
func (p *Base) GetPortfolioByExchange(exchangeName string) map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for x := range p.Addresses {
if strings.Contains(p.Addresses[x].Address, exchangeName) {
result[p.Addresses[x].CoinType] = p.Addresses[x].Balance
}
}
return result
}
// GetExchangePortfolio returns current portfolio base information
func (p *Base) GetExchangePortfolio() map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for _, x := range p.Addresses {
if x.Description != PortfolioAddressExchange {
continue
}
balance, ok := result[x.CoinType]
if !ok {
result[x.CoinType] = x.Balance
} else {
result[x.CoinType] = x.Balance + balance
}
}
return result
}
// GetPersonalPortfolio returns current portfolio base information
func (p *Base) GetPersonalPortfolio() map[currency.Code]float64 {
result := make(map[currency.Code]float64)
for _, x := range p.Addresses {
if x.Description == PortfolioAddressExchange {
continue
}
balance, ok := result[x.CoinType]
if !ok {
result[x.CoinType] = x.Balance
} else {
result[x.CoinType] = x.Balance + balance
}
}
return result
}
// getPercentage returns the percentage of the target coin amount against the
// total coin amount.
func getPercentage(input map[currency.Code]float64, target currency.Code, totals map[currency.Code]float64) float64 {
subtotal := input[target]
total := totals[target]
percentage := (subtotal / total) * 100 / 1
return percentage
}
// getPercentageSpecific returns the percentage a specific value of a target coin amount
// against the total coin amount.
func getPercentageSpecific(input float64, target currency.Code, totals map[currency.Code]float64) float64 {
total := totals[target]
percentage := (input / total) * 100 / 1
return percentage
}
// GetPortfolioSummary returns the complete portfolio summary, showing
// coin totals, offline and online summaries with their relative percentages.
func (p *Base) GetPortfolioSummary() Summary {
personalHoldings := p.GetPersonalPortfolio()
exchangeHoldings := p.GetExchangePortfolio()
totalCoins := make(map[currency.Code]float64)
for x, y := range personalHoldings {
totalCoins[x] = y
}
for x, y := range exchangeHoldings {
balance, ok := totalCoins[x]
if !ok {
totalCoins[x] = y
} else {
totalCoins[x] = y + balance
}
}
var portfolioOutput Summary
for x, y := range totalCoins {
coins := Coin{Coin: x, Balance: y}
portfolioOutput.Totals = append(portfolioOutput.Totals, coins)
}
for x, y := range personalHoldings {
coins := Coin{
Coin: x,
Balance: y,
Percentage: getPercentage(personalHoldings, x, totalCoins),
}
portfolioOutput.Offline = append(portfolioOutput.Offline, coins)
}
for x, y := range exchangeHoldings {
coins := Coin{
Coin: x,
Balance: y,
Percentage: getPercentage(exchangeHoldings, x, totalCoins),
}
portfolioOutput.Online = append(portfolioOutput.Online, coins)
}
var portfolioExchanges []string
for _, x := range p.Addresses {
if x.Description == PortfolioAddressExchange {
if !common.StringDataCompare(portfolioExchanges, x.Address) {
portfolioExchanges = append(portfolioExchanges, x.Address)
}
}
}
exchangeSummary := make(map[string]map[currency.Code]OnlineCoinSummary)
for x := range portfolioExchanges {
exchgName := portfolioExchanges[x]
result := p.GetPortfolioByExchange(exchgName)
coinSummary := make(map[currency.Code]OnlineCoinSummary)
for y, z := range result {
coinSum := OnlineCoinSummary{
Balance: z,
Percentage: getPercentageSpecific(z, y, totalCoins),
}
coinSummary[y] = coinSum
}
exchangeSummary[exchgName] = coinSummary
}
portfolioOutput.OnlineSummary = exchangeSummary
offlineSummary := make(map[currency.Code][]OfflineCoinSummary)
for _, x := range p.Addresses {
if x.Description != PortfolioAddressExchange {
coinSummary := OfflineCoinSummary{
Address: x.Address,
Balance: x.Balance,
Percentage: getPercentageSpecific(x.Balance, x.CoinType,
totalCoins),
}
result, ok := offlineSummary[x.CoinType]
if !ok {
offlineSummary[x.CoinType] = append(offlineSummary[x.CoinType],
coinSummary)
} else {
result = append(result, coinSummary)
offlineSummary[x.CoinType] = result
}
}
}
portfolioOutput.OfflineSummary = offlineSummary
return portfolioOutput
}
// GetPortfolioGroupedCoin returns portfolio base information grouped by coin
func (p *Base) GetPortfolioGroupedCoin() map[currency.Code][]string {
result := make(map[currency.Code][]string)
for _, x := range p.Addresses {
if strings.Contains(x.Description, PortfolioAddressExchange) {
continue
}
result[x.CoinType] = append(result[x.CoinType], x.Address)
}
return result
}
// Seed appends a portfolio base object with another base portfolio
// addresses
func (p *Base) Seed(port Base) {
p.Addresses = port.Addresses
}
// StartPortfolioWatcher observes the portfolio object
func StartPortfolioWatcher() {
addrCount := len(Portfolio.Addresses)
log.Debugf(log.PortfolioMgr,
"PortfolioWatcher started: Have %d entries in portfolio.\n", addrCount,
)
for {
data := Portfolio.GetPortfolioGroupedCoin()
for key, value := range data {
err := Portfolio.UpdatePortfolio(value, key)
if err != nil {
log.Errorf(log.PortfolioMgr,
"PortfolioWatcher error %s for currency %s, val %v\n",
err,
key,
value)
continue
}
log.Debugf(log.PortfolioMgr,
"PortfolioWatcher: Successfully updated address balance for %s address(es) %s\n",
key,
value)
}
time.Sleep(time.Minute * 10)
}
}
// GetPortfolio returns a pointer to the portfolio base
func GetPortfolio() *Base {
return &Portfolio
}
// IsExchangeSupported checks if exchange is supported by portfolio address
func IsExchangeSupported(exchange, address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
exchangeList := strings.Split(Portfolio.Addresses[x].SupportedExchanges, ",")
return common.StringDataContainsInsensitive(exchangeList, exchange)
}
return
}
// IsColdStorage checks if address is a cold storage wallet
func IsColdStorage(address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
return Portfolio.Addresses[x].ColdStorage
}
return
}
// IsWhiteListed checks if address is whitelisted for withdraw transfers
func IsWhiteListed(address string) (ret bool) {
for x := range Portfolio.Addresses {
if Portfolio.Addresses[x].Address != address {
continue
}
return Portfolio.Addresses[x].WhiteListed
}
return
}