From 6d0c6cc2c5adf8b8dd7200be1679cf5d8e9e51aa Mon Sep 17 00:00:00 2001 From: Neal <1107554021@qq.com> Date: Tue, 25 Jun 2024 16:54:30 +0800 Subject: [PATCH] fix: missing ERC data --- src/app.controller.ts | 14 ++--- src/app.module.ts | 6 +- src/app.service.ts | 142 +++++++++++++++++++++++++----------------- 3 files changed, 96 insertions(+), 66 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 7ae18f7..2a03490 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -52,7 +52,7 @@ class EIPsFilters { export class AppController { private readonly logger = new Logger('App'); - constructor(private readonly appService: AppService) {} + constructor(private readonly appService: AppService) { } @Get('/eips/search') @ApiOperation({ description: 'Search EIPs.' }) @@ -90,12 +90,12 @@ export class AppController { }; } - // @Get('/eips/update') - // @ApiOperation({ description: 'Updata Eips.' }) - // async updateAllEips() { - // const result = await this.appService.updateEips(); - // return { data: result }; - // } + @Get('/eips/update') + @ApiOperation({ description: 'Updata Eips.' }) + async updateAllEips() { + const result = await this.appService.updateEips(); + return { data: result }; + } @Get('/ercs/download') @ApiOperation({ description: 'download Ecs.' }) diff --git a/src/app.module.ts b/src/app.module.ts index 6b25084..2478e48 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -14,9 +14,9 @@ import { TypeOrmModule } from '@nestjs/typeorm'; TypeOrmModule.forRoot({ type: 'postgres', url: process.env.DATABASE_URL, - ssl: { - rejectUnauthorized: false, - }, + // ssl: { + // rejectUnauthorized: false, + // }, }), ], controllers: [AppController], diff --git a/src/app.service.ts b/src/app.service.ts index 3ceaa80..4a9ef02 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -248,6 +248,26 @@ export class AppService { return result; } + async downloadEips() { + const paths = './ETH-EIPS/'; + deleteFolder(paths); + return new Promise(function (res, rej) { + download( + 'direct:https://github.com/ethereum/EIPs.git', + 'ETH-EIPs', + { clone: true }, + function (err) { + if (err) { + rej(err); + } else { + res('download EIP success!'); + console.log('download EIP success!'); + } + }, + ); + }); + } + async downloadErcs() { const paths = './ETH-ERCS/'; deleteFolder(paths); @@ -261,7 +281,7 @@ export class AppService { rej(err); } else { res('download ERC success!'); - console.log('下载完成'); + console.log('download ERC success!'); } }, ); @@ -271,67 +291,77 @@ export class AppService { async updateEips() { // eslint-disable-next-line @typescript-eslint/no-this-alias const that = this; - const paths = './ETH-EIPs/'; - console.log('开始清理文件夹'); - deleteFolder(paths); - console.log('清理文件夹成功'); await this.downloadErcs(); - download( - 'direct:https://github.com/ethereum/EIPs.git', - 'ETH-EIPs', - { clone: true }, - (err) => { - console.log(err ? '拉取Error' : '拉取Success'); - if (!err) { - const writeData = []; - const directory = './ETH-EIPs/EIPS/'; - fs.readdir(directory, (err, files) => { + await this.downloadEips(); + + const writeData = []; + const directory = './ETH-EIPs/EIPS/'; + // erc + const getArr = async (path: string, type: 'eip' | 'erc') => { + let files; + try { + files = await fs.promises.readdir(path); + } catch (err) { + console.error(err); + return []; + } + return files.map((f) => f.replace(type + '-', '')); + }; + + const eipArr = await getArr('./ETH-EIPs/EIPS/', 'eip'); + const ercArr = await getArr('./ETH-ERCS/ERCS/', 'erc'); + const ercNew = ercArr.filter((item) => !eipArr.includes(item)); + ercNew.shift(); + ercNew.forEach(async (item) => { + let fileInfo; + try { + fileInfo = await fs.promises.readFile( + path.join('./ETH-ERCS/ERCS/', `erc-${item}`), + 'utf8', + ); + const ercData = this.formateData(fileInfo); + writeData.push(ercData); + } catch (err) { + console.error(err); + return; + } + }); + + fs.readdir(directory, async (err, files) => { + if (err) { + console.error(err); + return; + } + (async function getFileMeta(i) { + if (i === files.length) { + console.log('解析EIPS文件完成'); + await that.saveData(writeData); + console.log('写入DB完成'); + } else { + fs.readFile(path.join(directory, files[i]), 'utf8', (err, data) => { if (err) { - console.error(err); + console.log('读取文件失败', err); return; } - (async function getFileMeta(i) { - console.log(i, files.length); - - if (i === files.length) { - console.log('解析EIPS文件完成'); - await that.saveData(writeData); - console.log('写入DB完成'); - } else { - fs.readFile( - path.join(directory, files[i]), - 'utf8', - (err, data) => { - if (err) { - console.log('读取文件失败', err); - return; - } - const result = that.formateData(data); - if (result.status !== 'Moved') { - writeData.push(result); - } else { - const ercDirectory = './ETH-ERCS/ERCS/'; - const id = files[i].split('-')[1]; - const fsInfo = fs.readFileSync( - path.join(ercDirectory, `erc-${id}`), - 'utf8', - ); - const ercData = that.formateData(fsInfo); - writeData.push(ercData); - } - - getFileMeta(i + 1); - }, - ); - } - })(0); + const result = that.formateData(data); + if (result.status !== 'Moved') { + writeData.push(result); + } else { + const ercDirectory = './ETH-ERCS/ERCS/'; + const id = files[i].split('-')[1]; + const fsInfo = fs.readFileSync( + path.join(ercDirectory, `erc-${id}`), + 'utf8', + ); + const ercData = that.formateData(fsInfo); + writeData.push(ercData); + } + + getFileMeta(i + 1); }); - } else { - // console.log(err); - deleteFolder(paths); } - }, - ); + })(0); + }); } async sendEmail() {