Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show incoming link amount #1199

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/espressocash_app/lib/data/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OutgoingTransferRows extends Table {
Set<Column<Object>> get primaryKey => {id};
}

const int latestVersion = 48;
const int latestVersion = 49;

const _tables = [
OutgoingTransferRows,
Expand Down Expand Up @@ -211,6 +211,9 @@ class MyDatabase extends _$MyDatabase {
await m.addColumn(offRampOrderRows, offRampOrderRows.feeAmount);
await m.addColumn(offRampOrderRows, offRampOrderRows.feeToken);
}
if (from >= 39 && from < 49) {
await m.addColumn(iLPRows, iLPRows.receivedAmount);
}
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class ILPRows extends Table with EntityMixin, TxStatusMixin {
IntColumn get status => intEnum<ILPStatusDto>()();

IntColumn get feeAmount => integer().nullable()();
IntColumn get receivedAmount => integer().nullable()();
}

enum ILPStatusDto {
Expand Down Expand Up @@ -118,12 +119,19 @@ extension on ILPStatusDto {
);
case ILPStatusDto.success:
final feeAmount = row.feeAmount;
final receivedAmount = row.receivedAmount;

return ILPStatus.success(
tx: tx ?? StubSignedTx(txId!),
fee: feeAmount != null
? CryptoAmount(value: feeAmount, cryptoCurrency: Currency.usdc)
: null,
receivedAmount: receivedAmount != null
? CryptoAmount(
value: receivedAmount,
cryptoCurrency: Currency.usdc,
)
: null,
);
case ILPStatusDto.txFailure:
return ILPStatus.txFailure(
Expand All @@ -147,6 +155,10 @@ extension on IncomingLinkPayment {
ILPStatusSuccess(:final fee) => fee?.value,
_ => null,
},
receivedAmount: switch (status) {
ILPStatusSuccess(:final receivedAmount) => receivedAmount?.value,
_ => null,
},
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sealed class ILPStatus with _$ILPStatus {
const factory ILPStatus.success({
required SignedTx tx,
required CryptoAmount? fee,
required CryptoAmount? receivedAmount,
}) = ILPStatusSuccess;

/// Failed to create the tx, a new tx should be created.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:auto_route/auto_route.dart';
import 'package:dfunc/dfunc.dart';
import 'package:flutter/material.dart';

import '../../../core/amount.dart';
import '../../../core/presentation/format_amount.dart';
import '../../../di.dart';
Expand Down Expand Up @@ -60,7 +59,12 @@ class _IncomingLinkPaymentScreenState extends State<IncomingLinkPaymentScreen> {
onBack: () => context.router.pop(),
onOkPressed: () => context.router.pop(),
content: e.fee?.let(_FeeNotice.new),
statusContent: context.l10n.moneyReceived,
statusContent: e.receivedAmount?.let(
(it) => context.l10n.incomingLinkSuccessMessage(
it.format(context.locale, maxDecimals: 2),
),
) ??
context.l10n.moneyReceived,
),
txFailure: (it) => it.reason == TxFailureReason.escrowFailure
? const InvalidEscrowErrorWidget()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:solana/solana.dart';
import '../../../core/amount.dart';
import '../../../core/cancelable_job.dart';
import '../../../core/currency.dart';
import '../../accounts/models/ec_wallet.dart';
import '../../tokens/token.dart';
import '../../transactions/models/tx_results.dart';
import '../../transactions/services/tx_sender.dart';
import '../data/ilp_repository.dart';
Expand All @@ -21,16 +23,24 @@ import 'payment_watcher.dart';
/// confirmed.
@injectable
class TxSentWatcher extends PaymentWatcher {
TxSentWatcher(super._repository, this._sender, this._ecClient);
TxSentWatcher(
super._repository,
this._sender,
this._ecClient,
this._rpcClient,
this._wallet,
);

final TxSender _sender;
final EspressoCashClient _ecClient;
final RpcClient _rpcClient;
final ECWallet _wallet;

@override
CancelableJob<IncomingLinkPayment> createJob(
IncomingLinkPayment payment,
) =>
_ILPTxSentJob(payment, _sender, _ecClient);
_ILPTxSentJob(payment, _sender, _ecClient, _rpcClient, _wallet);

@override
Stream<IList<IncomingLinkPayment>> watchPayments(
Expand All @@ -40,11 +50,19 @@ class TxSentWatcher extends PaymentWatcher {
}

class _ILPTxSentJob extends CancelableJob<IncomingLinkPayment> {
const _ILPTxSentJob(this.payment, this.sender, this._ecClient);
const _ILPTxSentJob(
this.payment,
this.sender,
this._ecClient,
this._rpcClient,
this._wallet,
);

final IncomingLinkPayment payment;
final TxSender sender;
final EspressoCashClient _ecClient;
final RpcClient _rpcClient;
final ECWallet _wallet;

@override
Future<IncomingLinkPayment?> process() async {
Expand All @@ -64,11 +82,53 @@ class _ILPTxSentJob extends CancelableJob<IncomingLinkPayment> {
.then((value) => value.escrowPaymentAtaFee)
: null;

final tx = await _rpcClient.getTransaction(
status.tx.id,
commitment: Commitment.confirmed,
);

final tokenAddress = await findAssociatedTokenAddress(
owner: _wallet.publicKey,
mint: Token.usdc.publicKey,
);

final accountIndex = status.tx.compiledMessage.accountKeys.indexWhere(
(e) => e == tokenAddress,
);

final previousBalance = tx?.meta?.preTokenBalances
.where((e) => e.accountIndex == accountIndex)
.firstOrNull
?.uiTokenAmount
.amount
.let(int.tryParse) ??
0;

final postBalance = tx?.meta?.postTokenBalances
.where(
(e) => e.accountIndex == accountIndex,
)
.firstOrNull
?.uiTokenAmount
.amount
.let(int.tryParse) ??
0;

final amount = postBalance - previousBalance;

return ILPStatus.success(
tx: status.tx,
fee: fee?.let(
(fee) => CryptoAmount(value: fee, cryptoCurrency: Currency.usdc),
),
receivedAmount: amount.let(
(amount) => amount <= 0
? null
: CryptoAmount(
value: amount,
cryptoCurrency: Currency.usdc,
),
),
);
} on Object {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: invalid_annotation_target
// ignore_for_file: invalid_annotation_target, avoid-missing-interpolation

import 'package:dio/dio.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
Expand Down
4 changes: 3 additions & 1 deletion packages/espressocash_app/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1057,5 +1057,7 @@
"onRampMorePartnersTitle": "Additional Add Cash Partners:",
"@onRampMorePartnersTitle": {},
"offRampMorePartnersTitle": "Additional Payment Partners:",
"@offRampMorePartnersTitle": {}
"@offRampMorePartnersTitle": {},
"incomingLinkSuccessMessage": "{amount} Transaction successfully received.",
"@incomingLinkSuccessMessage": {}
}

Large diffs are not rendered by default.

Loading