forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
portfolio.go
168 lines (152 loc) · 4.51 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
package main
import (
"errors"
"fmt"
)
const (
BLOCKR_API_URL = "blockr.io/api"
BLOCKR_API_VERSION = "1"
BLOCKR_ADDRESS_BALANCE = "address/balance"
ETHERCHAIN_API_URL = "https://etherchain.org/api"
ETHERCHAIN_ACCOUNT_MULTIPLE = "account/multiple"
)
type PortfolioAddress struct {
Address string
CoinType string
Balance float64
}
type Portfolio struct {
Addresses []PortfolioAddress
}
type BlockrAddress struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
BalanceMultisig float64 `json:"balance_multisig"`
}
type BlockrAddressBalanceSingle struct {
Status string `json:"status"`
Data BlockrAddress `json:"data"`
Code int `json:"code"`
Message string `json:"message"`
}
type BlockrAddressBalanceMulti struct {
Status string `json:"status"`
Data []BlockrAddress `json:"data"`
Code int `json:"code"`
Message string `json:"message"`
}
type EtherchainBalanceResponse struct {
Status int `json:"status"`
Data []struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
Nonce interface{} `json:"nonce"`
Code string `json:"code"`
Name interface{} `json:"name"`
Storage interface{} `json:"storage"`
FirstSeen interface{} `json:"firstSeen"`
} `json:"data"`
}
func GetEthereumBalance(address []string) (EtherchainBalanceResponse, error) {
addresses := JoinStrings(address, ",")
url := fmt.Sprintf("%s/%s/%s", ETHERCHAIN_API_URL, ETHERCHAIN_ACCOUNT_MULTIPLE, addresses)
result := EtherchainBalanceResponse{}
err := SendHTTPGetRequest(url, true, &result)
if err != nil {
return result, err
}
if result.Status != 1 {
return result, errors.New("Status was not 1")
}
return result, nil
}
func GetBlockrBalanceSingle(address string, coinType string) (BlockrAddressBalanceSingle, error) {
url := fmt.Sprintf("https://%s.%s/v%s/%s/%s", StringToLower(coinType), BLOCKR_API_URL, BLOCKR_API_VERSION, BLOCKR_ADDRESS_BALANCE, address)
result := BlockrAddressBalanceSingle{}
err := SendHTTPGetRequest(url, true, &result)
if err != nil {
return result, err
}
if result.Status != "success" {
return result, errors.New(result.Message)
}
return result, nil
}
func GetBlockrAddressMulti(addresses []string, coinType string) (BlockrAddressBalanceMulti, error) {
addressesStr := JoinStrings(addresses, ",")
url := fmt.Sprintf("https://%s.%s/v%s/%s/%s", StringToLower(coinType), BLOCKR_API_URL, BLOCKR_API_VERSION, BLOCKR_ADDRESS_BALANCE, addressesStr)
result := BlockrAddressBalanceMulti{}
err := SendHTTPGetRequest(url, true, &result)
if err != nil {
return result, err
}
if result.Status != "success" {
return result, errors.New(result.Message)
}
return result, nil
}
func GetAddressBalance(address string) (float64, bool) {
for _, x := range bot.portfolio.Addresses {
if x.Address == address {
return x.Balance, true
}
}
return 0, false
}
func AddressExists(address string) bool {
for _, x := range bot.portfolio.Addresses {
if x.Address == address {
return true
}
}
return false
}
func UpdatePortfolio(addresses []string, coinType string) bool {
if coinType == "ETH" {
result, err := GetEthereumBalance(addresses)
if err != nil {
return false
}
for _, x := range result.Data {
if !AddressExists(x.Address) {
bot.portfolio.Addresses = append(bot.portfolio.Addresses, PortfolioAddress{Address: x.Address, CoinType: coinType, Balance: x.Balance / WEI_PER_ETHER})
}
}
return true
}
if len(addresses) > 1 {
result, err := GetBlockrAddressMulti(addresses, coinType)
if err != nil {
return false
}
for _, x := range result.Data {
if !AddressExists(x.Address) {
bot.portfolio.Addresses = append(bot.portfolio.Addresses, PortfolioAddress{Address: x.Address, CoinType: coinType, Balance: x.Balance})
}
}
} else {
result, err := GetBlockrBalanceSingle(addresses[0], coinType)
if err != nil {
return false
}
if !AddressExists(result.Data.Address) {
bot.portfolio.Addresses = append(bot.portfolio.Addresses, PortfolioAddress{Address: result.Data.Address, CoinType: coinType, Balance: result.Data.Balance})
}
}
return true
}
func GetPortfolioSummary(coinFilter string) map[string]float64 {
result := make(map[string]float64)
for _, x := range bot.portfolio.Addresses {
if coinFilter != "" && coinFilter != x.CoinType {
continue
}
balance, ok := result[x.CoinType]
if !ok {
result[x.CoinType] = x.Balance
} else {
result[x.CoinType] = x.Balance + balance
}
}
return result
}