-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.33 KB
/
index.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
const axios = require('axios')
const express = require('express')
const app = express()
const port = 5000
async function getBTCPrice() {
try {
let response = await axios({
method: 'get',
url: 'https://testnet-api.smartbit.com.au/v1/exchange-rates'
})
let price = response.data['exchange_rates'].filter(function(rate){ return rate['code'] == 'USD'})
return price[0]['rate']
} catch (e) {
console.log(e)
}
}
async function getBTCWallet(){
let wallet = '3P3QsMVK89JBNqZQv5zMAKG8FK3kJM4rjt'
let smartbitURL = 'https://api.smartbit.com.au/v1/blockchain/address/' + wallet
try {
let response = await axios({
method: 'get',
url: smartbitURL
})
let walletBalance = parseFloat(response.data['address']['total']['balance'])
return walletBalance
} catch (e) {
console.log(e)
}
}
app.get('/balance', async function (req, res) {
try {
let [ walletBalance, btcPrice ] = await Promise.all([
getBTCWallet(), getBTCPrice()
])
let balance = (walletBalance * btcPrice).toFixed(2)
const response = {
statusCode: 200,
body: balance
}
res.send(response)
} catch (err) {
const response = {
statusCode: 500,
body: err
}
res.send(response)
}
})
console.log("App is running on ", port);
app.listen(process.env.PORT || port)