2024-08-01 00:42:17 +03:00
|
|
|
const config = require('../config/main.js')();
|
|
|
|
const axios = require('axios');
|
|
|
|
const pool = require('../database/postgresql.js');
|
|
|
|
|
2024-08-04 13:55:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves exchange rate of the cryptocurrency
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2024-08-01 00:42:17 +03:00
|
|
|
function save_fiat() {
|
|
|
|
if (!config['currency']['collecting']['crypto']) return;
|
2024-08-02 21:52:16 +03:00
|
|
|
const depth = config['currency']['coinapiKeys']
|
2024-08-01 00:42:17 +03:00
|
|
|
|
|
|
|
if (depth <= 0) new Error('Rate limit on all API tokens');
|
|
|
|
|
|
|
|
config['currency']['crypto'].forEach(
|
|
|
|
(value) => config['currency']['crypto'].forEach(async (pair) => {
|
|
|
|
if (value !== pair) {
|
2024-08-02 21:52:16 +03:00
|
|
|
const coinapiKey = [0];
|
2024-08-01 00:42:17 +03:00
|
|
|
const res = await axios.get(`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`, {
|
|
|
|
timeout: 3000,
|
|
|
|
headers: {
|
2024-08-02 21:52:16 +03:00
|
|
|
'X-CoinAPI-Key': config['currency']['coinapiKeys'][coinapiKey[0]],
|
2024-08-01 00:42:17 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-02 21:52:16 +03:00
|
|
|
if (res.status === 429) rotate_key(config['currency']['coinapiKeys'], coinapiKey);
|
2024-08-01 00:42:17 +03:00
|
|
|
|
|
|
|
const data = res.data;
|
|
|
|
const point = data['rate'].toString().indexOf('.') + 4;
|
|
|
|
|
|
|
|
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
|
|
|
[
|
|
|
|
value,
|
|
|
|
pair,
|
|
|
|
new Date(data['time']).toLocaleDateString()
|
|
|
|
]).then(async (db) => {
|
|
|
|
if (db['rows'][0]) return;
|
|
|
|
|
|
|
|
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date)
|
|
|
|
VALUES ($1, $2, $3, $4) RETURNING *`,
|
|
|
|
[
|
|
|
|
value,
|
|
|
|
pair,
|
|
|
|
data['rate'].toString().slice(0, point),
|
|
|
|
new Date(data['time']).toLocaleDateString()
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-04 13:55:37 +03:00
|
|
|
/**
|
|
|
|
* Changing API keys
|
|
|
|
* @param {Array} list - List of all keys
|
|
|
|
* @param active - Active API key
|
|
|
|
* @returns {number} - Outputs the number of the key that should work
|
|
|
|
*/
|
|
|
|
|
2024-08-01 00:42:17 +03:00
|
|
|
function rotate_key(list, active) {
|
|
|
|
return active[0] = (active + 1) % list.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = save_fiat;
|