Skip to content

Commit

Permalink
fix(whale-api): Fix swap tx not found index (#2170)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:

Fix for random bug swap tx not found in indexer
> context: related to client.listpoolpairs (cache/fresh) pull

- replace IndexError by Log
- minimise the debugging scope from (com)swap tx to findPair utils
- retry findPair if undefined


#### Which issue(s) does this PR fixes?:
<!--
(Optional) Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

#### Additional comments?:

---------

Co-authored-by: Lyka Labrada <[email protected]>
  • Loading branch information
canonbrother and lykalabrada authored Nov 14, 2023
1 parent dd01b74 commit 97c28a9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { RawBlock } from '../_abstract'
import { Inject, Injectable } from '@nestjs/common'
import { NetworkName } from '@defichain/jellyfish-network'
import BigNumber from 'bignumber.js'
import { IndexerError } from '../../error'
import { PoolPairPathMapping } from './pool.pair.path.mapping'
import { PoolSwapIndexer } from './pool.swap'

Expand Down Expand Up @@ -51,10 +50,6 @@ export class CompositeSwapIndexer extends DfTxIndexer<CompositeSwap> {

const poolSwap = compositeSwap.poolSwap
const pair = await this.poolPairPathMapping.findPair(poolSwap.fromTokenId, poolSwap.toTokenId)
if (pair !== undefined) {
return [{ id: Number(pair.id) }]
}

throw new IndexerError(`Pool for pair ${poolSwap.fromTokenId}, ${poolSwap.toTokenId} not found in PoolPairPathMapping`)
return [{ id: Number(pair.id) }]
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { DeFiDCache, PoolPairInfoWithId } from '../../../module.api/cache/defid.cache'

@Injectable()
export class PoolPairPathMapping {
private readonly logger = new Logger(PoolPairPathMapping.name)
private readonly paths: Record<string, PoolPairInfoWithId> = {}

constructor (
protected readonly deFiDCache: DeFiDCache
) {
}

async findPair (tokenA: number, tokenB: number): Promise<PoolPairInfoWithId | undefined> {
async findPair (tokenA: number, tokenB: number): Promise<PoolPairInfoWithId> {
const pair = this.paths[`${tokenA}-${tokenB}`]
if (pair !== undefined) {
return pair
}

await this.updateMapping()
if (this.paths[`${tokenA}-${tokenB}`] === undefined) {
this.logger.error(`Pool for pair ${tokenA}, ${tokenB} not found in PoolPairPathMapping`)
await this.findPair(tokenA, tokenB)
}
return this.paths[`${tokenA}-${tokenB}`]
}

Expand Down
8 changes: 1 addition & 7 deletions apps/whale-api/src/module.indexer/model/dftx/pool.swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { HexEncoder } from '../../../module.model/_hex.encoder'
import { PoolSwapAggregatedMapper } from '../../../module.model/pool.swap.aggregated'
import { AggregatedIntervals } from './pool.swap.aggregated'
import { PoolPairInfoWithId } from '../../../module.api/cache/defid.cache'
import { IndexerError } from '../../error'
import { PoolPairPathMapping } from './pool.pair.path.mapping'

@Injectable()
Expand Down Expand Up @@ -89,11 +88,6 @@ export class PoolSwapIndexer extends DfTxIndexer<PoolSwap> {
}

async getPair (tokenA: number, tokenB: number): Promise<PoolPairInfoWithId> {
const pair = await this.poolPairPathMapping.findPair(tokenA, tokenB)
if (pair !== undefined) {
return pair
}

throw new IndexerError(`Pool for pair ${tokenA}, ${tokenB} not found in PoolPairPathMapping`)
return await this.poolPairPathMapping.findPair(tokenA, tokenB)
}
}

0 comments on commit 97c28a9

Please sign in to comment.