-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzb_margin.go
182 lines (165 loc) · 5.82 KB
/
zb_margin.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
package zb
import (
"fmt"
"github.com/tidwall/gjson"
"net/url"
)
// GetLeverAssetsInfo 获取用户杠杆信息
func (z *ZB) GetLeverAssetsInfo() (result LeverAssetsInfo, err error) {
params := url.Values{}
params.Set("method", "getLeverAssetsInfo")
z.buildSignParams(¶ms)
url := z.tradeURL + "getLeverAssetsInfo?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
// {"code":3001,"message":"挂单没有找到或已完成"}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
code := codeValue.Int()
if code != OK {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
}
err = json.Unmarshal(resp, &result)
return
}
// GetLoans 获取可借贷列表
// coin: qc
func (z *ZB) GetLoans(coin string, pageIndex int, pageSize int) (result []Loan, err error) {
params := url.Values{}
params.Set("method", "getLoans")
params.Set("coin", coin)
params.Set("pageIndex", fmt.Sprint(pageIndex))
params.Set("pageSize", fmt.Sprint(pageSize))
z.buildSignParams(¶ms)
url := z.tradeURL + "getLoans?" + params.Encode()
var ret LoansResponse
_, err = z.HttpGet(url, "", nil, &ret)
if err != nil {
return
}
if ret.Code != OK {
err = fmt.Errorf("code=%v message=%v",
ret.Code, ret.Message)
return
}
result = ret.Result
return
}
// GetLoanRecords 获取借贷记录
// loanId: 理财id(借出就传 借入不用传)
// marketName: btsqc
// status: 状态(1还款中 2已还款 3 需要平仓 4 平仓还款 5自动还款中 6自动还款)
func (z *ZB) GetLoanRecords(loanId string, marketName string, status int, pageIndex int, pageSize int) (result []LoanRecord, err error) {
params := url.Values{}
params.Set("method", "getLoanRecords")
if loanId != "" {
params.Set("loanId", loanId)
}
params.Set("marketName", marketName)
params.Set("pageIndex", fmt.Sprint(pageIndex))
params.Set("pageSize", fmt.Sprint(pageSize))
z.buildSignParams(¶ms)
url := z.tradeURL + "getLoanRecords?" + params.Encode()
var ret LoanRecordResponse
_, err = z.HttpGet(url, "", nil, &ret)
if err != nil {
return
}
if ret.Code != OK {
err = fmt.Errorf("code=%v message=%v",
ret.Code, ret.Message)
return
}
result = ret.Result
return
}
// Borrow 借款
// amount: 借入金额,小数位数不能超过4,超过系统自动截取前4位
// interestRateOfDay: 日利率 [0.05-0.2] 百分比,小数位数不能超过3,超过系统自动截取前3位
// repaymentDay: 借款期限 [10/20/30]天
// isLoop: 是否自动续借 [1/0](到期自动续借/到期自动还款)
func (z *ZB) Borrow(marketName string, coin string, amount float64, interestRateOfDay float64, repaymentDay int, isLoop int) (result GeneralResult, err error) {
params := url.Values{}
params.Set("method", "borrow")
params.Set("marketName", marketName)
params.Set("coin", coin)
params.Set("amount", fmt.Sprint(amount))
params.Set("interestRateOfDay", fmt.Sprint(interestRateOfDay))
params.Set("repaymentDay", fmt.Sprint(repaymentDay))
params.Set("isLoop", fmt.Sprint(isLoop))
z.buildSignParams(¶ms)
url := z.tradeURL + "borrow?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
// {"code":1001,"message":"您的资金安全密码已经被锁定,请等待24个小时后会自动解锁。"}
return
}
// AutoBorrow 自动借款
// amount: 借入金额,小数位数不能超过4,超过系统自动截取前4位
// interestRateOfDay: 日利率 [0.05-0.2] 百分比,小数位数不能超过3,超过系统自动截取前3位
// repaymentDay: 借款期限 [10/20/30]天
// safePwd: 资金安全密码
func (z *ZB) AutoBorrow(marketName string, coin string, amount float64, interestRateOfDay float64, repaymentDay int, safePwd string) (result GeneralResult, err error) {
params := url.Values{}
params.Set("method", "autoBorrow")
params.Set("marketName", marketName)
params.Set("coin", coin)
params.Set("amount", fmt.Sprint(amount))
params.Set("interestRateOfDay", fmt.Sprint(interestRateOfDay))
params.Set("repaymentDay", fmt.Sprint(repaymentDay))
params.Set("safePwd", safePwd)
z.buildSignParams(¶ms)
url := z.tradeURL + "autoBorrow?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
// {"code":1001,"message":"您的资金安全密码已经被锁定,请等待24个小时后会自动解锁。"}
return
}
// Repay 还款
// loanRecordId: 借贷记录id
// repayAmount: 还款金额
// repayType: 还款方式 [0/1](全部/部分)
func (z *ZB) Repay(loanRecordId string, repayAmount float64, repayType int) (result GeneralResult, err error) {
params := url.Values{}
params.Set("method", "repay")
params.Set("loanRecordId", loanRecordId)
params.Set("repayAmount", fmt.Sprint(repayAmount))
params.Set("repayType", fmt.Sprint(repayType))
z.buildSignParams(¶ms)
url := z.tradeURL + "repay?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
return
}
// DoAllRepay 一键还款
// marketName: 杠杆市场,示例:btcqc
func (z *ZB) DoAllRepay(marketName string) (result GeneralResult, err error) {
params := url.Values{}
params.Set("method", "doAllRepay")
params.Set("marketName", marketName)
z.buildSignParams(¶ms)
url := z.tradeURL + "doAllRepay?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
return
}
// GetRepayments 获取还款记录
// loanRecordId: 借贷记录id
func (z *ZB) GetRepayments(loanRecordId string, pageIndex int, pageSize int) (result []Repayment, err error) {
params := url.Values{}
params.Set("method", "getRepayments")
params.Set("loanRecordId", loanRecordId)
params.Set("pageIndex", fmt.Sprint(pageIndex))
params.Set("pageSize", fmt.Sprint(pageSize))
z.buildSignParams(¶ms)
url := z.tradeURL + "getRepayments?" + params.Encode()
var ret RepaymentsResponse
_, err = z.HttpGet(url, "", nil, &ret)
if err != nil {
return
}
result = ret.Result
return
}