From 42dad7095a42ab1b1508223ab2e2bbe7cb13fad1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 5 Oct 2025 16:17:57 +0800 Subject: [PATCH] :lipstick: Optimize the transfer --- DysonNetwork.Pass/Wallet/PaymentService.cs | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/DysonNetwork.Pass/Wallet/PaymentService.cs b/DysonNetwork.Pass/Wallet/PaymentService.cs index 6b0681f..dee39c2 100644 --- a/DysonNetwork.Pass/Wallet/PaymentService.cs +++ b/DysonNetwork.Pass/Wallet/PaymentService.cs @@ -441,6 +441,23 @@ public class PaymentService( // Calculate transfer fee (5%) decimal fee = Math.Round(amount * 0.05m, 2); + decimal finalCost = amount + fee; + + // Make sure the account has sufficient balanace for both fee and the transfer + var (payerPocket, isNewlyCreated) = + await wat.GetOrCreateWalletPocketAsync(payerWallet.Id, currency, amount); + + if (isNewlyCreated || payerPocket.Amount < finalCost) + throw new InvalidOperationException("Insufficient funds"); + + // Create main transfer transaction + var transaction = await CreateTransactionAsync( + payerWallet.Id, + payeeWallet.Id, + currency, + amount, + $"Transfer from account {payerAccountId} to {payeeAccountId}", + Shared.Models.TransactionType.Transfer); // Create fee transaction (to system) await CreateTransactionAsync( @@ -448,17 +465,10 @@ public class PaymentService( null, currency, fee, - $"Transfer fee for transfer from account {payerAccountId} to {payeeAccountId}", + $"Transfer fee for transaction #{transaction.Id}", Shared.Models.TransactionType.System); - // Create main transfer transaction - return await CreateTransactionAsync( - payerWallet.Id, - payeeWallet.Id, - currency, - amount, - $"Transfer from account {payerAccountId} to {payeeAccountId}", - Shared.Models.TransactionType.Transfer); + return transaction; } public async Task CreateFundAsync(