@@ -95,18 +95,17 @@ func (bot *TipBot) TransferToPot(user *lnbits.User, potName string, amount int64
9595 return fmt .Errorf ("amount must be positive" )
9696 }
9797
98- return bot .DB .Users .Transaction (func (tx * gorm.DB ) error {
99- // Get current user balance (within transaction)
100- var userWallet lnbits.User
101- if err := tx .Where ("id = ?" , user .ID ).First (& userWallet ).Error ; err != nil {
102- return fmt .Errorf ("failed to get user: %w" , err )
103- }
98+ // Check sender's available balance (excluding money already in pots)
99+ balance , err := bot .GetUserAvailableBalance (user )
100+ if err != nil {
101+ return fmt .Errorf ("could not get user available balance: %w" , err )
102+ }
104103
105- balance := userWallet . Wallet . Balance
106- // Check if sufficient funds
107- if balance < amount {
108- return fmt . Errorf ( "insufficient balance. Available: %d sats, Requested: %d sats" , balance , amount )
109- }
104+ if balance < amount {
105+ return fmt . Errorf ( "insufficient available balance. Available: %d sats, Requested: %d sats" , balance , amount )
106+ }
107+
108+ return bot . DB . Users . Transaction ( func ( tx * gorm. DB ) error {
110109
111110 // Verify the pot exists
112111 var pot lnbits.SavingsPot
@@ -117,19 +116,8 @@ func (bot *TipBot) TransferToPot(user *lnbits.User, potName string, amount int64
117116 return err
118117 }
119118
120- // Atomically deduct from user balance
121- result := tx .Model (& lnbits.User {}).Where ("id = ? AND wallet_balance >= ?" , user .ID , amount ).
122- UpdateColumn ("wallet_balance" , gorm .Expr ("wallet_balance - ?" , amount ))
123- if result .Error != nil {
124- return fmt .Errorf ("failed to update user balance: %w" , result .Error )
125- }
126- if result .RowsAffected == 0 {
127- return fmt .Errorf ("insufficient balance or user not found" )
128- }
129-
130- // Atomically add to pot balance
131- if err := tx .Model (& lnbits.SavingsPot {}).Where ("user_id = ? AND name = ?" , user .ID , strings .TrimSpace (potName )).
132- UpdateColumn ("balance" , gorm .Expr ("balance + ?" , amount )).Error ; err != nil {
119+ // Atomically add to pot balance (no need to update wallet_balance as it's handled by LNbits)
120+ if err := tx .Model (& lnbits.SavingsPot {}).Where ("user_id = ? AND name = ?" , user .ID , strings .TrimSpace (potName )).UpdateColumn ("balance" , gorm .Expr ("balance + ?" , amount )).Error ; err != nil {
133121 return fmt .Errorf ("failed to update pot balance: %w" , err )
134122 }
135123
@@ -145,21 +133,19 @@ func (bot *TipBot) WithdrawFromPot(user *lnbits.User, potName string, amount int
145133 return fmt .Errorf ("amount must be positive" )
146134 }
147135
148- return bot .DB .Users .Transaction (func (tx * gorm.DB ) error {
149- // Verify the pot exists and has sufficient balance
150- var pot lnbits.SavingsPot
151- if err := tx .Where ("user_id = ? AND name = ?" , user .ID , strings .TrimSpace (potName )).First (& pot ).Error ; err != nil {
152- if err == gorm .ErrRecordNotFound {
153- return fmt .Errorf ("pot '%s' not found" , potName )
154- }
155- return err
156- }
136+ // Pre-check that the pot exists and has sufficient balance
137+ pot , err := bot .GetPot (user , potName )
138+ if err != nil {
139+ return err
140+ }
157141
158- if pot .Balance < amount {
159- return fmt .Errorf ("insufficient pot balance. Available: %d sats, Requested: %d sats" , pot .Balance , amount )
160- }
142+ if pot .Balance < amount {
143+ return fmt .Errorf ("insufficient pot balance. Available: %d sats, Requested: %d sats" , pot .Balance , amount )
144+ }
161145
162- // Atomically deduct from pot balance
146+ return bot .DB .Users .Transaction (func (tx * gorm.DB ) error {
147+
148+ // Atomically deduct from pot balance (no need to update wallet_balance as it's handled by LNbits)
163149 result := tx .Model (& lnbits.SavingsPot {}).Where ("user_id = ? AND name = ? AND balance >= ?" , user .ID , strings .TrimSpace (potName ), amount ).
164150 UpdateColumn ("balance" , gorm .Expr ("balance - ?" , amount ))
165151 if result .Error != nil {
@@ -169,19 +155,6 @@ func (bot *TipBot) WithdrawFromPot(user *lnbits.User, potName string, amount int
169155 return fmt .Errorf ("insufficient pot balance or pot not found" )
170156 }
171157
172- // Atomically add to user wallet balance
173- result = tx .Model (& lnbits.User {}).Where ("id = ?" , user .ID ).
174- UpdateColumn ("wallet_balance" , gorm .Expr ("wallet_balance + ?" , amount ))
175- if result .Error != nil {
176- return fmt .Errorf ("failed to update user balance: %w" , result .Error )
177- }
178- if result .RowsAffected == 0 {
179- return fmt .Errorf ("user not found" )
180- }
181-
182- // Update in-memory user balance
183- user .Wallet .Balance += amount
184-
185158 return nil
186159 })
187160}
0 commit comments