-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbalance.go
150 lines (126 loc) · 3.7 KB
/
balance.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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"encoding/json"
)
type MultiBalanceResponse struct {
CurrentHeight int `json:"currentheight"`
LastSavedHeight int `json:"lastsavedheight"`
Balances []struct {
Ack int `json:"ack"`
Saved int `json:"saved"`
Err string `json:"err"`
} `json:"balances"`
}
// GetECBalance returns the Entry Credit balance of a given Entry
// Credit Public Address.
func GetECBalance(addr string) (int64, error) {
type balanceResponse struct {
Balance int64 `json:"balance"`
}
params := addressRequest{Address: addr}
req := NewJSON2Request("entry-credit-balance", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return -1, err
}
if resp.Error != nil {
return -1, resp.Error
}
balance := new(balanceResponse)
if err := json.Unmarshal(resp.JSONResult(), balance); err != nil {
return -1, err
}
return balance.Balance, nil
}
// GetFactoidBalance returns the balance in factoshi (factoid * 1e8) of a given
// Factoid Public Address.
func GetFactoidBalance(addr string) (int64, error) {
type balanceResponse struct {
Balance int64 `json:"balance"`
}
params := addressRequest{Address: addr}
req := NewJSON2Request("factoid-balance", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return -1, err
}
if resp.Error != nil {
return -1, resp.Error
}
balance := new(balanceResponse)
if err := json.Unmarshal(resp.JSONResult(), balance); err != nil {
return -1, err
}
return balance.Balance, nil
}
// GetBalanceTotals return the total value of Factoids and Entry Credits in the
// wallet according to the the server acknowledgement and the value saved in the
// blockchain.
func GetBalanceTotals() (fs, fa, es, ea int64, err error) {
type multiBalanceResponse struct {
FactoidAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"fctaccountbalances"`
EntryCreditAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"ecaccountbalances"`
}
req := NewJSON2Request("wallet-balances", APICounter(), nil)
resp, err := walletRequest(req)
if err != nil {
return
}
balances := new(multiBalanceResponse)
err = json.Unmarshal(resp.JSONResult(), balances)
if err != nil {
return
}
fs = balances.FactoidAccountBalances.Saved
fa = balances.FactoidAccountBalances.Ack
es = balances.EntryCreditAccountBalances.Saved
ea = balances.EntryCreditAccountBalances.Ack
return
}
// GetMultipleFCTBalances returns balances for multiple Factoid Addresses from
// the factomd API.
func GetMultipleFCTBalances(fas ...string) (*MultiBalanceResponse, error) {
type multiAddressRequest struct {
Addresses []string `json:"addresses"`
}
params := multiAddressRequest{fas}
req := NewJSON2Request("multiple-fct-balances", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
balances := new(MultiBalanceResponse)
err = json.Unmarshal(resp.JSONResult(), balances)
if err != nil {
return nil, err
}
return balances, nil
}
// GetMultipleECBalances returns balances for multiple Entry Credit Addresses
// from the factomd API.
func GetMultipleECBalances(ecs ...string) (*MultiBalanceResponse, error) {
type multiAddressRequest struct {
Addresses []string `json:"addresses"`
}
params := multiAddressRequest{ecs}
req := NewJSON2Request("multiple-ec-balances", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
balances := new(MultiBalanceResponse)
err = json.Unmarshal(resp.JSONResult(), balances)
if err != nil {
return nil, err
}
return balances, nil
}