mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
Send Max
This commit is contained in:
1 parent
e3cebf3a34
commit
fabbb0b50e
3 files changed
+151
-52
No files matched your search
@@ -12,7 +12,7 @@ import { useWalletTransactions } from '@/hooks/cashu/useWalletTransactions';
|
||||
import { useHistoryManager } from '@/hooks/cashu/useHistoryManager';
|
||||
import { useNsecAccess } from '@/hooks/useNsecAccess';
|
||||
import { useBitcoinBalance, useBitcoinTransactionHistory } from '@/hooks/useBitcoinBalance';
|
||||
import { nostrPubkeyToBitcoinAddress, npubToBitcoinAddress, fetchUTXOs, getFeeRates, createBitcoinTransaction, broadcastTransaction, UTXO } from '@/lib/bitcoin';
|
||||
import { nostrPubkeyToBitcoinAddress, npubToBitcoinAddress, fetchUTXOs, getFeeRates, createBitcoinTransaction, broadcastTransaction, calculateMaxSendAmount, UTXO } from '@/lib/bitcoin';
|
||||
import { QRModal } from '@/components/ui/qr-modal';
|
||||
import { WalletHeader } from './WalletHeader';
|
||||
import { TransactionCard } from './PaymentCard';
|
||||
@@ -183,6 +183,17 @@ export function CashuWallet() {
|
||||
const [utxoDropdownOpen, setUtxoDropdownOpen] = useState(false);
|
||||
const [selectedUtxo, setSelectedUtxo] = useState<UTXO | null>(null);
|
||||
const [isLoadingUtxos, setIsLoadingUtxos] = useState(false);
|
||||
const [feeRate, setFeeRate] = useState<number>(1);
|
||||
|
||||
// Calculate max send amount
|
||||
const bitcoinMaxAmount = utxos.length > 0 ? calculateMaxSendAmount(utxos, feeRate) : 0;
|
||||
|
||||
// Handle Send Max
|
||||
const handleSendMax = () => {
|
||||
if (bitcoinMaxAmount > 0) {
|
||||
setBitcoinAmount(bitcoinMaxAmount.toString());
|
||||
}
|
||||
};
|
||||
|
||||
// Handle drawer close with state reset
|
||||
const handleSendDrawerClose = (open: boolean) => {
|
||||
@@ -259,13 +270,17 @@ export function CashuWallet() {
|
||||
getFeeRates()
|
||||
]);
|
||||
|
||||
// Check if this is a Send Max transaction
|
||||
const isSendMax = amountSats === bitcoinMaxAmount && bitcoinMaxAmount > 0;
|
||||
|
||||
// Create and sign transaction
|
||||
const { txHex, fee } = await createBitcoinTransaction(
|
||||
privateKey,
|
||||
recipientAddress,
|
||||
amountSats,
|
||||
utxos,
|
||||
feeRates.fastestFee
|
||||
feeRates.fastestFee,
|
||||
isSendMax
|
||||
);
|
||||
|
||||
// Broadcast transaction
|
||||
@@ -316,8 +331,12 @@ export function CashuWallet() {
|
||||
|
||||
setIsLoadingUtxos(true);
|
||||
try {
|
||||
const fetchedUtxos = await fetchUTXOs(bitcoinAddress);
|
||||
const [fetchedUtxos, feeRates] = await Promise.all([
|
||||
fetchUTXOs(bitcoinAddress),
|
||||
getFeeRates()
|
||||
]);
|
||||
setUtxos(fetchedUtxos);
|
||||
setFeeRate(feeRates.fastestFee);
|
||||
} catch (error) {
|
||||
console.error('Error fetching UTXOs:', error);
|
||||
toast({
|
||||
@@ -529,9 +548,10 @@ export function CashuWallet() {
|
||||
<div className="max-w-4xl mx-auto">
|
||||
<button
|
||||
onClick={() => setShowHistoryModal(true)}
|
||||
className="w-full py-2 px-4 flex justify-center border-t border-muted hover:bg-muted/30 transition-colors"
|
||||
className="w-full py-2 px-4 flex flex-col items-center gap-1 border-t border-muted hover:bg-muted/30 transition-colors"
|
||||
>
|
||||
<ChevronUp className="h-5 w-5 text-muted-foreground" />
|
||||
<span className="text-xs text-muted-foreground font-medium">History</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -619,6 +639,8 @@ export function CashuWallet() {
|
||||
pendingBitcoinRecipient={pendingBitcoinRecipient}
|
||||
setPendingBitcoinRecipient={setPendingBitcoinRecipient}
|
||||
hasNsecAccess={hasNsecAccess}
|
||||
bitcoinMaxAmount={bitcoinMaxAmount}
|
||||
onSendMax={handleSendMax}
|
||||
/>
|
||||
</div>
|
||||
</DrawerContent>
|
||||
|
||||
Reference in new issue
Block a user