added ledger as souec of funds for payouts

This commit is contained in:
Arseni
2026-03-03 21:03:30 +03:00
parent 3f578353da
commit 51c72a87ae
29 changed files with 796 additions and 385 deletions

View File

@@ -15,7 +15,6 @@ import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/ledger.dart';
import 'package:pshared/utils/exception.dart';
class LedgerAccountsProvider with ChangeNotifier {
final LedgerService _service;
OrganizationsProvider? _organizations;
@@ -25,7 +24,9 @@ class LedgerAccountsProvider with ChangeNotifier {
Resource<List<LedgerAccount>> _resource = Resource(data: []);
Resource<List<LedgerAccount>> get resource => _resource;
List<LedgerAccount> get accounts => (_resource.data ?? []).where((la) => la.role == LedgerAccountRole.operating).toList();
List<LedgerAccount> get accounts => (_resource.data ?? [])
.where((la) => la.role == LedgerAccountRole.operating)
.toList();
bool get isLoading => _resource.isLoading;
Exception? get error => _resource.error;
@@ -33,11 +34,13 @@ class LedgerAccountsProvider with ChangeNotifier {
bool get isRefreshingBalances => _isRefreshingBalances;
final Set<String> _refreshingAccounts = <String>{};
bool isWalletRefreshing(String ledgerAccountRef) => _refreshingAccounts.contains(ledgerAccountRef);
bool isWalletRefreshing(String ledgerAccountRef) =>
_refreshingAccounts.contains(ledgerAccountRef);
// Expose current org id so UI controller can reset per-org state if needed.
String? get organizationRef =>
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
String? get organizationRef => (_organizations?.isOrganizationSet ?? false)
? _organizations!.current.id
: null;
// Used to ignore stale async results (org changes / overlapping requests).
int _opSeq = 0;
@@ -69,7 +72,10 @@ class LedgerAccountsProvider with ChangeNotifier {
_isRefreshingBalances = false;
_refreshingAccounts.clear();
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
_applyResource(
_resource.copyWith(isLoading: true, error: null),
notify: true,
);
try {
final base = await _service.list(orgRef);
@@ -78,7 +84,11 @@ class LedgerAccountsProvider with ChangeNotifier {
if (seq != _opSeq) return;
_applyResource(
Resource<List<LedgerAccount>>(data: result.wallets, isLoading: false, error: result.error),
Resource<List<LedgerAccount>>(
data: result.wallets,
isLoading: false,
error: result.error,
),
notify: true,
);
} catch (e) {
@@ -129,7 +139,9 @@ class LedgerAccountsProvider with ChangeNotifier {
if (_refreshingAccounts.contains(ledgerAccountRef)) return;
final existing = accounts.firstWhereOrNull((w) => w.ledgerAccountRef == ledgerAccountRef);
final existing = accounts.firstWhereOrNull(
(w) => w.ledgerAccountRef == ledgerAccountRef,
);
if (existing == null) return;
final orgRef = org.current.id;
@@ -141,12 +153,15 @@ class LedgerAccountsProvider with ChangeNotifier {
try {
final balance = await _service.getBalance(
organizationRef: orgRef,
organizationRef: orgRef,
ledgerAccountRef: ledgerAccountRef,
);
if ((_accountSeq[ledgerAccountRef] ?? 0) != seq) return;
final next = _replaceWallet(ledgerAccountRef, (w) => w.copyWith(balance: balance));
final next = _replaceWallet(
ledgerAccountRef,
(w) => w.copyWith(balance: balance),
);
if (next == null) return;
_applyResource(_resource.copyWith(data: next), notify: false);
@@ -170,7 +185,10 @@ class LedgerAccountsProvider with ChangeNotifier {
final org = _organizations;
if (org == null || !org.isOrganizationSet) return;
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
_applyResource(
_resource.copyWith(isLoading: true, error: null),
notify: true,
);
try {
await _service.create(
@@ -181,20 +199,31 @@ class LedgerAccountsProvider with ChangeNotifier {
);
await loadAccountsWithBalances();
} catch (e) {
_applyResource(_resource.copyWith(isLoading: false, error: toException(e)), notify: true);
_applyResource(
_resource.copyWith(isLoading: false, error: toException(e)),
notify: true,
);
rethrow;
}
}
// ---------- internals ----------
void _applyResource(Resource<List<LedgerAccount>> newResource, {required bool notify}) {
void _applyResource(
Resource<List<LedgerAccount>> newResource, {
required bool notify,
}) {
_resource = newResource;
if (notify) notifyListeners();
}
List<LedgerAccount>? _replaceWallet(String ledgerAccountRef, LedgerAccount Function(LedgerAccount) updater) {
final idx = accounts.indexWhere((w) => w.ledgerAccountRef == ledgerAccountRef);
List<LedgerAccount>? _replaceWallet(
String ledgerAccountRef,
LedgerAccount Function(LedgerAccount) updater,
) {
final idx = accounts.indexWhere(
(w) => w.ledgerAccountRef == ledgerAccountRef,
);
if (idx < 0) return null;
final next = List<LedgerAccount>.from(accounts);
@@ -202,7 +231,10 @@ class LedgerAccountsProvider with ChangeNotifier {
return next;
}
Future<_LedgerAccountLoadResult> _withBalances(String orgRef, List<LedgerAccount> base) async {
Future<_LedgerAccountLoadResult> _withBalances(
String orgRef,
List<LedgerAccount> base,
) async {
Exception? firstError;
final withBalances = await _mapConcurrent<LedgerAccount, LedgerAccount>(
@@ -211,7 +243,7 @@ class LedgerAccountsProvider with ChangeNotifier {
(ledgerAccount) async {
try {
final balance = await _service.getBalance(
organizationRef: orgRef,
organizationRef: orgRef,
ledgerAccountRef: ledgerAccount.ledgerAccountRef,
);
return ledgerAccount.copyWith(balance: balance);
@@ -243,7 +275,10 @@ class LedgerAccountsProvider with ChangeNotifier {
}
}
final workers = List.generate(min(concurrency, items.length), (_) => worker());
final workers = List.generate(
min(concurrency, items.length),
(_) => worker(),
);
await Future.wait(workers);
return results.cast<R>();

View File

@@ -1,13 +1,17 @@
import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/controllers/payment/source.dart';
import 'package:pshared/models/payment/asset.dart';
import 'package:pshared/models/payment/chain_network.dart';
import 'package:pshared/models/payment/customer.dart';
import 'package:pshared/models/payment/currency_pair.dart';
import 'package:pshared/models/payment/fees/treatment.dart';
import 'package:pshared/models/payment/fx/intent.dart';
import 'package:pshared/models/payment/fx/side.dart';
import 'package:pshared/models/payment/kind.dart';
import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/crypto_address.dart';
import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/methods/iban.dart';
import 'package:pshared/models/payment/methods/ledger.dart';
import 'package:pshared/models/payment/methods/managed_wallet.dart';
import 'package:pshared/models/payment/methods/russian_bank.dart';
import 'package:pshared/models/payment/methods/type.dart';
@@ -18,7 +22,6 @@ import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/payment/amount.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pshared/utils/currency.dart';
import 'package:pshared/utils/payment/fx_helpers.dart';
class QuotationIntentBuilder {
@@ -26,21 +29,23 @@ class QuotationIntentBuilder {
PaymentIntent? build({
required PaymentAmountProvider payment,
required WalletsController wallets,
required PaymentSourceController source,
required PaymentFlowProvider flow,
required RecipientsProvider recipients,
}) {
final selectedWallet = wallets.selectedWallet;
final sourceMethod = _resolveSourceMethod(source);
final sourceCurrency = source.selectedCurrencyCode;
final paymentData = flow.selectedPaymentData;
final selectedMethod = flow.selectedMethod;
if (selectedWallet == null || paymentData == null) return null;
if (sourceMethod == null || sourceCurrency == null || paymentData == null) {
return null;
}
final customer = _buildCustomer(
recipient: recipients.currentObject,
method: selectedMethod,
data: paymentData,
);
final sourceCurrency = currencyCodeToString(selectedWallet.currency);
final amountCurrency = payment.settlementMode == SettlementMode.fixReceived
? _settlementCurrency
: sourceCurrency;
@@ -48,26 +53,22 @@ class QuotationIntentBuilder {
amount: payment.amount.toString(),
currency: amountCurrency,
);
final isLedgerSource = source.selectedLedgerAccount != null;
final isCryptoToCrypto =
paymentData is CryptoAddressPaymentMethod &&
(paymentData.asset?.tokenSymbol ?? '').trim().toUpperCase() ==
amount.currency;
final fxIntent = FxIntentHelper.buildSellBaseBuyQuote(
baseCurrency: sourceCurrency,
quoteCurrency: _settlementCurrency, // TODO: exentd target currencies
final fxIntent = _buildFxIntent(
sourceCurrency: sourceCurrency,
settlementMode: payment.settlementMode,
isLedgerSource: isLedgerSource,
enabled: !isCryptoToCrypto,
);
return PaymentIntent(
kind: PaymentKind.payout,
amount: amount,
destination: paymentData,
source: ManagedWalletPaymentMethod(
managedWalletRef: selectedWallet.id,
asset: PaymentAsset(
tokenSymbol: selectedWallet.tokenSymbol ?? '',
chain: selectedWallet.network ?? ChainNetwork.unspecified,
),
),
source: sourceMethod,
fx: fxIntent,
feeTreatment: payment.payerCoversFee
? FeeTreatment.addToSource
@@ -77,6 +78,56 @@ class QuotationIntentBuilder {
);
}
FxIntent? _buildFxIntent({
required String sourceCurrency,
required SettlementMode settlementMode,
required bool isLedgerSource,
required bool enabled,
}) {
if (!enabled) return null;
// Ledger sources in fix_received mode need explicit reverse side.
// BFF maps only settlement currency + fx side, then quotation derives pair.
// For ledger this preserves source debit in ledger currency (e.g. USDT).
if (isLedgerSource && settlementMode == SettlementMode.fixReceived) {
final base = sourceCurrency.trim();
final quote = _settlementCurrency;
if (base.isEmpty || base.toUpperCase() == quote.toUpperCase()) {
return null;
}
return FxIntent(
pair: CurrencyPair(base: base, quote: quote),
side: FxSide.buyBaseSellQuote,
);
}
return FxIntentHelper.buildSellBaseBuyQuote(
baseCurrency: sourceCurrency,
quoteCurrency: _settlementCurrency, // TODO: exentd target currencies
enabled: true,
);
}
PaymentMethodData? _resolveSourceMethod(PaymentSourceController source) {
final wallet = source.selectedWallet;
if (wallet != null) {
return ManagedWalletPaymentMethod(
managedWalletRef: wallet.id,
asset: PaymentAsset(
tokenSymbol: wallet.tokenSymbol ?? '',
chain: wallet.network ?? ChainNetwork.unspecified,
),
);
}
final ledger = source.selectedLedgerAccount;
if (ledger != null) {
return LedgerPaymentMethod(ledgerAccountRef: ledger.ledgerAccountRef);
}
return null;
}
Customer? _buildCustomer({
required Recipient? recipient,
required PaymentMethod? method,

View File

@@ -7,7 +7,7 @@ import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/payment/quote.dart';
import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/controllers/payment/source.dart';
import 'package:pshared/data/mapper/payment/intent/payment.dart';
import 'package:pshared/models/asset.dart';
import 'package:pshared/models/payment/intent.dart';
@@ -36,6 +36,7 @@ class QuotationProvider extends ChangeNotifier {
late OrganizationsProvider _organizations;
bool _isLoaded = false;
PaymentIntent? _lastIntent;
String? _sourceCurrencyCode;
final QuotationIntentBuilder _intentBuilder = QuotationIntentBuilder();
final AutoRefreshScheduler _autoRefresh = AutoRefreshScheduler();
AutoRefreshMode _autoRefreshMode = AutoRefreshMode.on;
@@ -43,15 +44,16 @@ class QuotationProvider extends ChangeNotifier {
void update(
OrganizationsProvider venue,
PaymentAmountProvider payment,
WalletsController wallets,
PaymentSourceController source,
PaymentFlowProvider flow,
RecipientsProvider recipients,
PaymentMethodsProvider _,
) {
_organizations = venue;
_sourceCurrencyCode = source.selectedCurrencyCode;
final intent = _intentBuilder.build(
payment: payment,
wallets: wallets,
source: source,
flow: flow,
recipients: recipients,
);
@@ -77,7 +79,12 @@ class QuotationProvider extends ChangeNotifier {
}
Asset? get fee => _assetFromMoney(quoteFeeTotal(quotation));
Asset? get total => _assetFromMoney(quotation?.amounts?.sourceDebitTotal);
Asset? get total => _assetFromMoney(
quoteSourceDebitTotal(
quotation,
preferredSourceCurrency: _sourceCurrencyCode,
),
);
Asset? get recipientGets =>
_assetFromMoney(quotation?.amounts?.destinationSettlement);
@@ -139,6 +146,7 @@ class QuotationProvider extends ChangeNotifier {
void reset() {
_isLoaded = false;
_lastIntent = null;
_sourceCurrencyCode = null;
_setResource(Resource(data: null, isLoading: false, error: null));
}

View File

@@ -13,7 +13,6 @@ import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/payment/wallets.dart';
import 'package:pshared/utils/exception.dart';
class WalletsProvider with ChangeNotifier {
final WalletsService _service;
OrganizationsProvider? _organizations;
@@ -31,11 +30,13 @@ class WalletsProvider with ChangeNotifier {
bool get isRefreshingBalances => _isRefreshingBalances;
final Set<String> _refreshingWallets = <String>{};
bool isWalletRefreshing(String walletRef) => _refreshingWallets.contains(walletRef);
bool isWalletRefreshing(String walletRef) =>
_refreshingWallets.contains(walletRef);
// Expose current org id so UI controller can reset per-org state if needed.
String? get organizationRef =>
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
String? get organizationRef => (_organizations?.isOrganizationSet ?? false)
? _organizations!.current.id
: null;
// Used to ignore stale async results (org changes / overlapping requests).
int _opSeq = 0;
@@ -67,13 +68,25 @@ class WalletsProvider with ChangeNotifier {
_isRefreshingBalances = false;
_refreshingWallets.clear();
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
_applyResource(
_resource.copyWith(isLoading: true, error: null),
notify: true,
);
try {
final base = await _service.getWallets(orgId);
if (seq != _opSeq) return;
// Publish wallets as soon as the list is available, then hydrate balances.
_isRefreshingBalances = true;
_applyResource(
Resource<List<Wallet>>(data: base, isLoading: false, error: null),
notify: true,
);
final result = await _withBalances(orgId, base);
if (seq != _opSeq) return;
_isRefreshingBalances = false;
_applyResource(
Resource<List<Wallet>>(
@@ -85,6 +98,7 @@ class WalletsProvider with ChangeNotifier {
);
} catch (e) {
if (seq != _opSeq) return;
_isRefreshingBalances = false;
_applyResource(
_resource.copyWith(isLoading: false, error: toException(e)),
@@ -145,7 +159,10 @@ class WalletsProvider with ChangeNotifier {
final balance = await _service.getBalance(orgId, walletRef);
if ((_walletSeq[walletRef] ?? 0) != seq) return;
final next = _replaceWallet(walletRef, (w) => w.copyWith(balance: balance));
final next = _replaceWallet(
walletRef,
(w) => w.copyWith(balance: balance),
);
if (next == null) return;
_applyResource(_resource.copyWith(data: next), notify: false);
@@ -169,7 +186,10 @@ class WalletsProvider with ChangeNotifier {
final org = _organizations;
if (org == null || !org.isOrganizationSet) return;
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
_applyResource(
_resource.copyWith(isLoading: true, error: null),
notify: true,
);
try {
await _service.create(
@@ -180,19 +200,28 @@ class WalletsProvider with ChangeNotifier {
);
await loadWalletsWithBalances();
} catch (e) {
_applyResource(_resource.copyWith(isLoading: false, error: toException(e)), notify: true);
_applyResource(
_resource.copyWith(isLoading: false, error: toException(e)),
notify: true,
);
rethrow;
}
}
// ---------- internals ----------
void _applyResource(Resource<List<Wallet>> newResource, {required bool notify}) {
void _applyResource(
Resource<List<Wallet>> newResource, {
required bool notify,
}) {
_resource = newResource;
if (notify) notifyListeners();
}
List<Wallet>? _replaceWallet(String walletRef, Wallet Function(Wallet) updater) {
List<Wallet>? _replaceWallet(
String walletRef,
Wallet Function(Wallet) updater,
) {
final idx = wallets.indexWhere((w) => w.id == walletRef);
if (idx < 0) return null;
@@ -201,7 +230,10 @@ class WalletsProvider with ChangeNotifier {
return next;
}
Future<_WalletLoadResult> _withBalances(String orgRef, List<Wallet> base) async {
Future<_WalletLoadResult> _withBalances(
String orgRef,
List<Wallet> base,
) async {
Exception? firstError;
final withBalances = await _mapConcurrent<Wallet, Wallet>(
@@ -239,7 +271,10 @@ class WalletsProvider with ChangeNotifier {
}
}
final workers = List.generate(min(concurrency, items.length), (_) => worker());
final workers = List.generate(
min(concurrency, items.length),
(_) => worker(),
);
await Future.wait(workers);
return results.cast<R>();