Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching to protocol rates retrieval #194

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions summerfi-api/get-apy-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -106,6 +112,7 @@ const getUnifiedProtocolRates = async (
protocolId: ProtocolId,
event: APIGatewayProxyEventV2,
logger: Logger,
cache: DistributedCache,
subgraphsConfig: {
urlBase: string
chainId: ChainId
Expand All @@ -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<AaveSparkProtocolData>
return { isValid: true, protocolRates: parsed, position: parseResult.data }
}

const rates = await getAaveSparkRates({
collateralToken: parseResult.data.collateral[0],
debtToken: parseResult.data.debt[0],
Expand All @@ -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 }
}

Expand All @@ -147,13 +165,24 @@ 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<AjnaProtocolData>
return { isValid: true, protocolRates: parsed, position: parseResult.data }
}

const rates = await getAjnaRates({
poolId: parseResult.data.poolAddress,
logger,
timestamp: parseResult.data.referenceDate,
subgraphClient: getAjnaSubgraphClient({ ...subgraphsConfig, logger }),
})

await cache.set(cacheKey, JSON.stringify(rates))

return { isValid: true, protocolRates: rates, position: parseResult.data }
}

Expand All @@ -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<AjnaProtocolData>
return { isValid: true, protocolRates: parsed, position: parseResult.data }
}

const rates = await getMorphoBlueRates({
marketId: parseResult.data.marketId,
logger,
Expand All @@ -174,6 +211,8 @@ const getUnifiedProtocolRates = async (
}),
})

await cache.set(cacheKey, JSON.stringify(rates))

return { isValid: true, protocolRates: rates, position: parseResult.data }
}

Expand Down Expand Up @@ -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,
})
Expand Down
Loading