From 93148be1fa92407954c18063274a9a63584a291e Mon Sep 17 00:00:00 2001 From: Jakub Swierczek Date: Tue, 23 Apr 2024 10:16:43 +0200 Subject: [PATCH] Add caching to protocol rates retrieval The code alterations were focused on adding a caching mechanism to the protocol rates retrieval. This system checks the cache first before attempting to fetch from the getAaveSparkRates, getAjnaRates, and getMorphoBlueRates functions. As a result, it will reduce unnecessary repetitive data fetching and optimize performance. --- summerfi-api/get-apy-function/src/index.ts | 43 +++++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/summerfi-api/get-apy-function/src/index.ts b/summerfi-api/get-apy-function/src/index.ts index 364b093e6a..85373cacbc 100644 --- a/summerfi-api/get-apy-function/src/index.ts +++ b/summerfi-api/get-apy-function/src/index.ts @@ -22,7 +22,13 @@ import { getRedisInstance } from '@summerfi/redis-cache' import { getCachableYieldService } from '@summerfi/defi-llama-client' import { getTokenApyService } from './tokens-apy-service' import { CalculatedRates, ProtocolResponse } from './protocols/types' -import { getAaveSparkRates, getAjnaRates, getMorphoBlueRates } from './protocols' +import { + AaveSparkProtocolData, + AjnaProtocolData, + getAaveSparkRates, + getAjnaRates, + getMorphoBlueRates, +} from './protocols' import * as process from 'node:process' import { getFinalApy } from './final-apy-calculation' import { DistributedCache } from '@summerfi/abstractions' @@ -106,6 +112,7 @@ const getUnifiedProtocolRates = async ( protocolId: ProtocolId, event: APIGatewayProxyEventV2, logger: Logger, + cache: DistributedCache, subgraphsConfig: { urlBase: string chainId: ChainId @@ -129,6 +136,15 @@ const getUnifiedProtocolRates = async ( return { isValid: false, message: 'Invalid query parameters' } } + const cacheKey = `${protocolId}-${subgraphsConfig.chainId}-${JSON.stringify(parseResult.data)}` + + const fromCache = await cache.get(cacheKey) + + if (fromCache) { + const parsed = JSON.parse(fromCache) as ProtocolResponse + return { isValid: true, protocolRates: parsed, position: parseResult.data } + } + const rates = await getAaveSparkRates({ collateralToken: parseResult.data.collateral[0], debtToken: parseResult.data.debt[0], @@ -138,6 +154,8 @@ const getUnifiedProtocolRates = async ( subgraphClient: getAaveSparkSubgraphClient({ ...subgraphsConfig, logger }), }) + await cache.set(cacheKey, JSON.stringify(rates)) + return { isValid: true, protocolRates: rates, position: parseResult.data } } @@ -147,6 +165,15 @@ const getUnifiedProtocolRates = async ( return { isValid: false, message: 'Invalid query parameters' } } + const cacheKey = `${protocolId}-${subgraphsConfig.chainId}-${JSON.stringify(parseResult.data)}` + + const fromCache = await cache.get(cacheKey) + + if (fromCache) { + const parsed = JSON.parse(fromCache) as ProtocolResponse + return { isValid: true, protocolRates: parsed, position: parseResult.data } + } + const rates = await getAjnaRates({ poolId: parseResult.data.poolAddress, logger, @@ -154,6 +181,8 @@ const getUnifiedProtocolRates = async ( subgraphClient: getAjnaSubgraphClient({ ...subgraphsConfig, logger }), }) + await cache.set(cacheKey, JSON.stringify(rates)) + return { isValid: true, protocolRates: rates, position: parseResult.data } } @@ -163,6 +192,14 @@ const getUnifiedProtocolRates = async ( return { isValid: false, message: 'Invalid query parameters' } } + const cacheKey = `${protocolId}-${subgraphsConfig.chainId}-${JSON.stringify(parseResult.data)}` + + const fromCache = await cache.get(cacheKey) + if (fromCache) { + const parsed = JSON.parse(fromCache) as ProtocolResponse + return { isValid: true, protocolRates: parsed, position: parseResult.data } + } + const rates = await getMorphoBlueRates({ marketId: parseResult.data.marketId, logger, @@ -174,6 +211,8 @@ const getUnifiedProtocolRates = async ( }), }) + await cache.set(cacheKey, JSON.stringify(rates)) + return { isValid: true, protocolRates: rates, position: parseResult.data } } @@ -240,7 +279,7 @@ export const handler = async ( const { protocol, chainId } = path.data - const unifiedProtocolRates = await getUnifiedProtocolRates(protocol, event, logger, { + const unifiedProtocolRates = await getUnifiedProtocolRates(protocol, event, logger, cache, { urlBase: SUBGRAPH_BASE, chainId, })