From bf3667da85dea8f464c80eb6abe7f384dbbcc732 Mon Sep 17 00:00:00 2001 From: dethe <76167420+detherminal@users.noreply.github.com> Date: Mon, 29 Apr 2024 20:19:09 +0300 Subject: [PATCH 1/2] solana fee estimation fixes --- lib/wallets/wallet/impl/solana_wallet.dart | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/wallets/wallet/impl/solana_wallet.dart b/lib/wallets/wallet/impl/solana_wallet.dart index c9343ddfc..7a4aab388 100644 --- a/lib/wallets/wallet/impl/solana_wallet.dart +++ b/lib/wallets/wallet/impl/solana_wallet.dart @@ -18,7 +18,6 @@ import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/default_nodes.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/wallets/crypto_currency/coins/solana.dart'; import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; @@ -66,10 +65,8 @@ class SolanaWallet extends Bip39Wallet { recipientAccount: pubKey, lamports: transferAmount.raw.toInt(), ), - ]).compile( - recentBlockhash: latestBlockhash!.value.blockhash, - feePayer: pubKey, - ); + ComputeBudgetInstruction.setComputeUnitPrice(microLamports: 6000), + ]).compile(recentBlockhash: latestBlockhash!.value.blockhash, feePayer: (await _getKeyPair()).publicKey); return await _rpcClient?.getFeeForMessage( base64Encode(compiledMessage.toByteArray().toList()), @@ -118,19 +115,10 @@ class SolanaWallet extends Bip39Wallet { throw Exception("Insufficient available balance"); } - int feeAmount; - final currentFees = await fees; - switch (txData.feeRateType) { - case FeeRateType.fast: - feeAmount = currentFees.fast; - break; - case FeeRateType.slow: - feeAmount = currentFees.slow; - break; - case FeeRateType.average: - default: - feeAmount = currentFees.medium; - break; + final feeAmount = await _getEstimatedNetworkFee(sendAmount); + if (feeAmount == null) { + throw Exception( + "Failed to get fees, please check your node connection."); } // Rent exemption of Solana From 87a96b9b09335b64d983736c8aa501b8f8dbf0e8 Mon Sep 17 00:00:00 2001 From: dethe <76167420+detherminal@users.noreply.github.com> Date: Tue, 30 Apr 2024 00:18:54 +0300 Subject: [PATCH 2/2] sol: fee set fix and sent to self fix --- lib/wallets/wallet/impl/solana_wallet.dart | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/wallets/wallet/impl/solana_wallet.dart b/lib/wallets/wallet/impl/solana_wallet.dart index 7a4aab388..e7d93ed0f 100644 --- a/lib/wallets/wallet/impl/solana_wallet.dart +++ b/lib/wallets/wallet/impl/solana_wallet.dart @@ -64,8 +64,7 @@ class SolanaWallet extends Bip39Wallet { fundingAccount: pubKey, recipientAccount: pubKey, lamports: transferAmount.raw.toInt(), - ), - ComputeBudgetInstruction.setComputeUnitPrice(microLamports: 6000), + ) ]).compile(recentBlockhash: latestBlockhash!.value.blockhash, feePayer: (await _getKeyPair()).publicKey); return await _rpcClient?.getFeeForMessage( @@ -167,7 +166,12 @@ class SolanaWallet extends Bip39Wallet { recipientAccount: recipientPubKey, lamports: txData.amount!.raw.toInt()), 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 ], ); @@ -367,7 +371,7 @@ class SolanaWallet extends Bip39Wallet { for (final tx in transactionsList!) { final senderAddress = (tx.transaction as ParsedTransaction).message.accountKeys[0].pubkey; - final receiverAddress = + var receiverAddress = (tx.transaction as ParsedTransaction).message.accountKeys[1].pubkey; var txType = isar.TransactionType.unknown; final txAmount = Amount( @@ -376,9 +380,10 @@ class SolanaWallet extends Bip39Wallet { fractionDigits: cryptoCurrency.fractionDigits, ); - if ((senderAddress == (await _getKeyPair()).address) && - (receiverAddress == (await _getKeyPair()).address)) { + if ((senderAddress == (await _getKeyPair()).address) && (receiverAddress == "11111111111111111111111111111111")) { + // 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; + receiverAddress = senderAddress; } else if (senderAddress == (await _getKeyPair()).address) { txType = isar.TransactionType.outgoing; } else if (receiverAddress == (await _getKeyPair()).address) {