File poolpair.service.ts
has 410 lines of code (exceeds 250 allowed). Consider refactoring. Open
import { Inject, Injectable, NotFoundException } from '@nestjs/common'
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import BigNumber from 'bignumber.js'
import { PoolPairInfo } from '@defichain/jellyfish-api-core/dist/category/poolpair'
import { SemaphoreCache } from '@defichain-apps/libs/caches'
PoolPairService
has 22 functions (exceeds 20 allowed). Consider refactoring. Open
@Injectable()
export class PoolPairService {
constructor (
@Inject('NETWORK') protected readonly network: NetworkName,
private readonly poolSwapPathfindingService: PoolSwapPathFindingService,
Function checkSwapType
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
public async checkSwapType (swap: PoolSwapData): Promise<SwapType | undefined> {
const dftx = await this.findCompositeSwapDfTx(swap.txid)
// if dftx is undefined, no composite swap is returned so check for swap
if (dftx === undefined || dftx.pools.length <= 1) {
const poolPairInfo = await this.deFiDCache.getPoolPairInfo(swap.poolPairId)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getTokenUSDValue
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
private async getTokenUSDValue (id: string): Promise<BigNumber | undefined> {
return await this.cache.get<BigNumber>(`PRICE_FOR_TOKEN_${id}`, async () => {
const tokenInfo = await this.deFiDCache.getTokenInfo(id)
const token = tokenInfo?.symbol
Function getUSDVolume
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
public async getUSDVolume (id: string): Promise<PoolPairData['volume'] | undefined> {
return await this.cache.get<PoolPairData['volume']>(`POOLPAIR_VOLUME_${id}`, async () => {
const gatherAmount = async (interval: PoolSwapAggregatedInterval, count: number): Promise<number> => {
const aggregated: Record<string, number> = {}
const swaps = await this.poolSwapAggregatedMapper.query(`${id}-${interval as number}`, count)
Function getUSD_PER_DFI
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
async getUSD_PER_DFI (): Promise<BigNumber | undefined> {
return await this.cache.get<BigNumber>('USD_PER_DFI', async () => {
const usdt = await this.getPoolPair('DFI', 'USDT')
const usdc = await this.getPoolPair('DFI', 'USDC')
// const dusd = await this.getPoolPair('DFI', 'DUSD')
Function getPoolPair
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
private async getPoolPair (a: string, b: string): Promise<PoolPairInfo | undefined> {
try {
const result = await this.rpcClient.poolpair.getPoolPair(`${a}-${b}`, true)
if (Object.values(result).length > 0) {
return Object.values(result)[0]
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function findPoolSwapFromTo
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
function findPoolSwapFromTo (history: AccountHistory | undefined, from: boolean, displaySymbol: string): PoolSwapFromToData | undefined {
if (history?.amounts === undefined) {
return undefined
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this function. Open
return await this.getTotalLiquidityUsdByBestPath(info)
Avoid too many return
statements within this function. Open
return info.reserveB.multipliedBy(2).multipliedBy(USDT_PER_DFI)
Avoid too many return
statements within this function. Open
return dfiPair.reserveB.div(dfiPair.reserveA).times(usdPerDFI)
Avoid too many return
statements within this function. Open
return loanEmission.multipliedBy(split)
.times(60 * 60 * 24 / 30) // 30 seconds = 1 block
.times(365) // 1 year
.times(dfiPriceUSD)
Similar blocks of code found in 2 locations. Consider refactoring. Open
try {
const result = await this.rpcClient.poolpair.getPoolPair(`${a}-${b}`, true)
if (Object.values(result).length > 0) {
return Object.values(result)[0]
}
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 118.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 2 locations. Consider refactoring. Open
try {
const result = await this.rpcClient.poolpair.getPoolPair(`${b}-${a}`, true)
if (Object.values(result).length > 0) {
return Object.values(result)[0]
}
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 118.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76