feat: новая функция
This commit is contained in:
@@ -54,7 +54,7 @@ router.post('/read-dle-info', async (req, res) => {
|
||||
if (!rpcUrl) {
|
||||
return res.status(500).json({ success: false, error: `RPC URL для сети ${targetChainId} не найден` });
|
||||
}
|
||||
provider = new ethers.JsonRpcProvider(await rpcProviderService.getRpcUrlByChainId(chainId));
|
||||
provider = new ethers.JsonRpcProvider(rpcUrl);
|
||||
const code = await provider.getCode(dleAddress);
|
||||
if (!code || code === '0x') {
|
||||
return res.status(400).json({ success: false, error: `По адресу ${dleAddress} нет контракта в сети ${targetChainId}` });
|
||||
|
||||
@@ -52,11 +52,70 @@ const aiAssistantSettingsService = require('../services/aiAssistantSettingsServi
|
||||
const aiAssistantRulesService = require('../services/aiAssistantRulesService');
|
||||
const botsSettings = require('../services/botsSettings');
|
||||
const dbSettingsService = require('../services/dbSettingsService');
|
||||
const footerDleService = require('../services/footerDleService');
|
||||
const { broadcastAuthTokenAdded, broadcastAuthTokenDeleted, broadcastAuthTokenUpdated } = require('../wsHub');
|
||||
|
||||
// Логируем версию ethers для отладки
|
||||
logger.info(`Ethers version: ${ethers.version || 'unknown'}`);
|
||||
|
||||
// === FOOTER DLE SELECTION ===================================================
|
||||
|
||||
router.get('/footer-dle', async (req, res) => {
|
||||
try {
|
||||
const selection = await footerDleService.getFooterSelection();
|
||||
res.json({ success: true, data: selection });
|
||||
} catch (error) {
|
||||
logger.error('[Settings] Ошибка при получении footer DLE:', error);
|
||||
res.status(500).json({ success: false, error: 'Не удалось получить выбранный DLE для футера' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/footer-dle', requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const { dleAddress, chainId } = req.body || {};
|
||||
|
||||
if (!dleAddress) {
|
||||
return res.status(400).json({ success: false, error: 'Необходимо указать адрес DLE' });
|
||||
}
|
||||
|
||||
if (!ethers.isAddress(dleAddress)) {
|
||||
return res.status(400).json({ success: false, error: 'Указан некорректный адрес DLE' });
|
||||
}
|
||||
|
||||
let normalizedChainId = null;
|
||||
if (chainId !== undefined && chainId !== null && chainId !== '') {
|
||||
const parsed = Number(chainId);
|
||||
if (!Number.isFinite(parsed)) {
|
||||
return res.status(400).json({ success: false, error: 'Некорректный chainId' });
|
||||
}
|
||||
normalizedChainId = parsed;
|
||||
}
|
||||
|
||||
const updatedBy = req.session?.address || req.session?.userId || null;
|
||||
const selection = await footerDleService.setFooterSelection({
|
||||
address: ethers.getAddress(dleAddress),
|
||||
chainId: normalizedChainId,
|
||||
updatedBy,
|
||||
});
|
||||
|
||||
res.json({ success: true, data: selection });
|
||||
} catch (error) {
|
||||
logger.error('[Settings] Ошибка при сохранении footer DLE:', error);
|
||||
res.status(500).json({ success: false, error: 'Не удалось сохранить выбранный DLE для футера' });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/footer-dle', requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const updatedBy = req.session?.address || req.session?.userId || null;
|
||||
const selection = await footerDleService.clearFooterSelection(updatedBy);
|
||||
res.json({ success: true, data: selection });
|
||||
} catch (error) {
|
||||
logger.error('[Settings] Ошибка при очистке footer DLE:', error);
|
||||
res.status(500).json({ success: false, error: 'Не удалось очистить выбранный DLE для футера' });
|
||||
}
|
||||
});
|
||||
|
||||
// Получение RPC настроек
|
||||
router.get('/rpc', async (req, res, next) => {
|
||||
try {
|
||||
|
||||
83
backend/services/footerDleService.js
Normal file
83
backend/services/footerDleService.js
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* 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/VC-HB3-Accelerator
|
||||
*/
|
||||
|
||||
const logger = require('../utils/logger');
|
||||
const { getSecret, setSecret } = require('./secretStore');
|
||||
|
||||
const FOOTER_DLE_KEY = 'FOOTER_DLE_SELECTION';
|
||||
|
||||
function parseSelection(rawValue) {
|
||||
if (!rawValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof rawValue === 'object') {
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
if (typeof rawValue === 'string') {
|
||||
try {
|
||||
return JSON.parse(rawValue);
|
||||
} catch (error) {
|
||||
logger.warn('[FooterDleService] Не удалось распарсить сохраненное значение footer DLE:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getFooterSelection() {
|
||||
const storedValue = await getSecret(FOOTER_DLE_KEY);
|
||||
const parsed = parseSelection(storedValue);
|
||||
|
||||
if (!parsed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
address: parsed.address || null,
|
||||
chainId: parsed.chainId ?? null,
|
||||
updatedAt: parsed.updatedAt || null,
|
||||
updatedBy: parsed.updatedBy || null,
|
||||
};
|
||||
}
|
||||
|
||||
async function setFooterSelection({ address, chainId = null, updatedBy = null }) {
|
||||
const payload = {
|
||||
address,
|
||||
chainId: chainId !== undefined && chainId !== null ? Number(chainId) : null,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: updatedBy || null,
|
||||
};
|
||||
|
||||
await setSecret(FOOTER_DLE_KEY, JSON.stringify(payload));
|
||||
return payload;
|
||||
}
|
||||
|
||||
async function clearFooterSelection(updatedBy = null) {
|
||||
const payload = {
|
||||
address: null,
|
||||
chainId: null,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updatedBy: updatedBy || null,
|
||||
};
|
||||
|
||||
await setSecret(FOOTER_DLE_KEY, JSON.stringify(payload));
|
||||
return payload;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFooterSelection,
|
||||
setFooterSelection,
|
||||
clearFooterSelection,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user