-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
64 lines (50 loc) · 1.86 KB
/
handler.js
File metadata and controls
64 lines (50 loc) · 1.86 KB
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
import ccxt from 'ccxt'
export const getWalletBalance = async (event, context, callback) => {
const pathParams = event.params ? event.params.path : {}
const { exchange, coin } = pathParams
if (exchange && coin && ccxt.exchanges.indexOf(exchange) > -1) {
const response = await get(exchange, coin)
callback(null, response)
}
callback(null, null)
}
const get = async (exchange, coin) => {
try {
const exchangeObj = instantiateExchange(exchange)
if (exchangeObj) {
let balance = await exchangeObj.fetchBalance()
if (balance.total) {
return balance.total[coin]
}
if (balance[coin]) {
return balance[coin]
}
return balance
}
return null
} catch (e) {
if (e instanceof ccxt.DDoSProtection || e.message.includes ('ECONNRESET')) {
console.log('[DDoS Protection] ' + e.message)
} else if (e instanceof ccxt.RequestTimeout) {
console.log('[Request Timeout] ' + e.message)
} else if (e instanceof ccxt.AuthenticationError) {
console.log('[Authentication Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeNotAvailable) {
console.log('[Exchange Not Available Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeError) {
console.log('[Exchange Error] ' + e.message)
} else if (e instanceof ccxt.NetworkError) {
console.log('[Network Error] ' + e.message)
} else {
throw e;
}
}
}
const instantiateExchange = (exchange) => {
const apiKeyName = `${exchange}_api_key`
const secretKeyName = `${exchange}_secret_key`
return new ccxt[exchange]({
'apiKey': process.env[apiKeyName],
'secret': process.env[secretKeyName],
})
}