@@ -2,7 +2,8 @@ const ADDRESSES = require('../helper/coreAssets.json')
22const { getProvider, sumTokens2 } = require ( "../helper/solana" ) ;
33const { PublicKey } = require ( "@solana/web3.js" ) ;
44const anchor = require ( "@project-serum/anchor" ) ;
5- const bs58 = require ( 'bs58' ) ;
5+
6+ const bs58Encode = anchor . utils . bytes . bs58 . encode ;
67
78const solProgramId = "CRSeeBqjDnm3UPefJ9gxrtngTsnQRhEJiTA345Q83X3v" ;
89const usdcProgramId = "1avaAUcjccXCjSZzwUvB2gS3DzkkieV2Mw8CjdN65uu" ;
@@ -11,8 +12,8 @@ const TOKEN_PROGRAM_ID = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ
1112const SPL_ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey ( "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" ) ;
1213
1314const edgeCaseTimestamps = [
14- { start : '2024-04-23' , end : 1713885480 } , // 10:32 AM - 10:58 AM ET on April 23, 2024
15- { start : '2024-04-23' , end : 1713876060 } , // 8:15 AM - 8:41 AM ET on April 23, 2024
15+ { start : '2024-04-23' , end : 1713885480 } ,
16+ { start : '2024-04-23' , end : 1713876060 } ,
1617] ;
1718
1819const idleAccount = new PublicKey ( "bkhAyULeiXwju7Zmy4t3paDHtVZjNaofVQ4VgEdTWiE" ) ;
@@ -26,21 +27,16 @@ const iscPoolAccount = new PublicKey("CrsxVEF7YNGAk9QwwbB2vuesUWoDopfgFAhA9apoCJ
2627
2728function getPositionFilters ( ) {
2829 const sizeFilter = { dataSize : 178 } ;
30+
31+ const zeroBuf = Buffer . alloc ( 8 ) ;
32+
2933 const value = BigInt ( 9999 ) ;
3034 const valueBuffer = Buffer . alloc ( 8 ) ;
3135 valueBuffer . writeBigUInt64LE ( value , 0 ) ;
32- const val0Filter = {
33- memcmp : {
34- offset : 40 ,
35- bytes : bs58 . encode ( Buffer . from ( new Uint8Array ( 8 ) ) ) ,
36- } ,
37- }
38- const val9999Filter = {
39- memcmp : {
40- offset : 40 ,
41- bytes : bs58 . encode ( valueBuffer ) ,
42- } ,
43- }
36+
37+ const val0Filter = { memcmp : { offset : 40 , bytes : bs58Encode ( zeroBuf ) } }
38+ const val9999Filter = { memcmp : { offset : 40 , bytes : bs58Encode ( valueBuffer ) } }
39+
4440 return [
4541 [ sizeFilter , val0Filter ] ,
4642 [ sizeFilter , val9999Filter ] ,
@@ -49,52 +45,61 @@ function getPositionFilters() {
4945
5046async function tvl ( api ) {
5147 const provider = getProvider ( ) ;
52- for ( const programId of [ solProgramId , usdcProgramId ] ) {
5348
49+ for ( const programId of [ solProgramId , usdcProgramId ] ) {
5450 const program = new anchor . Program ( lavarageIDL , programId , provider ) ;
5551 const pools = await program . account . pool . all ( )
52+
5653 const poolMap = { }
57- pools . forEach ( ( pool ) => {
58- poolMap [ pool . publicKey . toBase58 ( ) ] = pool . account . collateralType . toBase58 ( )
59- } )
54+ pools . forEach ( ( pool ) => { poolMap [ pool . publicKey . toBase58 ( ) ] = pool . account . collateralType . toBase58 ( ) } )
55+
6056 for ( const filter of getPositionFilters ( ) ) {
6157 const positions = await program . account . position . all ( filter )
6258 positions . forEach ( ( { account } ) => {
6359 let { closeStatusRecallTimestamp, pool, collateralAmount, timestamp } = account
6460 const token = poolMap [ pool . toBase58 ( ) ]
65- const closeTS = closeStatusRecallTimestamp . toNumber ( )
66- const ts = timestamp . toNumber ( )
61+ const closeTS = Number ( closeStatusRecallTimestamp ?. toNumber ?. ( ) ?? 0 )
62+ const ts = Number ( timestamp ?. toNumber ?. ( ) ?? 0 )
63+
6764 if ( ( closeTS && ! isWithinEdgeCaseTimeRange ( ts ) ) || ! token ) return ;
6865 api . add ( token , collateralAmount . toString ( ) )
6966 } )
7067 }
7168 }
69+
7270 return sumTokens2 ( {
73- balances : api . getBalances ( ) , tokenAccounts : [
71+ balances : api . getBalances ( ) ,
72+ tokenAccounts : [
7473 getAssociatedTokenAddress ( usdcAddress , usdcPoolAccount ) ,
7574 getAssociatedTokenAddress ( iscAddress , iscPoolAccount ) ,
76- ] , solOwners : [
77- deployedAccount , pendingUnstakeAccount ,
75+ ] ,
76+ solOwners : [
77+ deployedAccount ,
78+ pendingUnstakeAccount ,
7879 ]
7980 } )
8081}
8182
82- function getAssociatedTokenAddress ( mint , owner , ) {
83- const [ associatedTokenAddress ] = PublicKey . findProgramAddressSync ( [ owner . toBuffer ( ) , TOKEN_PROGRAM_ID . toBuffer ( ) , mint . toBuffer ( ) ] , SPL_ASSOCIATED_TOKEN_PROGRAM_ID ) ;
83+ function getAssociatedTokenAddress ( mint , owner ) {
84+ const [ associatedTokenAddress ] = PublicKey . findProgramAddressSync (
85+ [ owner . toBuffer ( ) , TOKEN_PROGRAM_ID . toBuffer ( ) , mint . toBuffer ( ) ] ,
86+ SPL_ASSOCIATED_TOKEN_PROGRAM_ID
87+ ) ;
8488 return associatedTokenAddress ;
8589}
8690
8791function isWithinEdgeCaseTimeRange ( closeTimestamp ) {
8892 return edgeCaseTimestamps . some (
89- ( { start, end } ) => closeTimestamp >= start && closeTimestamp <= end
93+ ( { start, end } ) => {
94+ const startEpoch = typeof start === 'number' ? start : Math . floor ( ( Date . parse ( start ) || 0 ) / 1000 ) ;
95+ return closeTimestamp >= startEpoch && closeTimestamp <= end
96+ }
9097 ) ;
9198}
9299
93100module . exports = {
94101 timetravel : false ,
95- solana : {
96- tvl,
97- } ,
102+ solana : { tvl } ,
98103} ;
99104
100105const lavarageIDL = {
0 commit comments