From b49bd33a93b29e2537cd12d52f74832a131ba9e3 Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 7 Nov 2024 22:31:18 +0300 Subject: [PATCH] feat: CoinAPI cryptocurrency collection has been removed --- collect-currency/main.js | 4 +- collect-currency/save_crypto.js | 89 --------------------------------- config.example.yaml | 10 +--- docs/config/coinapi-keys.md | 5 -- docs/config/config-yaml.md | 23 +-------- mkdocs.yml | 1 - 6 files changed, 4 insertions(+), 128 deletions(-) delete mode 100644 collect-currency/save_crypto.js delete mode 100644 docs/config/coinapi-keys.md diff --git a/collect-currency/main.js b/collect-currency/main.js index 495433c..156736d 100644 --- a/collect-currency/main.js +++ b/collect-currency/main.js @@ -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() { diff --git a/collect-currency/save_crypto.js b/collect-currency/save_crypto.js deleted file mode 100644 index 68296b4..0000000 --- a/collect-currency/save_crypto.js +++ /dev/null @@ -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; diff --git a/config.example.yaml b/config.example.yaml index 155f301..706a06d 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,5 +1,5 @@ # For more information, see the documentation -# https://github.com/Redume/Kekkai/wiki +# https://kekkai-docs.redume.su/ database: # Postgresql database data, for connection user: 'DATABASE_USERNAME' @@ -26,7 +26,6 @@ currency: save: false # Enable or disable saving graphs to an image (Boolean) collecting: fiat: true # Turn off or turn on the collection of the fiat currency rate [Boolean] - crypto: false # Turn off or turn on the collection of the cryptocurrency rate [Boolean] schedule: '30 8 * * *' # Currency collection schedule in crontab format [String] fiat: # List of fiat currency to save the exchange rate [Array] - USD @@ -35,10 +34,3 @@ currency: - UAH - TRY - KZT - crypto: # List of cryptocurrency to save the exchange rate [Array] - - BTC - - ETH - coinapiKeys: # List of keys for the coinapi API [Array] - - CryptoKey1 - - CryptoKey2 - - CryptoKey3 \ No newline at end of file diff --git a/docs/config/coinapi-keys.md b/docs/config/coinapi-keys.md deleted file mode 100644 index 2a19f4c..0000000 --- a/docs/config/coinapi-keys.md +++ /dev/null @@ -1,5 +0,0 @@ -- Go to [coinapi.io](https://www.coinapi.io/get-free-api-key?product_id=market-data-api) website -- Enter any name and your current email, where the api keys will be sent -- In the `What products are you interested in?` section, you can click from one to four items. I recommend clicking all four, because this affects the number of API keys -- Check the email you specified. If there is no letter, then check your spam. You should receive as many keys as you specified in the `What products are you interested in?` -- Keys can be specified in the config `currency.coinapiKeys` \ No newline at end of file diff --git a/docs/config/config-yaml.md b/docs/config/config-yaml.md index 4108fdd..6af0418 100644 --- a/docs/config/config-yaml.md +++ b/docs/config/config-yaml.md @@ -30,7 +30,6 @@ Kekkai can be configured using the `config.yaml` file in the working directory. save: false collecting: fiat: true - crypto: false schedule: '30 8 * * *' fiat: - USD @@ -39,13 +38,6 @@ Kekkai can be configured using the `config.yaml` file in the working directory. - UAH - TRY - KZT - crypto: - - BTC - - ETH - coinapiKeys: # List of keys for the coinapi API [Array] - - CryptoKey1 - - CryptoKey2 - - CryptoKey3 ``` ## Database @@ -99,7 +91,7 @@ Kekkai uses [`Plausbile`](https://plausible.io/) as an analyst. Minimal data is - `work`: Enable or disable analytics. ## Currency -`DuckDuckGo` (fiat currency collection) and `CoinAPI` (cryptocurrency collection) are used to collect currency rates. +`DuckDuckGo` (fiat currency collection) is used to collect currency rates. ??? note ```yaml @@ -109,7 +101,6 @@ Kekkai uses [`Plausbile`](https://plausible.io/) as an analyst. Minimal data is save: false collecting: fiat: true - crypto: false schedule: '30 8 * * *' fiat: - USD @@ -118,19 +109,9 @@ Kekkai uses [`Plausbile`](https://plausible.io/) as an analyst. Minimal data is - UAH - TRY - KZT - crypto: - - BTC - - ETH - coinapiKeys: - - CryptoKey1 - - CryptoKey2 - - CryptoKey3 - ... ``` - `currency.chart.save`: Enable or disable saving graphs. - `currency.collecting`: Enable or disable cryptocurrency or fiat currency exchange rate collection. - `currency.schedule`: Currency collection interval (Configurable via cron. It is recommended to use [crontab.guru](https://crontab.guru), not supported in `Non-standard format`, like `@daily`). -- `currency.fiat`: A list of fiat currencies that will be saved to the database. -- `currency.crypto`: A list of cryptocurrencies that will be saved to the database. -- `currency.coinapiKeys`: List of API keys of the coinapi service \ No newline at end of file +- `currency.fiat`: A list of fiat currencies that will be saved to the database. \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index c5d5a0e..c4386fa 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -15,7 +15,6 @@ nav: - Manual: getting-started/manual.md - Contributing: getting-started/contributing.md - Config: - - How to create API keys for coinapi: config/coinapi-keys.md - Configure config.yaml: config/config-yaml.md - Configure .env: config/config-env.md - Configure nginx.conf: config/conf-nginx.md