-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathsolana_token_wallet.dart
More file actions
957 lines (831 loc) · 28.7 KB
/
solana_token_wallet.dart
File metadata and controls
957 lines (831 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2025 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
*
*/
import 'dart:convert';
import 'package:isar_community/isar.dart';
import 'package:solana/dto.dart' hide Instruction;
import 'package:solana/encoder.dart' show Instruction;
import 'package:solana/solana.dart' hide Wallet;
import '../../../../models/balance.dart';
import '../../../../models/isar/models/blockchain_data/v2/input_v2.dart';
import '../../../../models/isar/models/blockchain_data/v2/output_v2.dart';
import '../../../../models/isar/models/blockchain_data/v2/transaction_v2.dart';
import '../../../../models/isar/models/isar_models.dart';
import '../../../../models/paymint/fee_object_model.dart';
import '../../../../services/solana/solana_token_api.dart';
import '../../../../utilities/amount/amount.dart';
import '../../../../utilities/logger.dart';
import '../../../models/tx_data.dart';
import '../../wallet.dart';
import '../solana_wallet.dart';
class SolanaTokenWallet extends Wallet {
@override
int get isarTransactionVersion => 2;
SolanaTokenWallet(this.parentSolanaWallet, this.solContract)
: super(parentSolanaWallet.cryptoCurrency);
final SolanaWallet parentSolanaWallet;
final SolContract solContract;
String get tokenMint => solContract.address;
String get tokenName => solContract.name;
String get tokenSymbol => solContract.symbol;
int get tokenDecimals => solContract.decimals;
@override
FilterOperation? get changeAddressFilterOperation =>
parentSolanaWallet.changeAddressFilterOperation;
@override
FilterOperation? get receivingAddressFilterOperation =>
parentSolanaWallet.receivingAddressFilterOperation;
@override
FilterOperation? get transactionFilterOperation => FilterGroup.and([
FilterCondition.equalTo(property: r"contractAddress", value: tokenMint),
const FilterCondition.equalTo(
property: r"subType",
value: TransactionSubType.splToken,
),
]);
@override
Future<void> init() async {
await super.init();
parentSolanaWallet.checkClient();
await Future<void>.delayed(const Duration(milliseconds: 100));
}
@override
Future<TxData> prepareSend({required TxData txData}) async {
try {
if (txData.recipients == null || txData.recipients!.isEmpty) {
throw ArgumentError("At least one recipient is required");
}
if (txData.recipients!.length != 1) {
throw ArgumentError(
"SOL token transfers support only 1 recipient per transaction",
);
}
if (txData.amount == null || txData.amount!.raw <= BigInt.zero) {
throw ArgumentError("Send amount must be greater than zero");
}
final recipientAddress = txData.recipients!.first.address;
if (recipientAddress.isEmpty) {
throw ArgumentError("Recipient address cannot be empty");
}
try {
Ed25519HDPublicKey.fromBase58(recipientAddress);
} catch (e) {
throw ArgumentError("Invalid recipient address: $recipientAddress");
}
final rpcClient = parentSolanaWallet.getRpcClient();
if (rpcClient == null) {
throw Exception("RPC client not initialized");
}
final keyPair = await parentSolanaWallet.getKeyPair();
final walletAddress = keyPair.address;
final senderTokenAccount = await _findTokenAccount(
ownerAddress: walletAddress,
mint: tokenMint,
rpcClient: rpcClient,
);
if (senderTokenAccount == null) {
throw Exception(
"No token account found for mint $tokenMint. "
"Please ensure you have received tokens first.",
);
}
try {
final accountInfo = await rpcClient.getAccountInfo(
senderTokenAccount,
encoding: Encoding.jsonParsed,
);
if (accountInfo.value == null) {
throw Exception(
"Sender token account $senderTokenAccount not found on-chain",
);
}
} catch (e) {
throw Exception("Failed to validate sender token account: $e");
}
await rpcClient.getLatestBlockhash();
final recipientTokenAccount = await _findOrDeriveRecipientTokenAccount(
recipientAddress: recipientAddress,
mint: tokenMint,
rpcClient: rpcClient,
);
if (recipientTokenAccount.isEmpty) {
throw Exception(
"Cannot determine recipient token account for mint $tokenMint. "
"Recipient may not have a token account for this mint. "
"Please ensure the recipient has initialized an Associated Token Account (ATA) first.",
);
}
final senderTokenAccountKey = Ed25519HDPublicKey.fromBase58(
senderTokenAccount,
);
final recipientTokenAccountKey = Ed25519HDPublicKey.fromBase58(
recipientTokenAccount,
);
final mintPubkey = Ed25519HDPublicKey.fromBase58(tokenMint);
String tokenProgramId;
try {
final mintInfo = await rpcClient.getAccountInfo(
tokenMint,
encoding: Encoding.jsonParsed,
);
if (mintInfo.value != null) {
tokenProgramId = mintInfo.value!.owner;
Logging.instance.i(
"$runtimeType prepareSend: Token program owner = $tokenProgramId for mint $tokenMint",
);
} else {
// Fallback to SPL Token.
tokenProgramId = 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA';
Logging.instance.w(
"$runtimeType prepareSend: Could not query mint owner, using SPL Token",
);
}
} catch (e) {
// Fallback to SPL Token on error.
tokenProgramId = 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA';
Logging.instance.w(
"$runtimeType prepareSend: Error querying mint owner: $e, using SPL Token",
);
}
final TokenProgramType tokenProgram =
tokenProgramId == Token2022Program.programId
? TokenProgramType.token2022Program
: TokenProgramType.tokenProgram;
final recipientAccountInfo = await rpcClient.getAccountInfo(
recipientTokenAccount,
encoding: Encoding.jsonParsed,
);
AssociatedTokenAccountInstruction? createAccountInstruction;
if (recipientAccountInfo.value == null) {
createAccountInstruction =
AssociatedTokenAccountInstruction.createAccount(
funder: keyPair.publicKey,
address: recipientTokenAccountKey,
owner: Ed25519HDPublicKey.fromBase58(recipientAddress),
mint: mintPubkey,
);
} else {
try {
final accountData = recipientAccountInfo.value!;
// Verify account is owned by token program (not System Program).
if (accountData.owner == '11111111111111111111111111111111') {
throw Exception(
"Recipient token account $recipientTokenAccount is owned by the System Program, "
"not a token program. The account may not be a valid token account.",
);
}
} catch (e) {
if (e.toString().contains("does not exist") ||
e.toString().contains("not owned by")) {
rethrow;
}
throw Exception(
"Failed to validate recipient token account: $e. "
"Ensure the recipient has initialized their token account.",
);
}
}
final instruction = TokenInstruction.transferChecked(
source: senderTokenAccountKey,
destination: recipientTokenAccountKey,
mint: mintPubkey,
owner: keyPair.publicKey,
decimals: tokenDecimals,
amount: txData.amount!.raw.toInt(),
tokenProgram: tokenProgram,
);
final instructions = [
if (createAccountInstruction != null) createAccountInstruction,
instruction,
];
final feeEstimate =
await _getEstimatedTokenTransferFee(
ownerPublicKey: keyPair.publicKey,
rpcClient: rpcClient,
instructions: instructions,
memo: txData.memo,
) ??
5000;
return txData.copyWith(
fee: Amount(rawValue: BigInt.from(feeEstimate), fractionDigits: 9),
solInstructions: instructions,
);
} catch (e, s) {
Logging.instance.e(
"$runtimeType prepareSend failed: ",
error: e,
stackTrace: s,
);
rethrow;
}
}
@override
Future<TxData> confirmSend({required TxData txData}) async {
try {
// Validate that prepareSend was called.
if (txData.fee == null) {
throw Exception("Transaction not prepared. Call prepareSend() first.");
}
if (txData.recipients == null || txData.recipients!.isEmpty) {
throw ArgumentError("Transaction must have at least one recipient");
}
// Get wallet state.
final rpcClient = parentSolanaWallet.getRpcClient();
if (rpcClient == null) {
throw Exception("RPC client not initialized");
}
final keyPair = await parentSolanaWallet.getKeyPair();
final walletAddress = keyPair.address;
// Get sender's token account.
final senderTokenAccount = await _findTokenAccount(
ownerAddress: walletAddress,
mint: tokenMint,
rpcClient: rpcClient,
);
if (senderTokenAccount == null) {
throw Exception("Token account not found");
}
await rpcClient.getLatestBlockhash();
final instructions = txData.solInstructions;
if (instructions == null || instructions.isEmpty) {
throw Exception(
"Token transaction missing instructions. "
"Call prepareSend() first.",
);
}
final recipientTokenAccount = await _findOrDeriveRecipientTokenAccount(
recipientAddress: txData.recipients!.first.address,
mint: tokenMint,
rpcClient: rpcClient,
);
// Create message.
final message = Message(
instructions: [
if (txData.memo != null)
MemoInstruction(signers: const [], memo: txData.memo!),
...instructions,
],
);
// Sign and broadcast tx.
final txid = await rpcClient.signAndSendTransaction(message, [keyPair]);
if (txid.isEmpty) {
throw Exception(
"Failed to broadcast transaction: empty signature returned",
);
}
// Create temporary transaction (pending = unconfirmed) and save to db.
try {
// Build inputs and outputs for the transaction record.
final inputs = [
InputV2.isarCantDoRequiredInDefaultConstructor(
scriptSigHex: null,
scriptSigAsm: null,
sequence: null,
outpoint: null,
addresses: [senderTokenAccount],
valueStringSats: txData.amount!.raw.toString(),
witness: null,
innerRedeemScriptAsm: null,
coinbase: null,
walletOwns: true,
),
];
final outputs = [
OutputV2.isarCantDoRequiredInDefaultConstructor(
scriptPubKeyHex: "00",
valueStringSats: txData.amount!.raw.toString(),
addresses: [recipientTokenAccount],
walletOwns: false, // We don't own recipient account.
),
];
// Determine if this is a self-transfer.
final isToSelf = senderTokenAccount == recipientTokenAccount;
// Create the temporary transaction record.
final tempTx = TransactionV2(
walletId: walletId,
blockHash: null, // CRITICAL: null indicates pending.
hash: txid,
txid: txid,
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
height: null, // CRITICAL: null indicates pending.
inputs: List.unmodifiable(inputs),
outputs: List.unmodifiable(outputs),
version: -1,
type: isToSelf
? TransactionType.sentToSelf
: TransactionType.outgoing,
subType: TransactionSubType.splToken,
otherData: jsonEncode({
"mint": tokenMint,
"senderTokenAccount": senderTokenAccount,
"recipientTokenAccount": recipientTokenAccount,
"isCancelled": false,
"overrideFee": txData.fee!.toJsonString(),
}),
);
// Persist immediately to database so UI shows transaction right away.
await mainDB.updateOrPutTransactionV2s([tempTx]);
Logging.instance.i(
"$runtimeType confirmSend: Persisted pending transaction $txid to database",
);
} catch (e, s) {
// Log persistence error but don't fail the send operation.
Logging.instance.w(
"$runtimeType confirmSend: Failed to persist pending transaction to database: ",
error: e,
stackTrace: s,
);
}
// Wait for confirmation.
final confirmed = await _waitForConfirmation(
signature: txid,
maxWaitSeconds: 60,
rpcClient: rpcClient,
);
if (!confirmed) {
Logging.instance.w(
"$runtimeType confirmSend: Transaction not confirmed after 60 seconds, "
"but signature was successfully broadcast: $txid",
);
}
// Return signed TxData.
return txData.copyWith(txid: txid);
} catch (e, s) {
Logging.instance.e(
"$runtimeType confirmSend failed: ",
error: e,
stackTrace: s,
);
rethrow;
}
}
@override
Future<void> recover({required bool isRescan}) async {
// TODO.
}
@override
Future<void> updateNode() async {
await parentSolanaWallet.updateNode();
}
@override
Future<void> updateTransactions() async {
try {
final rpcClient = parentSolanaWallet.getRpcClient();
if (rpcClient == null) {
Logging.instance.w(
"$runtimeType updateTransactions: RPC client not initialized",
);
return;
}
final keyPair = await parentSolanaWallet.getKeyPair();
final walletAddress = keyPair.address;
// Find token account for this mint.
final myTokenAccount = await _findTokenAccount(
ownerAddress: walletAddress,
mint: tokenMint,
rpcClient: rpcClient,
);
if (myTokenAccount == null) {
return;
}
// Fetch recent transactions for this token account.
final txListIterable = await rpcClient.getTransactionsList(
Ed25519HDPublicKey.fromBase58(myTokenAccount),
encoding: Encoding.jsonParsed,
);
final txList = txListIterable.toList();
if (txList.isEmpty) {
return;
}
final txns = <TransactionV2>[];
int skippedCount = 0;
for (int i = 0; i < txList.length; i++) {
final txDetails = txList[i];
try {
// Skip failed transactions or those without metadata.
if (txDetails.meta == null) {
skippedCount++;
continue;
}
// Cast transaction to ParsedTransaction if available.
if (txDetails.transaction is! ParsedTransaction) {
skippedCount++;
continue;
}
final parsedTx = txDetails.transaction as ParsedTransaction;
final txid = parsedTx.signatures.isNotEmpty
? parsedTx.signatures[0]
: null;
if (parsedTx.signatures.length > 1) {
Logging.instance.w(
"SOL $walletId found tx with "
"${parsedTx.signatures.length} signatures",
);
}
if (txid == null) {
skippedCount++;
continue;
}
final splTransfers = parsedTx.message.instructions
.map((e) => e.toJson())
.where(
(e) =>
e.containsKey("parsed") &&
e["program"] == "spl-token" &&
(e["parsed"]["type"] == "transferChecked" ||
e["parsed"]["type"] == "transfer"),
);
if (splTransfers.length != 1) {
Logging.instance.w(
"SOL $walletId found tx with "
"${splTransfers.length} spl transfer! Skipping...",
);
skippedCount++;
continue;
}
final transfer = splTransfers.first;
final transferType = transfer["parsed"]["type"] as String;
final BigInt lamports;
if (transferType == "transferChecked") {
lamports = BigInt.parse(
transfer["parsed"]["info"]["tokenAmount"]["amount"].toString(),
);
} else {
lamports = BigInt.parse(
transfer["parsed"]["info"]["amount"].toString(),
);
}
final senderAddress = transfer["parsed"]["info"]["source"] as String;
final receiverAddress =
transfer["parsed"]["info"]["destination"] as String;
final TransactionType txType;
if ((senderAddress == myTokenAccount) &&
(receiverAddress == senderAddress)) {
txType = TransactionType.sentToSelf;
} else if (senderAddress == myTokenAccount) {
txType = TransactionType.outgoing;
} else if (receiverAddress == myTokenAccount) {
txType = TransactionType.incoming;
} else {
// probably should never get here? If so, then this fragile parsing
// is broken which isn't surprising...
txType = TransactionType.unknown;
}
// check for memo
final memos = parsedTx.message.instructions
.map((e) => e.toJson())
.where(
(e) => e["parsed"] is String && e["program"] == "spl-memo",
);
final String? memo = memos.isEmpty
? null
: memos.first["parsed"] as String;
// Create placeholder TransactionV2 object.
final txn = TransactionV2(
walletId: walletId,
blockHash: null,
hash: txid,
txid: txid,
timestamp:
txDetails.blockTime ??
DateTime.now().millisecondsSinceEpoch ~/ 1000,
height: txDetails.slot,
inputs: [
InputV2.isarCantDoRequiredInDefaultConstructor(
scriptSigHex: null,
scriptSigAsm: null,
sequence: null,
outpoint: null,
addresses: [senderAddress],
valueStringSats: lamports.toString(),
witness: null,
innerRedeemScriptAsm: null,
coinbase: null,
walletOwns: senderAddress == myTokenAccount,
),
],
outputs: [
OutputV2.isarCantDoRequiredInDefaultConstructor(
scriptPubKeyHex: "00",
valueStringSats: lamports.toString(),
addresses: [receiverAddress],
walletOwns: receiverAddress == myTokenAccount,
),
],
version: txDetails.version?.version?.toInt() ?? -1,
type: txType,
subType: TransactionSubType.splToken,
otherData: jsonEncode({
TxV2OdKeys.contractAddress: tokenMint,
TxV2OdKeys.isCancelled: (txDetails.meta!.err != null),
TxV2OdKeys.overrideFee: txDetails.meta!.fee.toString(),
if (memo != null) TxV2OdKeys.memo: memo,
}),
);
txns.add(txn);
} catch (e, s) {
Logging.instance.w(
"$runtimeType updateTransactions: Failed to parse transaction at index $i",
error: e,
stackTrace: s,
);
skippedCount++;
continue;
}
}
// Persist all transactions if any were parsed.
if (txns.isNotEmpty) {
await mainDB.updateOrPutTransactionV2s(txns);
Logging.instance.i(
"$runtimeType updateTransactions: Synced ${txns.length} transactions (skipped $skippedCount)",
);
}
} catch (e, s) {
Logging.instance.e(
"$runtimeType updateTransactions FAILED: ",
error: e,
stackTrace: s,
);
}
}
@override
Future<void> updateBalance() async {
try {
final rpcClient = parentSolanaWallet.getRpcClient();
if (rpcClient == null) {
return;
}
final keyPair = await parentSolanaWallet.getKeyPair();
final walletAddress = keyPair.address;
final senderTokenAccount = await _findTokenAccount(
ownerAddress: walletAddress,
mint: tokenMint,
rpcClient: rpcClient,
);
if (senderTokenAccount == null) {
return;
}
final tokenApi = SolanaTokenAPI();
tokenApi.initializeRpcClient(rpcClient);
final balanceResponse = await tokenApi.getTokenAccountBalance(
senderTokenAccount,
);
if (balanceResponse.isError) {
Logging.instance.w(
"$runtimeType updateBalance failed: ${balanceResponse.exception}",
);
return;
}
if (balanceResponse.value != null) {
final info = await mainDB.isar.walletSolanaTokenInfo
.where()
.walletIdTokenAddressEqualTo(walletId, tokenMint)
.findFirst();
if (info != null) {
final balanceAmount = Amount(
rawValue: balanceResponse.value!,
fractionDigits: tokenDecimals,
);
final balance = Balance(
total: balanceAmount,
spendable: balanceAmount,
blockedTotal: Amount(
rawValue: BigInt.zero,
fractionDigits: tokenDecimals,
),
pendingSpendable: Amount(
rawValue: BigInt.zero,
fractionDigits: tokenDecimals,
),
);
await info.updateCachedBalance(balance, isar: mainDB.isar);
}
}
} catch (e, s) {
Logging.instance.e(
"$runtimeType updateBalance error: ",
error: e,
stackTrace: s,
);
}
}
@override
Future<bool> updateUTXOs() async {
// Not applicable for Solana tokens.
return true;
}
@override
Future<void> updateChainHeight() async {
await parentSolanaWallet.updateChainHeight();
}
@override
Future<void> refresh() async {
await parentSolanaWallet.refresh();
await updateBalance();
await updateTransactions();
}
@override
Future<Amount> estimateFeeFor(Amount amount, BigInt feeRate) async {
return parentSolanaWallet.estimateFeeFor(amount, feeRate);
}
@override
Future<FeeObject> get fees async {
return parentSolanaWallet.fees;
}
@override
Future<bool> pingCheck() async {
return parentSolanaWallet.pingCheck();
}
@override
Future<void> checkSaveInitialReceivingAddress() async {
await parentSolanaWallet.checkSaveInitialReceivingAddress();
}
Future<String?> _findTokenAccount({
required String ownerAddress,
required String mint,
required RpcClient rpcClient,
}) async {
try {
final result = await rpcClient.getTokenAccountsByOwner(
ownerAddress,
TokenAccountsFilter.byMint(mint),
encoding: Encoding.jsonParsed,
);
if (result.value.isEmpty) {
Logging.instance.w(
"$runtimeType _findTokenAccount: No token account found for "
"owner=$ownerAddress, mint=$mint",
);
return null;
}
final tokenAccountAddress = result.value.first.pubkey;
Logging.instance.i(
"$runtimeType _findTokenAccount: Found token account $tokenAccountAddress "
"for owner=$ownerAddress, mint=$mint",
);
return tokenAccountAddress;
} catch (e) {
Logging.instance.w("$runtimeType _findTokenAccount error: $e");
return null;
}
}
Future<String> _findOrDeriveRecipientTokenAccount({
required String recipientAddress,
required String mint,
required RpcClient rpcClient,
}) async {
// First, try to find an existing token account
final existingAccount = await _findTokenAccount(
ownerAddress: recipientAddress,
mint: mint,
rpcClient: rpcClient,
);
if (existingAccount != null) {
Logging.instance.i(
"$runtimeType Found existing token account for recipient: $existingAccount",
);
return existingAccount;
}
// If no existing account found, try to derive the ATA
Logging.instance.i(
"$runtimeType No existing token account found, deriving ATA for recipient",
);
return await _deriveAtaAddress(
ownerAddress: recipientAddress,
mint: mint,
rpcClient: rpcClient,
);
}
Future<String> _deriveAtaAddress({
required String ownerAddress,
required String mint,
required RpcClient rpcClient,
}) async {
final ownerPubkey = Ed25519HDPublicKey.fromBase58(ownerAddress);
final mintPubkey = Ed25519HDPublicKey.fromBase58(mint);
final tokenApi = SolanaTokenAPI();
tokenApi.initializeRpcClient(rpcClient);
String tokenProgramId;
try {
final mintInfo = await rpcClient.getAccountInfo(
mint,
encoding: Encoding.jsonParsed,
);
if (mintInfo.value != null) {
tokenProgramId = mintInfo.value!.owner;
} else {
tokenProgramId = 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA';
}
} catch (e) {
tokenProgramId = 'TokenkegQfeZyiNwAJsyFbPVwwQQfg5bgUiqhStM5QA';
}
final tokenProgramPubkey = Ed25519HDPublicKey.fromBase58(tokenProgramId);
const associatedTokenProgramId =
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
final associatedTokenProgramPubkey = Ed25519HDPublicKey.fromBase58(
associatedTokenProgramId,
);
final seeds = [
ownerPubkey.bytes,
tokenProgramPubkey.bytes,
mintPubkey.bytes,
];
final ataAddress = await Ed25519HDPublicKey.findProgramAddress(
seeds: seeds,
programId: associatedTokenProgramPubkey,
);
final ataBase58 = ataAddress.toBase58();
return ataBase58;
}
Future<int?> _getEstimatedTokenTransferFee({
required Ed25519HDPublicKey ownerPublicKey,
required RpcClient rpcClient,
required List<Instruction> instructions,
required String? memo,
}) async {
try {
// Get latest blockhash for message compilation.
final latestBlockhash = await rpcClient.getLatestBlockhash();
// Compile the message with the blockhash.
final compiledMessage =
Message(
instructions: [
if (memo != null) MemoInstruction(signers: const [], memo: memo),
...instructions,
],
).compile(
recentBlockhash: latestBlockhash.value.blockhash,
feePayer: ownerPublicKey,
);
// Get the fee for this compiled message.
final feeEstimate = await rpcClient.getFeeForMessage(
base64Encode(compiledMessage.toByteArray().toList()),
commitment: Commitment.confirmed,
);
if (feeEstimate != null) {
Logging.instance.i(
"$runtimeType Estimated token transfer fee: $feeEstimate lamports (from RPC)",
);
return feeEstimate;
}
Logging.instance.w("$runtimeType getFeeForMessage returned null");
return null;
} catch (e) {
Logging.instance.w(
"$runtimeType _getEstimatedTokenTransferFee error: $e",
);
return null;
}
}
Future<bool> _waitForConfirmation({
required String signature,
required int maxWaitSeconds,
required RpcClient rpcClient,
}) async {
final startTime = DateTime.now();
while (true) {
try {
final status = await rpcClient.getSignatureStatuses([
signature,
], searchTransactionHistory: true);
if (status.value.isNotEmpty) {
final txStatus = status.value.first;
// Check if transaction failed
if (txStatus?.err != null) {
Logging.instance.e(
"$runtimeType Transaction failed: ${txStatus?.err}",
);
return false;
}
// Check if transaction confirmed
if (txStatus?.confirmationStatus == Commitment.confirmed ||
txStatus?.confirmationStatus == Commitment.finalized) {
Logging.instance.i(
"$runtimeType Transaction confirmed: $signature",
);
return true;
}
}
} catch (e) {
Logging.instance.w(
"$runtimeType Error checking transaction confirmation: $e",
);
}
// Check timeout
final elapsed = DateTime.now().difference(startTime).inSeconds;
if (elapsed > maxWaitSeconds) {
Logging.instance.w(
"$runtimeType Transaction confirmation timeout after $maxWaitSeconds seconds",
);
return false;
}
// Wait before next check (2 seconds)
await Future<void>.delayed(const Duration(seconds: 2));
}
}
}