-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinancep2p.go
119 lines (104 loc) · 2.83 KB
/
binancep2p.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
package marketdata
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type binanceP2P struct {
domURL string
//historyURL string
}
type BinanceP2PConfig struct {
Fiat string // fiat currency ticker
Asset string // asset curency ticker
Page int // amount of pages
Rows int // amount of rows on each page
Countries []string // Countries for fiat currency
PayTypes []string // Payment types
}
func BinanceP2P() binanceP2P {
return binanceP2P{
domURL: "https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search",
}
}
//####################################################################
// Depth Of Market
//####################################################################
func (g binanceP2P) GetDOM(config BinanceP2PConfig) (DOM, error) {
get := func(body []byte) ([]DOMPosition, error) {
responseBody := bytes.NewBuffer(body)
resp, err := http.Post(g.domURL, "application/json", responseBody)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request returned bad status code %s", resp.Status)
}
respBody, _ := io.ReadAll(resp.Body)
respBytes := []byte(respBody)
var model binanceP2PDOMResponse
err = json.Unmarshal(respBytes, &model)
if err != nil {
return nil, err
}
return model.toPositions(), err
}
bidsPostBody, _ := json.Marshal(map[string]any{
"fiat": strings.ToUpper(config.Fiat),
"asset": strings.ToUpper(config.Asset),
"page": config.Page,
"rows": config.Rows,
"tradeType": "SELL",
"payTypes": config.PayTypes,
"countries": config.Countries,
"proMerchantAds": false,
"shieldMerchantAds": false,
"publisherType": nil,
})
asksPostBody, _ := json.Marshal(map[string]any{
"fiat": strings.ToUpper(config.Fiat),
"asset": strings.ToUpper(config.Asset),
"page": config.Page,
"rows": config.Rows,
"tradeType": "BUY",
"payTypes": config.PayTypes,
"countries": config.Countries,
"proMerchantAds": false,
"shieldMerchantAds": false,
"publisherType": nil,
})
bids, err := get(bidsPostBody)
if err != nil {
return DOM{}, err
}
asks, err := get(asksPostBody)
if err != nil {
return DOM{}, err
}
return DOM{
Date: time.Now().UTC(),
Bids: bids,
Asks: asks,
}, nil
}
// binanceP2PDOM responce
type binanceP2PDOMResponse struct {
Data []struct {
Adv struct {
Price string `json:"price"`
Amount string `json:"tradableQuantity"`
} `json:"adv"`
} `json:"data"`
}
func (r binanceP2PDOMResponse) toPositions() []DOMPosition {
result := make([]DOMPosition, 0, len(r.Data))
for _, p := range r.Data {
result = append(result, DOMPosition(p.Adv))
}
return result
}