Skip to content

Commit

Permalink
feat: add api for fee unlock config
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-TanHoang committed Oct 15, 2024
1 parent 99737db commit 5922fb8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 14 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.6'

services:
postgres:
container_name: mina-bridge-${NODE_ENV}-postgres
Expand Down
2 changes: 0 additions & 2 deletions src/constants/env.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ 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',
COINMARKET_URL = 'COINMARKET_URL',
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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AddFeeTableCommonConfig1728965288359 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
return queryRunner.dropColumns('common_configuration', ['fee_unlock_mina', 'fee_unlock_eth']);
}
}
2 changes: 1 addition & 1 deletion src/modules/crawler/crawler.evmbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/crawler/crawler.minabridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventLog> = {
senderAddress: JSON.parse(JSON.stringify(event.event.data.locker)),
Expand Down
12 changes: 12 additions & 0 deletions src/modules/crawler/entities/common-config.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions src/modules/users/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/modules/users/dto/common-config-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 8 additions & 5 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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 },
}),
Expand All @@ -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<GetTokensPriceResponseDto> {
const result = {
Expand Down

0 comments on commit 5922fb8

Please sign in to comment.