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
5 changes: 5 additions & 0 deletions backend/src/modules/savings/savings.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ describe('SavingsService', () => {
userId: 'user-1',
productId: 'product-1',
amount: 12.5,
product: { interestRate: 10 },
createdAt: new Date('2026-01-01'),
},
]);
Expand All @@ -246,6 +247,8 @@ describe('SavingsService', () => {
balanceSource: 'rpc',
vaultContractId:
'CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M',
// 2.5 * 0.10 / (365 * 24 * 3600) ≈ 0.0000000079
estimatedYieldPerSecond: expect.any(Number),
}),
]);
expect(blockchainSavingsService.getUserVaultBalance).toHaveBeenCalledWith(
Expand All @@ -261,6 +264,7 @@ describe('SavingsService', () => {
userId: 'user-1',
productId: 'product-1',
amount: 8.75,
product: { interestRate: 5 },
createdAt: new Date('2026-01-01'),
},
]);
Expand All @@ -275,6 +279,7 @@ describe('SavingsService', () => {
liveBalance: 8.75,
balanceSource: 'cache',
vaultContractId: null,
estimatedYieldPerSecond: expect.any(Number),
}),
]);
expect(blockchainSavingsService.getUserVaultBalance).not.toHaveBeenCalled();
Expand Down
6 changes: 6 additions & 0 deletions backend/src/modules/savings/savings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface UserSubscriptionWithLiveBalance extends UserSubscription {
liveBalanceStroops: number;
balanceSource: 'rpc' | 'cache';
vaultContractId: string | null;
estimatedYieldPerSecond: number;
}

const STROOPS_PER_XLM = 10_000_000;
Expand Down Expand Up @@ -451,13 +452,18 @@ export class SavingsService {
balanceSource: 'rpc' | 'cache',
vaultContractId: string | null,
): UserSubscriptionWithLiveBalance {
const annualRate = Number(subscription.product?.interestRate ?? 0) / 100;
const estimatedYieldPerSecond = parseFloat(
((liveBalance * annualRate) / (365 * 24 * 3600)).toFixed(10),
);
return {
...subscription,
indexedAmount: Number(subscription.amount),
liveBalance,
liveBalanceStroops,
balanceSource,
vaultContractId,
estimatedYieldPerSecond,
};
}

Expand Down
Loading