-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
295d492
commit e9bd89c
Showing
5 changed files
with
137 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...espressocash_app/lib/features/ramp/partners/moneygram/service/moneygram_fees_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.