Skip to content

Commit

Permalink
Wrap balancer fetch top pools with try and catch [TKR-481] (#515)
Browse files Browse the repository at this point in the history
* Wrap balancer fetch top pool with try and catch

* Update CHANGELOG.json
  • Loading branch information
kyu-c committed Jul 12, 2022
1 parent e779584 commit 9821734
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/asset-swapper/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "16.63.1",
"changes": [
{
"note": "Better error handling for balancer cache",
"pr": 515
}
]
},
{
"version": "16.63.0",
"changes": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getPoolsWithTokens, parsePoolData } from 'balancer-labs-sor-v1';
import { Pool } from 'balancer-labs-sor-v1/dist/types';
import { gql, request } from 'graphql-request';

import { DEFAULT_WARNING_LOGGER } from '../../../constants';
import { LogFunction } from '../../../types';
import { BALANCER_MAX_POOLS_FETCHED, BALANCER_SUBGRAPH_URL, BALANCER_TOP_POOLS_FETCHED } from '../constants';

import { CacheValue, PoolsCache } from './pools_cache';
Expand All @@ -24,6 +26,7 @@ export class BalancerPoolsCache extends PoolsCache {
cache: { [key: string]: CacheValue } = {},
private readonly maxPoolsFetched: number = BALANCER_MAX_POOLS_FETCHED,
private readonly _topPoolsFetched: number = BALANCER_TOP_POOLS_FETCHED,
private readonly _warningLogger: LogFunction = DEFAULT_WARNING_LOGGER,
) {
super(cache);
void this._loadTopPoolsAsync();
Expand All @@ -49,7 +52,14 @@ export class BalancerPoolsCache extends PoolsCache {
[from: string]: { [to: string]: Pool[] };
} = {};

const pools = await this._fetchTopPoolsAsync();
let pools: BalancerPoolResponse[];
try {
pools = await this._fetchTopPoolsAsync();
} catch (err) {
this._warningLogger(err, 'Failed to fetch top pools for Balancer V1');
return;
}

for (const pool of pools) {
const { tokensList } = pool;
for (const from of tokensList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ export class BalancerV2PoolsCache extends PoolsCache {
[from: string]: { [to: string]: Pool[] };
} = {};

const pools = await this._fetchTopPoolsAsync();
let pools: BalancerPoolResponse[];
try {
pools = await this._fetchTopPoolsAsync();
} catch (err) {
this._warningLogger(err, 'Failed to fetch top pools for Balancer V2');
return;
}

for (const pool of pools) {
const { tokensList } = pool;
for (const from of tokensList) {
Expand Down

0 comments on commit 9821734

Please sign in to comment.