Merge pull request #846 from detherminal/testing

solana fee estimation fixes
This commit is contained in:
julian-CStack 2024-05-03 09:05:44 -06:00 committed by GitHub
commit 5aa1f7b08a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,7 +18,6 @@ import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/default_nodes.dart'; import 'package:stackwallet/utilities/default_nodes.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/wallets/crypto_currency/coins/solana.dart'; import 'package:stackwallet/wallets/crypto_currency/coins/solana.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
@ -65,11 +64,8 @@ class SolanaWallet extends Bip39Wallet<Solana> {
fundingAccount: pubKey, fundingAccount: pubKey,
recipientAccount: pubKey, recipientAccount: pubKey,
lamports: transferAmount.raw.toInt(), lamports: transferAmount.raw.toInt(),
), )
]).compile( ]).compile(recentBlockhash: latestBlockhash!.value.blockhash, feePayer: (await _getKeyPair()).publicKey);
recentBlockhash: latestBlockhash!.value.blockhash,
feePayer: pubKey,
);
return await _rpcClient?.getFeeForMessage( return await _rpcClient?.getFeeForMessage(
base64Encode(compiledMessage.toByteArray().toList()), base64Encode(compiledMessage.toByteArray().toList()),
@ -118,19 +114,10 @@ class SolanaWallet extends Bip39Wallet<Solana> {
throw Exception("Insufficient available balance"); throw Exception("Insufficient available balance");
} }
int feeAmount; final feeAmount = await _getEstimatedNetworkFee(sendAmount);
final currentFees = await fees; if (feeAmount == null) {
switch (txData.feeRateType) { throw Exception(
case FeeRateType.fast: "Failed to get fees, please check your node connection.");
feeAmount = currentFees.fast;
break;
case FeeRateType.slow:
feeAmount = currentFees.slow;
break;
case FeeRateType.average:
default:
feeAmount = currentFees.medium;
break;
} }
// Rent exemption of Solana // Rent exemption of Solana
@ -179,7 +166,12 @@ class SolanaWallet extends Bip39Wallet<Solana> {
recipientAccount: recipientPubKey, recipientAccount: recipientPubKey,
lamports: txData.amount!.raw.toInt()), lamports: txData.amount!.raw.toInt()),
ComputeBudgetInstruction.setComputeUnitPrice( ComputeBudgetInstruction.setComputeUnitPrice(
microLamports: txData.fee!.raw.toInt()), microLamports: txData.fee!.raw.toInt() - 5000),
// 5000 lamports is the base fee for a transaction. This instruction adds the necessary fee on top of base fee if it is needed.
ComputeBudgetInstruction.setComputeUnitLimit(units: 1000000),
// 1000000 is the multiplication number to turn the compute unit price of microLamports to lamports.
// These instructions also help the user to not pay more than the shown fee.
// See: https://solanacookbook.com/references/basic-transactions.html#how-to-change-compute-budget-fee-priority-for-a-transaction
], ],
); );
@ -379,7 +371,7 @@ class SolanaWallet extends Bip39Wallet<Solana> {
for (final tx in transactionsList!) { for (final tx in transactionsList!) {
final senderAddress = final senderAddress =
(tx.transaction as ParsedTransaction).message.accountKeys[0].pubkey; (tx.transaction as ParsedTransaction).message.accountKeys[0].pubkey;
final receiverAddress = var receiverAddress =
(tx.transaction as ParsedTransaction).message.accountKeys[1].pubkey; (tx.transaction as ParsedTransaction).message.accountKeys[1].pubkey;
var txType = isar.TransactionType.unknown; var txType = isar.TransactionType.unknown;
final txAmount = Amount( final txAmount = Amount(
@ -388,9 +380,10 @@ class SolanaWallet extends Bip39Wallet<Solana> {
fractionDigits: cryptoCurrency.fractionDigits, fractionDigits: cryptoCurrency.fractionDigits,
); );
if ((senderAddress == (await _getKeyPair()).address) && if ((senderAddress == (await _getKeyPair()).address) && (receiverAddress == "11111111111111111111111111111111")) {
(receiverAddress == (await _getKeyPair()).address)) { // The account that is only 1's are System Program accounts which means there is no receiver except the sender, see: https://explorer.solana.com/address/11111111111111111111111111111111
txType = isar.TransactionType.sentToSelf; txType = isar.TransactionType.sentToSelf;
receiverAddress = senderAddress;
} else if (senderAddress == (await _getKeyPair()).address) { } else if (senderAddress == (await _getKeyPair()).address) {
txType = isar.TransactionType.outgoing; txType = isar.TransactionType.outgoing;
} else if (receiverAddress == (await _getKeyPair()).address) { } else if (receiverAddress == (await _getKeyPair()).address) {