@@ -61,7 +61,7 @@ export default function FortuneTigerBetCard({
6161 onLoadBalance,
6262 walletType,
6363} : FortuneTigerBetCardProps ) {
64- const { walletBalance, contractBalance, placeBet, connectWallet, balance, refreshBalance, isLoadingBalance, balanceVerified, setBalance, setContractBalance } = useWallet ( ) ;
64+ const { walletBalance, contractBalance, placeBet, connectWallet, balance, refreshBalance, isLoadingBalance, balanceVerified, setBalance, setContractBalance, claimBalance } = useWallet ( ) ;
6565 const { isConnected, getContractStateForToken, getContractIdForToken, allBets, address } = useHathor ( ) ;
6666 const [ showMobileDisconnect , setShowMobileDisconnect ] = useState ( false ) ;
6767 const contractBalanceInTokens = Number ( contractBalance ) / 100 ;
@@ -96,6 +96,7 @@ export default function FortuneTigerBetCard({
9696 const [ showAnimationSelector , setShowAnimationSelector ] = useState ( false ) ;
9797 const [ debugMode , setDebugMode ] = useState ( false ) ;
9898 const [ hasAttemptedBalance , setHasAttemptedBalance ] = useState ( false ) ;
99+ const [ isWithdrawing , setIsWithdrawing ] = useState ( false ) ;
99100
100101 const contractState = getContractStateForToken ( selectedToken ) ;
101102 const randomBitLength = contractState ?. random_bit_length || 16 ;
@@ -339,6 +340,39 @@ export default function FortuneTigerBetCard({
339340 }
340341 } ;
341342
343+ const handleWithdrawGains = async ( ) => {
344+ if ( ! isConnected || contractBalance <= 0n ) {
345+ return ;
346+ }
347+
348+ const contractId = getContractIdForToken ( selectedToken ) ;
349+ if ( ! contractId ) {
350+ toast . error ( 'Contract not found for token' ) ;
351+ return ;
352+ }
353+
354+ const tokenUid = contractState ?. token_uid || '00' ;
355+ const withdrawAmount = Number ( contractBalance ) / 100 ; // Convert cents to tokens
356+
357+ setIsWithdrawing ( true ) ;
358+ toast . info ( '⏳ Please confirm the withdrawal in your wallet...' ) ;
359+
360+ try {
361+ const result = await claimBalance ( withdrawAmount , selectedToken , contractId , tokenUid ) ;
362+
363+ // Update balances locally IMMEDIATELY after successful withdrawal
364+ const withdrawAmountCents = Math . round ( withdrawAmount * 100 ) ;
365+ setBalance ( prev => prev + BigInt ( withdrawAmountCents ) ) ;
366+ setContractBalance ( 0n ) ;
367+
368+ toast . success ( `Withdrawal successful! TX: ${ result . response . hash ?. slice ( 0 , 10 ) } ...` ) ;
369+ } catch ( error : any ) {
370+ toast . error ( error . message || 'Failed to withdraw' ) ;
371+ } finally {
372+ setIsWithdrawing ( false ) ;
373+ }
374+ } ;
375+
342376 return (
343377 < >
344378 < div className = "min-h-screen bg-[#0f0518] flex items-center justify-center p-4 font-serif" >
@@ -383,7 +417,6 @@ export default function FortuneTigerBetCard({
383417 < NetworkSelector
384418 value = { network }
385419 onChange = { onNetworkChange }
386- disabled = { isConnected }
387420 />
388421 </ div >
389422 ) }
@@ -664,14 +697,25 @@ export default function FortuneTigerBetCard({
664697 { /* Mobile Balance & Statistics - Below SPIN button on mobile only */ }
665698 < div className = "flex md:hidden items-stretch justify-center gap-3 mt-2 w-full" >
666699 { localFormattedBalance ? (
667- < div className = "flex-1 px-4 py-2 rounded-full border-2 border-yellow-500/60 bg-gradient-to-br from-yellow-900/30 via-black/50 to-yellow-900/30 backdrop-blur-sm flex items-center justify-center" >
668- < div className = "flex items-center justify-center gap-2" >
669- < div className = "text-base" > 💰</ div >
670- < span className = "text-xs font-bold text-transparent bg-clip-text bg-gradient-to-r from-yellow-200 via-yellow-400 to-yellow-200 font-mono" >
671- { localFormattedBalance }
672- </ span >
700+ < >
701+ < div className = "flex-1 px-4 py-2 rounded-full border-2 border-yellow-500/60 bg-gradient-to-br from-yellow-900/30 via-black/50 to-yellow-900/30 backdrop-blur-sm flex items-center justify-center" >
702+ < div className = "flex items-center justify-center gap-2" >
703+ < div className = "text-base" > 💰</ div >
704+ < span className = "text-xs font-bold text-transparent bg-clip-text bg-gradient-to-r from-yellow-200 via-yellow-400 to-yellow-200 font-mono" >
705+ { localFormattedBalance }
706+ </ span >
707+ </ div >
673708 </ div >
674- </ div >
709+ { contractBalance > 0n && (
710+ < button
711+ onClick = { handleWithdrawGains }
712+ disabled = { isWithdrawing || isPlacingBet || isSpinning }
713+ className = "flex-1 px-4 py-2 rounded-full text-xs font-bold transition-all bg-white/10 backdrop-blur-md border-2 border-white/20 shadow-xl text-white/90 hover:text-white hover:bg-white/20 flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed"
714+ >
715+ { isWithdrawing ? 'Withdrawing...' : 'Withdraw gains' }
716+ </ button >
717+ ) }
718+ </ >
675719 ) : isLoadingBalance ? (
676720 < div className = "flex-1 px-4 py-2 rounded-full text-xs font-medium text-slate-300 text-center flex items-center justify-center" >
677721 Authorize to view balance
0 commit comments