From 5922fb823d2a965000811dd6e1984d39191b38bc Mon Sep 17 00:00:00 2001 From: "tan.hoang" Date: Tue, 15 Oct 2024 11:22:16 +0700 Subject: [PATCH] feat: add api for fee unlock config --- docker-compose.yml | 2 -- src/constants/env.constant.ts | 2 -- .../1728965288359-add-fee-table-common-config.ts | 14 ++++++++++++++ src/modules/crawler/crawler.evmbridge.ts | 2 +- src/modules/crawler/crawler.minabridge.ts | 4 ++-- .../crawler/entities/common-config.entity.ts | 12 ++++++++++++ src/modules/users/admin.controller.ts | 5 +++-- src/modules/users/dto/common-config-request.dto.ts | 12 ++++++++++++ src/modules/users/users.service.ts | 13 ++++++++----- 9 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/database/migrations/1728965288359-add-fee-table-common-config.ts diff --git a/docker-compose.yml b/docker-compose.yml index 1017c9c..3077ae7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.6' - services: postgres: container_name: mina-bridge-${NODE_ENV}-postgres diff --git a/src/constants/env.constant.ts b/src/constants/env.constant.ts index ae227b5..9d7f3ed 100644 --- a/src/constants/env.constant.ts +++ b/src/constants/env.constant.ts @@ -33,7 +33,6 @@ export enum EEnvKey { SIGNER_PRIVATE_KEY = 'SIGNER_PRIVATE_KEY', NUMBER_OF_BLOCK_PER_JOB = 'NUMBER_OF_BLOCK_PER_JOB', ADMIN_MESSAGE_FOR_SIGN = 'ADMIN_MESSAGE_FOR_SIGN', - GASFEEMINA = 'GASFEEMINA', SIGNER_MINA_PRIVATE_KEY = 'SIGNER_MINA_PRIVATE_KEY', DECIMAL_TOKEN_MINA = 'DECIMAL_TOKEN_MINA', COINMARKET_KEY = 'COINMARKET_KEY', @@ -41,7 +40,6 @@ export enum EEnvKey { BASE_MINA_BRIDGE_FEE = 'BASE_MINA_BRIDGE_FEE', ETH_BRIDGE_DOMAIN_NAME = 'ETH_BRIDGE_DOMAIN_NAME', ETH_BRIDGE_DOMAIN_VERSION = 'ETH_BRIDGE_DOMAIN_VERSION', - GAS_FEE_EVM = 'GAS_FEE_EVM', DECIMAL_TOKEN_EVM = 'DECIMAL_TOKEN_EVM', MINA_VALIDATOR_THRESHHOLD = 'MINA_VALIDATOR_THRESHHOLD', EVM_VALIDATOR_THRESHHOLD = 'EVM_VALIDATOR_THRESHHOLD', diff --git a/src/database/migrations/1728965288359-add-fee-table-common-config.ts b/src/database/migrations/1728965288359-add-fee-table-common-config.ts new file mode 100644 index 0000000..19814b7 --- /dev/null +++ b/src/database/migrations/1728965288359-add-fee-table-common-config.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'; + +export class AddFeeTableCommonConfig1728965288359 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + return queryRunner.addColumns('common_configuration', [ + new TableColumn({ name: 'fee_unlock_mina', type: 'varchar', default: '0' }), + new TableColumn({ name: 'fee_unlock_eth', type: 'varchar', default: '0' }), + ]); + } + + public async down(queryRunner: QueryRunner): Promise { + return queryRunner.dropColumns('common_configuration', ['fee_unlock_mina', 'fee_unlock_eth']); + } +} diff --git a/src/modules/crawler/crawler.evmbridge.ts b/src/modules/crawler/crawler.evmbridge.ts index 2f91a04..a672738 100644 --- a/src/modules/crawler/crawler.evmbridge.ts +++ b/src/modules/crawler/crawler.evmbridge.ts @@ -94,7 +94,7 @@ export class BlockchainEVMCrawler { fromDecimal: fromTokenDecimal, toDecimal: toTokenDecimal, inputAmountNoDecimalPlaces: inputAmount, - gasFeeWithDecimalPlaces: this.configService.get(EEnvKey.GASFEEMINA)!, + gasFeeWithDecimalPlaces: config.feeUnlockMina, tipPercent: +config!.tip, }); if (error) { diff --git a/src/modules/crawler/crawler.minabridge.ts b/src/modules/crawler/crawler.minabridge.ts index 5877990..b69d5a0 100644 --- a/src/modules/crawler/crawler.minabridge.ts +++ b/src/modules/crawler/crawler.minabridge.ts @@ -143,8 +143,8 @@ export class SCBridgeMinaCrawler { fromDecimal: fromTokenDecimal, toDecimal: toTokenDecimal, inputAmountNoDecimalPlaces: inputAmount, - gasFeeWithDecimalPlaces: this.configService.get(EEnvKey.GAS_FEE_EVM)!, - tipPercent: +config!.tip, + gasFeeWithDecimalPlaces: config.feeUnlockEth, + tipPercent: Number(config.tip).valueOf(), }); const eventUnlock: Partial = { senderAddress: JSON.parse(JSON.stringify(event.event.data.locker)), diff --git a/src/modules/crawler/entities/common-config.entity.ts b/src/modules/crawler/entities/common-config.entity.ts index 986a613..3f8ae92 100644 --- a/src/modules/crawler/entities/common-config.entity.ts +++ b/src/modules/crawler/entities/common-config.entity.ts @@ -25,6 +25,18 @@ export class CommonConfig extends BaseEntityIncludeTime { }) tip: number; + @Column({ + name: 'fee_unlock_mina', + type: 'varchar', + }) + feeUnlockMina: string; + + @Column({ + name: 'fee_unlock_eth', + type: 'varchar', + }) + feeUnlockEth: string; + @Column({ name: 'asset', type: 'varchar', nullable: true }) asset: string; diff --git a/src/modules/users/admin.controller.ts b/src/modules/users/admin.controller.ts index b1bb1ac..82338a4 100644 --- a/src/modules/users/admin.controller.ts +++ b/src/modules/users/admin.controller.ts @@ -2,6 +2,7 @@ import { Body, Controller, Get, Param, Put, Query, UseGuards } from '@nestjs/com import { AuthGuard } from '@nestjs/passport'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; +import { GuardPublic } from '../../guards/guard.decorator.js'; import { AuthAdminGuard } from '../../shared/decorators/http.decorator.js'; import { UpdateCommonConfigBodyDto } from './dto/common-config-request.dto.js'; import { GetCommonConfigResponseDto } from './dto/common-config-response.dto.js'; @@ -22,8 +23,8 @@ export class AdminController { } @Get('common-config') - @AuthAdminGuard() - @UseGuards(AuthGuard('jwt')) + @GuardPublic() + // @UseGuards(AuthGuard('jwt')) @ApiOkResponse({ type: GetCommonConfigResponseDto }) getCommonConfig() { return this.userService.getCommonConfig(); diff --git a/src/modules/users/dto/common-config-request.dto.ts b/src/modules/users/dto/common-config-request.dto.ts index edb6978..fbbd300 100644 --- a/src/modules/users/dto/common-config-request.dto.ts +++ b/src/modules/users/dto/common-config-request.dto.ts @@ -12,4 +12,16 @@ export class UpdateCommonConfigBodyDto { required: false, }) dailyQuota: number; + + @NumberField({ + example: 500, + required: false, + }) + feeUnlockMina: string; + + @NumberField({ + example: 500, + required: false, + }) + feeUnlockEth: string; } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 38fcfc2..6c64bfb 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +import assert from 'assert'; import { DataSource } from 'typeorm'; import { EAsset } from '../../constants/api.constant.js'; @@ -57,7 +58,8 @@ export class UsersService { } async updateCommonConfig(id: number, updateConfig: UpdateCommonConfigBodyDto) { - return this.commonConfigRepository.updateCommonConfig(id, updateConfig); + await this.commonConfigRepository.updateCommonConfig(id, updateConfig); + return updateConfig; } async getDailyQuotaOfUser(address: string) { @@ -75,7 +77,7 @@ export class UsersService { async getProtocolFee({ pairId }: GetProtocolFeeBodyDto) { let gasFee, decimal; - const [tokenPair, configTip] = await Promise.all([ + const [tokenPair, config] = await Promise.all([ this.dataSource.getRepository(TokenPair).findOne({ where: { id: pairId }, }), @@ -84,15 +86,16 @@ export class UsersService { if (!tokenPair) { httpNotFound(EError.RESOURCE_NOT_FOUND); } + assert(config, 'system common config not found!'); if (tokenPair!.toChain == ENetworkName.MINA) { decimal = this.configService.get(EEnvKey.DECIMAL_TOKEN_MINA); - gasFee = addDecimal(this.configService.get(EEnvKey.GASFEEMINA)!, decimal); + gasFee = addDecimal(config.feeUnlockMina, decimal); } else { decimal = this.configService.get(EEnvKey.DECIMAL_TOKEN_EVM); - gasFee = addDecimal(this.configService.get(EEnvKey.GAS_FEE_EVM)!, decimal); + gasFee = addDecimal(config.feeUnlockMina, decimal); } - return { gasFee, tipRate: configTip!.tip, decimal }; + return { gasFee, tipRate: config.tip, decimal }; } async getTokensPrices(): Promise { const result = {