forked from perich/ethereum-dex-prices-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
97 lines (89 loc) · 3.05 KB
/
handler.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
const { main } = require('./main')
const { TOP_TOKENS_DECIMAL_MAP } = require('./constants')
const makeResponse = (response, origin = null) => ({
statusCode: 200,
body: JSON.stringify(response),
headers: {
'Access-Control-Allow-Origin': origin || '*',
'Access-Control-Allow-Credentials': true,
'Content-Type': 'application/json',
'Cache-Control': 'max-age=0',
},
})
module.exports = {
hello: (event, context, callback) => {
console.log('-----------------------')
console.log(event)
console.log('-----------------------')
console.log(context)
console.log('-----------------------')
callback(null, makeResponse('hello world'))
},
buy: (event, context, callback) => {
try {
const { queryStringParameters } = event
if (!queryStringParameters) throw new Error('must include query strings')
const { amount, symbol, decimals } = queryStringParameters
if (!amount || !symbol) throw new Error('must include "amount" and "symbol" parameters')
main(symbol, amount, 'BUY', decimals || null).then(sortedResponses => {
callback(null, makeResponse(sortedResponses))
})
} catch (error) {
callback(error, null)
}
},
sell: (event, context, callback) => {
try {
const { queryStringParameters } = event
if (!queryStringParameters) throw new Error('must include query strings')
const { amount, symbol, decimals } = queryStringParameters
if (!amount || !symbol) throw new Error('must include "amount" and "symbol" parameters')
main(symbol, amount, 'SELL', decimals || null).then(sortedResponses => {
callback(null, makeResponse(sortedResponses))
})
} catch (error) {
callback(error, null)
}
},
buyPriceSnapshot: async (event, context, callback) => {
const results = {}
Object.keys(TOP_TOKENS_DECIMAL_MAP).forEach(symbol => {
results[symbol] = {}
})
const doWorkForPriceLevel = priceLevel => {
const tokens = [...Object.entries(TOP_TOKENS_DECIMAL_MAP)]
return new Promise(resolve => {
async function workLoop(level) {
const tuple = tokens.pop()
if (!tuple) {
resolve(results)
return
}
console.log(tokens.length)
const [token, data] = tuple
try {
const sortedResponses = await main(token, data.levels[level], 'BUY', data.decimals)
results[token][data.levels[level]] = sortedResponses
} catch (error) {
results[token][data.levels[level]] = error
}
// sleep for 3s to avoid api rate limits
setTimeout(() => {
workLoop(level)
}, 3000)
}
workLoop(priceLevel)
})
}
// don't use Promise.all.. we don't want these to run concurrently
// calls are intentionally staggered to avoid API rate limits
try {
await doWorkForPriceLevel(0)
await doWorkForPriceLevel(1)
await doWorkForPriceLevel(2)
callback(null, results)
} catch (error) {
callback(error, null)
}
},
}