forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarDex.test.js
More file actions
192 lines (161 loc) · 5.47 KB
/
Copy pathstellarDex.test.js
File metadata and controls
192 lines (161 loc) · 5.47 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
'use strict';
const mockOrderbook = jest.fn();
const mockServer = { orderbook: mockOrderbook };
const mockServerConstructor = jest.fn(() => mockServer);
const mockNativeAsset = { native: true };
const mockAsset = jest.fn(function Asset(code, issuer) {
return { code, issuer };
});
mockAsset.native = jest.fn(() => mockNativeAsset);
jest.mock('@stellar/stellar-sdk', () => ({
Horizon: {
Server: mockServerConstructor,
},
Asset: mockAsset,
}));
jest.mock('../src/logger', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
}));
const config = require('../src/config');
const logger = require('../src/logger');
const stellarDex = require('../src/services/sources/stellarDex');
const ISSUER = 'G'.padEnd(56, 'A');
function queueOrderBook(result) {
const call = jest.fn();
if (result instanceof Error) {
call.mockRejectedValue(result);
} else {
call.mockResolvedValue(result);
}
const limit = jest.fn(() => ({ call }));
mockOrderbook.mockImplementationOnce(() => ({ limit }));
return { call, limit };
}
beforeEach(() => {
mockOrderbook.mockReset();
mockServerConstructor.mockClear();
mockAsset.mockClear();
mockAsset.native.mockClear();
logger.warn.mockClear();
logger.debug.mockClear();
});
describe('Stellar DEX source', () => {
test('returns midpoint of best ask and best bid for issued assets', async () => {
queueOrderBook({
bids: [{ price: '2.0' }],
asks: [{ price: '2.2' }],
});
queueOrderBook({
bids: [{ price: '0.10' }],
asks: [{ price: '0.12' }],
});
await expect(stellarDex.fetchPrice('TEST', ISSUER)).resolves.toBeCloseTo(0.231);
expect(mockOrderbook).toHaveBeenNthCalledWith(
1,
{ code: 'TEST', issuer: ISSUER },
mockNativeAsset
);
expect(mockOrderbook).toHaveBeenNthCalledWith(
2,
mockNativeAsset,
{ code: 'USDC', issuer: config.stellar.usdcIssuer }
);
});
test('uses best bid when asks are empty', async () => {
queueOrderBook({
bids: [{ price: '0.11' }],
asks: [],
});
await expect(stellarDex.fetchPrice('XLM')).resolves.toBe(0.11);
});
test('uses best ask when bids are empty', async () => {
queueOrderBook({
bids: [],
asks: [{ price: '0.12' }],
});
await expect(stellarDex.fetchPrice('XLM')).resolves.toBe(0.12);
});
test('returns null when asks and bids are empty', async () => {
queueOrderBook({
bids: [],
asks: [],
});
await expect(stellarDex.fetchPrice('XLM')).resolves.toBeNull();
});
test('returns null for issued assets when issuer is missing', async () => {
await expect(stellarDex.fetchPrice('USDC')).resolves.toBeNull();
expect(mockOrderbook).not.toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalledWith(
'Stellar DEX issuer required for issued asset',
{ assetCode: 'USDC' }
);
});
test('throws when Horizon returns a non-200 error', async () => {
const error = new Error('Horizon request failed with status 500');
error.response = { status: 500 };
queueOrderBook(error);
await expect(stellarDex.fetchPrice('XLM')).rejects.toThrow('status 500');
expect(logger.warn).toHaveBeenCalledWith(
'Stellar DEX price fetch failed',
expect.objectContaining({ assetCode: 'XLM', error: error.message })
);
});
test('throws timeout errors from Horizon', async () => {
const error = new Error('timeout of 10000ms exceeded');
error.code = 'ECONNABORTED';
queueOrderBook(error);
await expect(stellarDex.fetchPrice('XLM')).rejects.toThrow('timeout');
expect(logger.warn).toHaveBeenCalledWith(
'Stellar DEX price fetch failed',
expect.objectContaining({ assetCode: 'XLM', error: error.message })
);
});
test('throws and logs when XLM/USD conversion lookup fails', async () => {
queueOrderBook({
bids: [{ price: '2.0' }],
asks: [{ price: '2.2' }],
});
const error = new Error('XLM/USDC lookup failed');
queueOrderBook(error);
await expect(stellarDex.fetchPrice('TEST', ISSUER)).rejects.toThrow(
'XLM/USDC lookup failed'
);
expect(logger.warn).toHaveBeenCalledWith('XLM/USDC price fetch failed', {
error: error.message,
});
expect(logger.warn).toHaveBeenCalledWith(
'Stellar DEX price fetch failed',
expect.objectContaining({ assetCode: 'TEST', issuer: ISSUER })
);
});
test('uses the native XLM/USDC orderbook for XLM without issuer', async () => {
queueOrderBook({
bids: [{ price: '0.11' }],
asks: [{ price: '0.12' }],
});
await expect(stellarDex.fetchPrice('XLM')).resolves.toBeCloseTo(0.115);
expect(mockOrderbook).toHaveBeenCalledTimes(1);
expect(mockAsset.native).toHaveBeenCalledTimes(1);
expect(mockAsset).toHaveBeenCalledWith('USDC', config.stellar.usdcIssuer);
expect(mockOrderbook).toHaveBeenCalledWith(
mockNativeAsset,
{ code: 'USDC', issuer: config.stellar.usdcIssuer }
);
});
describe('isSupported', () => {
test('is true for XLM regardless of issuer', () => {
expect(stellarDex.isSupported('XLM')).toBe(true);
expect(stellarDex.isSupported('xlm')).toBe(true);
});
test('is true for a non-XLM asset with an issuer', () => {
expect(stellarDex.isSupported('USDC', ISSUER)).toBe(true);
});
test('is false for a non-XLM asset with no issuer', () => {
expect(stellarDex.isSupported('USDC')).toBe(false);
expect(stellarDex.isSupported('USDC', null)).toBe(false);
});
});
});