|
| 1 | +import { IBtInfo, IGetFeeEstimatesResponse } from 'beignet'; |
| 2 | +import { getFees } from '../src/utils/lightning'; |
| 3 | + |
| 4 | +jest.mock('../src/utils/wallet', () => ({ |
| 5 | + getSelectedNetwork: jest.fn(() => 'bitcoin'), |
| 6 | +})); |
| 7 | + |
| 8 | +describe('getFees', () => { |
| 9 | + const MEMPOOL_URL = 'https://mempool.space/api/v1/fees/recommended'; |
| 10 | + const BLOCKTANK_URL = 'https://api1.blocktank.to/api/info'; |
| 11 | + |
| 12 | + const mockMempoolResponse: IGetFeeEstimatesResponse = { |
| 13 | + fastestFee: 111, |
| 14 | + halfHourFee: 110, |
| 15 | + hourFee: 109, |
| 16 | + minimumFee: 108, |
| 17 | + }; |
| 18 | + |
| 19 | + const mockBlocktankResponse: IBtInfo = { |
| 20 | + onchain: { |
| 21 | + feeRates: { |
| 22 | + fast: 999, |
| 23 | + mid: 998, |
| 24 | + slow: 997, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } as IBtInfo; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 32 | + if (url === MEMPOOL_URL) { |
| 33 | + return Promise.resolve({ |
| 34 | + ok: true, |
| 35 | + json: () => Promise.resolve(mockMempoolResponse), |
| 36 | + }); |
| 37 | + } |
| 38 | + if (url === BLOCKTANK_URL) { |
| 39 | + return Promise.resolve({ |
| 40 | + ok: true, |
| 41 | + json: () => Promise.resolve(mockBlocktankResponse), |
| 42 | + }); |
| 43 | + } |
| 44 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should use mempool.space when both APIs succeed', async () => { |
| 49 | + const result = await getFees(); |
| 50 | + |
| 51 | + expect(result).toEqual({ |
| 52 | + onChainSweep: 111, |
| 53 | + maxAllowedNonAnchorChannelRemoteFee: Math.max(25, 111 * 10), |
| 54 | + minAllowedAnchorChannelRemoteFee: 108, |
| 55 | + minAllowedNonAnchorChannelRemoteFee: 107, |
| 56 | + anchorChannelFee: 109, |
| 57 | + nonAnchorChannelFee: 110, |
| 58 | + channelCloseMinimum: 108, |
| 59 | + }); |
| 60 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 61 | + expect(fetch).toHaveBeenCalledWith(MEMPOOL_URL); |
| 62 | + expect(fetch).toHaveBeenCalledWith(BLOCKTANK_URL); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should use blocktank when mempool.space fails', async () => { |
| 66 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 67 | + if (url === MEMPOOL_URL) { |
| 68 | + return Promise.reject('Mempool failed'); |
| 69 | + } |
| 70 | + if (url === BLOCKTANK_URL) { |
| 71 | + return Promise.resolve({ |
| 72 | + ok: true, |
| 73 | + json: () => Promise.resolve(mockBlocktankResponse), |
| 74 | + }); |
| 75 | + } |
| 76 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 77 | + }); |
| 78 | + |
| 79 | + const result = await getFees(); |
| 80 | + expect(result).toEqual({ |
| 81 | + onChainSweep: 999, |
| 82 | + maxAllowedNonAnchorChannelRemoteFee: Math.max(25, 999 * 10), |
| 83 | + minAllowedAnchorChannelRemoteFee: 997, |
| 84 | + minAllowedNonAnchorChannelRemoteFee: 996, |
| 85 | + anchorChannelFee: 997, |
| 86 | + nonAnchorChannelFee: 998, |
| 87 | + channelCloseMinimum: 997, |
| 88 | + }); |
| 89 | + expect(fetch).toHaveBeenCalledTimes(3); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should retry mempool once and succeed even if blocktank fails', async () => { |
| 93 | + let mempoolAttempts = 0; |
| 94 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 95 | + if (url === MEMPOOL_URL) { |
| 96 | + mempoolAttempts++; |
| 97 | + return mempoolAttempts === 1 |
| 98 | + ? Promise.reject('First mempool try failed') |
| 99 | + : Promise.resolve({ |
| 100 | + ok: true, |
| 101 | + json: () => Promise.resolve(mockMempoolResponse), |
| 102 | + }); |
| 103 | + } |
| 104 | + if (url === BLOCKTANK_URL) { |
| 105 | + return Promise.reject('Blocktank failed'); |
| 106 | + } |
| 107 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 108 | + }); |
| 109 | + |
| 110 | + const result = await getFees(); |
| 111 | + expect(result.onChainSweep).toBe(111); |
| 112 | + expect(fetch).toHaveBeenCalledTimes(4); |
| 113 | + expect(fetch).toHaveBeenCalledWith(MEMPOOL_URL); |
| 114 | + expect(fetch).toHaveBeenCalledWith(BLOCKTANK_URL); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should throw error when all fetches fail', async () => { |
| 118 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 119 | + if (url === MEMPOOL_URL || url === BLOCKTANK_URL) { |
| 120 | + return Promise.reject('API failed'); |
| 121 | + } |
| 122 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 123 | + }); |
| 124 | + |
| 125 | + await expect(getFees()).rejects.toThrow(); |
| 126 | + expect(fetch).toHaveBeenCalledTimes(4); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle invalid mempool response', async () => { |
| 130 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 131 | + if (url === MEMPOOL_URL) { |
| 132 | + return Promise.resolve({ |
| 133 | + ok: true, |
| 134 | + json: () => Promise.resolve({ fastestFee: 0 }), |
| 135 | + }); |
| 136 | + } |
| 137 | + if (url === BLOCKTANK_URL) { |
| 138 | + return Promise.resolve({ |
| 139 | + ok: true, |
| 140 | + json: () => Promise.resolve(mockBlocktankResponse), |
| 141 | + }); |
| 142 | + } |
| 143 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 144 | + }); |
| 145 | + |
| 146 | + const result = await getFees(); |
| 147 | + expect(result.onChainSweep).toBe(999); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle invalid blocktank response', async () => { |
| 151 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 152 | + if (url === MEMPOOL_URL) { |
| 153 | + return Promise.resolve({ |
| 154 | + ok: true, |
| 155 | + json: () => Promise.resolve(mockMempoolResponse), |
| 156 | + }); |
| 157 | + } |
| 158 | + if (url === BLOCKTANK_URL) { |
| 159 | + return Promise.resolve({ |
| 160 | + ok: true, |
| 161 | + json: () => Promise.resolve({ onchain: { feeRates: { fast: 0 } } }), |
| 162 | + }); |
| 163 | + } |
| 164 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 165 | + }); |
| 166 | + |
| 167 | + const result = await getFees(); |
| 168 | + expect(result.onChainSweep).toBe(111); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should handle timeout errors gracefully', async () => { |
| 172 | + jest.useFakeTimers(); |
| 173 | + |
| 174 | + (global.fetch as jest.Mock) = jest.fn(url => { |
| 175 | + if (url === MEMPOOL_URL) { |
| 176 | + return new Promise(resolve => { |
| 177 | + setTimeout(() => resolve({ |
| 178 | + ok: true, |
| 179 | + json: () => Promise.resolve(mockMempoolResponse), |
| 180 | + }), 15000); // longer than timeout |
| 181 | + }); |
| 182 | + } |
| 183 | + if (url === BLOCKTANK_URL) { |
| 184 | + return new Promise(resolve => { |
| 185 | + setTimeout(() => resolve({ |
| 186 | + ok: true, |
| 187 | + json: () => Promise.resolve(mockBlocktankResponse), |
| 188 | + }), 15000); // longer than timeout |
| 189 | + }); |
| 190 | + } |
| 191 | + return Promise.reject(new Error(`Unexpected URL: ${url}`)); |
| 192 | + }); |
| 193 | + |
| 194 | + const feesPromise = getFees(); |
| 195 | + |
| 196 | + jest.advanceTimersByTime(11000); |
| 197 | + |
| 198 | + await expect(feesPromise).rejects.toThrow(); |
| 199 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 200 | + |
| 201 | + jest.useRealTimers(); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
0 commit comments