change Solana node (#1903)

* change Solana node

* Fix reaching limit for fetching transactions
This commit is contained in:
Omar Hatem 2024-12-27 00:42:36 +02:00 committed by GitHub
parent 3e93a5ecb8
commit ed12ff6afe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 90 additions and 48 deletions

View file

@ -21,22 +21,23 @@ class SolanaWalletClient {
bool connect(Node node) {
try {
Uri? rpcUri;
String webSocketUrl;
bool isModifiedNodeUri = false;
Uri rpcUri = node.uri;
String webSocketUrl = 'wss://${node.uriRaw}';
if (node.uriRaw == 'rpc.ankr.com') {
isModifiedNodeUri = true;
String ankrApiKey = secrets.ankrApiKey;
rpcUri = Uri.https(node.uriRaw, '/solana/$ankrApiKey');
webSocketUrl = 'wss://${node.uriRaw}/solana/ws/$ankrApiKey';
} else {
webSocketUrl = 'wss://${node.uriRaw}';
} else if (node.uriRaw == 'solana-mainnet.core.chainstack.com') {
String chainStackApiKey = secrets.chainStackApiKey;
rpcUri = Uri.https(node.uriRaw, '/$chainStackApiKey');
webSocketUrl = 'wss://${node.uriRaw}/$chainStackApiKey';
}
_client = SolanaClient(
rpcUrl: isModifiedNodeUri ? rpcUri! : node.uri,
rpcUrl: rpcUri,
websocketUrl: Uri.parse(webSocketUrl),
timeout: const Duration(minutes: 2),
);
@ -115,10 +116,14 @@ class SolanaWalletClient {
final message =
_getMessageForNativeTransaction(ownerKeypair, ownerKeypair.address, lamportsPerSol);
final recentBlockhash = await _getRecentBlockhash(commitment);
final latestBlockhash = await _getLatestBlockhash(commitment);
final estimatedFee =
_getFeeFromCompiledMessage(message, ownerKeypair.publicKey, recentBlockhash, commitment);
final estimatedFee = _getFeeFromCompiledMessage(
message,
ownerKeypair.publicKey,
latestBlockhash,
commitment,
);
return estimatedFee;
}
@ -131,13 +136,25 @@ class SolanaWalletClient {
List<SolanaTransactionModel> transactions = [];
try {
final response = await _client!.rpcClient.getTransactionsList(
publicKey,
final signatures = await _client!.rpcClient.getSignaturesForAddress(
publicKey.toBase58(),
commitment: Commitment.confirmed,
limit: 1000,
);
for (final tx in response) {
final List<TransactionDetails> transactionDetails = [];
for (int i = 0; i < signatures.length; i += 20) {
final response = await _client!.rpcClient.getMultipleTransactions(
signatures.sublist(i, math.min(i + 20, signatures.length)),
commitment: Commitment.confirmed,
encoding: Encoding.jsonParsed,
);
transactionDetails.addAll(response);
// to avoid reaching the node RPS limit
await Future.delayed(Duration(milliseconds: 500));
}
for (final tx in transactionDetails) {
if (tx.transaction is ParsedTransaction) {
final parsedTx = (tx.transaction as ParsedTransaction);
final message = parsedTx.message;
@ -310,16 +327,16 @@ class SolanaWalletClient {
}
}
Future<RecentBlockhash> _getRecentBlockhash(Commitment commitment) async {
final latestBlockhash =
Future<LatestBlockhash> _getLatestBlockhash(Commitment commitment) async {
final latestBlockHashResult =
await _client!.rpcClient.getLatestBlockhash(commitment: commitment).value;
final recentBlockhash = RecentBlockhash(
blockhash: latestBlockhash.blockhash,
feeCalculator: const FeeCalculator(lamportsPerSignature: 500),
final latestBlockhash = LatestBlockhash(
blockhash: latestBlockHashResult.blockhash,
lastValidBlockHeight: latestBlockHashResult.lastValidBlockHeight,
);
return recentBlockhash;
return latestBlockhash;
}
Message _getMessageForNativeTransaction(
@ -342,11 +359,11 @@ class SolanaWalletClient {
Future<double> _getFeeFromCompiledMessage(
Message message,
Ed25519HDPublicKey feePayer,
RecentBlockhash recentBlockhash,
LatestBlockhash latestBlockhash,
Commitment commitment,
) async {
final compile = message.compile(
recentBlockhash: recentBlockhash.blockhash,
recentBlockhash: latestBlockhash.blockhash,
feePayer: feePayer,
);
@ -391,12 +408,12 @@ class SolanaWalletClient {
final signers = [ownerKeypair];
RecentBlockhash recentBlockhash = await _getRecentBlockhash(commitment);
LatestBlockhash latestBlockhash = await _getLatestBlockhash(commitment);
final fee = await _getFeeFromCompiledMessage(
message,
signers.first.publicKey,
recentBlockhash,
latestBlockhash,
commitment,
);
@ -422,14 +439,14 @@ class SolanaWalletClient {
message: updatedMessage,
signers: signers,
commitment: commitment,
recentBlockhash: recentBlockhash,
latestBlockhash: latestBlockhash,
);
} else {
signedTx = await _signTransactionInternal(
message: message,
signers: signers,
commitment: commitment,
recentBlockhash: recentBlockhash,
latestBlockhash: latestBlockhash,
);
}
@ -507,12 +524,12 @@ class SolanaWalletClient {
final signers = [ownerKeypair];
RecentBlockhash recentBlockhash = await _getRecentBlockhash(commitment);
LatestBlockhash latestBlockhash = await _getLatestBlockhash(commitment);
final fee = await _getFeeFromCompiledMessage(
message,
signers.first.publicKey,
recentBlockhash,
latestBlockhash,
commitment,
);
@ -530,7 +547,7 @@ class SolanaWalletClient {
message: message,
signers: signers,
commitment: commitment,
recentBlockhash: recentBlockhash,
latestBlockhash: latestBlockhash,
);
sendTx() async => await sendTransaction(
@ -552,9 +569,9 @@ class SolanaWalletClient {
required Message message,
required List<Ed25519HDKeyPair> signers,
required Commitment commitment,
required RecentBlockhash recentBlockhash,
required LatestBlockhash latestBlockhash,
}) async {
final signedTx = await signTransaction(recentBlockhash, message, signers);
final signedTx = await signTransaction(latestBlockhash, message, signers);
return signedTx;
}

View file

@ -25,9 +25,7 @@ class SolanaSignNativeTokenTransactionRentException
extends SignNativeTokenTransactionRentException {}
class SolanaCreateAssociatedTokenAccountException extends CreateAssociatedTokenAccountException {
SolanaCreateAssociatedTokenAccountException(this.exceptionMessage);
final String exceptionMessage;
SolanaCreateAssociatedTokenAccountException(super.errorMessage);
}
class SolanaSignSPLTokenTransactionRentException extends SignSPLTokenTransactionRentException {}