-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprices.js
37 lines (33 loc) · 1.12 KB
/
prices.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
const CronJob = require('cron').CronJob;
const axios = require('axios');
const mongoose = require('mongoose');
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to MongoDB');
});
// Schema and model for prices
const priceSchema = new mongoose.Schema({
price: Number,
timestamp: Number,
});
const Price = mongoose.model('Price', priceSchema);
// cron job to fetch price every 10 minutes
new CronJob('0 */10 * * * *', async function() {
try {
const response = await axios.get(
'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=inr'
);
const price = response.data.ethereum.inr;
const timestamp = new Date().getTime();
const savedPrice = await Price.create({ price, timestamp });
console.log(`Saved price: ${savedPrice.price} at ${new Date(savedPrice.timestamp)}`);
} catch (error) {
console.error(error);
}
}, null, true, 'Asia/Kolkata');