-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance.js
149 lines (136 loc) · 4.14 KB
/
binance.js
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
const { config } = require("./config")
const binanceClient = require("node-binance-api")
const account = new binanceClient().options({
APIKEY: config.APIKEY,
APISECRET: config.APISECRET,
reconnect: true,
useServerTime: true
})
let markets = []
let pairsInfo = []
let pairs = []
/**
* Connects to the Binance API and fetches exchange info and trading pairs.
*/
let initExchangeInfo = async () => {
try {
let data = await account.exchangeInfo()
if (data.symbols) {
for (let obj of data.symbols) {
if(obj.status!="TRADING") {
continue
}
if(!pairs.includes(obj.baseAsset)) {
pairs.push(obj.baseAsset)
} else if(!pairs.includes(obj.quoteAsset)) {
pairs.push(obj.quoteAsset)
}
let filters = { status: obj.status }
for (let filter of obj.filters) {
if (filter.filterType == "MIN_NOTIONAL") {
filters.minNotional = filter.minNotional
} else if (filter.filterType == "PRICE_FILTER") {
filters.minPrice = filter.minPrice
filters.maxPrice = filter.maxPrice
filters.tickSize = filter.tickSize
} else if (filter.filterType == "LOT_SIZE") {
filters.stepSize = filter.stepSize
filters.minQty = filter.minQty
filters.maxQty = filter.maxQty
}
}
filters.orderTypes = obj.orderTypes
pairsInfo[obj.symbol] = {}
pairsInfo[obj.symbol].status = obj.status
pairsInfo[obj.symbol].tokenName = obj.baseAsset
pairsInfo[obj.symbol].marketName = obj.quoteAsset
pairsInfo[obj.symbol].filters = filters
pairsInfo[obj.symbol].trades = []
pairsInfo[obj.symbol].ticks = []
pairsInfo[obj.symbol].bollinger = []
}
}
for (let pair in pairsInfo) {
markets.push(pair)
}
return
} catch (e) {
console.log(e)
return e
}
}
let ifMarketExists = (base, converted) => {
for(let pair in pairsInfo) {
if(pairsInfo[pair].tokenName == base && pairsInfo[pair].marketName == converted) {
return true
} else if(pairsInfo[pair].tokenName == converted && pairsInfo[pair].marketName ==base) {
return true
}
}
return false
}
let getTradingPairs = (askedPair) => {
let array = []
for(let pair in pairsInfo) {
if(pairsInfo[pair].tokenName == askedPair || pairsInfo[pair].marketName == askedPair) {
array.push({tokenName: pairsInfo[pair].tokenName, marketName: pairsInfo[pair].marketName})
}
}
return array
}
let matchTradingPairs = (base, converted) => {
let arr = []
for(let b of base) {
for(let c of converted) {
if(b.marketName == c.marketName || b.marketName == c.tokenName) {
arr.push([b, c])
}
}
}
return arr
}
/**
* Only supports up to 3 step conversion
* @param {string} ba base asset
* @param {string} co converted asset
*/
let getConversionPath = (ba,co) => {
// If pairs cannot convert directly
if(!binanceEngine.ifMarketExists(ba,co)) {
let base = binanceEngine.getTradingPairs(ba)
let converted = binanceEngine.getTradingPairs(co)
let match = binanceEngine.matchTradingPairs(base,converted)
let integratedMatch = match
// If pairs cannot convert within two step
if(match.length==0) {
integratedMatch = []
for(let b of base) {
if(binanceEngine.ifMarketExists(b.marketName, co)) {
integratedMatch.push([{tokenName: ba, marketName:b.marketName}, {tokenName: b.marketName, marketName: co}])
continue
}
let newBase = binanceEngine.getTradingPairs(b.marketName)
let newMatch = binanceEngine.matchTradingPairs(newBase, converted)
if(newMatch.length) {
for(let i=0; i<newMatch.length; i++) {
newMatch[i].unshift({tokenName: ba, marketName: b.marketName})
}
integratedMatch.push(newMatch[0])
}
}
}
return integratedMatch
}
return [[{tokenName: ba, marketName: co}]]
}
const binanceEngine = {
markets,
pairsInfo,
pairs,
initExchangeInfo,
ifMarketExists,
getTradingPairs,
matchTradingPairs,
getConversionPath
}
module.exports = binanceEngine