Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(whale-api): Fix swap tx not found index #2170

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Check warning on line 23 in apps/whale-api/src/module.indexer/model/dftx/pool.pair.path.mapping.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.indexer/model/dftx/pool.pair.path.mapping.ts#L22-L23

Added lines #L22 - L23 were not covered by tests
}
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)
}
}