@@ -21,9 +21,7 @@ class SolanaTokenApiException implements Exception {
2121 String toString () => 'SolanaTokenApiException: $message ' ;
2222}
2323
24- /// Response wrapper for Solana token API calls.
25- ///
26- /// Follows the pattern that the result is either value or exception
24+ /// Result wrapper for Solana token API calls.
2725class SolanaTokenApiResponse <T > {
2826 final T ? value;
2927 final Exception ? exception;
@@ -127,8 +125,6 @@ class SolanaTokenAPI {
127125
128126 RpcClient ? _rpcClient;
129127
130- /// Initialize with a configured RPC client.
131- /// This should be called with the same RPC client from SolanaWallet.
132128 void initializeRpcClient (RpcClient rpcClient) {
133129 _rpcClient = rpcClient;
134130 }
@@ -141,9 +137,6 @@ class SolanaTokenAPI {
141137 }
142138 }
143139
144- /// Get token accounts owned by a wallet address for a specific mint.
145- ///
146- /// Returns a list of token account addresses owned by the wallet.
147140 Future <SolanaTokenApiResponse <List <String >>> getTokenAccountsByOwner (
148141 String ownerAddress, {
149142 String ? mint,
@@ -155,14 +148,12 @@ class SolanaTokenAPI {
155148
156149 final result = await _rpcClient! .getTokenAccountsByOwner (
157150 ownerAddress,
158- // Create the appropriate filter: by mint if specified, or else all SPL tokens.
159151 mint != null
160152 ? TokenAccountsFilter .byMint (mint)
161153 : TokenAccountsFilter .byProgramId (splTokenProgramId),
162154 encoding: Encoding .jsonParsed,
163155 );
164156
165- // Extract token account addresses from the RPC response.
166157 final accountAddresses = result.value
167158 .map ((account) => account.pubkey)
168159 .toList ();
@@ -178,170 +169,73 @@ class SolanaTokenAPI {
178169 }
179170 }
180171
181- /// Get the balance of a specific token account.
182- ///
183- /// Parameters:
184- /// - tokenAccountAddress: The token account address to query.
185- ///
186- /// Returns the balance as a BigInt (in smallest units).
187172 Future <SolanaTokenApiResponse <BigInt >> getTokenAccountBalance (
188173 String tokenAccountAddress,
189174 ) async {
190175 try {
191176 _checkClient ();
192177
193- // Query the token account with jsonParsed encoding to get token amount.
194178 final response = await _rpcClient! .getAccountInfo (
195179 tokenAccountAddress,
196180 encoding: Encoding .jsonParsed,
197181 );
198182
199183 if (response.value == null ) {
200- // Token account doesn't exist.
201184 return SolanaTokenApiResponse <BigInt >(value: BigInt .zero);
202185 }
203186
204187 final accountData = response.value! ;
205188
206- // Extract token amount from parsed data.
207189 try {
208- // Debug: Print the structure of accountData.
209- print (
210- '[SOLANA_TOKEN_API] accountData type: ${accountData .runtimeType }' ,
211- );
212- print (
213- '[SOLANA_TOKEN_API] accountData.data type: ${accountData .data .runtimeType }' ,
214- );
215- print ('[SOLANA_TOKEN_API] accountData.data: ${accountData .data }' );
216-
217- // The solana package returns a ParsedAccountData which is a sealed class/union type.
218- // For SPL Token accounts, it contains SplTokenProgramAccountData.
219-
220190 final parsedData = accountData.data;
221191
222192 if (parsedData is ParsedAccountData ) {
223- print ('[SOLANA_TOKEN_API] ParsedAccountData detected' );
224-
225193 try {
226194 final extractedBalance = parsedData.when (
227195 splToken: (spl) {
228- print ('[SOLANA_TOKEN_API] Handling splToken variant' );
229- print ('[SOLANA_TOKEN_API] spl type: ${spl .runtimeType }' );
230-
231196 return spl.when (
232197 account: (info, type, accountType) {
233- print ('[SOLANA_TOKEN_API] Handling account variant' );
234- print ('[SOLANA_TOKEN_API] info type: ${info .runtimeType }' );
235- print (
236- '[SOLANA_TOKEN_API] info.tokenAmount: ${info .tokenAmount }' ,
237- );
238-
239198 try {
240199 final tokenAmount = info.tokenAmount;
241- print (
242- '[SOLANA_TOKEN_API] tokenAmount.amount: ${tokenAmount .amount }' ,
243- );
244- print (
245- '[SOLANA_TOKEN_API] tokenAmount.decimals: ${tokenAmount .decimals }' ,
246- );
247-
248- final balanceBigInt = BigInt .parse (tokenAmount.amount);
249- print (
250- '[SOLANA_TOKEN_API] Successfully extracted balance: $balanceBigInt ' ,
251- );
252- return balanceBigInt;
200+ return BigInt .parse (tokenAmount.amount);
253201 } catch (e) {
254- print ('[SOLANA_TOKEN_API] Error extracting balance: $e ' );
255202 return null ;
256203 }
257204 },
258- mint: (info, type, accountType) {
259- print (
260- '[SOLANA_TOKEN_API] Got mint variant (not expected for token account balance)' ,
261- );
262- return null ;
263- },
264- unknown: (type) {
265- print ('[SOLANA_TOKEN_API] Got unknown account variant' );
266- return null ;
267- },
268- );
269- },
270- stake: (_) {
271- print (
272- '[SOLANA_TOKEN_API] Got stake account type (not expected)' ,
205+ mint: (info, type, accountType) => null ,
206+ unknown: (type) => null ,
273207 );
274- return null ;
275208 },
209+ stake: (_) => null ,
276210 token2022: (token2022Data) {
277- print (
278- '[SOLANA_TOKEN_API] Handling token2022 account type' ,
279- );
280- print ('[SOLANA_TOKEN_API] token2022Data type: ${token2022Data .runtimeType }' );
281-
282211 return token2022Data.when (
283212 account: (info, type, accountType) {
284- print ('[SOLANA_TOKEN_API] Handling token2022 account variant' );
285- print ('[SOLANA_TOKEN_API] info type: ${info .runtimeType }' );
286- print (
287- '[SOLANA_TOKEN_API] info.tokenAmount: ${info .tokenAmount }' ,
288- );
289-
290213 try {
291214 final tokenAmount = info.tokenAmount;
292- print (
293- '[SOLANA_TOKEN_API] tokenAmount.amount: ${tokenAmount .amount }' ,
294- );
295- print (
296- '[SOLANA_TOKEN_API] tokenAmount.decimals: ${tokenAmount .decimals }' ,
297- );
298-
299- final balanceBigInt = BigInt .parse (tokenAmount.amount);
300- print (
301- '[SOLANA_TOKEN_API] Successfully extracted token2022 balance: $balanceBigInt ' ,
302- );
303- return balanceBigInt;
215+ return BigInt .parse (tokenAmount.amount);
304216 } catch (e) {
305- print ('[SOLANA_TOKEN_API] Error extracting token2022 balance: $e ' );
306217 return null ;
307218 }
308219 },
309- mint: (info, type, accountType) {
310- print (
311- '[SOLANA_TOKEN_API] Got token2022 mint variant (not expected for token account balance)' ,
312- );
313- return null ;
314- },
315- unknown: (type) {
316- print ('[SOLANA_TOKEN_API] Got unknown token2022 account variant' );
317- return null ;
318- },
220+ mint: (info, type, accountType) => null ,
221+ unknown: (type) => null ,
319222 );
320223 },
321- unsupported: (_) {
322- print ('[SOLANA_TOKEN_API] Got unsupported account type' );
323- return null ;
324- },
224+ unsupported: (_) => null ,
325225 );
326226
327227 if (extractedBalance != null && extractedBalance is BigInt ) {
328- print ('[SOLANA_TOKEN_API] Extracted balance: $extractedBalance ' );
329228 return SolanaTokenApiResponse <BigInt >(
330229 value: extractedBalance as BigInt ,
331230 );
332231 }
333232 } catch (e) {
334- print ('[SOLANA_TOKEN_API] Error using when() method: $e ' );
335- print ('[SOLANA_TOKEN_API] Stack trace: ${StackTrace .current }' );
233+ // Ignore parsing errors.
336234 }
337235 }
338236
339- // If we can't extract from the Dart object, return zero.
340- print ('[SOLANA_TOKEN_API] Returning zero balance' );
341237 return SolanaTokenApiResponse <BigInt >(value: BigInt .zero);
342238 } catch (e) {
343- // If parsing fails, return zero balance.
344- print ('[SOLANA_TOKEN_API] Exception during parsing: $e ' );
345239 return SolanaTokenApiResponse <BigInt >(value: BigInt .zero);
346240 }
347241 } on Exception catch (e) {
@@ -354,23 +248,11 @@ class SolanaTokenAPI {
354248 }
355249 }
356250
357- /// Get the total supply of a token.
358- ///
359- /// Parameters:
360- /// - mint: The token mint address.
361- ///
362- /// Returns the total supply as a BigInt.
363- ///
364- /// NOTE: Currently returns placeholder data for UI placeholders.
365- ///
366- /// TODO: Implement full RPC call when API is ready.
251+ // TODO: Implement full RPC call when API is ready.
367252 Future <SolanaTokenApiResponse <BigInt >> getTokenSupply (String mint) async {
368253 try {
369254 _checkClient ();
370-
371255 // TODO: Get the mint account info when RPC APIs are stable.
372- //
373- // For now return placeholder mock data.
374256 return SolanaTokenApiResponse <BigInt >(
375257 value: BigInt .parse ('1000000000000000000' ),
376258 );
@@ -384,16 +266,7 @@ class SolanaTokenAPI {
384266 }
385267 }
386268
387- /// Get token account information with balance and metadata.
388- ///
389- /// Parameters:
390- /// - tokenAccountAddress: The token account address.
391- ///
392- /// Returns detailed token account information.
393- ///
394- /// Currently returns placeholder data for UI placeholders.
395- ///
396- /// TODO: Implement full RPC call when API is ready.
269+ // TODO: Implement full RPC call when API is ready.
397270 Future <SolanaTokenApiResponse <TokenAccountInfo >> getTokenAccountInfo (
398271 String tokenAccountAddress,
399272 ) async {
@@ -423,23 +296,13 @@ class SolanaTokenAPI {
423296 }
424297 }
425298
426- /// Find the Associated Token Account (ATA) for a wallet and mint.
427- ///
428- /// Parameters:
429- /// - ownerAddress: The wallet address.
430- /// - mint: The token mint address.
431- ///
432- /// Returns the derived ATA address.
433299 String findAssociatedTokenAddress (String ownerAddress, String mint) {
434300 // Return a placeholder.
435301 //
436302 // TODO: Implement ATA derivation using Solana package.
437303 return '' ;
438304 }
439305
440- /// Check if a wallet owns a token (has a token account for the given mint).
441- ///
442- /// Returns true if the wallet has a token account for this mint, false otherwise.
443306 Future <SolanaTokenApiResponse <bool >> ownsToken (
444307 String ownerAddress,
445308 String mint,
@@ -468,16 +331,6 @@ class SolanaTokenAPI {
468331 }
469332 }
470333
471- /// Fetch SPL token metadata from Solana metadata program.
472- ///
473- /// The Solana Token Metadata program (metaqbxxUerdq28cj1RbAqWwTRiWLs6nshmbbuP3xqb)
474- /// stores token metadata at a PDA derived from the mint address.
475- ///
476- /// Returns: Map with name, symbol, decimals, and optional logo URI
477- /// Returns null if metadata cannot be found (user can enter custom details),
478- ///
479- /// Note: Full PDA derivation is not yet implemented in the solana package.
480- /// Currently returns null to allow users to manually enter token details.
481334 Future <SolanaTokenApiResponse <Map <String , dynamic >?>>
482335 fetchTokenMetadataByMint (
483336 String mintAddress,
0 commit comments