forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-currency.integration.test.ts
More file actions
331 lines (297 loc) · 11.8 KB
/
Copy pathmulti-currency.integration.test.ts
File metadata and controls
331 lines (297 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Multi-Currency Integration Tests
* Tests for multi-currency payment processing with path payments and exchange rate tracking
*/
import { Test, TestingModule } from '@nestjs/testing';
import { Repository } from 'typeorm';
import { getRepositoryToken } from '@nestjs/typeorm';
import { MultiCurrencyService } from './multi-currency.service';
import { ExchangeRateTrackerService } from './exchange-rate-tracker.service';
import { PathPaymentService } from './path-payment.service';
import { MultiCurrencyPayment } from './entities/multi-currency-payment.entity';
import { Payment, PaymentProcessingStatus } from '../entities/payment.entity';
import { Split } from '../entities/split.entity';
import { Participant } from '../entities/participant.entity';
import { StellarService } from '../stellar/stellar.service';
describe('Multi-Currency Integration Tests', () => {
let multiCurrencyService: MultiCurrencyService;
let exchangeRateTracker: ExchangeRateTrackerService;
let pathPaymentService: PathPaymentService;
let paymentRepository: Repository<Payment>;
let splitRepository: Repository<Split>;
let participantRepository: Repository<Participant>;
let multiCurrencyPaymentRepository: Repository<MultiCurrencyPayment>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MultiCurrencyService,
ExchangeRateTrackerService,
PathPaymentService,
{
provide: StellarService,
useValue: {
verifyTransaction: jest.fn(),
getAccountDetails: jest.fn(),
},
},
{
provide: getRepositoryToken(Payment),
useClass: Repository,
},
{
provide: getRepositoryToken(Split),
useClass: Repository,
},
{
provide: getRepositoryToken(Participant),
useClass: Repository,
},
{
provide: getRepositoryToken(MultiCurrencyPayment),
useClass: Repository,
},
],
}).compile();
multiCurrencyService = module.get<MultiCurrencyService>(MultiCurrencyService);
exchangeRateTracker = module.get<ExchangeRateTrackerService>(ExchangeRateTrackerService);
pathPaymentService = module.get<PathPaymentService>(PathPaymentService);
paymentRepository = module.get<Repository<Payment>>(getRepositoryToken(Payment));
splitRepository = module.get<Repository<Split>>(getRepositoryToken(Split));
participantRepository = module.get<Repository<Participant>>(getRepositoryToken(Participant));
multiCurrencyPaymentRepository = module.get<Repository<MultiCurrencyPayment>>(
getRepositoryToken(MultiCurrencyPayment),
);
});
describe('ExchangeRateTrackerService', () => {
it('should parse XLM asset correctly', () => {
const asset = exchangeRateTracker.parseAsset('XLM');
expect(asset.isNative()).toBe(true);
});
it('should parse asset with issuer correctly', () => {
const asset = exchangeRateTracker.parseAsset('USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN');
expect(asset.isNative()).toBe(false);
expect(asset.getCode()).toBe('USDC');
});
it('should format asset correctly', () => {
const asset = exchangeRateTracker.parseAsset('XLM');
const formatted = exchangeRateTracker.formatAsset(asset);
expect(formatted).toBe('XLM');
});
it('should validate asset format', () => {
expect(exchangeRateTracker.validateAssetFormat('XLM')).toBe(true);
expect(exchangeRateTracker.validateAssetFormat('USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN')).toBe(true);
expect(exchangeRateTracker.validateAssetFormat('INVALID')).toBe(false);
expect(exchangeRateTracker.validateAssetFormat('USDC')).toBe(false);
});
it('should get supported assets', () => {
const assets = exchangeRateTracker.getSupportedAssets();
expect(assets).toContain('XLM');
expect(assets.length).toBeGreaterThan(0);
});
});
describe('PathPaymentService', () => {
it('should calculate slippage correctly', () => {
const slippage = pathPaymentService.calculateSlippage(100, 99);
expect(slippage).toBe(1); // 1% slippage
});
it('should check if slippage is acceptable', () => {
expect(pathPaymentService.isSlippageAcceptable(100, 99, 0.02)).toBe(true); // 1% slippage, 2% tolerance
expect(pathPaymentService.isSlippageAcceptable(100, 95, 0.02)).toBe(false); // 5% slippage, 2% tolerance
});
});
describe('MultiCurrencyService', () => {
const mockSplit: Partial<Split> = {
id: 'split-1',
totalAmount: 100,
preferredCurrency: 'XLM',
creatorWalletAddress: 'GCREATOR123456789012345678901234567890123456789012345678',
};
const mockParticipant: Partial<Participant> = {
id: 'participant-1',
splitId: 'split-1',
amountOwed: 50,
walletAddress: 'GPARTICIPANT1234567890123456789012345678901234567890123456',
};
const mockPayment: Partial<Payment> = {
id: 'payment-1',
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
amount: 50,
asset: 'XLM',
status: PaymentProcessingStatus.CONFIRMED,
createdAt: new Date(),
updatedAt: new Date(),
};
beforeEach(() => {
jest.spyOn(splitRepository, 'findOne').mockResolvedValue(mockSplit as Split);
jest.spyOn(participantRepository, 'findOne').mockResolvedValue(mockParticipant as Participant);
jest.spyOn(paymentRepository, 'findOne').mockResolvedValue(mockPayment as Payment);
jest.spyOn(paymentRepository, 'create').mockReturnValue(mockPayment as Payment);
jest.spyOn(paymentRepository, 'save').mockResolvedValue(mockPayment as Payment);
jest.spyOn(multiCurrencyPaymentRepository, 'create').mockReturnValue({
id: 'multi-currency-1',
paymentId: 'payment-1',
paidAsset: 'USDC:GA5Z...',
paidAmount: 50,
receivedAsset: 'XLM',
receivedAmount: 100,
exchangeRate: 2.0,
pathPaymentTxHash: 'tx-hash-123',
createdAt: new Date(),
} as MultiCurrencyPayment);
jest.spyOn(multiCurrencyPaymentRepository, 'save').mockResolvedValue({
id: 'multi-currency-1',
paymentId: 'payment-1',
paidAsset: 'USDC:GA5Z...',
paidAmount: 50,
receivedAsset: 'XLM',
receivedAmount: 100,
exchangeRate: 2.0,
pathPaymentTxHash: 'tx-hash-123',
createdAt: new Date(),
} as MultiCurrencyPayment);
});
it('should process multi-currency payment with conversion', async () => {
jest.spyOn(exchangeRateTracker, 'getBestExchangeRate').mockResolvedValue({
rate: 2.0,
sourceAmount: 50,
destinationAmount: 100,
path: [],
timestamp: new Date(),
});
const result = await multiCurrencyService.processMultiCurrencyPayment({
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
paidAsset: 'USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',
paidAmount: 50,
});
expect(result.success).toBe(true);
expect(result.requiresConversion).toBe(true);
expect(result.exchangeRate).toBe(2.0);
expect(result.receivedAmount).toBe(100);
});
it('should process payment without conversion when same asset', async () => {
jest.spyOn(exchangeRateTracker, 'getBestExchangeRate').mockResolvedValue({
rate: 1.0,
sourceAmount: 50,
destinationAmount: 50,
path: [],
timestamp: new Date(),
});
const result = await multiCurrencyService.processMultiCurrencyPayment({
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
paidAsset: 'XLM',
paidAmount: 50,
});
expect(result.success).toBe(true);
expect(result.requiresConversion).toBe(false);
expect(result.exchangeRate).toBe(1.0);
});
it('should get supported assets', () => {
const assets = multiCurrencyService.getSupportedAssets();
expect(assets).toContain('XLM');
expect(assets.length).toBeGreaterThan(0);
});
it('should validate asset format', () => {
expect(multiCurrencyService.validateAsset('XLM')).toBe(true);
expect(multiCurrencyService.validateAsset('USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN')).toBe(true);
expect(multiCurrencyService.validateAsset('INVALID')).toBe(false);
});
it('should throw error when split not found', async () => {
jest.spyOn(splitRepository, 'findOne').mockResolvedValue(null);
await expect(
multiCurrencyService.processMultiCurrencyPayment({
splitId: 'non-existent',
participantId: 'participant-1',
txHash: 'tx-hash-123',
paidAsset: 'XLM',
paidAmount: 50,
}),
).rejects.toThrow('Split non-existent not found');
});
it('should throw error when participant not found', async () => {
jest.spyOn(participantRepository, 'findOne').mockResolvedValue(null);
await expect(
multiCurrencyService.processMultiCurrencyPayment({
splitId: 'split-1',
participantId: 'non-existent',
txHash: 'tx-hash-123',
paidAsset: 'XLM',
paidAmount: 50,
}),
).rejects.toThrow('Participant non-existent not found');
});
});
describe('Multi-Currency Payment Flow', () => {
it('should handle complete flow: payment -> conversion -> tracking', async () => {
const mockSplit: Partial<Split> = {
id: 'split-1',
totalAmount: 100,
preferredCurrency: 'XLM',
};
const mockParticipant: Partial<Participant> = {
id: 'participant-1',
splitId: 'split-1',
amountOwed: 50,
};
jest.spyOn(splitRepository, 'findOne').mockResolvedValue(mockSplit as Split);
jest.spyOn(participantRepository, 'findOne').mockResolvedValue(mockParticipant as Participant);
jest.spyOn(paymentRepository, 'findOne').mockResolvedValue(null);
jest.spyOn(paymentRepository, 'create').mockReturnValue({
id: 'payment-1',
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
amount: 100,
asset: 'XLM',
status: 'confirmed',
createdAt: new Date(),
updatedAt: new Date(),
} as Payment);
jest.spyOn(paymentRepository, 'save').mockResolvedValue({
id: 'payment-1',
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
amount: 100,
asset: 'XLM',
status: 'confirmed',
createdAt: new Date(),
updatedAt: new Date(),
} as Payment);
jest.spyOn(exchangeRateTracker, 'getBestExchangeRate').mockResolvedValue({
rate: 2.0,
sourceAmount: 50,
destinationAmount: 100,
path: [],
timestamp: new Date(),
});
jest.spyOn(exchangeRateTracker, 'trackExchangeRate').mockResolvedValue({
id: 'multi-currency-1',
paymentId: 'payment-1',
paidAsset: 'USDC:GA5Z...',
paidAmount: 50,
receivedAsset: 'XLM',
receivedAmount: 100,
exchangeRate: 2.0,
pathPaymentTxHash: 'tx-hash-123',
createdAt: new Date(),
} as MultiCurrencyPayment);
const result = await multiCurrencyService.processMultiCurrencyPayment({
splitId: 'split-1',
participantId: 'participant-1',
txHash: 'tx-hash-123',
paidAsset: 'USDC:GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN',
paidAmount: 50,
});
expect(result.success).toBe(true);
expect(result.paymentId).toBe('payment-1');
expect(result.multiCurrencyPaymentId).toBe('multi-currency-1');
expect(exchangeRateTracker.trackExchangeRate).toHaveBeenCalled();
});
});
});