From 4c9fd81b753206f950463b04a8b93742b21518d8 Mon Sep 17 00:00:00 2001 From: Rafael Ugolini Date: Mon, 16 Oct 2023 21:30:27 +0200 Subject: [PATCH 1/3] fix: apy --- src/processors/oeth/utils.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/processors/oeth/utils.ts b/src/processors/oeth/utils.ts index 972f166d..ed9f4836 100644 --- a/src/processors/oeth/utils.ts +++ b/src/processors/oeth/utils.ts @@ -63,7 +63,7 @@ export async function createRebaseAPY( // get last APY to compare with current one let lastApy = - apies.slice(apies.length - 2).find((apy) => apy.id < dateId) ?? + apies.find((apy) => apy.id < dateId) ?? (await ctx.store.findOne(APY, { where: { id: LessThan(dateId) }, order: { id: 'DESC' }, @@ -71,7 +71,7 @@ export async function createRebaseAPY( // check if there is already an APY for the current date let apy = - apies.slice(apies.length - 1).find((apy) => apy.id === dateId) ?? + apies.find((apy) => apy.id === dateId) ?? (await ctx.store.findOne(APY, { where: { id: dateId } })) // ctx.log.info(`APY: ${dateId} ${apy}, ${lastDateId} ${lastApy}`); // create a new APY if it doesn't exist @@ -106,16 +106,15 @@ export async function createRebaseAPY( apy.rebasingCreditsPerToken = rebaseEvent.rebasingCreditsPerToken // this should normally be 1 day but more secure to calculate it - const diffTime = Math.abs(Date.parse(apy.id) - Date.parse(lastApy.id)) - const dayDiff = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + const diffTime = apy.timestamp.getTime() - lastApy.timestamp.getTime() + const dayDiff = diffTime / (1000 * 60 * 60 * 24) apy.apr = - ((Number(lastApy.rebasingCreditsPerToken) / + (Number(lastApy.rebasingCreditsPerToken) / Number(apy.rebasingCreditsPerToken) - 1) * - 365.25) / - dayDiff - const periods_per_year = 365.25 / dayDiff + (365.25 / dayDiff) + const periods_per_year = 365.25 / Number(dayDiff) apy.apy = (1 + apy.apr / periods_per_year) ** periods_per_year - 1 const last7daysDateId = { @@ -143,5 +142,7 @@ export async function createRebaseAPY( }), ) + // FIXME: we need something better than this + await ctx.store.save(apy) return rebase } From 344427ee46049732d2c40693ec0f01b51bdd8ebe Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 16 Oct 2023 12:39:19 -0700 Subject: [PATCH 2/3] fix: add apy to apies when pulled from db --- src/processors/oeth/utils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/processors/oeth/utils.ts b/src/processors/oeth/utils.ts index ed9f4836..d534792d 100644 --- a/src/processors/oeth/utils.ts +++ b/src/processors/oeth/utils.ts @@ -70,9 +70,13 @@ export async function createRebaseAPY( })) // check if there is already an APY for the current date - let apy = - apies.find((apy) => apy.id === dateId) ?? - (await ctx.store.findOne(APY, { where: { id: dateId } })) + let apy = apies.find((apy) => apy.id === dateId) + if (!apy) { + apy = await ctx.store.findOne(APY, { where: { id: dateId } }) + if (apy) { + apies.push(apy) + } + } // ctx.log.info(`APY: ${dateId} ${apy}, ${lastDateId} ${lastApy}`); // create a new APY if it doesn't exist if (!apy) { From 5a94cae8d594a60b36275e81adb17c494152deef Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 16 Oct 2023 12:40:16 -0700 Subject: [PATCH 3/3] fix: remove save in utils file --- src/processors/oeth/utils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/processors/oeth/utils.ts b/src/processors/oeth/utils.ts index d534792d..c3e54753 100644 --- a/src/processors/oeth/utils.ts +++ b/src/processors/oeth/utils.ts @@ -146,7 +146,5 @@ export async function createRebaseAPY( }), ) - // FIXME: we need something better than this - await ctx.store.save(apy) return rebase }