@@ -19,7 +19,8 @@ import {
1919} from '@originprotocol/squid-utils'
2020import { getLatestExchangeRateForDate } from '@shared/post-processors/exchange-rates/exchange-rates'
2121import { baseAddresses } from '@utils/addresses-base'
22- import { armProducts , otokenProducts } from '@utils/products'
22+ import { plumeAddresses } from '@utils/addresses-plume'
23+ import { ProductName , armProducts , otokenProducts } from '@utils/products'
2324
2425const startDate = '2022-01-01'
2526
@@ -54,53 +55,21 @@ export const protocolProcessor = defineProcessor({
5455 dailyStat . rateUSD = await getLatestExchangeRateForDate ( ctx , 'ETH_USD' , dailyStat . timestamp ) . then (
5556 ( rate ) => ( rate ?. rate ?? 0n ) * 10n ** 10n ,
5657 )
57- dailyStat . earningTVL = sumBigIntBy ( dateDetails , 'earningTVL ' )
58- dailyStat . tvl = sumBigIntBy ( dateDetails , 'tvl' )
58+ dailyStat . earningTvl = sumBigIntBy ( dateDetails , 'earningTvl ' )
59+ dailyStat . tvl = sumBigIntBy ( dateDetails , 'tvl' ) - sumBigIntBy ( dateDetails , 'inheritedTvl' )
5960 dailyStat . revenue = sumBigIntBy ( dateDetails , 'revenue' )
61+ dailyStat . yield = sumBigIntBy ( dateDetails , 'yield' ) + dailyStat . revenue
6062
61- // Find overlapping TVL
62- const superOETHbWrappedOETH = await ctx . store . findOne ( StrategyBalance , {
63- where : {
64- strategy : baseAddresses . superOETHb . strategies . bridgedWOETH ,
65- timestamp : LessThanOrEqual ( dayjs . utc ( date ) . endOf ( 'day' ) . toDate ( ) ) ,
66- } ,
67- order : {
68- timestamp : 'desc' ,
69- } ,
70- } )
71- const superOETHbWrappedOethBalance = superOETHbWrappedOETH ?. balanceETH ?? 0n
72-
73- // Adjust TVL for overlapping strategy balance.
74- dailyStat . tvl -= superOETHbWrappedOethBalance
75- dailyStat . apy =
76- dailyStat . tvl === 0n
77- ? 0
78- : dateDetails . reduce ( ( acc , detail ) => {
79- // We lessen the OETH TVL for APY calculation since that APY is also included in Super OETHb.
80- const tvl = detail . id === 'OETH' ? detail . tvl - superOETHbWrappedOethBalance : detail . tvl
81- return acc + detail . apy * Number ( tvl )
82- } , 0 ) / Number ( dailyStat . tvl )
83-
84- dailyStat . meta = {
85- tvlAdjustments : superOETHbWrappedOETH
86- ? [
87- {
88- name : 'Super OETHb Wrapped OETH Strategy' ,
89- blockNumber : superOETHbWrappedOETH . blockNumber ,
90- timestamp : superOETHbWrappedOETH . timestamp ,
91- balanceETH : superOETHbWrappedOETH . balanceETH . toString ( ) ,
92- } ,
93- ]
94- : [ ] ,
95- }
63+ const apyYield = dailyStat . yield - dailyStat . revenue
64+ dailyStat . apy = dailyStat . earningTvl !== 0n ? ( Number ( apyYield ) / Number ( dailyStat . earningTvl ) ) * 365 : 0
9665
9766 dailyStats . push ( dailyStat )
9867 }
9968 await ctx . store . upsert ( dailyStats )
10069 } ,
10170} )
10271
103- const getLatestProtocolDailyStatDetail = async ( ctx : Context , product : string ) => {
72+ const getLatestProtocolDailyStatDetail = async ( ctx : Context , product : ProductName ) => {
10473 const latestProtocolDailyStatDetail = await ctx . store . findOne ( ProtocolDailyStatDetail , {
10574 order : {
10675 date : 'desc' ,
@@ -126,10 +95,12 @@ const getProtocolDailyStat = async (ctx: Context, date: string) => {
12695 date,
12796 timestamp : dayjs . utc ( date ) . endOf ( 'day' ) . toDate ( ) ,
12897 rateUSD : 0n ,
129- earningTVL : 0n ,
98+ earningTvl : 0n ,
13099 tvl : 0n ,
100+ yield : 0n ,
131101 revenue : 0n ,
132102 apy : 0 ,
103+ meta : { } ,
133104 } )
134105}
135106const getProtocolDailyStatDetail = async ( ctx : Context , date : string , product : string ) => {
@@ -146,10 +117,14 @@ const getProtocolDailyStatDetail = async (ctx: Context, date: string, product: s
146117 product,
147118 timestamp : dayjs . utc ( date ) . endOf ( 'day' ) . toDate ( ) ,
148119 rateUSD : 0n ,
149- earningTVL : 0n ,
120+ earningTvl : 0n ,
150121 tvl : 0n ,
122+ yield : 0n ,
151123 revenue : 0n ,
152124 apy : 0 ,
125+ inheritedTvl : 0n ,
126+ inheritedYield : 0n ,
127+ inheritedRevenue : 0n ,
153128 } )
154129}
155130
@@ -161,16 +136,25 @@ const getOTokenDetails = async (
161136 otokenAddress,
162137 } : {
163138 processorId : string
164- product : string
139+ product : ProductName
165140 otokenAddress : string
166141 } ,
167142) => {
168143 const last = await getLatestProtocolDailyStatDetail ( ctx , product )
144+
145+ // Since we depend on superOETHb and superOETHp, we need to continue updating
146+ // our product's details until our dependencies are all caught up.
147+ const lastSuperOETHb = await getLatestProtocolDailyStatDetail ( ctx , 'superOETHb' )
148+ const lastSuperOETHp = await getLatestProtocolDailyStatDetail ( ctx , 'superOETHp' )
149+ const lastDates = [ last ?. date , lastSuperOETHb ?. date , lastSuperOETHp ?. date ] . filter ( Boolean ) as string [ ]
150+ const oldestDate = lastDates . length > 0 ? lastDates . reduce ( ( min , d ) => ( d < min ? d : min ) , lastDates [ 0 ] ) : undefined
151+
169152 const status = await ctx . store . findOne ( ProcessingStatus , { where : { id : processorId } } )
170153 if ( ! status ) return [ ]
171154
172155 const details : ProtocolDailyStatDetail [ ] = [ ]
173- const dates = getDatesBetween ( last ?. date ?? startDate , getDateForTimestamp ( status . timestamp . valueOf ( ) ) )
156+
157+ const dates = getDatesBetween ( oldestDate ?? startDate , getDateForTimestamp ( status . timestamp . valueOf ( ) ) )
174158 const dailyStats = await ctx . store . find ( OTokenDailyStat , { where : { date : In ( dates ) , otoken : otokenAddress } } )
175159 for ( const date of dates ) {
176160 const otokenDailyStat = dailyStats . find ( ( d ) => d . date === date )
@@ -179,11 +163,64 @@ const getOTokenDetails = async (
179163 return details
180164 }
181165 const detail = await getProtocolDailyStatDetail ( ctx , date , product )
166+ const eth = ( value : bigint ) => ( value * otokenDailyStat . rateETH ) / BigInt ( 10 ** 18 )
182167 detail . rateUSD = otokenDailyStat . rateUSD
183- detail . earningTVL = ( otokenDailyStat . rebasingSupply * otokenDailyStat . rateETH ) / BigInt ( 10 ** 18 )
184- detail . tvl = ( otokenDailyStat . totalSupply * otokenDailyStat . rateETH ) / BigInt ( 10 ** 18 )
185- detail . revenue = ( otokenDailyStat . fees * otokenDailyStat . rateETH ) / BigInt ( 10 ** 18 )
168+ detail . earningTvl = eth ( otokenDailyStat . rebasingSupply )
169+ detail . tvl = eth ( otokenDailyStat . totalSupply )
170+ detail . yield = eth ( otokenDailyStat . yield + otokenDailyStat . fees )
171+ detail . revenue = eth ( otokenDailyStat . fees )
186172 detail . apy = otokenDailyStat . apy
173+ detail . inheritedTvl = 0n
174+ detail . inheritedYield = 0n
175+ detail . inheritedRevenue = 0n
176+ detail . bridgedTvl = 0n
177+
178+ const getLatestStrategyBalance = async ( ctx : Context , strategy : string , date : string ) => {
179+ return await ctx . store . findOne ( StrategyBalance , {
180+ where : {
181+ strategy,
182+ timestamp : LessThanOrEqual ( dayjs . utc ( date ) . endOf ( 'day' ) . toDate ( ) ) ,
183+ } ,
184+ order : {
185+ timestamp : 'desc' ,
186+ } ,
187+ } )
188+ }
189+
190+ if ( detail . product === 'OETH' ) {
191+ // Pull superOETHp and superOETHb overlapping details.
192+ const superOETHbWrappedOETH = await getLatestStrategyBalance (
193+ ctx ,
194+ baseAddresses . superOETHb . strategies . bridgedWOETH ,
195+ date ,
196+ )
197+ const superOETHpWrappedOETH = await getLatestStrategyBalance (
198+ ctx ,
199+ plumeAddresses . superOETHp . strategies . bridgedWOETH ,
200+ date ,
201+ )
202+
203+ detail . inheritedTvl = ( superOETHbWrappedOETH ?. balanceETH ?? 0n ) + ( superOETHpWrappedOETH ?. balanceETH ?? 0n )
204+ detail . inheritedYield = detail . earningTvl !== 0n ? ( detail . yield * detail . inheritedTvl ) / detail . earningTvl : 0n
205+ detail . inheritedRevenue =
206+ detail . earningTvl !== 0n ? ( detail . revenue * detail . inheritedTvl ) / detail . earningTvl : 0n
207+ detail . bridgedTvl = ( superOETHbWrappedOETH ?. balanceETH ?? 0n ) + ( superOETHpWrappedOETH ?. balanceETH ?? 0n )
208+ } else if ( detail . product === 'superOETHb' ) {
209+ const superOETHbWrappedOETH = await getLatestStrategyBalance (
210+ ctx ,
211+ baseAddresses . superOETHb . strategies . bridgedWOETH ,
212+ date ,
213+ )
214+ detail . bridgedTvl = superOETHbWrappedOETH ?. balanceETH ?? 0n
215+ } else if ( detail . product === 'superOETHp' ) {
216+ const superOETHpWrappedOETH = await getLatestStrategyBalance (
217+ ctx ,
218+ plumeAddresses . superOETHp . strategies . bridgedWOETH ,
219+ date ,
220+ )
221+ detail . bridgedTvl = superOETHpWrappedOETH ?. balanceETH ?? 0n
222+ }
223+
187224 details . push ( detail )
188225 }
189226 return details
@@ -196,7 +233,7 @@ const getArmDetails = async (
196233 armAddress,
197234 } : {
198235 processorId : string
199- product : string
236+ product : ProductName
200237 armAddress : string
201238 } ,
202239) => {
@@ -215,10 +252,14 @@ const getArmDetails = async (
215252 }
216253 const detail = await getProtocolDailyStatDetail ( ctx , date , product )
217254 detail . rateUSD = BigInt ( Math . round ( armDailyStat . rateUSD * 1e18 ) )
218- detail . earningTVL = armDailyStat . totalSupply
255+ detail . earningTvl = armDailyStat . totalSupply
219256 detail . tvl = armDailyStat . totalSupply
257+ detail . yield = armDailyStat . yield + armDailyStat . fees
220258 detail . revenue = armDailyStat . fees
221259 detail . apy = armDailyStat . apy
260+ detail . inheritedTvl = 0n
261+ detail . inheritedYield = 0n
262+ detail . inheritedRevenue = 0n
222263 details . push ( detail )
223264 }
224265 return details
0 commit comments