-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccount.go
279 lines (255 loc) · 8.83 KB
/
account.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
// Copyright 2018 Myndshft Technologies, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nemgo
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// AccountInfo describes basic information for an account.
type AccountInfo struct {
// Each account has a unique address. First letter of an address
// indicate the network the account belongs to. Currently two networks
// are defined: the test network whose account addresses start with a
// capital T and the main network whose account addresses always start
// with a capital N. Addresses have always a length of 40 characters
// and are base-32 encoded.
Address string
// Each account has a balance which is an integer greater or equal to
// zero and denotes the number of micro NEMs which the account owns.
// Thus a balance of 123456789 means the account owns 123.456789 NEM.
// A balance is split into its vested and unvested part.
// Only the vested part is relevant for the importance calculation.
// For transfers from one account to another only the balance itself
// is relevant.
Balance int
// vestedBalance contains the vested part of the balance of the account
// in micro NEM.
VestedBalance float64
// Each account is assigned an importance. The importance is a decimal
// number between 0 and 1. It denotes the probability of an account to
// harvest the next block in case the account has harvesting turned on
// and all other accounts are harvesting too. The exact formula for
// calculating the importance is not public yet.
// Accounts need at least 10k vested NEM to be included
// in the importance calculation.
Importance float64
// The public key of an account can be used to verify signatures of the
// account. Only accounts that have already published a transaction have
// a public key assigned to the account. Otherwise the field is null.
PublicKey string
// Label has the label of the account (not used, always null).
Label string
// Harvesting is the process of generating new blocks. The field
// denotes the number of blocks that the account harvested so far.
// For a new account the number is 0.
HarvestedBlocks int
}
// AccountMetadata describes additional information for the account.
type AccountMetadata struct {
// Status contains the harvesting status of a queried account.
// The harvesting status can be one of the following values:
// "UNKNOWN": The harvesting status of the account is not known.
// "LOCKED": The account is not harvesting.
// "UNLOCKED": The account is harvesting.
Status string
// RemoteStatus contains the status of teh remote harvesting of a queried account.
// The remote harvesting status can be one of the following values:
// "REMOTE": The account is a remote account and therefore remoteStatus is not applicable for it.
// "ACTIVATING": The account has activated remote harvesting but it is not yet active.
// "ACTIVE": The account has activated remote harvesting and remote harvesting is active.
// "DEACTIVATING": The account has deactivated remote harvesting but remote harvesting is still active.
// "INACTIVE": The account has inactive remote harvesting, or it has deactivated remote harvesting
// and deactivation is operational.
RemoteStatus string
// CosignatoryOf is a JSON array of AccountInfo structures.
// The account is cosignatory for each of the accounts in the array.
CosignatoryOf []AccountInfo
// Cosignatories is a JSON array of AccountInfo structures.
// The array holds all accounts that are a cosignatory for this account.
Cosignatories []AccountInfo
}
// AccountMetadataPair includes durable information for an account and additional information about its state.
type AccountMetadataPair struct {
// Account contains the account object.
Account AccountInfo `json:"account"`
// Meta contain the account meta data object.
Meta AccountMetadata `json:"meta"`
}
// GetBatchAccountData gets the AccountMetaDataPair of an array of accounts
func (c Client) GetBatchAccountData(addresses []string) ([]AccountMetadataPair, error) {
data := struct{ Data []AccountMetadataPair }{}
var pb []map[string]string
for _, address := range addresses {
pb = append(pb, map[string]string{"account": address})
}
payload, err := json.Marshal(map[string][]map[string]string{"data": pb})
if err != nil {
return data.Data, err
}
c.url.Path = "/account/batch"
req, err := c.buildReq(nil, payload, http.MethodPost)
if err != nil {
return data.Data, err
}
body, err := c.request(req)
if err != nil {
return data.Data, err
}
if err = json.Unmarshal(body, &data); err != nil {
return data.Data, err
}
return data.Data, nil
}
type accountName interface {
isValid() bool
String() string
}
// Address is a Nem account address as a string
type Address string
func (a Address) isValid() bool {
// check if address is valid
return true
}
func (a Address) String() string {
return string(a)
}
// PublicKey is a Nem account public key as a string
type PublicKey string
func (pk PublicKey) isValid() bool {
// check if address is valid
return true
}
func (pk PublicKey) String() string {
return string(pk)
}
// AccountData gets all information for a given address
func (c Client) AccountData(acc accountName) (AccountMetadataPair, error) {
var data AccountMetadataPair
var req *http.Request
var err error
if acc.isValid() {
switch acc.(type) {
case Address:
c.url.Path = "/account/get"
req, err = c.buildReq(map[string]string{"address": acc.String()}, nil, http.MethodGet)
if err != nil {
return data, err
}
case PublicKey:
c.url.Path = "/account/get/from-public-key"
req, err = c.buildReq(map[string]string{"publicKey": acc.String()}, nil, http.MethodGet)
if err != nil {
return data, err
}
default:
return data, errors.New("Use an Address or PublicKey type")
}
}
body, err := c.request(req)
if err != nil {
return data, err
}
if err := json.Unmarshal(body, &data); err != nil {
fmt.Println(err)
return data, err
}
return data, nil
}
// GetDelegated returns the account meta and data info for the account
// for which the given account is the delegate account
func (c Client) GetDelegated(address string) (AccountMetadataPair, error) {
var data AccountMetadataPair
c.url.Path = "/account/get/forwarded"
req, err := c.buildReq(map[string]string{"address": address}, nil, http.MethodGet)
if err != nil {
return data, err
}
body, err := c.request(req)
if err != nil {
return data, err
}
if err := json.Unmarshal(body, &data); err != nil {
fmt.Println(err)
return data, err
}
return data, nil
}
// AccountStatus gets the current metadata about an account
func (c Client) AccountStatus(address string) (AccountMetadata, error) {
var data AccountMetadata
c.url.Path = "/account/status"
req, err := c.buildReq(map[string]string{"address": address}, nil, http.MethodGet)
if err != nil {
return data, err
}
body, err := c.request(req)
if err != nil {
return data, err
}
if err := json.Unmarshal(body, &data); err != nil {
fmt.Println(err)
return data, err
}
return data, nil
}
// HarvestInfo is information about harvested blocks
type HarvestInfo struct {
TimeStamp int
Difficulty int
TotalFee int
ID int
Height int
}
// Harvested gets an array of harvest info objects for an account
func (c Client) Harvested(address string, hash string) ([]HarvestInfo, error) {
var data = struct{ Data []HarvestInfo }{}
c.url.Path = "/account/harvests"
req, err := c.buildReq(map[string]string{"address": address, "hash": hash}, nil, http.MethodGet)
if err != nil {
return data.Data, err
}
body, err := c.request(req)
if err != nil {
return data.Data, err
}
if err := json.Unmarshal(body, &data); err != nil {
return data.Data, err
}
return data.Data, nil
}
// OwnedMosaic is an array of basic information about a mosaic
type OwnedMosaic struct {
MosaicID struct {
NamespaceID string
Name string
}
Quantity int
}
// MosaicsOwned will find information about what mosaics an address
// currently holds
func (c Client) MosaicsOwned(address string) ([]OwnedMosaic, error) {
var data = struct{ Data []OwnedMosaic }{}
c.url.Path = "/account/mosaic/owned"
req, err := c.buildReq(map[string]string{"address": address}, nil, http.MethodGet)
if err != nil {
return data.Data, err
}
body, err := c.request(req)
if err != nil {
return data.Data, err
}
if err := json.Unmarshal(body, &data); err != nil {
return data.Data, err
}
return data.Data, nil
}