70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2024-2025 Тарабанов Александр Викторович
|
|
* All rights reserved.
|
|
*
|
|
* This software is proprietary and confidential.
|
|
* Unauthorized copying, modification, or distribution is prohibited.
|
|
*
|
|
* For licensing inquiries: info@hb3-accelerator.com
|
|
* Website: https://hb3-accelerator.com
|
|
* GitHub: https://github.com/HB3-ACCELERATOR
|
|
*/
|
|
|
|
const encryptedDb = require('./encryptedDatabaseService');
|
|
|
|
async function getAllRpcProviders() {
|
|
const providers = await encryptedDb.getData('rpc_providers', {}, null, 'id');
|
|
return providers;
|
|
}
|
|
|
|
async function saveAllRpcProviders(rpcConfigs) {
|
|
// Удаляем все существующие провайдеры
|
|
await encryptedDb.deleteData('rpc_providers', {});
|
|
|
|
// Сохраняем новые провайдеры
|
|
for (const cfg of rpcConfigs) {
|
|
await encryptedDb.saveData('rpc_providers', {
|
|
network_id: cfg.networkId,
|
|
rpc_url: cfg.rpcUrl,
|
|
chain_id: cfg.chainId || null
|
|
});
|
|
}
|
|
}
|
|
|
|
async function upsertRpcProvider(cfg) {
|
|
// Проверяем, существует ли провайдер
|
|
const existing = await encryptedDb.getData('rpc_providers', { network_id: cfg.networkId }, 1);
|
|
|
|
if (existing.length > 0) {
|
|
// Обновляем существующий провайдер
|
|
await encryptedDb.saveData('rpc_providers', {
|
|
rpc_url: cfg.rpcUrl,
|
|
chain_id: cfg.chainId || null
|
|
}, {
|
|
network_id: cfg.networkId
|
|
});
|
|
} else {
|
|
// Создаем новый провайдер
|
|
await encryptedDb.saveData('rpc_providers', {
|
|
network_id: cfg.networkId,
|
|
rpc_url: cfg.rpcUrl,
|
|
chain_id: cfg.chainId || null
|
|
});
|
|
}
|
|
}
|
|
|
|
async function deleteRpcProvider(networkId) {
|
|
await encryptedDb.deleteData('rpc_providers', { network_id: networkId });
|
|
}
|
|
|
|
async function getRpcUrlByNetworkId(networkId) {
|
|
const providers = await encryptedDb.getData('rpc_providers', { network_id: networkId }, 1);
|
|
return providers[0]?.rpc_url || null;
|
|
}
|
|
|
|
async function getRpcUrlByChainId(chainId) {
|
|
const providers = await encryptedDb.getData('rpc_providers', { chain_id: chainId }, 1);
|
|
return providers[0]?.rpc_url || null;
|
|
}
|
|
|
|
module.exports = { getAllRpcProviders, saveAllRpcProviders, upsertRpcProvider, deleteRpcProvider, getRpcUrlByNetworkId, getRpcUrlByChainId };
|