Skip to content

Commit

Permalink
feat: added caching for vaults get (#114)
Browse files Browse the repository at this point in the history
* feat: added caching for vaults get

* chore: linted
  • Loading branch information
jstashh authored Sep 13, 2021
1 parent 21b09ff commit d46ea48
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CachedFetcher<T> {
this.chainId = chainId;
}

async fetch(): Promise<T | undefined> {
async fetch(queryParameters?: string): Promise<T | undefined> {
if (!this.ctx.cache.useCache || !this.ctx.cache.url) {
return undefined;
}
Expand All @@ -24,7 +24,12 @@ export class CachedFetcher<T> {
return cached;
}

const call = await fetch(`${this.ctx.cache.url}/v1/chains/${this.chainId}/${this.path}`);
let path = `${this.ctx.cache.url}/v1/chains/${this.chainId}/${this.path}`;
if (queryParameters) {
path += `?${queryParameters}`;
}

const call = await fetch(path);
if (call.status !== 200) {
const { url, status, statusText } = call;
console.warn(`Call to cache failed at ${url} (status ${status} ${statusText})`);
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CallOverrides, Contract } from "@ethersproject/contracts";
import { TransactionRequest, TransactionResponse } from "@ethersproject/providers";
import { JsonRpcSigner } from "@ethersproject/providers";

import { CachedFetcher } from "../cache";
import { ChainId } from "../chain";
import { ServiceInterface } from "../common";
import { chunkArray, EthAddress, WethAddress } from "../helpers";
Expand All @@ -27,13 +28,20 @@ import { Position, Vault } from "../types";
const VaultAbi = ["function deposit(uint256 amount) public", "function withdraw(uint256 amount) public"];

export class VaultInterface<T extends ChainId> extends ServiceInterface<T> {
private cachedFetcher = new CachedFetcher<Vault[]>("vaults/get", this.ctx, this.chainId);

/**
* Get all yearn vaults.
* @param addresses filter, if not provided all positions are returned
* @param overrides
* @returns
*/
async get(addresses?: Address[], overrides?: CallOverrides): Promise<Vault[]> {
const cached = await this.cachedFetcher.fetch(addresses ? `addresses=${addresses.join()}` : undefined);
if (cached) {
return cached;
}

const assetsStatic = await this.getStatic(addresses, overrides);
let assetsDynamic: VaultDynamic[] = [];
try {
Expand Down

0 comments on commit d46ea48

Please sign in to comment.