Skip to content

Commit 75427be

Browse files
committed
feat(spl): implement Token-2022 transfers
1 parent 5b97929 commit 75427be

File tree

2 files changed

+330
-40
lines changed

2 files changed

+330
-40
lines changed

lib/services/solana/solana_token_api.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,47 @@ class SolanaTokenAPI {
532532
}
533533
}
534534

535+
/// Detect which token program owns a mint address.
536+
///
537+
/// Queries the RPC to get the mint account info and checks which program owns it.
538+
/// This is needed to determine whether to use standard SPL Token instructions
539+
/// or Token-2022 (Token Extensions) instructions for transfers.
540+
///
541+
/// Returns: "spl" for standard SPL Token, "token2022" for Token Extensions, or null if detection fails.
542+
Future<String?> getTokenProgramType(String mintAddress) async {
543+
try {
544+
_checkClient();
545+
546+
// Query the mint account to check its owner program.
547+
final response = await _rpcClient!.getAccountInfo(
548+
mintAddress,
549+
encoding: Encoding.jsonParsed,
550+
);
551+
552+
if (response.value == null) {
553+
return null;
554+
}
555+
556+
final owner = response.value!.owner;
557+
558+
// Check which program owns this mint.
559+
// SPL Token: TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA
560+
// Token-2022: TokenzQdBNbLvnVCrqtsvQQrXTVkDkAydS7d5xgqfnb
561+
if (owner == 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA') {
562+
return 'spl';
563+
}
564+
if (owner.startsWith('Token') && owner != 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA') {
565+
print('[SOLANA_TOKEN_API] Detected Token-2022 variant: $owner');
566+
return 'token2022';
567+
}
568+
569+
return null;
570+
} catch (e) {
571+
print('[SOLANA_TOKEN_API] Error detecting token program: $e');
572+
return null;
573+
}
574+
}
575+
535576
/// Derive the metadata PDA for a given mint address.
536577
///
537578
/// This is a temporary implementation that queries known metadata endpoints.

0 commit comments

Comments
 (0)