@@ -4,18 +4,25 @@ import {
44 conetDepinProvider ,
55 conetProvider ,
66 ethProvider ,
7+ XMLHttpRequestTimeout ,
8+ solanaRPC ,
79} from "../utils/constants" ;
810import {
911 CoNET_Data ,
1012 processingBlock ,
1113 setCoNET_Data ,
1214 setProcessingBlock ,
15+ lastProceeeTime ,
16+ setLastProceeeTime
1317} from "../utils/globals" ;
1418import contracts from "../utils/contracts" ;
1519import { initProfileTokens } from "../utils/utils" ;
16- import { getVpnTimeUsed } from "./wallets" ;
20+ import { getVpnTimeUsed } from "./wallets" ;
21+ import { Connection , PublicKey , Keypair } from "@solana/web3.js"
22+ import Bs58 from "bs58"
1723
1824let epoch = 0 ;
25+ const SOLANA_CONNECTION = new Connection ( solanaRPC )
1926
2027const listenProfileVer = async ( callback : ( profiles : profile [ ] ) => void ) => {
2128 epoch = await conetProvider . getBlockNumber ( ) ;
@@ -25,8 +32,10 @@ const listenProfileVer = async (callback: (profiles: profile[]) => void) => {
2532 epoch ++ ;
2633
2734 if ( processingBlock === true ) return ;
28-
29- if ( block % 10 === 0 ) {
35+
36+ const currentTime = new Date ( ) . getTime ( )
37+ // over 30 seconds!
38+ if ( currentTime > lastProceeeTime + XMLHttpRequestTimeout ) {
3039 setProcessingBlock ( true ) ;
3140
3241 const profiles = CoNET_Data ?. profiles ;
@@ -36,20 +45,106 @@ const listenProfileVer = async (callback: (profiles: profile[]) => void) => {
3645 const runningList : any [ ] = [ ] ;
3746
3847 runningList . push ( getProfileAssets ( profiles [ 0 ] ) ) ;
39-
48+ runningList . push ( getSolanaAssets ( profiles [ 1 ] ) )
4049 await Promise . all ( runningList ) ;
4150
4251 await getVpnTimeUsed ( ) ;
4352
4453 if ( CoNET_Data ?. profiles [ 0 ] ) callback ( CoNET_Data ?. profiles ) ;
45-
46- setProcessingBlock ( false ) ;
54+ setProcessingBlock ( false )
55+ setLastProceeeTime ( new Date ( ) . getTime ( ) )
4756 }
4857 }
4958 } ) ;
5059
5160 epoch = await conetProvider . getBlockNumber ( ) ;
5261} ;
62+ const scan_spl_balance = async ( walletAddr : string , tokenAddress : string ) => {
63+ try {
64+ const TOKEN_PROGRAM_ID = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" ; // Solana SPL Token Program ID
65+
66+ const payload = {
67+ jsonrpc : "2.0" ,
68+ id : 1 ,
69+ method : "getTokenAccountsByOwner" ,
70+ params : [
71+ walletAddr ,
72+ { programId : TOKEN_PROGRAM_ID } ,
73+ { encoding : "jsonParsed" } ,
74+ ] ,
75+ } ;
76+
77+ const response = await fetch ( solanaRPC , {
78+ method : "POST" ,
79+ headers : {
80+ "Content-Type" : "application/json" ,
81+ } ,
82+ body : JSON . stringify ( payload ) ,
83+ } ) ;
84+
85+ if ( ! response . ok ) {
86+ throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
87+ }
88+
89+ const data = await response . json ( ) ;
90+ const tokenAccounts = data . result ?. value ?? [ ] ;
91+
92+ for ( let account of tokenAccounts ) {
93+ const info = account . account . data . parsed . info ;
94+ if ( info . mint === tokenAddress ) {
95+ return info . tokenAmount . uiAmount ; // Return balance in tokens
96+ }
97+ }
98+
99+ return 0 ; // No balance found
100+ } catch ( error ) {
101+ console . error ( "Error fetching SPL balance:" , error ) ;
102+ return false ;
103+ }
104+ } ;
105+ const getSolanaAssets = async ( profile : profile ) => {
106+
107+ const publciKey = new PublicKey ( profile . keyID )
108+ const [ balanceSo , balanceSP ] = await Promise . all ( [
109+ SOLANA_CONNECTION . getBalance ( publciKey ) ,
110+ scan_spl_balance ( profile . keyID , contracts . SP . address )
111+ ] )
112+
113+ if ( profile . tokens ?. solana ) {
114+ const solanaObj = profile . tokens . solana
115+ profile . tokens . solana . balance =
116+ parseFloat (
117+ ethers . formatUnits ( balanceSo . toString ( ) , solanaObj . decimal ) . toString ( )
118+ ) . toFixed ( 6 )
119+ } else {
120+ profile . tokens . solana = {
121+ balance :
122+ parseFloat (
123+ ethers . formatUnits ( balanceSo . toString ( ) , 9 ) . toString ( )
124+ ) . toFixed ( 6 ) ,
125+ network : "Solana" ,
126+ decimal : 9 ,
127+ contract : "" ,
128+ name : "Solana" ,
129+ }
130+ }
131+
132+ if ( profile . tokens ?. SP ) {
133+ const spOBJ = profile . tokens . SP
134+ profile . tokens . SP . balance = balanceSP ? balanceSP . toFixed ( 4 ) : "0"
135+ } else {
136+ profile . tokens . SP = {
137+ balance : balanceSP ? balanceSP . toFixed ( 4 ) : "0" ,
138+ network : "Solana" ,
139+ decimal : contracts . SP . decimal ,
140+ contract : contracts . SP . address ,
141+ name : "Solana" ,
142+ }
143+ }
144+
145+
146+ }
147+
53148
54149const getProfileAssets = async ( profile : profile ) => {
55150 const key = profile . keyID ;
@@ -59,45 +154,45 @@ const getProfileAssets = async (profile: profile) => {
59154 profile . tokens = initProfileTokens ( ) ;
60155 }
61156
62- const [ cCNTP , conet , conetDepin , conet_eth , eth ] = await Promise . all ( [
63- scanCCNTP ( key ) ,
64- scanCONETHolesky ( key ) ,
157+ const [ conetDepin , conet_eth , eth ] = await Promise . all ( [
158+ // scanCCNTP(key),
159+ // scanCONETHolesky(key),
65160 scanCONETDepin ( key ) ,
66161 scanConetETH ( key ) ,
67162 scanETH ( key ) ,
68163 ] ) ;
69164
70- if ( profile . tokens ?. cCNTP ) {
71- profile . tokens . cCNTP . balance =
72- cCNTP === false ? "" : parseFloat ( ethers . formatEther ( cCNTP ) ) . toFixed ( 6 ) ;
73- } else {
74- profile . tokens . cCNTP = {
75- balance :
76- cCNTP === false
77- ? ""
78- : parseFloat ( ethers . formatEther ( cCNTP ) ) . toFixed ( 6 ) ,
79- network : "CONET Holesky" ,
80- decimal : 18 ,
81- contract : contracts . ClaimableConetPoint . address ,
82- name : "cCNTP" ,
83- } ;
84- }
85-
86- if ( profile . tokens ?. conet ) {
87- profile . tokens . conet . balance =
88- conet === false ? "" : parseFloat ( ethers . formatEther ( conet ) ) . toFixed ( 6 ) ;
89- } else {
90- profile . tokens . conet = {
91- balance :
92- conet === false
93- ? ""
94- : parseFloat ( ethers . formatEther ( conet ) ) . toFixed ( 6 ) ,
95- network : "CONET Holesky" ,
96- decimal : 18 ,
97- contract : "" ,
98- name : "conet" ,
99- } ;
100- }
165+ // if (profile.tokens?.cCNTP) {
166+ // profile.tokens.cCNTP.balance =
167+ // cCNTP === false ? "" : parseFloat(ethers.formatEther(cCNTP)).toFixed(6);
168+ // } else {
169+ // profile.tokens.cCNTP = {
170+ // balance:
171+ // cCNTP === false
172+ // ? ""
173+ // : parseFloat(ethers.formatEther(cCNTP)).toFixed(6),
174+ // network: "CONET Holesky",
175+ // decimal: 18,
176+ // contract: contracts.ClaimableConetPoint.address,
177+ // name: "cCNTP",
178+ // };
179+ // }
180+
181+ // if (profile.tokens?.conet) {
182+ // profile.tokens.conet.balance =
183+ // conet === false ? "" : parseFloat(ethers.formatEther(conet)).toFixed(6);
184+ // } else {
185+ // profile.tokens.conet = {
186+ // balance:
187+ // conet === false
188+ // ? ""
189+ // : parseFloat(ethers.formatEther(conet)).toFixed(6),
190+ // network: "CONET Holesky",
191+ // decimal: 18,
192+ // contract: "",
193+ // name: "conet",
194+ // };
195+ // }
101196
102197 if ( profile . tokens ?. conetDepin ) {
103198 profile . tokens . conetDepin . balance =
0 commit comments