DeFiCh/jellyfish

View on GitHub
apps/whale-api/src/module.api/poolpair.service.ts

Summary

Maintainability
D
2 days
Test Coverage

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'
Severity: Minor
Found in apps/whale-api/src/module.api/poolpair.service.ts - About 5 hrs to fix

    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,
    Severity: Minor
    Found in apps/whale-api/src/module.api/poolpair.service.ts - About 2 hrs to fix

      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)
      Severity: Minor
      Found in apps/whale-api/src/module.api/poolpair.service.ts - About 2 hrs to fix

      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
      
      
      Severity: Minor
      Found in apps/whale-api/src/module.api/poolpair.service.ts - About 1 hr to fix

        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)
        Severity: Minor
        Found in apps/whale-api/src/module.api/poolpair.service.ts - About 1 hr to fix

          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')
          Severity: Minor
          Found in apps/whale-api/src/module.api/poolpair.service.ts - About 1 hr to fix

            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]
            Severity: Minor
            Found in apps/whale-api/src/module.api/poolpair.service.ts - About 45 mins to fix

            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
              }
            
            
            Severity: Minor
            Found in apps/whale-api/src/module.api/poolpair.service.ts - About 35 mins to fix

            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)
            Severity: Major
            Found in apps/whale-api/src/module.api/poolpair.service.ts - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                    return info.reserveB.multipliedBy(2).multipliedBy(USDT_PER_DFI)
              Severity: Major
              Found in apps/whale-api/src/module.api/poolpair.service.ts - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                        return dfiPair.reserveB.div(dfiPair.reserveA).times(usdPerDFI)
                Severity: Major
                Found in apps/whale-api/src/module.api/poolpair.service.ts - About 30 mins to fix

                  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)
                  Severity: Major
                  Found in apps/whale-api/src/module.api/poolpair.service.ts - About 30 mins to fix

                    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]
                          }
                    Severity: Major
                    Found in apps/whale-api/src/module.api/poolpair.service.ts and 1 other location - About 2 hrs to fix
                    apps/whale-api/src/module.api/poolpair.service.ts on lines 62..71

                    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

                    Further Reading

                    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]
                          }
                    Severity: Major
                    Found in apps/whale-api/src/module.api/poolpair.service.ts and 1 other location - About 2 hrs to fix
                    apps/whale-api/src/module.api/poolpair.service.ts on lines 51..60

                    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

                    Further Reading

                    There are no issues that match your filters.

                    Category
                    Status