@@ -8,6 +8,8 @@ const comptrollerABI = {
88 getAllMarkets : "address[]:getAllMarkets" ,
99 accrueInterest : "event AccrueInterest(uint256 cashPrior,uint256 interestAccumulated,uint256 borrowIndex,uint256 totalBorrows)" ,
1010 reserveFactor : "uint256:reserveFactorMantissa" ,
11+ exchangeRateCurrent : "uint256:exchangeRateCurrent" ,
12+ totalSupply : "uint256:totalSupply" ,
1113} ;
1214
1315export async function getFees ( market : string , { createBalances, api, getLogs, } : FetchOptions , {
@@ -49,6 +51,47 @@ export async function getFees(market: string, { createBalances, api, getLogs, }:
4951 return { dailyFees, dailyRevenue }
5052}
5153
54+ export async function getFeesUseExchangeRates ( market : string , { createBalances, api, fromApi, toApi, } : FetchOptions , {
55+ dailyFees,
56+ dailyRevenue,
57+ abis = { } ,
58+ } : {
59+ dailyFees ?: sdk . Balances ,
60+ dailyRevenue ?: sdk . Balances ,
61+ abis ?: any
62+ } ) {
63+ if ( ! dailyFees ) dailyFees = createBalances ( )
64+ if ( ! dailyRevenue ) dailyRevenue = createBalances ( )
65+ const markets = await api . call ( { target : market , abi : comptrollerABI . getAllMarkets , } )
66+ const underlyings = await api . multiCall ( { calls : markets , abi : comptrollerABI . underlying , permitFailure : true , } ) ;
67+ underlyings . forEach ( ( underlying , index ) => {
68+ if ( ! underlying ) underlyings [ index ] = ADDRESSES . null
69+ } )
70+ const reserveFactors = await api . multiCall ( { calls : markets , abi : abis . reserveFactor ?? comptrollerABI . reserveFactor , } ) ;
71+
72+ const marketExchangeRatesBefore = await fromApi . multiCall ( { calls : markets , abi : comptrollerABI . exchangeRateCurrent , } ) ;
73+ const marketExchangeRatesAfter = await toApi . multiCall ( { calls : markets , abi : comptrollerABI . exchangeRateCurrent , } ) ;
74+ const totalSupplies = await toApi . multiCall ( { calls : markets , abi : comptrollerABI . totalSupply , } ) ;
75+ const underlyingDecimals = await toApi . multiCall ( { calls : underlyings , abi : 'uint8:decimals' , } ) ;
76+
77+ for ( let i = 0 ; i < markets . length ; i ++ ) {
78+ const underlying = underlyings [ i ] ;
79+ const underlyingDecimal = Number ( underlyingDecimals [ i ] ) ;
80+ const reserveFactor = reserveFactors [ i ] ;
81+ const rateGrowth = Number ( marketExchangeRatesAfter [ i ] ) - Number ( marketExchangeRatesBefore [ i ] )
82+ if ( rateGrowth > 0 ) {
83+ const mantissa = 18 + underlyingDecimal - 8
84+ const interestAccumulated = rateGrowth * Number ( totalSupplies [ i ] ) * ( 10 ** underlyingDecimal ) / ( 10 ** mantissa ) / 1e8
85+ const revenueAccumulated = interestAccumulated * reserveFactor / 1e18
86+
87+ dailyFees ! . add ( underlying , interestAccumulated , METRIC . BORROW_INTEREST ) ;
88+ dailyRevenue ! . add ( underlying , revenueAccumulated , METRIC . BORROW_INTEREST ) ;
89+ }
90+ }
91+
92+ return { dailyFees, dailyRevenue }
93+ }
94+
5295export function getFeesExport ( market : string ) {
5396 return ( async ( timestamp : number , _ : any , options : FetchOptions ) => {
5497 const { dailyFees, dailyRevenue } = await getFees ( market , options , { } )
@@ -62,12 +105,12 @@ export function getFeesExport(market: string) {
62105 } ) as Fetch
63106}
64107
65- export function compoundV2Export ( config : IJSON < string > ) {
108+ export function compoundV2Export ( config : IJSON < string > , useExchangeRate ?: boolean ) {
66109 const exportObject : BaseAdapter = { }
67110 Object . entries ( config ) . map ( ( [ chain , market ] ) => {
68111 exportObject [ chain ] = {
69112 fetch : ( async ( options : FetchOptions ) => {
70- const { dailyFees, dailyRevenue } = await getFees ( market , options , { } )
113+ const { dailyFees, dailyRevenue } = useExchangeRate ? await getFeesUseExchangeRates ( market , options , { } ) : await getFees ( market , options , { } )
71114 const dailyHoldersRevenue = dailyRevenue
72115 const dailySupplySideRevenue = options . createBalances ( )
73116 dailySupplySideRevenue . addBalances ( dailyFees )
0 commit comments