forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OKExSpot.go
72 lines (59 loc) · 1.59 KB
/
OKExSpot.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
package okcoin
import (
"encoding/json"
"errors"
. "github.com/GitTsewell/GoEx"
"net/http"
"net/url"
"strconv"
)
type OKExSpot struct {
OKCoinCN_API
}
func NewOKExSpot(client *http.Client, accesskey, secretkey string) *OKExSpot {
return &OKExSpot{
OKCoinCN_API: OKCoinCN_API{client, accesskey, secretkey, "https://www.okex.com/api/v1/"}}
}
func (ctx *OKExSpot) GetExchangeName() string {
return OKEX
}
func (ctx *OKExSpot) GetAccount() (*Account, error) {
postData := url.Values{}
err := ctx.buildPostForm(&postData)
if err != nil {
return nil, err
}
body, err := HttpPostForm(ctx.client, ctx.api_base_url+url_userinfo, postData)
if err != nil {
return nil, err
}
var respMap map[string]interface{}
err = json.Unmarshal(body, &respMap)
if err != nil {
return nil, err
}
if errcode, isok := respMap["error_code"].(float64); isok {
errcodeStr := strconv.FormatFloat(errcode, 'f', 0, 64)
return nil, errors.New(errcodeStr)
}
//log.Println(respMap)
info, ok := respMap["info"].(map[string]interface{})
if !ok {
return nil, errors.New(string(body))
}
funds := info["funds"].(map[string]interface{})
free := funds["free"].(map[string]interface{})
freezed := funds["freezed"].(map[string]interface{})
account := new(Account)
account.Exchange = ctx.GetExchangeName()
account.SubAccounts = make(map[Currency]SubAccount, 6)
for k, v := range free {
currencyKey := NewCurrency(k, "")
subAcc := SubAccount{
Currency: currencyKey,
Amount: ToFloat64(v),
ForzenAmount: ToFloat64(freezed[k])}
account.SubAccounts[currencyKey] = subAcc
}
return account, nil
}