Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ export class MorphoBorrowApyProvider implements BorrowApyFetcher {
// Fetch market ID using the lending adapter
const marketId = await fetchMorphoMarketId(tokenAddress, chainId, config)

// Fetch market data from Morpho using GraphQL with uniqueKey (marketId from contract)
// Fetch market data from Morpho using GraphQL with marketId from contract
const response = await fetchMorphoMarketBorrowRate(marketId, chainId)

if (!response.marketByUniqueKey) {
throw new Error(`No market data found for unique key: ${marketId}`)
if (!response.marketById) {
throw new Error(`No market data found for market ID: ${marketId}`)
}

const marketData = response.marketByUniqueKey
const marketData = response.marketById

// Use 24-hour average borrow APY for current rates
// Note: Zod validation in fetcher ensures this is number | null
Expand Down
11 changes: 5 additions & 6 deletions src/lib/graphql/fetchers/morpho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const MorphoMarketStateSchema = z.object({
})

const MorphoMarketDataSchema = z.object({
uniqueKey: z.string(),
id: z.string(),
marketId: z.string(),
collateralAsset: z.object({
address: z.string(),
name: z.string(),
Expand All @@ -21,14 +20,14 @@ const MorphoMarketDataSchema = z.object({
})

const MorphoResponseSchema = z.object({
marketByUniqueKey: MorphoMarketDataSchema.nullable().optional(),
marketById: MorphoMarketDataSchema.nullable().optional(),
})

/**
* Fetches Morpho market borrow rate data using GraphQL
*/
export async function fetchMorphoMarketBorrowRate(
uniqueKey: string,
marketId: string,
chainId: number = 8453, // Default to Base
): Promise<z.infer<typeof MorphoResponseSchema>> {
const MORPHO_GRAPHQL_ENDPOINT = 'https://api.morpho.org/graphql'
Expand All @@ -40,7 +39,7 @@ export async function fetchMorphoMarketBorrowRate(
},
body: JSON.stringify({
query: MORPHO_MARKET_BORROW_RATE_QUERY,
variables: { uniqueKey, chainId },
variables: { marketId, chainId },
}),
})

Expand All @@ -65,7 +64,7 @@ export async function fetchMorphoMarketBorrowRate(
// Log validation error with response snippet for debugging
const responseSnippet = JSON.stringify(result.data).slice(0, 500)
logger.error('Morpho API response validation failed', {
uniqueKey,
marketId,
chainId,
validationErrors: error.issues,
responseSnippet,
Expand Down
7 changes: 3 additions & 4 deletions src/lib/graphql/queries/morpho.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export const MORPHO_MARKET_BORROW_RATE_QUERY = `
query MorphoMarketBorrowRate($uniqueKey: String!, $chainId: Int!) {
marketByUniqueKey(uniqueKey: $uniqueKey, chainId: $chainId) {
uniqueKey
id
query MorphoMarketBorrowRate($marketId: String!, $chainId: Int!) {
marketById(marketId: $marketId, chainId: $chainId) {
marketId
collateralAsset {
address
name
Expand Down
5 changes: 2 additions & 3 deletions src/lib/graphql/types/morpho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*/

export interface MorphoMarketData {
uniqueKey: string
id: string
marketId: string
collateralAsset: {
address: string
name: string
Expand All @@ -16,5 +15,5 @@ export interface MorphoMarketData {
}

export interface MorphoMarketBorrowRateResponse {
marketByUniqueKey?: MorphoMarketData | null
marketById?: MorphoMarketData | null
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -180,15 +179,15 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: null,
marketById: null,
}

mockGetLeverageManagerAddress.mockReturnValue(mockManagerAddress)
mockReadContract.mockResolvedValueOnce(mockManagerResult).mockResolvedValueOnce(mockMarketId)
mockFetchMorphoMarketBorrowRate.mockResolvedValue(mockGraphQLResponse)

await expect(provider.fetchBorrowApy(tokenAddress, chainId, mockConfig)).rejects.toThrow(
'No market data found for unique key: 0xabcdef1234567890abcdef1234567890abcdef12',
'No market data found for market ID: 0xabcdef1234567890abcdef1234567890abcdef12',
)
})

Expand Down Expand Up @@ -240,9 +239,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -280,9 +278,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -320,9 +317,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -356,9 +352,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -392,9 +387,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down Expand Up @@ -428,9 +422,8 @@ describe('MorphoBorrowApyProvider', () => {
}

const mockGraphQLResponse = {
marketByUniqueKey: {
uniqueKey: mockMarketId,
id: 'market-id-123',
marketById: {
marketId: mockMarketId,
collateralAsset: {
address: '0x1234567890123456789012345678901234567890',
name: 'WETH',
Expand Down
Loading