1- const { sumTokens2 } = require ( "../helper/unwrapLPs" )
2- const sdk = require ( '@defillama/sdk' )
3-
4- const FUNDRAISING_CONTRACT = '0xf435A133D6cDCb81061F18a4763560f9931DB57D' ;
5- const dataAbi = "function projects(uint256) view returns (uint256 hardCap, uint256 softCap, uint256 totalInvested, uint256 startAt, uint256 preFundDuration, uint256 investorInterestRate, uint256 openStageEndAt, (uint256 platformInterestRate, uint256 totalRepaid, address borrower, uint256 fundedTime, address loanToken, uint8 stage) innerStruct)"
6- const COMING_SOON_STAGE = 0 ;
7- const OPENED_STAGE = 1 ;
8- const CANCELED_STAGE = 2 ;
9- const PREFUNDED_STAGE = 3 ;
10- const FUNDED_STAGE = 4 ;
11- const REPAID_STAGE = 5 ;
1+ const { sumTokens2 } = require ( "../helper/unwrapLPs" ) ;
2+ const sdk = require ( "@defillama/sdk" ) ;
3+
4+ const FUNDRAISING_CONTRACT = "0xf435A133D6cDCb81061F18a4763560f9931DB57D" ;
5+ const dataAbi = "function projects(uint256) view returns (uint256 hardCap, uint256 softCap, uint256 totalInvested, uint256 startAt, uint256 preFundDuration, uint256 investorInterestRate, uint256 openStageEndAt, (uint256 platformInterestRate, uint256 totalRepaid, address borrower, uint256 fundedTime, address loanToken, uint8 stage) innerStruct)" ;
6+
7+ const STAGES = {
8+ COMING_SOON : 0 ,
9+ OPENED : 1 ,
10+ CANCELED : 2 ,
11+ PREFUNDED : 3 ,
12+ FUNDED : 4 ,
13+ REPAID : 5 ,
14+ } ;
15+
16+ const ACTIVE_STAGES = new Set ( [ STAGES . OPENED , STAGES . PREFUNDED , STAGES . FUNDED ] ) ;
17+
18+ async function getTokens ( api ) {
19+ const projects = await api . fetchList ( { target : FUNDRAISING_CONTRACT , lengthAbi : "projectCount" , itemAbi : dataAbi } ) ;
20+ return { projects, tokens : projects . map ( project => project . innerStruct . loanToken ) }
21+ }
1222
1323async function tvl ( api ) {
14- const projectsData = await api . fetchList ( { lengthAbi : 'projectCount' , itemAbi : dataAbi , target : FUNDRAISING_CONTRACT } )
15- const tokens = projectsData . map ( project => project . innerStruct . loanToken )
16- return sumTokens2 ( { api, owner : FUNDRAISING_CONTRACT , tokens, } )
24+ const { tokens } = await getTokens ( api )
25+ return sumTokens2 ( { api, owner : FUNDRAISING_CONTRACT , tokens } ) ;
1726}
1827
1928async function borrowed ( api ) {
20- const projectsData = await api . fetchList ( { lengthAbi : 'projectCount' , itemAbi : dataAbi , target : FUNDRAISING_CONTRACT } )
21- const tokens = projectsData . map ( project => project . innerStruct . loanToken )
22- const tvlApi = new sdk . ChainApi ( { chain : api . chain , block : api . block , } )
23- // Filter only projects that are in OPENED, PREFUNDED, or FUNDED stages
24- // These are actively fundraising or have been funded
25- const filtered = projectsData . filter ( project => {
26- const stage = Number ( project . innerStruct . stage ) ;
27- return stage === OPENED_STAGE || stage === PREFUNDED_STAGE || stage === FUNDED_STAGE ;
28- } )
29- filtered . forEach ( project => api . add ( project . innerStruct . loanToken , project . totalInvested ) )
30- await sumTokens2 ( { api : tvlApi , owner : FUNDRAISING_CONTRACT , tokens, } )
31- api . getBalancesV2 ( ) . subtract ( tvlApi . getBalancesV2 ( ) )
29+ const { projects, tokens } = await getTokens ( api )
30+ projects . forEach ( ( project ) => {
31+ const stage = Number ( project ?. innerStruct ?. stage ?? - 1 ) ;
32+ if ( ACTIVE_STAGES . has ( stage ) ) api . add ( project . innerStruct . loanToken , project . totalInvested )
33+ } )
34+
35+ const tvlApi = new sdk . ChainApi ( { chain : api . chain , block : api . block } ) ;
36+ await sumTokens2 ( { api : tvlApi , owner : FUNDRAISING_CONTRACT , tokens } ) ;
37+ api . getBalancesV2 ( ) . subtract ( tvlApi . getBalancesV2 ( ) ) ;
3238}
3339
3440module . exports = {
35- methodology : ' Funds in the contract not yet withdrawn by the borrower are counted as TVL, rest of the investment is considered borrowed.' ,
36- base : { tvl, borrowed, }
37- } ;
41+ methodology : " Funds still held in the fundraising contract are counted as TVL; the remainder of invested funds is counted as borrowed." ,
42+ base : { tvl, borrowed } ,
43+ } ;
0 commit comments