3. Call the subgraph API to get the TVL of the corresponding pool.
https://graph.sirius.finance/static/volume.json
4. Get the single LP token price using the TVL to divide the total supply of the LP token.
LPPrice = poolTVL / LPTokenTotalSupply
5. Retrieve the total staked TVL of the farm.
farmStakedValue = LpAmount * LPPrice
6. Retrieve the base rewards in SRS.
// SRS price is fixed to 0.1 at this momentconstSRS_PRICE=0.1;// Sirius' rewards reset at 0:00 UTC every Thursday, so we need to get the nearest time point.functiongetNearThursday() {conststr=newDate()constToday0=moment(str).utcOffset(0)let ago =0Today0.set({ hour:0, minute:0, second:0, millisecond:0 })constday=newDate(str).getUTCDay()constdiff=0if (day <4) { ago =7- (4- day) } else { ago = day -4 }constfinal_time=Today0.subtract(ago,"days").unix()return final_time}constnearThursday=getNearThursday()// srsContract.rate returns of how many SRS is mined per second.// so the following formula is to determine how many SRS can be retrieved by the farm for a year.oneYearRewardValue =srsContract.rate() *60*60*24*365*SRS_PRICE*FarmControllerContract.gaugeRelativeWeight( FarmAddress, nearThursday,)// the reward for each dollar.constbaseRewards= oneYearRewardValue / farmStakedValue
7. Get farm extra rewards. Besides SRS base reward, some farms could provide extra rewards as well such as incentive programs or partnership programs. please notice that some rewarding tokens might also use fixed price.
// get all extra reward tokens.constrewardCount=FarmContract.rewardCount()constrewardTokensList=FarmContract.getRewardTokensList(0, rewardCount,)// calculate APR for each token.constextraRewards=0;for (tokenAddress in rewardTokensList) { // this token price can be fixed just like SRS, or it needs to be retrieved from external resources. (DEX/CEX/Oracle, etc).
consttokenPrice=0.01; constsecondRate=FarmContract.rewardData(tokenAddress)constrewardPerYearValue=secondRate.rate *86400*365* tokenPriceconsttokenExtraRewards= rewardPerYearValue / farmStakedValue extraRewards += tokenExtraRewards;}// here goes the total extra rewards.cosole.log(extraRewards);