diff --git a/packages/morpho-blue-subgraph/queries/intrest-rates.graphql b/packages/morpho-blue-subgraph/queries/intrest-rates.graphql new file mode 100644 index 0000000000..036e4dfd53 --- /dev/null +++ b/packages/morpho-blue-subgraph/queries/intrest-rates.graphql @@ -0,0 +1,19 @@ +query GetInterestRates($marketId: String!, $marketIdBytes: ID!, $fromTimestamp: BigInt!, $toTimestamp: BigInt!) { + market(id: $marketIdBytes) { + id + collateralToken { + address + symbol + } + debtToken { + address + symbol + } + } + interestRates(where: { market: $marketId, timestamp_gte: $fromTimestamp, timestamp_lte: $toTimestamp }, orderBy: timestamp, orderDirection: desc) { + id + rate + timestamp + type + } +} \ No newline at end of file diff --git a/packages/morpho-blue-subgraph/src/index.ts b/packages/morpho-blue-subgraph/src/index.ts index bb215752b5..73ebb477ed 100644 --- a/packages/morpho-blue-subgraph/src/index.ts +++ b/packages/morpho-blue-subgraph/src/index.ts @@ -1,9 +1,9 @@ import { request } from 'graphql-request' -import { ChainId } from '@summerfi/serverless-shared' import type { Address } from '@summerfi/serverless-shared' +import { ChainId } from '@summerfi/serverless-shared' import { Logger } from '@aws-lambda-powertools/logger' -import { CollateralLockedDocument } from './types/graphql/generated' +import { CollateralLockedDocument, GetInterestRatesDocument } from './types/graphql/generated' const chainIdSubgraphMap: Partial> = { [ChainId.MAINNET]: 'summer-morpho-blue', @@ -33,11 +33,6 @@ export interface CollateralLocked { owner: Address } -/** - * The key of the first record is the owner of the collaterals, second record is the collateral address - */ -export type CollateralLockedRecord = Record> - export interface CollateralLockedResult { lockedCollaterals: CollateralLocked[] } @@ -46,6 +41,35 @@ export type GetCollateralLocked = ( params: GetCollateralLockedParams, ) => Promise +export interface GetInterestRateParams { + marketId: `0x${string}` + fromTimestamp: bigint + toTimestamp: bigint +} + +export interface InterestRate { + rate: number + type: 'borrow' | 'lend' + timestamp: bigint +} + +export interface MorphoBlueMarketInterestRateResult { + marketId: `0x${string}` + collateralToken: { + symbol: string + address: Address + } + debtToken: { + symbol: string + address: Address + } + interestRates: InterestRate[] +} + +export type GetInterestRate = ( + params: GetInterestRateParams, +) => Promise + async function getCollateralLocked( params: GetCollateralLockedParams, config: SubgraphClientConfig, @@ -67,8 +91,64 @@ async function getCollateralLocked( } } +async function getInterestRates( + params: GetInterestRateParams, + config: SubgraphClientConfig, +): Promise { + const url = getEndpoint(config.chainId, config.urlBase) + const result = await request(url, GetInterestRatesDocument, { + marketId: params.marketId, + marketIdBytes: params.marketId, + fromTimestamp: params.fromTimestamp, + toTimestamp: params.toTimestamp, + }) + + if (result.market == null) { + config.logger?.warn('No market found for marketId', { marketId: params.marketId }) + return { + marketId: params.marketId, + collateralToken: { + symbol: 'UNKNOWN', + address: '0x0', + }, + debtToken: { + symbol: 'UNKNOWN', + address: '0x0', + }, + interestRates: [], + } + } + + config.logger?.info('Interest rates found', { + length: result.interestRates.length, + marketId: params.marketId, + collateralToken: result.market.collateralToken?.symbol, + debtToken: result.market.debtToken?.symbol, + }) + + const mapped: InterestRate[] = result.interestRates.map((c) => ({ + rate: Number(c.rate), + type: c.type === 'borrow' ? ('borrow' as const) : ('lend' as const), + timestamp: c.timestamp, + })) + + return { + marketId: result.market.id as `0x${string}`, + collateralToken: { + symbol: result.market.collateralToken!.symbol, + address: result.market.collateralToken!.address as Address, + }, + debtToken: { + symbol: result.market.debtToken!.symbol, + address: result.market.debtToken!.address as Address, + }, + interestRates: mapped, + } +} + export interface MorphoBlueSubgraphClient { getCollateralLocked: GetCollateralLocked + getInterestRate: GetInterestRate } export function getMorphoBlueSubgraphClient( @@ -76,5 +156,6 @@ export function getMorphoBlueSubgraphClient( ): MorphoBlueSubgraphClient { return { getCollateralLocked: (params: GetCollateralLockedParams) => getCollateralLocked(params, config), + getInterestRate: (params: GetInterestRateParams) => getInterestRates(params, config), } }