Compare commits
No commits in common. "6125f99ec6fb9442c4f743fc3c6b084ba468573b" and "c0701775c7821950ca201764d2f2a53e5d443f29" have entirely different histories.
6125f99ec6
...
c0701775c7
7 changed files with 21 additions and 94 deletions
|
@ -5,7 +5,7 @@
|
|||
name: DATABASE_NAME
|
||||
password: DATABASE_PASSWORD
|
||||
}
|
||||
currency: {
|
||||
currecy: {
|
||||
collecting: {
|
||||
fiat: true
|
||||
crypto: false
|
||||
|
@ -27,9 +27,6 @@
|
|||
USDT,
|
||||
TON
|
||||
]
|
||||
api_keys: {
|
||||
coinmarketcap: TOKEN_COINMARKETCAP
|
||||
}
|
||||
}
|
||||
schedule: 30 8 * * *
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
const pg = require('pg');
|
||||
const fs = require('fs');
|
||||
const config = require('../utils/load_config.js')();
|
||||
const hjson = require('hjson');
|
||||
|
||||
const config = hjson.parse(fs.readFileSync('config.hjson', 'utf-8'));
|
||||
|
||||
const pool = new pg.Pool({
|
||||
user: config['database']['user'],
|
||||
|
|
19
main.js
19
main.js
|
@ -1,11 +1,12 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const hjson = require('hjson');
|
||||
const schedule = require('node-schedule');
|
||||
const cron = require('cron-validator');
|
||||
|
||||
const { validateCurrency } = require('./models/Currency.js');
|
||||
const { create_table, pool } = require('./database/data.js');
|
||||
const config = require('./utils/load_config.js')();
|
||||
const config = hjson.parse(fs.readFileSync('config.hjson', 'utf-8'));
|
||||
|
||||
async function main() {
|
||||
if (!config['schedule'])
|
||||
|
@ -33,28 +34,28 @@ async function main() {
|
|||
console.log('Running scheduled task at:', new Date());
|
||||
|
||||
for (const srv of services) {
|
||||
const results = await srv.parseCurrencies();
|
||||
try {
|
||||
const result = await srv.parseCurrencies();
|
||||
|
||||
if (Array.isArray(results) && results.length > 0) {
|
||||
for (const result of results) {
|
||||
if (result) {
|
||||
try {
|
||||
const currency = await validateCurrency(result);
|
||||
|
||||
await pool.query(
|
||||
'INSERT INTO currency (from_currency, conv_currency, rate, date) VALUES ($1, $2, $3, $4)',
|
||||
'INSERT INTO currency (from_currency, conv_currency, rate, date) ' +
|
||||
'VALUES ($1, $2, $3, $4)',
|
||||
[
|
||||
currency.from_currency,
|
||||
currency.conv_currency,
|
||||
currency.rate,
|
||||
currency.date,
|
||||
]
|
||||
);
|
||||
]);
|
||||
} catch (validationError) {
|
||||
console.error(validationError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error("Data not received for writing to the database.");
|
||||
} catch (err) {
|
||||
console.error(`Error in service ${srv.name || 'unknown'}:`, err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
const Joi = require('joi');
|
||||
|
||||
const currencySchema = Joi.object({
|
||||
from_currency: Joi.string().min(3).max(4).required().messages({
|
||||
from_currency: Joi.string().length(3).required().messages({
|
||||
'string.base': 'from_currency must be a string',
|
||||
'string.min': 'from_currency must be at least 3 characters long',
|
||||
'string.max': 'from_currency must be no more than 4 characters long',
|
||||
'string.length': 'from_currency must be exactly 3 characters long',
|
||||
'any.required': 'from_currency is required'
|
||||
}),
|
||||
conv_currency: Joi.string().min(3).max(4).required().messages({
|
||||
conv_currency: Joi.string().length(3).required().messages({
|
||||
'string.base': 'conv_currency must be a string',
|
||||
'string.min': 'conv_currency must be at least 3 characters long',
|
||||
'string.max': 'conv_currency must be no more than 4 characters long',
|
||||
'string.length': 'conv_currency must be exactly 3 characters long',
|
||||
'any.required': 'conv_currency is required'
|
||||
}),
|
||||
date: Joi.date().iso().required().messages({
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
const axios = require('axios');
|
||||
const config = require('../utils/load_config.js')();
|
||||
const { truncate_number } = require('../utils/truncate_number.js');
|
||||
|
||||
module.exports = {
|
||||
parseCurrencies: async () => {
|
||||
const promises = config['currency']['crypto'].map(fromCurrency => {
|
||||
return config['currency']['crypto'].map(convCurrency => {
|
||||
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
||||
|
||||
return axios.get(
|
||||
'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
|
||||
{
|
||||
params: {
|
||||
symbol: fromCurrency,
|
||||
convert: convCurrency,
|
||||
},
|
||||
headers: {
|
||||
'X-CMC_PRO_API_KEY': config['currency']['api_keys']['coinmarketcap'],
|
||||
}
|
||||
}
|
||||
)
|
||||
.then((res) => {
|
||||
const data = res.data.data[fromCurrency].quote[convCurrency];
|
||||
const truncatedPriceStr = truncate_number(data.price, 3);
|
||||
const rate = parseFloat(truncatedPriceStr);
|
||||
|
||||
return {
|
||||
from_currency: fromCurrency,
|
||||
conv_currency: convCurrency,
|
||||
rate: rate,
|
||||
date: new Date(data['last_updated']).toISOString().substring(0, 10),
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err.respone.data);
|
||||
return null;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const flattenedPromises = promises.flat();
|
||||
const results = await Promise.all(flattenedPromises);
|
||||
|
||||
return results.filter(result => result !== null);
|
||||
}
|
||||
};
|
|
@ -1,10 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const hjson = require('hjson');
|
||||
|
||||
const config = () => {
|
||||
if (!fs.existsSync('../config.hjson')) throw new Error('Config not found');
|
||||
|
||||
return hjson.parse(fs.readFileSync('../config.hjson', 'utf-8'));
|
||||
}
|
||||
|
||||
module.exports = config;
|
|
@ -1,14 +0,0 @@
|
|||
function truncate_number(value, decimals) {
|
||||
const valueStr = value.toString();
|
||||
const dotIndex = valueStr.indexOf('.');
|
||||
if (dotIndex === -1) return valueStr;
|
||||
const desiredLength = dotIndex + decimals + 1;
|
||||
let truncated = valueStr.slice(0, desiredLength);
|
||||
|
||||
if (parseFloat(truncated) === 0 && value > 0) {
|
||||
return valueStr;
|
||||
}
|
||||
return truncated;
|
||||
}
|
||||
|
||||
module.exports = { truncate_number };
|
Loading…
Add table
Reference in a new issue