From f7113d25b2652e2c25b0271d20c2c01d2d067d79 Mon Sep 17 00:00:00 2001 From: Jakub Swierczek Date: Wed, 17 Apr 2024 17:52:54 +0200 Subject: [PATCH] Update package scripts and enhance queries for interest rates calculation Removed the "tsc" and "watch" scripts from the scripts section of various package.json files and added new "dev" scripts for development mode. Improved the GraphQL query in MorphoBlueSubgraph for fetching the interest rates by introducing pagination logic. Enhanced the 'get-apy-function' to calculate APY for various time periods and include a detailed breakdown. --- package.json | 1 + packages/aave-spark-subgraph/package.json | 3 +- packages/abis/package.json | 3 +- packages/ajna-subgraph/package.json | 3 +- packages/automation-subgraph/package.json | 3 +- packages/common/package.json | 3 +- packages/contracts-utils/package.json | 1 + packages/core-contracts/package.json | 1 + packages/deployment-configs/package.json | 3 +- packages/deployment-types/package.json | 3 +- packages/deployment-utils/package.json | 3 +- .../package.json | 3 +- packages/morpho-blue-subgraph/.eslintrc.cjs | 5 +- packages/morpho-blue-subgraph/jest.config.js | 27 ++++ packages/morpho-blue-subgraph/package.json | 4 +- .../queries/intrest-rates.graphql | 67 +++++++++- .../morpho-blue-subgraph/src/index.spec.ts | 47 +++++++ packages/morpho-blue-subgraph/src/index.ts | 126 +++++++++++++----- .../morpho-blue-subgraph/tsconfig.test.json | 18 +++ packages/prices-subgraph/package.json | 3 +- packages/serverless-shared/package.json | 3 +- packages/tenderly-utils/package.json | 3 +- packages/triggers-calculations/package.json | 3 +- packages/triggers-shared/package.json | 3 +- pnpm-lock.yaml | 3 + summerfi-api/get-apy-function/package.json | 2 - summerfi-api/get-apy-function/src/index.ts | 56 +++++++- .../get-apy-function/src/morpho-blue/index.ts | 45 +++---- .../package.json | 2 - .../get-migrations-function/package.json | 2 - .../get-morpho-claims-function/package.json | 2 - .../get-triggers-function/package.json | 2 - .../portfolio-assets-function/package.json | 2 - .../portfolio-overview-function/package.json | 2 - 34 files changed, 350 insertions(+), 107 deletions(-) create mode 100644 packages/morpho-blue-subgraph/jest.config.js create mode 100644 packages/morpho-blue-subgraph/src/index.spec.ts create mode 100644 packages/morpho-blue-subgraph/tsconfig.test.json diff --git a/package.json b/package.json index 9ebfde3a0e..e24838a9c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "private": true, "scripts": { + "dev": "turbo run dev --concurrency=50", "prebuild": "turbo run prebuild --cache-dir=.turbo", "build": "turbo run build --cache-dir=.turbo", "lint": "turbo run lint --cache-dir=.turbo", diff --git a/packages/aave-spark-subgraph/package.json b/packages/aave-spark-subgraph/package.json index db2f3d4b18..eafd3f9cea 100644 --- a/packages/aave-spark-subgraph/package.json +++ b/packages/aave-spark-subgraph/package.json @@ -5,10 +5,9 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", diff --git a/packages/abis/package.json b/packages/abis/package.json index 456b682a71..adcddc0d98 100644 --- a/packages/abis/package.json +++ b/packages/abis/package.json @@ -2,10 +2,9 @@ "name": "@summerfi/abis", "version": "1.0.0", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/ajna-subgraph/package.json b/packages/ajna-subgraph/package.json index 8c57733dbc..671e09bf08 100644 --- a/packages/ajna-subgraph/package.json +++ b/packages/ajna-subgraph/package.json @@ -5,10 +5,9 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", diff --git a/packages/automation-subgraph/package.json b/packages/automation-subgraph/package.json index 6f05a71b5d..b5b2a60077 100644 --- a/packages/automation-subgraph/package.json +++ b/packages/automation-subgraph/package.json @@ -5,10 +5,9 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", diff --git a/packages/common/package.json b/packages/common/package.json index f241a2b792..96a1f98cf7 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -10,11 +10,10 @@ } }, "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "testw": "jest --watch", "build": "tsc -b -v ", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/contracts-utils/package.json b/packages/contracts-utils/package.json index f0ac5461e6..68323cf154 100644 --- a/packages/contracts-utils/package.json +++ b/packages/contracts-utils/package.json @@ -6,6 +6,7 @@ "scripts": { "tsc": "tsc", "build": "tsc -b -v", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/core-contracts/package.json b/packages/core-contracts/package.json index 586baaa91a..3df615ce41 100644 --- a/packages/core-contracts/package.json +++ b/packages/core-contracts/package.json @@ -18,6 +18,7 @@ "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b tsconfig.build.json", + "dev": "tsc -b tsconfig.build.json -w", "lint": "eslint .", "lint:fix": "eslint . --fix", "gen-versions": "hardhat run scripts/generate-versions.ts", diff --git a/packages/deployment-configs/package.json b/packages/deployment-configs/package.json index 8409f8134d..a0d89c86b8 100644 --- a/packages/deployment-configs/package.json +++ b/packages/deployment-configs/package.json @@ -10,9 +10,8 @@ } }, "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "build": "tsc -b tsconfig.build.json", + "dev": "tsc -b tsconfig.build.json -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/deployment-types/package.json b/packages/deployment-types/package.json index 3e886af2c7..d21b9699f2 100644 --- a/packages/deployment-types/package.json +++ b/packages/deployment-types/package.json @@ -10,9 +10,8 @@ } }, "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "build": "tsc -b tsconfig.json", + "dev": "tsc -b tsconfig.json -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/deployment-utils/package.json b/packages/deployment-utils/package.json index 836c7e3997..2251d86423 100644 --- a/packages/deployment-utils/package.json +++ b/packages/deployment-utils/package.json @@ -10,9 +10,8 @@ } }, "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "build": "tsc -b tsconfig.json", + "dev": "tsc -b tsconfig.json -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/morpho-blue-external-api-client/package.json b/packages/morpho-blue-external-api-client/package.json index 47bfbe75c7..ebcfc23ff3 100644 --- a/packages/morpho-blue-external-api-client/package.json +++ b/packages/morpho-blue-external-api-client/package.json @@ -4,10 +4,9 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", diff --git a/packages/morpho-blue-subgraph/.eslintrc.cjs b/packages/morpho-blue-subgraph/.eslintrc.cjs index a4fa827416..58de30e670 100644 --- a/packages/morpho-blue-subgraph/.eslintrc.cjs +++ b/packages/morpho-blue-subgraph/.eslintrc.cjs @@ -1,10 +1,7 @@ /** @type {import("eslint").Linter.Config} */ module.exports = { root: true, - ignorePatterns: ['src/types/graphql/**'], + ignorePatterns: ['src/types/graphql/**', 'jest.config.js'], extends: ['@summerfi/eslint-config/library.cjs'], parser: '@typescript-eslint/parser', - parserOptions: { - project: true, - }, } diff --git a/packages/morpho-blue-subgraph/jest.config.js b/packages/morpho-blue-subgraph/jest.config.js new file mode 100644 index 0000000000..0cb682995f --- /dev/null +++ b/packages/morpho-blue-subgraph/jest.config.js @@ -0,0 +1,27 @@ +const { pathsToModuleNameMapper } = require('ts-jest') + +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + roots: ['/src'], + testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'], + silent: true, + maxWorkers: 1, + testTimeout: 10000, + testEnvironment: 'node', + setupFilesAfterEnv: ['jest-expect-message'], + testPathIgnorePatterns: ['dist', 'node_modules'], + moduleNameMapper: pathsToModuleNameMapper({ + '@summerfi/serverless-shared': ['/../../packages/serverless-shared/src/index.ts'], + '@summerfi/serverless-shared/*': ['/../../packages/serverless-shared/src/*'], + '@summerfi/triggers-shared': ['/../../packages/triggers-shared/src/index.ts'], + }), + transform: { + '^.+\\.(ts|tsx)$': [ + 'ts-jest', + { + tsconfig: '/tsconfig.test.json', + }, + ], + }, +} diff --git a/packages/morpho-blue-subgraph/package.json b/packages/morpho-blue-subgraph/package.json index 283f65b4b3..d88312f431 100644 --- a/packages/morpho-blue-subgraph/package.json +++ b/packages/morpho-blue-subgraph/package.json @@ -8,6 +8,7 @@ "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", @@ -22,6 +23,7 @@ "@summerfi/eslint-config": "workspace:*", "@summerfi/typescript-config": "workspace:*", "@types/node": "^20.12.7", - "eslint": "^8.57.0" + "eslint": "^8.57.0", + "jest": "^29.7.0" } } diff --git a/packages/morpho-blue-subgraph/queries/intrest-rates.graphql b/packages/morpho-blue-subgraph/queries/intrest-rates.graphql index fc4eb1350c..3c50027583 100644 --- a/packages/morpho-blue-subgraph/queries/intrest-rates.graphql +++ b/packages/morpho-blue-subgraph/queries/intrest-rates.graphql @@ -1,4 +1,10 @@ -query GetInterestRates($marketId: String!, $marketIdBytes: ID!, $fromTimestamp: BigInt!, $toTimestamp: BigInt!) { +query GetInterestRates( + $marketId: String!, + $marketIdBytes: ID!, + $fromTimestamp: BigInt!, + $toTimestamp: BigInt! + $first: Int!, + $skip: Int!) { market(id: $marketIdBytes) { id collateralToken { @@ -10,7 +16,64 @@ query GetInterestRates($marketId: String!, $marketIdBytes: ID!, $fromTimestamp: symbol } } - interestRates(where: { market: $marketId, timestamp_gte: $fromTimestamp, timestamp_lte: $toTimestamp }, orderBy: timestamp, orderDirection: asc) { + interestRates( + where: { market: $marketId, timestamp_gte: $fromTimestamp, timestamp_lte: $toTimestamp }, + orderBy: timestamp, + orderDirection: asc, + first: $first, + skip: $skip) { + id + rate + timestamp + type + } + previousBorrowRate: interestRates( + where: { market: $marketId, timestamp_lt: $fromTimestamp, type: "borrow" }, + orderBy: timestamp, + orderDirection: desc, + first: 1) { + id + rate + timestamp + type + } + + previousLendRate: interestRates( + where: { market: $marketId, timestamp_lt: $fromTimestamp, type: "lend" }, interestRates( + where: { market: $marketId, timestamp_gte: $fromTimestamp, timestamp_lte: $toTimestamp }, + orderBy: timestamp, + orderDirection: asc, + first: $first, + skip: $skip) { + id + rate + timestamp + type + } + previousBorrowRate: interestRates( + where: { market: $marketId, timestamp_lt: $fromTimestamp, type: "borrow" }, + orderBy: timestamp, + orderDirection: desc, + first: 1) { + id + rate + timestamp + type + } + + previousLendRate: interestRates( + where: { market: $marketId, timestamp_lt: $fromTimestamp, type: "lend" }, + orderBy: timestamp, + orderDirection: desc, + first: 1) { + id + rate + timestamp + type + } + orderBy: timestamp, + orderDirection: desc, + first: 1) { id rate timestamp diff --git a/packages/morpho-blue-subgraph/src/index.spec.ts b/packages/morpho-blue-subgraph/src/index.spec.ts new file mode 100644 index 0000000000..6dc005251e --- /dev/null +++ b/packages/morpho-blue-subgraph/src/index.spec.ts @@ -0,0 +1,47 @@ +import { getMorphoBlueSubgraphClient, MorphoBlueSubgraphClient } from '.' +import { Logger } from '@aws-lambda-powertools/logger' +import { ChainId } from '@summerfi/serverless-shared' + +const logger = new Logger({ + serviceName: 'moorpho-blue-subgraph-tests', + environment: 'dev', + logLevel: 'debug', +}) + +function getTimestampInSeconds(unixTimestamp: number): number { + return Math.floor(unixTimestamp / 1000) +} + +describe.skip('Exploration test MorphoBlue InterestRates', () => { + let client: MorphoBlueSubgraphClient + const wstETH_ETH_marketId: `0x${string}` = + '0xd0e50cdac92fe2172043f5e0c36532c6369d24947e40968f34a5e8819ca9ec5d' + const fromTimestamp_2024_04_01 = getTimestampInSeconds(new Date().setFullYear(2024, 3, 1)) + const toTimestamp_2024_04_15 = getTimestampInSeconds(new Date().setFullYear(2024, 3, 15)) + + beforeAll(() => { + client = getMorphoBlueSubgraphClient({ + chainId: ChainId.MAINNET, + urlBase: '', // set your urlBase here + logger, + }) + }) + it('should return arrays of interest rates', async () => { + logger.debug('Fetching interest rates', { + marketId: wstETH_ETH_marketId, + fromTimestamp: fromTimestamp_2024_04_01, + toTimestamp: toTimestamp_2024_04_15, + }) + + const result = await client.getInterestRate({ + marketId: wstETH_ETH_marketId, + fromTimestamp: fromTimestamp_2024_04_01, + toTimestamp: toTimestamp_2024_04_15, + }) + + expect(result.debtToken.symbol).toBe('WETH') + expect(result.collateralToken.symbol).toBe('wstETH') + + expect(result.interestRates.borrow[0].fromTimestamp).toBe(fromTimestamp_2024_04_01) + }) +}) diff --git a/packages/morpho-blue-subgraph/src/index.ts b/packages/morpho-blue-subgraph/src/index.ts index ed74c09ada..e2e1f46f29 100644 --- a/packages/morpho-blue-subgraph/src/index.ts +++ b/packages/morpho-blue-subgraph/src/index.ts @@ -102,14 +102,20 @@ async function getInterestRates( ): Promise { const url = getEndpoint(config.chainId, config.urlBase) config.logger?.info('Fetching interest rates', { url, params }) + let skip = 0 + const first = 1000 const result = await request(url, GetInterestRatesDocument, { marketId: params.marketId, marketIdBytes: params.marketId, - fromTimestamp: '0', - toTimestamp: BigInt(params.toTimestamp.toFixed()).toString(), + fromTimestamp: params.fromTimestamp.toFixed(), + toTimestamp: params.toTimestamp.toFixed(), + skip, + first, }) - if (result.market == null) { + const market = result.market + + if (market == null) { config.logger?.warn('No market found for marketId', { marketId: params.marketId }) return { marketId: params.marketId, @@ -128,37 +134,91 @@ async function getInterestRates( } } + const previousBorrowRate = result.previousBorrowRate[0] + const previousLendRate = result.previousLendRate[0] + + let currentResponseInterestRates = result.interestRates.length + + const interestRates = [...result.interestRates] + config.logger?.info('Interest rates found', { length: result.interestRates.length, + totalLength: interestRates.length, marketId: params.marketId, - collateralToken: result.market.collateralToken?.symbol, - debtToken: result.market.debtToken?.symbol, + collateralToken: market.collateralToken?.symbol, + debtToken: market.debtToken?.symbol, }) - const mapped = result.interestRates.reduce( - (acc, rate) => { - if (rate.type === 'borrow') { - acc.borrow.push({ - rate: Number(rate.rate), - type: 'borrow' as const, - fromTimestamp: Number(rate.timestamp), - }) - } - if (rate.type === 'lend') { - acc.lend.push({ - rate: Number(rate.rate), - type: 'lend' as const, - fromTimestamp: Number(rate.timestamp), - }) - } + let interestRatesRequests = 1 + while (currentResponseInterestRates === first) { + if (interestRatesRequests > 10) { + throw new Error('Too many requests') + } + skip = skip + first + const nextResult = await request(url, GetInterestRatesDocument, { + marketId: params.marketId, + marketIdBytes: params.marketId, + fromTimestamp: params.fromTimestamp.toFixed(), + toTimestamp: params.toTimestamp.toFixed(), + skip, + first, + }) - return acc - }, - { borrow: [], lend: [] } as { - borrow: Omit[] - lend: Omit[] - }, - ) + interestRatesRequests++ + + config.logger?.info('Next Interest rates found', { + length: nextResult.interestRates.length, + totalLength: interestRates.length, + marketId: params.marketId, + collateralToken: market.collateralToken?.symbol, + debtToken: market.debtToken?.symbol, + requestCount: interestRatesRequests, + }) + + interestRates.push(...nextResult.interestRates) + + currentResponseInterestRates = nextResult.interestRates.length + } + + const acc = { borrow: [], lend: [] } as { + borrow: Omit[] + lend: Omit[] + } + + if (previousBorrowRate) { + acc.borrow.push({ + rate: Number(previousBorrowRate.rate), + type: 'borrow', + fromTimestamp: params.fromTimestamp, + }) + } + + if (previousLendRate) { + acc.lend.push({ + rate: Number(previousLendRate.rate), + type: 'lend', + fromTimestamp: params.fromTimestamp, + }) + } + + const mapped = interestRates.reduce((acc, rate) => { + if (rate.type === 'borrow') { + acc.borrow.push({ + rate: Number(rate.rate), + type: 'borrow' as const, + fromTimestamp: Number(rate.timestamp), + }) + } + if (rate.type === 'lend') { + acc.lend.push({ + rate: Number(rate.rate), + type: 'lend' as const, + fromTimestamp: Number(rate.timestamp), + }) + } + + return acc + }, acc) const lend = mapped.lend .sort((a, b) => a.fromTimestamp - b.fromTimestamp) @@ -191,14 +251,14 @@ async function getInterestRates( }) return { - marketId: result.market.id as `0x${string}`, + marketId: market.id as `0x${string}`, collateralToken: { - symbol: result.market.collateralToken!.symbol, - address: result.market.collateralToken!.address as Address, + symbol: market.collateralToken!.symbol, + address: market.collateralToken!.address as Address, }, debtToken: { - symbol: result.market.debtToken!.symbol, - address: result.market.debtToken!.address as Address, + symbol: market.debtToken!.symbol, + address: market.debtToken!.address as Address, }, interestRates: { lend, diff --git a/packages/morpho-blue-subgraph/tsconfig.test.json b/packages/morpho-blue-subgraph/tsconfig.test.json new file mode 100644 index 0000000000..79b5cea09e --- /dev/null +++ b/packages/morpho-blue-subgraph/tsconfig.test.json @@ -0,0 +1,18 @@ +{ + "extends": "@summerfi/typescript-config/tsconfig.test.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "baseUrl": "src", + "types": [ + "node", + "jest" + ] + }, + "include": [ + "src/**/*.ts" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/packages/prices-subgraph/package.json b/packages/prices-subgraph/package.json index 321718928a..ad5b9c1ff5 100644 --- a/packages/prices-subgraph/package.json +++ b/packages/prices-subgraph/package.json @@ -5,10 +5,9 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "prebuild": "pnpm run generate-ts-types", "generate-ts-types": "graphql-codegen --config graphql.config.yml", "lint": "eslint .", diff --git a/packages/serverless-shared/package.json b/packages/serverless-shared/package.json index 4405f157fa..b44b739802 100644 --- a/packages/serverless-shared/package.json +++ b/packages/serverless-shared/package.json @@ -6,7 +6,8 @@ "tsc": "tsc", "watch": "tsc -w", "test": "jest --passWithNoTests", - "build": "tsc -b -v", + "build": "tsc -b", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/tenderly-utils/package.json b/packages/tenderly-utils/package.json index c228835e75..3180815297 100644 --- a/packages/tenderly-utils/package.json +++ b/packages/tenderly-utils/package.json @@ -17,7 +17,8 @@ "e2e": "jest e2e/", "lint": "eslint .", "lint:fix": "eslint . --fix", - "build": "tsc -b -v tsconfig.build.json" + "build": "tsc -b -v tsconfig.build.json", + "dev": "tsc -b tsconfig.build.json -w" }, "dependencies": { "@inquirer/prompts": "^4.3.3", diff --git a/packages/triggers-calculations/package.json b/packages/triggers-calculations/package.json index af3027b19a..102accd791 100644 --- a/packages/triggers-calculations/package.json +++ b/packages/triggers-calculations/package.json @@ -2,10 +2,9 @@ "name": "@summerfi/triggers-calculations", "version": "1.0.0", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/packages/triggers-shared/package.json b/packages/triggers-shared/package.json index 4460d25294..910fbd19a7 100644 --- a/packages/triggers-shared/package.json +++ b/packages/triggers-shared/package.json @@ -3,10 +3,9 @@ "version": "1.0.0", "type": "module", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", + "dev": "tsc -b -w", "lint": "eslint .", "lint:fix": "eslint . --fix" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a8d9d92a30..6f042adca2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -475,6 +475,9 @@ importers: eslint: specifier: ^8.57.0 version: 8.57.0 + jest: + specifier: ^29.7.0 + version: 29.7.0(@types/node@20.12.7) packages/prices-subgraph: dependencies: diff --git a/summerfi-api/get-apy-function/package.json b/summerfi-api/get-apy-function/package.json index 88e4479fd4..9e5274ac70 100644 --- a/summerfi-api/get-apy-function/package.json +++ b/summerfi-api/get-apy-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/get-apy-function", "version": "0.0.2", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", "lint": "eslint .", diff --git a/summerfi-api/get-apy-function/src/index.ts b/summerfi-api/get-apy-function/src/index.ts index d7cd657727..b899db42f2 100644 --- a/summerfi-api/get-apy-function/src/index.ts +++ b/summerfi-api/get-apy-function/src/index.ts @@ -86,6 +86,32 @@ interface ApyResponse { apy30d: number apy90d: number } + breakdowns: { + borrowCost: { + apy: number + apy7d: number + apy30d: number + apy90d: number + } + supplyReward: { + apy: number + apy7d: number + apy30d: number + apy90d: number + } + underlyingBorrowedTokenYield: { + apy: number + apy7d: number + apy30d: number + apy90d: number + } + underlyingSuppliedTokenYield: { + apy: number + apy7d: number + apy30d: number + apy90d: number + } + } debug: unknown } @@ -169,10 +195,12 @@ export const handler = async ( urlBase: SUBGRAPH_BASE, }) + const timestamp = Math.floor(positionData.data.referenceDate.getTime() / 1000) + rates = await getMorphoBlueApy({ ltv: positionData.data.ltv, marketId: positionData.data.marketId, - timestamp: positionData.data.referenceDate.getTime(), + timestamp, logger: logger, subgraphClient: client, }) @@ -187,6 +215,32 @@ export const handler = async ( apy30d: 0, apy90d: 0, }, + breakdowns: { + borrowCost: { + apy: 0, + apy7d: 0, + apy30d: 0, + apy90d: 0, + }, + supplyReward: { + apy: 0, + apy7d: 0, + apy30d: 0, + apy90d: 0, + }, + underlyingBorrowedTokenYield: { + apy: 0, + apy7d: 0, + apy30d: 0, + apy90d: 0, + }, + underlyingSuppliedTokenYield: { + apy: 0, + apy7d: 0, + apy30d: 0, + apy90d: 0, + }, + }, } return ResponseOk({ body: result }) diff --git a/summerfi-api/get-apy-function/src/morpho-blue/index.ts b/summerfi-api/get-apy-function/src/morpho-blue/index.ts index cd035af537..aeea9cf24d 100644 --- a/summerfi-api/get-apy-function/src/morpho-blue/index.ts +++ b/summerfi-api/get-apy-function/src/morpho-blue/index.ts @@ -9,13 +9,13 @@ import { import { Logger } from '@aws-lambda-powertools/logger' const ONE_YEAR_TIMESTAMP = 365 * 24 * 60 * 60 -const ONE_DAY_TIMESTAMP = 24 * 60 * 60 function calculateBorrowForTimestamp( morphoSubgraphResponse: MorphoBlueMarketInterestRateResult, timestamp: number, + days: number, ) { - return morphoSubgraphResponse.interestRates.borrow + const apy = morphoSubgraphResponse.interestRates.borrow .filter((rate) => rate.toTimestamp >= BigInt(timestamp)) .map((rate) => { if (rate.fromTimestamp < timestamp) { @@ -32,6 +32,8 @@ function calculateBorrowForTimestamp( return Math.pow(1 + rate.rate, rate.duration / (60 * 60 * 24 * 365)) }) .reduce((acc, rate) => acc * rate, 1) + + return Math.pow(apy, 365 / days) - 1 } export const getMorphoBlueApy = async (params: { @@ -41,7 +43,7 @@ export const getMorphoBlueApy = async (params: { logger: Logger subgraphClient: MorphoBlueSubgraphClient }) => { - const earliestTimestamp = params.timestamp - ONE_YEAR_TIMESTAMP + ONE_DAY_TIMESTAMP // 1 year and 1 day + const earliestTimestamp = params.timestamp - ONE_YEAR_TIMESTAMP const morphoSubgraphResponse = await params.subgraphClient.getInterestRate({ marketId: params.marketId, @@ -49,7 +51,7 @@ export const getMorphoBlueApy = async (params: { toTimestamp: params.timestamp, }) - // avarege for 7 days + const timestamp1dayAgo = params.timestamp - 1 * 24 * 60 * 60 const timestamp7daysAgo = params.timestamp - 7 * 24 * 60 * 60 const timestamp30daysAgo = params.timestamp - 30 * 24 * 60 * 60 @@ -58,31 +60,22 @@ export const getMorphoBlueApy = async (params: { const timestamp1yearAgo = params.timestamp - 365 * 24 * 60 * 60 - const timestamps = [timestamp7daysAgo, timestamp30daysAgo, timestamp90daysAgo, timestamp1yearAgo] + const timestamps = [ + [timestamp1dayAgo, 1], + [timestamp7daysAgo, 7], + [timestamp30daysAgo, 30], + [timestamp90daysAgo, 90], + [timestamp1yearAgo, 365], + ] - const [borrowRate7d, borrowRate30d, borrowRate90d, borrowRate] = timestamps.map((timestamp) => { - return calculateBorrowForTimestamp(morphoSubgraphResponse, timestamp) - }) - // - // const borrowRate7d = morphoSubgraphResponse.interestRates.borrow - // .filter((rate) => rate.toTimestamp >= BigInt(timestamp7daysAgo)) - // .map((rate) => { - // if (rate.fromTimestamp < timestamp7daysAgo) { - // return { - // ...rate, - // fromTimestamp: timestamp7daysAgo, - // duration: rate.toTimestamp - timestamp7daysAgo, - // } - // } - // return rate - // }) - // .map((rate) => { - // // (1 + r)^(duration / (60 * 60 * 24 * 365)) - // return Math.pow(1 + rate.rate, rate.duration / (60 * 60 * 24 * 365)) - // }) - // .reduce((acc, rate) => acc * rate, 1) + const [borrowRate1d, borrowRate7d, borrowRate30d, borrowRate90d, borrowRate] = timestamps.map( + ([timestamp, days]) => { + return calculateBorrowForTimestamp(morphoSubgraphResponse, timestamp, days) + }, + ) return { + borrowRate1d, borrowRate7d, borrowRate30d, borrowRate90d, diff --git a/summerfi-api/get-meta-morpho-details-function/package.json b/summerfi-api/get-meta-morpho-details-function/package.json index 524618a30d..e1bf44a30e 100644 --- a/summerfi-api/get-meta-morpho-details-function/package.json +++ b/summerfi-api/get-meta-morpho-details-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/get-meta-morpho-details-function", "version": "0.0.2", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", "lint": "eslint .", diff --git a/summerfi-api/get-migrations-function/package.json b/summerfi-api/get-migrations-function/package.json index 66b085276d..bd5fa554e0 100644 --- a/summerfi-api/get-migrations-function/package.json +++ b/summerfi-api/get-migrations-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/get-migrations-function", "version": "0.0.1", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "lint": "eslint .", "lint:fix": "eslint . --fix", diff --git a/summerfi-api/get-morpho-claims-function/package.json b/summerfi-api/get-morpho-claims-function/package.json index 5c191e7cb1..17d158dfd2 100644 --- a/summerfi-api/get-morpho-claims-function/package.json +++ b/summerfi-api/get-morpho-claims-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/get-morpho-claims-function", "version": "0.0.1", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", "lint": "eslint .", diff --git a/summerfi-api/get-triggers-function/package.json b/summerfi-api/get-triggers-function/package.json index 2a43e7e9fc..6deba28cfd 100644 --- a/summerfi-api/get-triggers-function/package.json +++ b/summerfi-api/get-triggers-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/get-triggers-function", "version": "0.0.1", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "tsc -b -v", "lint": "eslint .", diff --git a/summerfi-api/portfolio-assets-function/package.json b/summerfi-api/portfolio-assets-function/package.json index 681bafb6d1..64ae7e271a 100644 --- a/summerfi-api/portfolio-assets-function/package.json +++ b/summerfi-api/portfolio-assets-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/portfolio-assets-function", "version": "1.0.0", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "build": "esbuild src/index.ts --bundle --minify --platform=node --target=node20 --outfile=dist/index.js --sourcemap", "lint": "eslint .", diff --git a/summerfi-api/portfolio-overview-function/package.json b/summerfi-api/portfolio-overview-function/package.json index 561c7964b8..c53f6304dc 100644 --- a/summerfi-api/portfolio-overview-function/package.json +++ b/summerfi-api/portfolio-overview-function/package.json @@ -2,8 +2,6 @@ "name": "@summerfi/portfolio-overview-function", "version": "1.0.0", "scripts": { - "tsc": "tsc", - "watch": "tsc -w", "test": "jest --passWithNoTests", "testw": "jest --watch", "lint": "eslint .",