Files
DLE/backend/scripts/generate-flattened.js
2026-03-01 22:03:48 +03:00

106 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Copyright (c) 2024-2026 Тарабанов Александр Викторович
* 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
*/
/**
* Автоматическая генерация flattened контракта для верификации
* Создает DLE_flattened.sol из DLE.sol с помощью hardhat flatten
*/
const { spawn, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Пути к файлам
const contractsDir = path.join(__dirname, '../contracts');
const dleContractPath = path.join(contractsDir, 'DLE.sol');
const flattenedPath = path.join(contractsDir, 'DLE_flattened.sol');
// Функция для генерации flattened контракта
function generateFlattened() {
return new Promise((resolve, reject) => {
console.log('🔨 Генерация flattened контракта...');
// Проверяем существование исходного контракта
if (!fs.existsSync(dleContractPath)) {
reject(new Error(`Исходный контракт не найден: ${dleContractPath}`));
return;
}
// Запускаем hardhat flatten с перенаправлением в файл
try {
console.log('🔨 Выполняем hardhat flatten...');
execSync(`npx hardhat flatten contracts/DLE.sol > "${flattenedPath}"`, {
cwd: path.join(__dirname, '..'),
shell: true
});
// Проверяем, что файл создался
if (fs.existsSync(flattenedPath)) {
const stats = fs.statSync(flattenedPath);
console.log('✅ Flattened контракт создан:', flattenedPath);
console.log(`📊 Размер файла: ${(stats.size / 1024).toFixed(2)} KB`);
resolve();
} else {
reject(new Error('Файл не был создан'));
}
} catch (error) {
console.error('❌ Ошибка выполнения hardhat flatten:', error.message);
reject(new Error(`Ошибка flatten: ${error.message}`));
}
});
}
// Функция для проверки актуальности
function checkFlattenedFreshness() {
if (!fs.existsSync(flattenedPath)) {
return false;
}
if (!fs.existsSync(dleContractPath)) {
return false;
}
const flattenedStats = fs.statSync(flattenedPath);
const contractStats = fs.statSync(dleContractPath);
// Flattened файл старше контракта
return flattenedStats.mtime >= contractStats.mtime;
}
// Основная функция
async function main() {
try {
console.log('🔍 Проверка актуальности flattened контракта...');
const isFresh = checkFlattenedFreshness();
if (isFresh) {
console.log('✅ Flattened контракт актуален');
return;
}
console.log('🔄 Flattened контракт устарел, генерируем новый...');
await generateFlattened();
} catch (error) {
console.error('❌ Ошибка генерации flattened контракта:', error.message);
process.exit(1);
}
}
// Запуск если вызван напрямую
if (require.main === module) {
main();
}
module.exports = { generateFlattened, checkFlattenedFreshness };