Skip to content

Commit

Permalink
Morpho rewards claim type handling (#403)
Browse files Browse the repository at this point in the history
- handled different type of claims (for_borrow, for_supply)
- added chainId handling
- reverted redis cache time to 6h
  • Loading branch information
piekczyk authored Jun 27, 2024
1 parent 9834682 commit 64385cc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
3 changes: 1 addition & 2 deletions summerfi-api/get-apy-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export const handler = async (
: await getRedisInstance(
{
url: REDIS_CACHE_URL,
// Temporarily reduced to 20 min, standard value is 6h
ttlInSeconds: ONE_HOUR / 3,
ttlInSeconds: ONE_HOUR * 6,
username: REDIS_CACHE_USER,
password: REDIS_CACHE_PASSWORD,
stage: STAGE,
Expand Down
52 changes: 38 additions & 14 deletions summerfi-api/get-morpho-claims-function/src/get-claims.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { safeParseBigInt } from '@summerfi/serverless-shared'
import { ChainId, safeParseBigInt } from '@summerfi/serverless-shared'
import {
UserRewardsResponse,
UserRewardsDistributionResponse,
MorphoAggregatedClaims,
GetClaimsParams,
MorphoClaims,
ClaimableMorphoReward,
MetaMorphoClaims,
ClaimsRawKeys,
} from './types'

/**
Expand Down Expand Up @@ -45,14 +47,22 @@ const aggregateClaimsByToken = (claims: MorphoAggregatedClaims[]): MorphoAggrega
return Object.values(aggregatedClaims)
}

export const getClaims = async ({ account, logger }: GetClaimsParams): Promise<MorphoClaims> => {
export const getClaims = async ({
account,
chainId,
claimType,
logger,
}: GetClaimsParams): Promise<MorphoClaims> => {
try {
if (![ChainId.MAINNET, ChainId.BASE].includes(chainId)) {
throw new Error(`Not supported chain id ${chainId}. Supported chain ids are: 1, 8453.`)
}

const [responseRewards, responseDistribution] = await Promise.all([
fetch(`https://rewards.morpho.org/v1/users/${account}/rewards`),
fetch(`https://rewards.morpho.org/v1/users/${account}/distributions`),
fetch(`https://rewards.morpho.org/v1/users/${account}/rewards?chain_id=${chainId}`),
fetch(`https://rewards.morpho.org/v1/users/${account}/distributions?chain_id=${chainId}`),
])

// const responseRewards = await fetch(`https://rewards.morpho.org/v1/users/${account}/rewards`)
if (responseRewards.status !== 200 || responseDistribution.status !== 200) {
logger?.warn('Failed to fetch data from morpho rewards endpointa', {
error: responseRewards.statusText,
Expand All @@ -73,26 +83,40 @@ export const getClaims = async ({ account, logger }: GetClaimsParams): Promise<M
}
})

const resolvedClaimType = {
[MetaMorphoClaims.BORROW]: ClaimsRawKeys.FOR_BORROW,
[MetaMorphoClaims.SUPPLY]: ClaimsRawKeys.FOR_SUPPLY,
}[claimType]

const claimsAggregated: MorphoAggregatedClaims[] = aggregateClaimsByToken(
userRewardsResponse.data.map((item) => {
return {
rewardTokenAddress: item.program.asset.address,
claimable: safeParseBigInt(item.for_supply?.claimable_now) || 0n,
accrued: safeParseBigInt(item.for_supply?.total) || 0n,
claimed: safeParseBigInt(item.for_supply?.claimed) || 0n,
}
}),
userRewardsResponse.data
.map((item) => {
const rewards = item[resolvedClaimType]

if (rewards) {
return {
rewardTokenAddress: item.program.asset.address,
claimable: safeParseBigInt(rewards.claimable_now) || 0n,
accrued: safeParseBigInt(rewards.total) || 0n,
claimed: safeParseBigInt(rewards.claimed) || 0n,
}
}

return null
})
.filter((item) => !!item) as MorphoAggregatedClaims[],
)

return {
claimable,
claimable, // IMPORTANT - claimable are user specific, not claimType or even position specific
claimsAggregated,
}
} catch (error) {
console.error('Get morpho reward claims failed', error)
return {
claimable: [],
claimsAggregated: [],
error: `Get morpho reward claims failed. ${error}`,
}
}
}
7 changes: 6 additions & 1 deletion summerfi-api/get-morpho-claims-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import {
ResponseInternalServerError,
ResponseOk,
} from '@summerfi/serverless-shared/responses'
import { addressSchema } from '@summerfi/serverless-shared/validators'
import { addressSchema, chainIdSchema } from '@summerfi/serverless-shared/validators'

import { Logger } from '@aws-lambda-powertools/logger'
import { getClaims } from './get-claims'
import { MetaMorphoClaims } from './types'

const logger = new Logger({ serviceName: 'get-morpho-claims-function' })

const paramsSchema = z.object({
account: addressSchema,
claimType: z.enum([MetaMorphoClaims.BORROW, MetaMorphoClaims.SUPPLY]),
chainId: chainIdSchema,
})

export const handler = async (
Expand Down Expand Up @@ -45,6 +48,8 @@ export const handler = async (

const claims = await getClaims({
account: params.account,
chainId: params.chainId,
claimType: params.claimType,
})

return ResponseOk({ body: claims })
Expand Down
14 changes: 14 additions & 0 deletions summerfi-api/get-morpho-claims-function/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Address } from '@summerfi/serverless-shared'
import { Logger } from '@aws-lambda-powertools/logger'

export enum ClaimsRawKeys {
FOR_SUPPLY = 'for_supply',
FOR_BORROW = 'for_borrow',
}

export enum MetaMorphoClaims {
BORROW = 'borrow',
SUPPLY = 'supply',
}

export interface GetClaimsParams {
account: Address
chainId: number
claimType: MetaMorphoClaims
logger?: Logger
}

type Proof = Array<`0x${string}`>

export interface MorphoReward {
Expand All @@ -25,6 +38,7 @@ export interface MorphoAggregatedClaims {
export interface MorphoClaims {
claimable: ClaimableMorphoReward[]
claimsAggregated: MorphoAggregatedClaims[]
error?: string
}

type Identity = {
Expand Down

0 comments on commit 64385cc

Please sign in to comment.