diff --git a/DysonNetwork.Pass/Wallet/PaymentService.cs b/DysonNetwork.Pass/Wallet/PaymentService.cs index 64ca2cb..43cd9cc 100644 --- a/DysonNetwork.Pass/Wallet/PaymentService.cs +++ b/DysonNetwork.Pass/Wallet/PaymentService.cs @@ -165,9 +165,7 @@ public class PaymentService( db.PaymentTransactions.Add(transaction); if (autoSave) - { await db.SaveChangesAsync(); - } if (!silent) await NotifyNewTransaction(transaction, payerWallet, payeeWallet); @@ -614,7 +612,7 @@ public class PaymentService( public async Task ReceiveFundAsync(Guid recipientAccountId, Guid fundId) { // Use a transaction to ensure atomicity - await using var transactionScope = await db.Database.BeginTransactionAsync(IsolationLevel.Serializable); + await using var transactionScope = await db.Database.BeginTransactionAsync(); try { @@ -649,9 +647,12 @@ public class PaymentService( { RecipientAccountId = recipientAccountId, Amount = amount, - IsReceived = false + IsReceived = false, + FundId = fund.Id, }; - fund.Recipients.Add(recipient); + db.WalletFundRecipients.Add(recipient); + await db.SaveChangesAsync(); + fund.RemainingAmount -= amount; } else if (recipient is null) @@ -670,6 +671,9 @@ public class PaymentService( fund.RemainingAmount -= amount; } + db.Update(fund); + await db.SaveChangesAsync(); + if (recipient.IsReceived) throw new InvalidOperationException("You have already received this fund"); @@ -678,15 +682,14 @@ public class PaymentService( throw new InvalidOperationException("Recipient wallet not found"); // Create transaction to transfer funds to recipient + // This call will save changes once var walletTransaction = await CreateTransactionAsync( payerWalletId: null, // System transfer payeeWalletId: recipientWallet.Id, currency: fund.Currency, amount: recipient.Amount, remarks: $"Received fund portion from {fund.CreatorAccountId}", - type: Shared.Models.TransactionType.System, - silent: true, - autoSave: false + type: Shared.Models.TransactionType.System ); // Mark as received @@ -708,6 +711,7 @@ public class PaymentService( : Shared.Models.FundStatus.PartiallyReceived; } + db.Update(fund); await db.SaveChangesAsync(); await transactionScope.CommitAsync(); diff --git a/DysonNetwork.Pass/Wallet/WalletController.cs b/DysonNetwork.Pass/Wallet/WalletController.cs index d18325a..e1163dd 100644 --- a/DysonNetwork.Pass/Wallet/WalletController.cs +++ b/DysonNetwork.Pass/Wallet/WalletController.cs @@ -17,7 +17,8 @@ public class WalletController( WalletService ws, PaymentService payment, AuthService auth, - ICacheService cache + ICacheService cache, + ILogger logger ) : ControllerBase { [HttpPost] @@ -369,6 +370,7 @@ public class WalletController( } catch (Exception err) { + logger.LogError(err, "Failed to receive fund..."); return BadRequest(err.Message); } } diff --git a/DysonNetwork.Shared/Proto/wallet.proto b/DysonNetwork.Shared/Proto/wallet.proto index b0f93ab..a3ede5b 100644 --- a/DysonNetwork.Shared/Proto/wallet.proto +++ b/DysonNetwork.Shared/Proto/wallet.proto @@ -44,7 +44,7 @@ message WalletFund { string remaining_amount = 10; FundSplitType split_type = 4; FundStatus status = 5; - optional string message = 6; + google.protobuf.StringValue message = 6; string creator_account_id = 7; google.protobuf.Timestamp expired_at = 8; repeated WalletFundRecipient recipients = 9;