-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ls
More file actions
77 lines (66 loc) · 2.18 KB
/
server.ls
File metadata and controls
77 lines (66 loc) · 2.18 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
65
66
67
68
69
70
71
72
73
74
require! {
\superagent : { get }
\fs : { read-file-sync, write-file-sync, exists-sync }
\express
\./template.json
\fetch-base64
\cors
\greenlock-express : ge
}
get-file = (name, cb)->
data = read-file-sync name, \utf8
obj = JSON.parse data
cb null, obj
save-file = (name, data, cb)->
str = JSON.stringify data
write-file-sync name, str
cb null
get-cached = (url, cb)->
name = \./cache/ + url.replace(/\//g, '_')
return get-file name, cb if exists-sync name
err, data <- get url .end
return cb err if err?
err <- save-file name, data.body
return cb err if err?
cb null, data.body
base = "https://api.coingecko.com/api/v3"
get-token = (contract_address, cb)->
err, data <- get-cached "#{base}/coins/ethereum/contract/#{contract_address}"
info = { ...template }
image <- fetch-base64.remote data.image.small .then
info.image = image.1
info.token = data.symbol
SYMBOL = data.symbol.to-upper-case!
usd-url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=#{SYMBOL}&tsyms=USD"
err, data <- get-cached usd-url
info.usd-info =
| not err? => "url(#{usd-url}).#{SYMBOL}.USD"
| _ => "0.01"
info.mainnet.address = contract_address
err, token-info <- get-cached "http://api.ethplorer.io/getTokenInfo/#{contract_address}?apiKey=freekey"
return cb err if err?
info.mainnet.decimals = +token-info.decimals
cb null, info
get-cached-token = (contract_address, cb)->
name = \./cache/ + contract_address
return get-file name, cb if exists-sync name
err, data <- get-token contract_address
return cb err if err?
err <- save-file name, data
return cb err if err?
cb null, data
app = express!
app.use cors!
app.get \/token/:contract_address , (req, res)->
err, data <- get-cached-token req.params.contract_address #"0x83984d6142934bb535793a82adb0a46ef0f66b6d"
return res.status(400).send("#err") if err?
res.send data
require(\greenlock-express).create({
email: \some@gmail.com
agreeTos: yes
configDir: \./.ssl/
communityMember: yes
telemetry: no
app: app
debug: true
}).listen(80, 443)