Skip to content

Commit

Permalink
Merge pull request #187 from sotatek-dev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Sotatek-TanHoang authored Oct 16, 2024
2 parents 77c7300 + d5a8afb commit d7a7018
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/modules/users/tests/fee.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ENetworkName } from '../../../constants/blockchain.constant.js';
import { CommonConfigRepository } from '../../../database/repositories/common-configuration.repository.js';
import { TokenPairRepository } from '../../../database/repositories/token-pair.repository.js';
import { initModuleTest } from '../../../shared/__test__/base/test.lib.js';
import { addDecimal } from '../../../shared/utils/bignumber.js';
import { UsersService } from '../users.service.js';

describe('estimate unlock/minting fee for users', () => {
let userService: UsersService;
let configRepo: jest.Mocked<CommonConfigRepository>;
let tokenPairRepo: jest.Mocked<TokenPairRepository>;

beforeAll(async () => {
const { unit, unitRef } = await initModuleTest(UsersService);
userService = unit;
configRepo = unitRef.get(CommonConfigRepository);
tokenPairRepo = unitRef.get(TokenPairRepository);

configRepo.getCommonConfig.mockResolvedValue({
id: 1,
feeUnlockEth: '0.001',
feeUnlockMina: '0.002',
} as any);
});
it('fee from mina -> eth', async () => {
tokenPairRepo.findOneBy.mockResolvedValue({
id: 1,
toChain: ENetworkName.ETH,
} as any);
const res = await userService.getProtocolFee({ pairId: 1 });
expect(res.gasFee).toEqual(addDecimal(0.001, 18));
});
it('fee from eth -> mina', async () => {
tokenPairRepo.findOneBy.mockResolvedValue({
id: 1,
toChain: ENetworkName.MINA,
} as any);
const res = await userService.getProtocolFee({ pairId: 1 });
expect(res.gasFee).toEqual(addDecimal(0.002, 9));
});
});
2 changes: 2 additions & 0 deletions src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CustomRepositoryModule } from 'nestjs-typeorm-custom-repository';

import { CommonConfigRepository } from '../../database/repositories/common-configuration.repository.js';
import { EventLogRepository } from '../../database/repositories/event-log.repository.js';
import { TokenPairRepository } from '../../database/repositories/token-pair.repository.js';
import { TokenPriceRepository } from '../../database/repositories/token-price.repository.js';
import { UserRepository } from '../../database/repositories/user.repository.js';
import { AdminController } from './admin.controller.js';
Expand All @@ -16,6 +17,7 @@ import { UsersService } from './users.service.js';
EventLogRepository,
CommonConfigRepository,
TokenPriceRepository,
TokenPairRepository,
]),
],
controllers: [UsersController, AdminController],
Expand Down
11 changes: 5 additions & 6 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +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';
import { ENetworkName } from '../../constants/blockchain.constant.js';
Expand All @@ -10,9 +9,9 @@ import { EError } from '../../constants/error.constant.js';
import { toPageDto } from '../../core/paginate-typeorm.js';
import { CommonConfigRepository } from '../../database/repositories/common-configuration.repository.js';
import { EventLogRepository } from '../../database/repositories/event-log.repository.js';
import { TokenPairRepository } from '../../database/repositories/token-pair.repository.js';
import { TokenPriceRepository } from '../../database/repositories/token-price.repository.js';
import { UserRepository } from '../../database/repositories/user.repository.js';
import { TokenPair } from '../../modules/users/entities/tokenpair.entity.js';
import { httpBadRequest, httpNotFound } from '../../shared/exceptions/http-exeption.js';
import { LoggerService } from '../../shared/modules/logger/logger.service.js';
import { addDecimal } from '../../shared/utils/bignumber.js';
Expand All @@ -27,10 +26,10 @@ export class UsersService {
private readonly usersRepository: UserRepository,
private readonly eventLogRepository: EventLogRepository,
private readonly commonConfigRepository: CommonConfigRepository,
private readonly dataSource: DataSource,
private readonly configService: ConfigService,
private readonly loggerService: LoggerService,
private readonly tokenPriceRepository: TokenPriceRepository,
private readonly tokenPairRepostitory: TokenPairRepository,
) {}
private readonly logger = this.loggerService.getLogger('USER_SERVICE');
async getProfile(userId: number) {
Expand Down Expand Up @@ -72,14 +71,14 @@ export class UsersService {
}

async getListTokenPair() {
return this.dataSource.getRepository(TokenPair).find();
return this.tokenPairRepostitory.find();
}

async getProtocolFee({ pairId }: GetProtocolFeeBodyDto) {
let gasFee, decimal;
const [tokenPair, config] = await Promise.all([
this.dataSource.getRepository(TokenPair).findOne({
where: { id: pairId },
this.tokenPairRepostitory.findOneBy({
id: pairId,
}),
this.commonConfigRepository.getCommonConfig(),
]);
Expand Down

0 comments on commit d7a7018

Please sign in to comment.