Skip to content
Merged
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
64 changes: 64 additions & 0 deletions packages/anchor-service/src/anchor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
CustomerStatus,
IdentityDocument,
} from './interfaces/customer.interface';
import type { FeeQuote, TransactionType } from './interfaces/fee.interface';

interface CustomerRecord {
customerId: string;
Expand Down Expand Up @@ -303,6 +304,69 @@ export class AnchorService {
return Array.from(this.customers.values());
}

// ---------------------------------------------------------------------------
// Fee Calculation
// ---------------------------------------------------------------------------

private readonly feeSchedule: Record<string, { flat: string; percentage: string }> = {
USDC: { flat: '1.00', percentage: '0' },
USDT: { flat: '1.00', percentage: '0' },
BTC: { flat: '0.0001', percentage: '0.5' },
ETH: { flat: '0.001', percentage: '0.3' },
XLM: { flat: '0.01', percentage: '0' },
};

async calculateAnchorFee(
amount: string,
asset: string,
type: TransactionType,
): Promise<FeeQuote> {
const schedule = this.feeSchedule[asset] ?? { flat: '0', percentage: '1' };
const parsedAmount = parseFloat(amount);

if (isNaN(parsedAmount) || parsedAmount <= 0) {
throw new Error(`Invalid amount: ${amount}`);
}

const breakdown: FeeQuote['feeBreakdown'] = [];

let totalFee = 0;

if (schedule.flat !== '0') {
const flatFee = parseFloat(schedule.flat);
totalFee += flatFee;
breakdown.push({
type: 'flat',
description: `Flat fee for ${asset}`,
amount: schedule.flat,
});
}

if (schedule.percentage !== '0') {
const percentageFee = parsedAmount * (parseFloat(schedule.percentage) / 100);
totalFee += percentageFee;
breakdown.push({
type: 'percentage',
description: `${schedule.percentage}% fee on ${asset}`,
amount: percentageFee.toFixed(7),
});
}

const effectiveRate = parsedAmount > 0 ? ((totalFee / parsedAmount) * 100).toFixed(4) : '0';

return {
totalFee: totalFee.toFixed(7),
asset,
type,
amount,
feeBreakdown: breakdown,
effectiveRate: `${effectiveRate}%`,
quoteId: `fee_${crypto.randomUUID().split('-').join('').slice(0, 16)}`,
expiresAt: new Date(Date.now() + 60_000).toISOString(),
createdAt: new Date().toISOString(),
};
}

private validateRequiredFields(data: CustomerData, isUpdate: boolean): string[] {
const missing: string[] = [];

Expand Down
1 change: 1 addition & 0 deletions packages/anchor-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export type {
IdentityDocument,
FileContent,
} from './interfaces/customer.interface';
export type { FeeQuote, FeeBreakdown, TransactionType } from './interfaces/fee.interface';
19 changes: 19 additions & 0 deletions packages/anchor-service/src/interfaces/fee.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type TransactionType = 'deposit' | 'withdrawal' | 'send';

export interface FeeBreakdown {
type: 'flat' | 'percentage';
description: string;
amount: string;
}

export interface FeeQuote {
totalFee: string;
asset: string;
type: TransactionType;
amount: string;
feeBreakdown: FeeBreakdown[];
effectiveRate: string;
quoteId: string;
expiresAt: string;
createdAt: string;
}
Loading