-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59cfcdf
commit a3b07ad
Showing
8 changed files
with
333 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import { IBtInfo, IGetFeeEstimatesResponse } from "beignet"; | ||
import { getFees } from "../src/utils/lightning"; | ||
|
||
jest.mock('../src/utils/wallet', () => ({ | ||
getSelectedNetwork: jest.fn(() => 'bitcoin') | ||
Check failure on line 5 in __tests__/lightning.ts GitHub Actions / Run lint check
|
||
})); | ||
|
||
describe('getFees', () => { | ||
const MEMPOOL_URL = 'https://mempool.space/api/v1/fees/recommended'; | ||
const BLOCKTANK_URL = 'https://api1.blocktank.to/api/info'; | ||
|
||
const mockMempoolResponse: IGetFeeEstimatesResponse = { | ||
fastestFee: 111, | ||
halfHourFee: 110, | ||
hourFee: 109, | ||
minimumFee: 108, | ||
}; | ||
|
||
const mockBlocktankResponse: IBtInfo = { | ||
onchain: { | ||
feeRates: { | ||
fast: 999, | ||
mid: 998, | ||
slow: 997, | ||
} | ||
} | ||
} as IBtInfo; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockMempoolResponse) | ||
}); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockBlocktankResponse) | ||
}); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
}); | ||
|
||
it('should use mempool.space when both APIs succeed', async () => { | ||
const result = await getFees(); | ||
|
||
expect(result).toEqual({ | ||
onChainSweep: 111, | ||
maxAllowedNonAnchorChannelRemoteFee: Math.max(25, 111 * 10), | ||
minAllowedAnchorChannelRemoteFee: 108, | ||
minAllowedNonAnchorChannelRemoteFee: 107, | ||
anchorChannelFee: 109, | ||
nonAnchorChannelFee: 110, | ||
channelCloseMinimum: 108, | ||
}); | ||
expect(fetch).toHaveBeenCalledTimes(2); | ||
expect(fetch).toHaveBeenCalledWith(MEMPOOL_URL); | ||
expect(fetch).toHaveBeenCalledWith(BLOCKTANK_URL); | ||
}); | ||
|
||
it('should use blocktank when mempool.space fails', async () => { | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
return Promise.reject('Mempool failed'); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockBlocktankResponse) | ||
}); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
const result = await getFees(); | ||
expect(result).toEqual({ | ||
onChainSweep: 999, | ||
maxAllowedNonAnchorChannelRemoteFee: Math.max(25, 999 * 10), | ||
minAllowedAnchorChannelRemoteFee: 997, | ||
minAllowedNonAnchorChannelRemoteFee: 996, | ||
anchorChannelFee: 997, | ||
nonAnchorChannelFee: 998, | ||
channelCloseMinimum: 997, | ||
}); | ||
expect(fetch).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should retry mempool once and succeed even if blocktank fails', async () => { | ||
let mempoolAttempts = 0; | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
mempoolAttempts++; | ||
return mempoolAttempts === 1 | ||
? Promise.reject('First mempool try failed') | ||
: Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockMempoolResponse) | ||
}); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return Promise.reject('Blocktank failed'); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
const result = await getFees(); | ||
expect(result.onChainSweep).toBe(111); | ||
expect(fetch).toHaveBeenCalledTimes(4); | ||
expect(fetch).toHaveBeenCalledWith(MEMPOOL_URL); | ||
expect(fetch).toHaveBeenCalledWith(BLOCKTANK_URL); | ||
}); | ||
|
||
it('should throw error when all fetches fail', async () => { | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL || url === BLOCKTANK_URL) { | ||
return Promise.reject('API failed'); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
await expect(getFees()).rejects.toThrow(); | ||
expect(fetch).toHaveBeenCalledTimes(4); | ||
}); | ||
|
||
it('should handle invalid mempool response', async () => { | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ fastestFee: 0 }) | ||
}); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockBlocktankResponse) | ||
}); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
const result = await getFees(); | ||
expect(result.onChainSweep).toBe(999); | ||
}); | ||
|
||
it('should handle invalid blocktank response', async () => { | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockMempoolResponse) | ||
}); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ onchain: { feeRates: { fast: 0 } } }) | ||
}); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
const result = await getFees(); | ||
expect(result.onChainSweep).toBe(111); | ||
}); | ||
|
||
it('should handle timeout errors gracefully', async () => { | ||
// Enable fake timers | ||
jest.useFakeTimers(); | ||
|
||
// Mock slow responses | ||
(global.fetch as jest.Mock) = jest.fn(url => { | ||
if (url === MEMPOOL_URL) { | ||
return new Promise(resolve => | ||
setTimeout(() => resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockMempoolResponse) | ||
}), 15000) // longer than timeout | ||
); | ||
} | ||
if (url === BLOCKTANK_URL) { | ||
return new Promise(resolve => | ||
setTimeout(() => resolve({ | ||
ok: true, | ||
json: () => Promise.resolve(mockBlocktankResponse) | ||
}), 15000) // longer than timeout | ||
); | ||
} | ||
return Promise.reject(new Error(`Unexpected URL: ${url}`)); | ||
}); | ||
|
||
// Start the getFees call (don't await yet) | ||
const feesPromise = getFees(); | ||
|
||
// Fast-forward past the timeout | ||
jest.advanceTimersByTime(11000); | ||
|
||
// Now await the promise and expect it to fail | ||
await expect(feesPromise).rejects.toThrow(); | ||
expect(fetch).toHaveBeenCalledTimes(2); | ||
|
||
// Restore real timers | ||
jest.useRealTimers(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.