@@ -7,6 +7,7 @@ import { useHathor } from '@/contexts/HathorContext';
77import {
88 calculatePayout ,
99 formatTokenAmount ,
10+ formatBalanceWithCommas ,
1011 multiplierToThreshold ,
1112 thresholdToWinChance ,
1213 calculateMaxMultiplier ,
@@ -60,12 +61,22 @@ export default function FortuneTigerBetCard({
6061 onLoadBalance,
6162 walletType,
6263} : FortuneTigerBetCardProps ) {
63- const { walletBalance, contractBalance, placeBet, connectWallet, balance, refreshBalance, isLoadingBalance, balanceVerified } = useWallet ( ) ;
64+ const { walletBalance, contractBalance, placeBet, connectWallet, balance, refreshBalance, isLoadingBalance, balanceVerified, setBalance , setContractBalance } = useWallet ( ) ;
6465 const { isConnected, getContractStateForToken, getContractIdForToken, allBets, address } = useHathor ( ) ;
6566 const [ showMobileDisconnect , setShowMobileDisconnect ] = useState ( false ) ;
6667 const contractBalanceInTokens = Number ( contractBalance ) / 100 ;
6768 const totalBalance = walletBalance + contractBalanceInTokens ;
6869
70+ // Compute displayed balance locally from context values (instead of prop)
71+ // This ensures immediate updates when we modify balance/contractBalance
72+ const computedTotalBalance = balance + contractBalance ;
73+ const localFormattedBalance = isConnected && balanceVerified && computedTotalBalance > 0n
74+ ? `${ formatBalanceWithCommas ( computedTotalBalance ) } ${ selectedToken } `
75+ : formattedBalance ; // Fallback to prop if conditions not met
76+
77+ // Track pending bet info for local balance updates
78+ const pendingBetInfoRef = useRef < { betAmountCents : number ; depositAmountCents : number } | null > ( null ) ;
79+
6980 const [ betAmount , setBetAmount ] = useState ( 1 ) ;
7081 const [ selectedMultiplier , setSelectedMultiplier ] = useState ( 1.5 ) ;
7182 const [ showBetInput , setShowBetInput ] = useState ( false ) ;
@@ -109,6 +120,12 @@ export default function FortuneTigerBetCard({
109120 setPotentialPayout ( payout ) ;
110121 } , [ betAmount , selectedMultiplier , randomBitLength , houseEdgeBasisPoints , contractMaxMultiplierTenths ] ) ;
111122
123+ // Keep a ref to the latest contract balance for use in the bet result effect
124+ const contractBalanceRef = useRef ( contractBalance ) ;
125+ useEffect ( ( ) => {
126+ contractBalanceRef . current = contractBalance ;
127+ } , [ contractBalance ] ) ;
128+
112129 // Watch for bet results
113130 useEffect ( ( ) => {
114131 if ( ! pendingBetTxId ) return ;
@@ -121,6 +138,14 @@ export default function FortuneTigerBetCard({
121138 setBetResult ( bet . result as 'win' | 'lose' ) ;
122139 setPendingBetTxId ( null ) ;
123140
141+ // Update contract balance based on result
142+ if ( bet . result === 'win' && bet . payout > 0 ) {
143+ // Add payout to contract balance (payout is in cents)
144+ setContractBalance ( contractBalanceRef . current + BigInt ( Math . round ( bet . payout ) ) ) ;
145+ }
146+ // Clear pending bet info
147+ pendingBetInfoRef . current = null ;
148+
124149 // Randomly select an animation based on result
125150 const winAnimations : AnimationType [ ] = [
126151 'winner-chicken-dinner' ,
@@ -144,6 +169,7 @@ export default function FortuneTigerBetCard({
144169 // Show animation immediately - no delay
145170 setActiveAnimation ( selectedAnimation ) ;
146171 }
172+ // eslint-disable-next-line react-hooks/exhaustive-deps
147173 } , [ allBets , pendingBetTxId ] ) ;
148174
149175 // Trigger balance request when auth popup shows, and close popup when balance is loaded
@@ -280,9 +306,27 @@ export default function FortuneTigerBetCard({
280306 setIsPlacingBet ( true ) ;
281307
282308 try {
309+ // Calculate deposit and bet amounts in cents for local balance tracking
310+ const betAmountCents = Math . round ( betAmount * 100 ) ;
311+ const contractBalanceCents = Number ( contractBalance ) ;
312+ const depositAmountCents = Math . max ( 0 , betAmountCents - contractBalanceCents ) ;
313+
283314 const result = await placeBet ( betAmount , threshold , selectedToken , contractId , tokenUid , contractBalance ) ;
284315 setPendingBetTxId ( result . response . hash ) ;
285316
317+ // Store pending bet info for balance update on result
318+ pendingBetInfoRef . current = { betAmountCents, depositAmountCents } ;
319+
320+ // Update balances locally after successful bet submission
321+ // Wallet balance decreases by deposit amount
322+ if ( depositAmountCents > 0 ) {
323+ setBalance ( balance - BigInt ( depositAmountCents ) ) ;
324+ }
325+ // Contract balance: receives deposit, then bet is deducted
326+ // New contract balance = old + deposit - bet
327+ const newContractBalance = BigInt ( contractBalanceCents ) + BigInt ( depositAmountCents ) - BigInt ( betAmountCents ) ;
328+ setContractBalance ( newContractBalance >= 0n ? newContractBalance : 0n ) ;
329+
286330 // Start spinning AFTER wallet confirmation
287331 setIsSpinning ( true ) ;
288332 toast . success ( '🎰 Transaction confirmed! Spinning...' ) ;
@@ -604,7 +648,7 @@ export default function FortuneTigerBetCard({
604648 { /* Content */ }
605649 < div className = "relative z-10" >
606650 { isPlacingBet ? (
607- < div className = "flex items-center justify-center gap-2 " >
651+ < div className = "flex items-center justify-center gap-4 " >
608652 < GoddessSpinner size = { 48 } interval = { 500 } />
609653 < span className = "text-xl md:text-2xl" > SENDING...</ span >
610654 </ div >
@@ -619,12 +663,12 @@ export default function FortuneTigerBetCard({
619663
620664 { /* Mobile Balance & Statistics - Below SPIN button on mobile only */ }
621665 < div className = "flex md:hidden items-stretch justify-center gap-3 mt-2 w-full" >
622- { formattedBalance ? (
666+ { localFormattedBalance ? (
623667 < 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" >
624668 < div className = "flex items-center justify-center gap-2" >
625669 < div className = "text-base" > 💰</ div >
626670 < 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" >
627- { formattedBalance }
671+ { localFormattedBalance }
628672 </ span >
629673 </ div >
630674 </ div >
0 commit comments