forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pools-helper.ts
58 lines (50 loc) · 1.52 KB
/
pools-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { LoggerConstructor, PoolLiquidity, Logger, Address } from './types';
import { DexAdapterService } from './dex';
export class PoolsHelper {
logger: Logger;
constructor(
protected dexAdapterService: DexAdapterService,
loggerConstructor: LoggerConstructor,
) {
this.logger = loggerConstructor(`PoolsHelper_${dexAdapterService.network}`);
}
public getAllDexKeys(): string[] {
return this.dexAdapterService.getAllDexKeys();
}
private async getTopPoolsDex(
tokenAddress: Address,
dexKey: string,
count: number,
): Promise<PoolLiquidity[]> {
try {
const dex = this.dexAdapterService.getDexByKey(dexKey);
return await dex.getTopPoolsForToken(tokenAddress, count);
} catch (e) {
this.logger.error(`getTopPools_${dexKey}`, e);
return [];
}
}
async updateDexPoolState(dexKey: string) {
try {
const dexInstance = this.dexAdapterService.getDexByKey(dexKey);
if (!dexInstance.updatePoolState) return;
return await dexInstance.updatePoolState();
} catch (e) {
this.logger.error('Error_updateDexPoolState:', e);
}
}
async updateAllPoolState(dexKeys: string[]) {
return await Promise.all(dexKeys.map(key => this.updateDexPoolState(key)));
}
public async getTopPools(
tokenAddress: Address,
dexKeys: string[],
countPerDex: number,
): Promise<PoolLiquidity[]> {
return (
await Promise.all(
dexKeys.map(key => this.getTopPoolsDex(tokenAddress, key, countPerDex)),
)
).flat();
}
}