feat: CoinAPI cryptocurrency collection has been removed
Some checks are pending
Create and publish a Docker image / build-and-push-server (push) Waiting to run
Create and publish a Docker image / build-and-push-chart (push) Waiting to run
Create and publish a Docker image / build-and-push-CR (push) Waiting to run
Deploy docs / deploy (push) Waiting to run

This commit is contained in:
Данил 2024-11-07 22:31:18 +03:00
parent 526d185048
commit b49bd33a93
6 changed files with 4 additions and 128 deletions

View file

@ -3,8 +3,6 @@ const config = require('../shared/config/src/main.js')();
const cron = require('cron-validator');
const save_fiat = require('./save_fiat');
const save_crypto = require('./save_crypto');
const logger = require('../shared/logger/src/main.js');
async function validateSchedule(schedule) {
@ -18,7 +16,7 @@ async function initialize() {
}
async function runTasks() {
await Promise.all([save_fiat(), save_crypto()]);
await Promise.all([save_fiat()]);
}
async function main() {

View file

@ -1,89 +0,0 @@
const config = require('../shared/config/src/main.js')();
const axios = require('axios');
const pool = require('../shared/database/src/postgresql.js');
const logger = require('../shared/logger/src/main.js');
const coinapiKeys = config['currency']['coinapiKeys'];
let apiKeyIndex = 0;
let depth = coinapiKeys.length;
function save_crypto() {
if (!config['currency']['collecting']['crypto']) return;
if (coinapiKeys[apiKeyIndex] === undefined) return;
if (depth <= 0) {
logger.info('Rate limit on all coinapi API keys');
new Error('Rate limit on all coinapi API keys');
return;
}
logger.info(
`Active coinapi key: ${coinapiKeys[apiKeyIndex]} (${coinapiKeys.length - 1} / ${apiKeyIndex})`,
);
config['currency']['crypto'].forEach((value) =>
config['currency']['crypto'].forEach((pair) => {
if (value === pair) return;
axios
.get(
`https://rest.coinapi.io/v1/exchangerate/${value}/${pair}`,
{
timeout: 3000,
headers: {
'X-CoinAPI-Key': coinapiKeys[apiKeyIndex],
},
},
)
.then(async (res) => {
const data = res.data;
const point = data['rate'].toString().indexOf('.') + 4;
logger.debug(JSON.stringify(data));
const db = await pool.query(
'SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
[
value,
pair,
new Date(data['time']).toLocaleDateString(),
],
);
if (db['rows'][0]) return;
await pool.query(
`INSERT INTO currency (from_currency, conv_currency, rate, date)
VALUES ($1, $2, $3, $4)`,
[
value,
pair,
data['rate'].toString().slice(0, point),
new Date(data['time']).toLocaleDateString(),
],
);
})
.catch((err) => {
if (err.response?.data.detail)
logger.error(err.response.data.detail);
if (err.response?.data.status === 429) {
logger.info('CoinAPI rate limited, rotating token');
rotate_key(coinapiKeys);
depth--;
save_crypto();
}
});
}),
);
}
/**
* Changing API keys
* @param {Array} list - List of all keys
* @returns {number} - Outputs the number of the key that should work
*/
function rotate_key(list) {
apiKeyIndex = list.indexOf(coinapiKeys[apiKeyIndex]) + 1;
}
module.exports = save_crypto;