-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APR] refactor calculatepoolstats from round to date (#320)
* Refactor getBALPriceByRound to DateRange * Refactor tokenYield to recieve start and end * Refactor calculatePoolStats to remove Rounds * Update fetchDataForPoolId.ts * Remove Selected shape for HistoricalChart * Removed dates params from HistoricalChart * Formatting * Update apps/balancer-tools/src/app/apr/(utils)/calculatePoolStats.ts Co-authored-by: José Ribeiro <[email protected]> * Refactor getBALPriceForDateRange to recieve timestamp * Refactor unixtimestamp as arg * Fix getBALPriceForDateRange --------- Co-authored-by: José Ribeiro <[email protected]>
- Loading branch information
1 parent
0fae013
commit b2782b5
Showing
12 changed files
with
188 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
apps/balancer-tools/src/app/apr/(utils)/getBALPriceByRound.ts
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
apps/balancer-tools/src/app/apr/(utils)/getBALPriceForDateRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { networkFor } from "@bleu-balancer-tools/utils"; | ||
|
||
import { withCache } from "#/lib/cache"; | ||
import { DefiLlamaAPI } from "#/lib/coingecko"; | ||
|
||
import { calculateDaysBetween } from "../api/(utils)/date"; | ||
|
||
const BAL_TOKEN_ADDRESS = "0xba100000625a3754423978a60c9317c58a424e3d"; | ||
const BAL_TOKEN_NETWORK = 1; | ||
|
||
/** | ||
* Calculates the average of an array of numbers. | ||
*/ | ||
const calculateAverage = (arr: number[]) => | ||
arr.reduce((sum, val) => sum + val, 0) / arr.length; | ||
|
||
export const getBALPriceForDateRange = withCache( | ||
async function getBALPriceByRoundFn( | ||
startAtTimestamp: number, | ||
endAtTimestamp: number, | ||
) { | ||
const numberOfDays = calculateDaysBetween( | ||
startAtTimestamp * 1000, | ||
endAtTimestamp * 1000, | ||
); | ||
const pricePromises = Array.from({ length: numberOfDays }, (_) => { | ||
return getTokenPriceByDate( | ||
endAtTimestamp * 1000, | ||
BAL_TOKEN_ADDRESS, | ||
BAL_TOKEN_NETWORK, | ||
); | ||
}); | ||
try { | ||
const prices = await Promise.all(pricePromises); | ||
return calculateAverage(prices); | ||
} catch (error) { | ||
// TODO: BAL-782 - Add sentry here | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`Error fetching BAL price between ${startAtTimestamp} and ${endAtTimestamp}`, | ||
); | ||
throw error; | ||
} | ||
}, | ||
); | ||
|
||
export const getTokenPriceByDate = withCache(async function getTokenPriceByDate( | ||
dateTimestamp: number, | ||
tokenAddress: string, | ||
tokenNetwork: number, | ||
) { | ||
const token = `${networkFor(tokenNetwork).toLowerCase()}:${tokenAddress}`; | ||
const relevantDateForPrice = Math.min(Date.now(), dateTimestamp); | ||
const response = await new DefiLlamaAPI().getHistoricalPrice( | ||
new Date(relevantDateForPrice), | ||
[token], | ||
); | ||
|
||
return response.coins[token]?.price; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.