Skip to content

Commit

Permalink
feat: Add a refresh method on pools that should efficiently refresh a…
Browse files Browse the repository at this point in the history
… pools SG data.
  • Loading branch information
johngrantuk committed Jul 5, 2024
1 parent f94c6e9 commit c2bf4cc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
38 changes: 36 additions & 2 deletions balancer-js/src/modules/data/pool/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,42 @@ export class PoolsSubgraphRepository
return pools.map((pool) => mapType(pool, this.chainId));
}

async find(id: string): Promise<Pool | undefined> {
return await this.findBy('id', id);
/**
* Find pool data for id
* @param id
* @param refresh If true will refetch from SG and update cache
* @returns
*/
async find(id: string, refresh = false): Promise<Pool | undefined> {
// If we're not refreshing and the pool exists in caches then return
if (!refresh && this.pools) {
const cachedPool = (await this.pools).find((pool) => pool.id === id);
if (cachedPool) return cachedPool;
}

// Fetch pool data from SG, update cache and return
const logger = Logger.getInstance();

// SG fetch
logger.time(`fetching pool ${id}`);
const poolQuery = await this.client.Pool({ id });
logger.timeEnd(`fetching pool ${id}`);

if (!poolQuery.pool) return undefined;

const pool = mapType(poolQuery.pool, this.chainId);
// If the pool is already cached, replace it with the new one
logger.time(`updating cache`);
const pools = await this.pools;
if (pools) {
const index = pools.findIndex((p) => p.address === pool.address);
if (index !== -1) {
this.pools = Promise.resolve([...pools.splice(index, 1), pool]);
} else this.pools = Promise.resolve([...pools, pool]);
} else this.pools = Promise.resolve([pool]);
logger.timeEnd(`updating cache`);

return pool;
}

async findBy(param: PoolAttribute, value: string): Promise<Pool | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion balancer-js/src/modules/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export * from './pool-shares/types';
export * from './gauge-shares/types';

export interface Findable<T, P = string, V = any> {
find: (id: string) => Promise<T | undefined>;
find: (id: string, refresh?: boolean) => Promise<T | undefined>;
findBy: (attribute: P, value: V) => Promise<T | undefined>;
}

Expand Down
12 changes: 12 additions & 0 deletions balancer-js/src/modules/pools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ export class Pools implements Findable<PoolWithMethods> {
return this.repositories.pools;
}

/**
* Fetches new data from subgraph for pool
* (Will update PoolsSubgraphRepository cache)
* @param pool
* @returns
*/
async refresh(poolId: string): Promise<PoolWithMethods | undefined> {
const poolRefreshed = await this.repositories.pools.find(poolId, true);
if (!poolRefreshed) return poolRefreshed;
return Pools.wrap(poolRefreshed, this.networkConfig);
}

/**
* Calculates APR on any pool data
*
Expand Down

0 comments on commit c2bf4cc

Please sign in to comment.