-
Notifications
You must be signed in to change notification settings - Fork 1
/
pnl.go
111 lines (96 loc) · 3.08 KB
/
pnl.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
package client
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type PNLPayload struct {
Currency string `json:"currency"`
PnlCurrency string `json:"pnl_currency"`
TotalCredit string `json:"total_credit"`
TotalDebit string `json:"total_debit"`
TotalCreditValue string `json:"total_credit_value"`
TotalDebitValue string `json:"total_debit_value"`
AverageBuyPrice string `json:"average_buy_price"`
AverageSellPrice string `json:"average_sell_price"`
AverageBalancePrice string `json:"average_balance_price"`
TotalBalanceValue string `json:"total_balance_value"`
}
/*
{
"status": "success",
"message": "Retrieval Successful",
"payload": [
{
"currency": "btc",
"pnl_currency": "usdc",
"total_credit": "30.2311686",
"total_debit": "8.6779552",
"total_credit_value": "951376.399690608",
"total_debit_value": "263030.247889309",
"average_buy_price": "31470.05040653995757213302",
"average_sell_price": "30310.16429876349211851197",
"average_balance_price": "31446.4028484392969497",
"total_balance_value": "677771.031254780024102"
},
{
"currency": "eth",
"pnl_currency": "usdc",
"total_credit": "30.2311686",
"total_debit": "8.6779552",
"total_credit_value": "951376.399690608",
"total_debit_value": "263030.247889309",
"average_buy_price": "31470.05040653995757213302",
"average_sell_price": "30310.16429876349211851197",
"average_balance_price": "31446.4028484392969497",
"total_balance_value": "677771.031254780024102"
}
]
}
*/
type PNLResponse struct {
Status Status `json:"status"`
Message string `json:"message"`
Payload []PNLPayload `json:"payload"`
}
/*
Retrieve details of a user’s portfolio i.e., the profit and loss incurred in the various markets.
Please note that this is a Private 🔒 route which means it needs to be authorised by the account initiating this request.
*/
func (c *Client) PNL(ctx context.Context) (PNLResponse, error) {
err := c.CheckAuth()
if err != nil {
return PNLResponse{}, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.pnlURL.String(), nil)
if err != nil {
return PNLResponse{}, err
}
req.Header.Set("Authorization", fmt.Sprintf("JWT %s", c.jwtToken))
resp, err := c.httpClient.Do(req)
if err != nil {
return PNLResponse{}, err
}
defer resp.Body.Close()
var pnlResponse PNLResponse
err = json.NewDecoder(resp.Body).Decode(&pnlResponse)
if pnlResponse.Status == ERROR {
// handling 4xx and 5xx errors
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
return PNLResponse{}, &ErrClient{
Status: resp.StatusCode,
Err: fmt.Errorf(pnlResponse.Message),
}
} else if resp.StatusCode >= 500 {
return PNLResponse{}, &ErrServer{
Status: resp.StatusCode,
Err: fmt.Errorf(pnlResponse.Message),
}
}
return PNLResponse{}, fmt.Errorf("status: %d\nerror: %s", resp.StatusCode, pnlResponse.Message)
} else if err != nil {
return PNLResponse{}, &ErrJSONDecoding{Err: err}
}
return pnlResponse, nil
}