Skip to content

Commit

Permalink
feat: update fee calculation for withdraw (#1226)
Browse files Browse the repository at this point in the history
Co-authored-by: ookami-kb <ookami.kb@gmail.com>
justinenerio and ookami-kb authored Jan 14, 2024
1 parent 0075478 commit 6070b62
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -16,5 +16,6 @@ sealed class FeeType with _$FeeType {
const factory FeeType.withdraw({
required int amount,
required RampPartner partner,
required Ed25519HDPublicKey? address,
}) = FeeTypeWithdraw;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:espressocash_api/espressocash_api.dart';
import 'package:injectable/injectable.dart';
import 'package:solana/solana.dart';
@@ -19,29 +21,36 @@ class FeeCalculator {
(fees) async {
switch (type) {
case FeeTypeDirect(:final address):
final hasAta = await _solanaClient.hasAssociatedTokenAccount(
owner: address,
mint: Token.usdc.publicKey,
);

return hasAta
return await _hasUsdcAta(address)
? fees.directPayment.ataExists
: fees.directPayment.ataDoesNotExist;
case FeeTypeLink():
return fees.escrowPayment;
case FeeTypeWithdraw(:final amount, :final partner):
final feePercentage = switch (partner) {
case FeeTypeWithdraw(:final amount, :final partner, :final address):
final percentageFee = switch (partner) {
RampPartner.scalex => fees.withdrawFeePercentage.scalex,
RampPartner.coinflow => fees.withdrawFeePercentage.coinflow,
RampPartner.guardarian => fees.withdrawFeePercentage.guardarian,
RampPartner.rampNetwork =>
fees.withdrawFeePercentage.rampNetwork,
RampPartner.kado => fees.withdrawFeePercentage.kado,
};
final calculatedFee = (amount * feePercentage).ceil();
final percentageFeeAmount = (amount * percentageFee).ceil();

return fees.directPayment.ataExists + calculatedFee;
final accountCreationFeeAmount = address == null
? 0
: await _hasUsdcAta(address)
? fees.directPayment.ataExists
: fees.directPayment.ataDoesNotExist;

return max(percentageFeeAmount, accountCreationFeeAmount);
}
},
).then((fee) => CryptoAmount(value: fee, cryptoCurrency: Currency.usdc));

Future<bool> _hasUsdcAta(Ed25519HDPublicKey address) =>
_solanaClient.hasAssociatedTokenAccount(
owner: address,
mint: Token.usdc.publicKey,
);
}
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ typedef OffRampOrder = ({
DateTime? resolved,
FiatAmount? receiveAmount,
String partnerOrderId,
Ed25519HDPublicKey? depositAddress,
});

@Singleton(scope: authScope)
@@ -112,6 +113,10 @@ class OffRampOrderService implements Disposable {
) as FiatAmount,
);

final depositAddress = row.depositAddress
.maybeWhere((it) => it.isNotEmpty)
?.let(Ed25519HDPublicKey.fromBase58);

return (
id: row.id,
created: row.created,
@@ -121,6 +126,7 @@ class OffRampOrderService implements Disposable {
resolved: row.resolvedAt,
receiveAmount: receiveAmount,
partnerOrderId: row.partnerOrderId,
depositAddress: depositAddress,
);
});
}
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ class OffRampConfirmation extends StatelessWidget {
type: FeeType.withdraw(
amount: order.amount.value,
partner: order.partner,
address: order.depositAddress,
),
),
const SizedBox(height: 21),
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ final offRampOrderScreenStory = Story(
partner: RampPartner.scalex,
resolved: null,
partnerOrderId: 'PARTNER_ORDER_ID',
depositAddress: null,
),
),
);

0 comments on commit 6070b62

Please sign in to comment.