🐛 Tried to fix fund claim cocurrency issue

This commit is contained in:
2025-11-17 00:18:57 +08:00
parent 6313f15375
commit 861fc7cafa

View File

@@ -609,6 +609,12 @@ public class PaymentService(
public async Task<SnWalletTransaction> ReceiveFundAsync(Guid recipientAccountId, Guid fundId) public async Task<SnWalletTransaction> ReceiveFundAsync(Guid recipientAccountId, Guid fundId)
{ {
// Use a transaction to ensure atomicity
await using var transactionScope = await db.Database.BeginTransactionAsync();
try
{
// Load fund with proper locking to prevent concurrent modifications
var fund = await db.WalletFunds var fund = await db.WalletFunds
.Include(f => f.Recipients) .Include(f => f.Recipients)
.FirstOrDefaultAsync(f => f.Id == fundId); .FirstOrDefaultAsync(f => f.Id == fundId);
@@ -668,7 +674,7 @@ public class PaymentService(
throw new InvalidOperationException("Recipient wallet not found"); throw new InvalidOperationException("Recipient wallet not found");
// Create transaction to transfer funds to recipient // Create transaction to transfer funds to recipient
var transaction = await CreateTransactionAsync( var walletTransaction = await CreateTransactionAsync(
payerWalletId: null, // System transfer payerWalletId: null, // System transfer
payeeWalletId: recipientWallet.Id, payeeWalletId: recipientWallet.Id,
currency: fund.Currency, currency: fund.Currency,
@@ -700,8 +706,15 @@ public class PaymentService(
} }
await db.SaveChangesAsync(); await db.SaveChangesAsync();
await transactionScope.CommitAsync();
return transaction; return walletTransaction;
}
catch
{
await transactionScope.RollbackAsync();
throw;
}
} }
public async Task ProcessExpiredFundsAsync() public async Task ProcessExpiredFundsAsync()