Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
justinenerio committed Oct 25, 2024
1 parent 295d492 commit e9bd89c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 61 deletions.
8 changes: 7 additions & 1 deletion packages/espressocash_app/lib/data/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OutgoingTransferRows extends Table {
Set<Column<Object>> get primaryKey => {id};
}

const int latestVersion = 58;
const int latestVersion = 59;

const _tables = [
OutgoingTransferRows,
Expand Down Expand Up @@ -164,6 +164,10 @@ class MyDatabase extends _$MyDatabase {
if (from < 58) {
await m.createTable(tokenRows);
}
if (from < 59) {
await m.addColumn(offRampOrderRows, offRampOrderRows.priorityFee);
await m.addColumn(offRampOrderRows, offRampOrderRows.gasFee);
}
},
);
}
Expand Down Expand Up @@ -224,6 +228,8 @@ class OffRampOrderRows extends Table with AmountMixin, EntityMixin {
IntColumn get bridgeAmount => integer().nullable()();
TextColumn get referenceNumber => text().nullable()();
IntColumn get refundAmount => integer().nullable()();
IntColumn get priorityFee => integer().nullable()();
IntColumn get gasFee => integer().nullable()();
}

enum OnRampOrderStatus {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:async/async.dart';
import 'package:decimal/decimal.dart';
import 'package:espressocash_api/espressocash_api.dart';
import 'package:injectable/injectable.dart';

import '../../../../accounts/auth_scope.dart';
import '../../../../currency/models/amount.dart';
import '../../../../currency/models/currency.dart';
import '../../../models/ramp_type.dart';

typedef MoneygramFees = ({
Amount receiveAmount,
Amount moneygramFee,
Amount bridgeFee,
Amount gasFeeInUsdc,
int? priorityFee,
});

@Singleton(scope: authScope)
class MoneygramFeesService {
MoneygramFeesService(this._client);

final EspressoCashClient _client;
final _cache = AsyncCache<MoneygramFees>(const Duration(seconds: 30));

Amount? _lastAmount;
RampType? _lastType;

Future<MoneygramFees> fetchFees({
required Amount amount,
required RampType type,
}) {
if (amount != _lastAmount || type != _lastType) {
_cache.invalidate();
_lastAmount = amount;
_lastType = type;
}

return _cache.fetch(() => _fetchFeesFromApi(amount: amount, type: type));
}

Future<MoneygramFees> _fetchFeesFromApi({
required Amount amount,
required RampType type,
}) async {
final fee = await _client.calculateMoneygramFee(
MoneygramFeeRequestDto(
type: type.toDto(),
amount: amount.decimal.toString(),
),
);

return (
receiveAmount: Amount.fromDecimal(
value: Decimal.parse(fee.totalAmount),
currency: switch (type) {
RampType.onRamp => Currency.usdc,
RampType.offRamp => Currency.usd
},
),
moneygramFee: Amount.fromDecimal(
value: Decimal.parse(fee.moneygramFee),
currency: switch (type) {
RampType.onRamp => Currency.usd,
RampType.offRamp => Currency.usdc
},
),
bridgeFee: Amount.fromDecimal(
value: Decimal.parse(fee.bridgeFee),
currency: Currency.usdc,
),
gasFeeInUsdc: Amount.fromDecimal(
value: Decimal.parse(fee.gasFeeInUsdc ?? '0'),
currency: Currency.usdc,
),
priorityFee: fee.priorityFee,
);
}
}

extension on RampType {
RampTypeDto toDto() => switch (this) {
RampType.onRamp => RampTypeDto.onRamp,
RampType.offRamp => RampTypeDto.offRamp
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ class MoneygramOffRampOrderService implements Disposable {

AsyncResult<String> createMoneygramOrder({
required CryptoAmount submittedAmount,
required FiatAmount? receiveAmount,
required FiatAmount receiveAmount,
required String countryCode,
required int priorityFee,
required CryptoAmount gasFee,
}) =>
tryEitherAsync((_) async {
{
Expand All @@ -182,8 +184,8 @@ class MoneygramOffRampOrderService implements Disposable {
partnerOrderId: '',
amount: submittedAmount.value,
token: Token.usdc.address,
receiveAmount: receiveAmount?.value,
fiatSymbol: receiveAmount?.fiatCurrency.symbol,
receiveAmount: receiveAmount.value,
fiatSymbol: receiveAmount.fiatCurrency.symbol,
created: DateTime.now(),
humanStatus: '',
machineStatus: '',
Expand All @@ -193,6 +195,8 @@ class MoneygramOffRampOrderService implements Disposable {
depositAddress: '',
slot: BigInt.zero,
bridgeAmount: null,
priorityFee: priorityFee,
gasFee: gasFee.value,
);

await _db.into(_db.offRampOrderRows).insert(order);
Expand Down Expand Up @@ -343,11 +347,18 @@ class MoneygramOffRampOrderService implements Disposable {
) async {
final accountId = _stellarWallet.address;

final cashOutAmount = CryptoAmount(
final gasFee = CryptoAmount(
value: order.gasFee ?? 0,
cryptoCurrency: Currency.usdc,
);

final inputAmount = CryptoAmount(
value: order.amount,
cryptoCurrency: Currency.usdc,
);

final cashOutAmount = inputAmount - gasFee;

final xlmBalance = await _stellarClient.getXlmBalance();

if (xlmBalance <= _minimumInitBalance) {
Expand All @@ -368,6 +379,7 @@ class MoneygramOffRampOrderService implements Disposable {
amount: cashOutAmount.value.toString(),
solanaSenderAddress: _ecWallet.address,
stellarReceiverAddress: accountId,
priorityFee: order.priorityFee,
),
)
.then((e) => e.encodedTx);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:decimal/decimal.dart';
import 'package:dfunc/dfunc.dart';
import 'package:espressocash_api/espressocash_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Expand Down Expand Up @@ -28,6 +26,7 @@ import '../../../screens/on_ramp_order_screen.dart';
import '../../../screens/ramp_amount_screen.dart';
import '../data/dto.dart';
import '../data/moneygram_client.dart';
import '../service/moneygram_fees_service.dart';
import '../service/moneygram_off_ramp_service.dart';
import '../service/moneygram_on_ramp_service.dart';
import 'style.dart';
Expand Down Expand Up @@ -80,7 +79,8 @@ extension BuildContextExt on BuildContext {

final receiveAmount = await runWithLoader<Amount>(
this,
() async => _fetchFees(amount: usdcAmount, type: type)
() async => sl<MoneygramFeesService>()
.fetchFees(amount: usdcAmount, type: type)
.letAsync((p) => p.receiveAmount),
) as CryptoAmount;

Expand Down Expand Up @@ -200,17 +200,26 @@ window.addEventListener("message", (event) => {

if (submittedAmount is! CryptoAmount) return;

final receiveAmount = await runWithLoader<Amount>(
final fees = await runWithLoader<MoneygramFees>(
this,
() async => _fetchFees(amount: submittedAmount, type: type)
.letAsync((p) => p.receiveAmount),
) as FiatAmount;
() => sl<MoneygramFeesService>()
.fetchFees(amount: submittedAmount, type: type),
);

final priorityFee = fees.priorityFee;
if (priorityFee == null) {
showCpErrorSnackbar(this, message: l10n.tryAgainLater);

return;
}

await sl<MoneygramOffRampOrderService>()
.createMoneygramOrder(
submittedAmount: submittedAmount,
receiveAmount: receiveAmount,
receiveAmount: fees.receiveAmount as FiatAmount,
countryCode: profile.country.code,
priorityFee: priorityFee,
gasFee: fees.gasFeeInUsdc as CryptoAmount,
)
.then((order) {
switch (order) {
Expand Down Expand Up @@ -261,7 +270,7 @@ window.addEventListener("message", (event) => {
required Amount amount,
required RampType type,
}) async {
final fees = await _fetchFees(
final fees = await sl<MoneygramFeesService>().fetchFees(
amount: amount,
type: type,
);
Expand All @@ -273,22 +282,27 @@ window.addEventListener("message", (event) => {
required Amount amount,
required RampType type,
}) async {
final fees = await _fetchFees(
final fees = await sl<MoneygramFeesService>().fetchFees(
amount: amount,
type: type,
);

final totalFees = switch (type) {
RampType.onRamp => fees.bridgeFee,
RampType.offRamp => fees.moneygramFee + fees.bridgeFee,
RampType.offRamp =>
fees.moneygramFee + fees.bridgeFee + fees.gasFeeInUsdc,
};

final bridgeFee = fees.bridgeFee.format(locale, maxDecimals: 2);
final moneygramFee = fees.moneygramFee.format(locale, maxDecimals: 2);
final bridgeFee = fees.bridgeFee.format(locale, maxDecimals: 2);
final withdrawFee = (fees.gasFeeInUsdc + fees.bridgeFee).format(
locale,
maxDecimals: 2,
);

final partnerFee = switch (type) {
RampType.onRamp => bridgeFee,
RampType.offRamp => '$moneygramFee + $bridgeFee',
RampType.offRamp => '$moneygramFee + $withdrawFee ',
};

final extraFee = switch (type) {
Expand All @@ -305,47 +319,4 @@ window.addEventListener("message", (event) => {
),
);
}

Future<({Amount receiveAmount, Amount moneygramFee, Amount bridgeFee})>
_fetchFees({
required Amount amount,
required RampType type,
}) async {
final client = sl<EspressoCashClient>();

final fee = await client.calculateMoneygramFee(
MoneygramFeeRequestDto(
type: type.toDto(),
amount: amount.decimal.toString(),
),
);

return (
receiveAmount: Amount.fromDecimal(
value: Decimal.parse(fee.totalAmount),
currency: switch (type) {
RampType.onRamp => Currency.usdc,
RampType.offRamp => Currency.usd
},
),
moneygramFee: Amount.fromDecimal(
value: Decimal.parse(fee.moneygramFee),
currency: switch (type) {
RampType.onRamp => Currency.usd,
RampType.offRamp => Currency.usdc
},
),
bridgeFee: Amount.fromDecimal(
value: Decimal.parse(fee.bridgeFee),
currency: Currency.usdc,
),
);
}
}

extension on RampType {
RampTypeDto toDto() => switch (this) {
RampType.onRamp => RampTypeDto.onRamp,
RampType.offRamp => RampTypeDto.offRamp
};
}

Large diffs are not rendered by default.

0 comments on commit e9bd89c

Please sign in to comment.