fix deprecated fee func and null fees.

This commit is contained in:
dethe 2024-04-29 19:19:01 +03:00
parent 420c73ed5d
commit f279bf1f3b

View file

@ -1,9 +1,11 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:isar/isar.dart';
import 'package:socks5_proxy/socks_client.dart';
import 'package:solana/dto.dart';
import 'package:solana/encoder.dart';
import 'package:solana/solana.dart';
import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart'
@ -54,6 +56,21 @@ class SolanaWallet extends Bip39Wallet<Solana> {
return balance!.value;
}
Future<int?> _getEstimatedNetworkFee(Amount transferAmount) async {
final latestBlockhash = await _rpcClient?.getLatestBlockhash();
final compiledMessage = Message(instructions: [
SystemInstruction.transfer(
fundingAccount: (await _getKeyPair()).publicKey,
recipientAccount: (await _getKeyPair()).publicKey,
lamports: transferAmount.raw.toInt()),
]).compile(recentBlockhash: latestBlockhash!.value.blockhash, feePayer: (await _getKeyPair()).publicKey);
return await _rpcClient?.getFeeForMessage(
base64Encode(compiledMessage.toByteArray().toList()),
);
}
@override
FilterOperation? get changeAddressFilterOperation =>
throw UnimplementedError();
@ -185,11 +202,13 @@ class SolanaWallet extends Bip39Wallet<Solana> {
);
}
final fee = await _rpcClient?.getFees();
// TODO [prio=low]: handle null fee.
final fee = await _getEstimatedNetworkFee(amount);
if (fee == null) {
throw Exception("Failed to get fees, please check your node connection.");
}
return Amount(
rawValue: BigInt.from(fee!.value.feeCalculator.lamportsPerSignature),
rawValue: BigInt.from(fee as num),
fractionDigits: cryptoCurrency.fractionDigits,
);
}
@ -198,15 +217,21 @@ class SolanaWallet extends Bip39Wallet<Solana> {
Future<FeeObject> get fees async {
_checkClient();
final fees = await _rpcClient?.getFees();
// TODO [prio=low]: handle null fees.
final fee = await _getEstimatedNetworkFee(Amount(
rawValue: BigInt.from(1000000000), // 1 SOL
fractionDigits: cryptoCurrency.fractionDigits,
));
if (fee == null) {
throw Exception("Failed to get fees, please check your node connection.");
}
return FeeObject(
numberOfBlocksFast: 1,
numberOfBlocksAverage: 1,
numberOfBlocksSlow: 1,
fast: fees!.value.feeCalculator.lamportsPerSignature,
medium: fees!.value.feeCalculator.lamportsPerSignature,
slow: fees!.value.feeCalculator.lamportsPerSignature);
fast: fee,
medium: fee,
slow: fee);
}
@override