-
Notifications
You must be signed in to change notification settings - Fork 11
/
finance_record.go
154 lines (128 loc) · 3.65 KB
/
finance_record.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
package swap
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type (
FinancialRecordRequest struct {
contractCode string
typ []string
startTime int
endTime int
fromID int
direct string
}
FinancialRecord struct {
ID int `json:"id"`
Symbol string `json:"symbol"`
Type int `json:"type"`
Amount float64 `json:"amount"`
TS int64 `json:"ts"`
ContractCode string `json:"contract_code"`
QueryID int64 `json:"query_id"`
}
FinancialRecordResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
TS int64 `json:"ts"`
Data []FinancialRecord `json:"data"`
}
)
const (
FinancialRecordEndPoint = "/swap-api/v3/swap_financial_record"
FinancialRecordTypeFundingIncome = 30
FinancialRecordTypeFundingOutCome = 31
)
func NewFinancialRecordRequest(contractCode string) *FinancialRecordRequest {
return &FinancialRecordRequest{
typ: make([]string, 0),
contractCode: contractCode,
}
}
func (frr *FinancialRecordRequest) Serialize() ([]byte, error) {
param := map[string]interface{}{
"contract": frr.contractCode,
}
if len(frr.typ) != 0 {
param["type"] = strings.Join(frr.typ, ",")
}
if frr.startTime != 0 {
param["start_time"] = frr.startTime
}
if frr.endTime != 0 {
param["end_time"] = frr.endTime
}
if frr.fromID != 0 {
param["from_id"] = frr.fromID
}
if frr.direct != "" {
param["direct"] = frr.direct
}
return json.Marshal(param)
}
func (frr *FinancialRecordRequest) Type(types ...int) *FinancialRecordRequest {
for _, t := range types {
frr.typ = append(frr.typ, strconv.Itoa(t))
}
return frr
}
// StartTime specific record query start timestamp in milliseconds
func (frr *FinancialRecordRequest) StartTime(ts int) *FinancialRecordRequest {
frr.startTime = ts
return frr
}
// EndTime specific record query end timestamp in milliseconds
func (frr *FinancialRecordRequest) EndTime(ts int) *FinancialRecordRequest {
frr.endTime = ts
return frr
}
func (frr *FinancialRecordRequest) FromID(fromID int) *FinancialRecordRequest {
frr.fromID = fromID
return frr
}
func (frr *FinancialRecordRequest) Direct(direct string) *FinancialRecordRequest {
frr.direct = direct
return frr
}
func (cl *RestClient) FinancialRecord(ctx context.Context, req *FinancialRecordRequest) (*FinancialRecordResponse, error) {
var ret FinancialRecordResponse
body, err := req.Serialize()
if err != nil {
return nil, errors.WithMessage(err, "serialzie request fail")
}
if err := cl.RequestWithRawResp(ctx, http.MethodPost, FinancialRecordEndPoint, nil, bytes.NewBuffer(body), true, &ret); err != nil {
return nil, errors.WithMessage(err, "fetch financial record fail")
}
if ret.Code != 200 {
return nil, errors.Errorf("error response code=%d msg=%s", ret.Code, ret.Msg)
}
return &ret, nil
}
// Transform financeRecord to finacial currently only funding type is support
func (fr *FinancialRecord) Transform() (*exchange.Finance, error) {
symbol, err := ParseSymbol(fr.ContractCode)
if err != nil {
return nil, err
}
if fr.Type != FinancialRecordTypeFundingIncome && fr.Type != FinancialRecordTypeFundingOutCome {
return nil, errors.Errorf("unsupport type %d", fr.Type)
}
return &exchange.Finance{
ID: fmt.Sprintf("%d", fr.ID),
Symbol: symbol,
Currency: fr.Symbol,
Amount: decimal.NewFromFloat(fr.Amount),
Type: exchange.FinanceTypeFunding,
Time: huobi.ParseTS(fr.TS),
Raw: fr,
}, nil
}