-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check balance before creating swap (#663)
- Loading branch information
1 parent
e4568d1
commit ce479fd
Showing
8 changed files
with
138 additions
and
0 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,20 @@ | ||
import WalletManager from '../wallet/WalletManager'; | ||
import Errors from './Errors'; | ||
|
||
class BalanceCheck { | ||
constructor(private readonly walletManager: WalletManager) {} | ||
|
||
public checkBalance = async (symbol: string, amount: number) => { | ||
const wallet = this.walletManager.wallets.get(symbol); | ||
if (wallet === undefined) { | ||
throw Errors.CURRENCY_NOT_FOUND(symbol); | ||
} | ||
|
||
const balance = await wallet.getBalance(); | ||
if (balance.confirmedBalance < amount) { | ||
throw Errors.INSUFFICIENT_LIQUIDITY(); | ||
} | ||
}; | ||
} | ||
|
||
export default BalanceCheck; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Logger from '../../../lib/Logger'; | ||
import BalanceCheck from '../../../lib/service/BalanceCheck'; | ||
import Errors from '../../../lib/service/Errors'; | ||
import WalletManager from '../../../lib/wallet/WalletManager'; | ||
import CoreWalletProvider from '../../../lib/wallet/providers/CoreWalletProvider'; | ||
import { bitcoinClient } from '../Nodes'; | ||
|
||
jest.mock('../../../lib/db/repositories/ChainTipRepository'); | ||
|
||
describe('BalanceCheck', () => { | ||
const wallet = new CoreWalletProvider(Logger.disabledLogger, bitcoinClient); | ||
const balanceCheck = new BalanceCheck({ | ||
wallets: new Map<string, any>([['BTC', wallet]]), | ||
} as unknown as WalletManager); | ||
|
||
beforeAll(async () => { | ||
await bitcoinClient.connect(); | ||
}); | ||
|
||
afterAll(() => { | ||
bitcoinClient.disconnect(); | ||
}); | ||
|
||
test('should throw when no wallet can be found', async () => { | ||
const symbol = 'notFound'; | ||
await expect(balanceCheck.checkBalance(symbol, 1)).rejects.toEqual( | ||
Errors.CURRENCY_NOT_FOUND(symbol), | ||
); | ||
}); | ||
|
||
test('should not throw when amount is less than balance', async () => { | ||
await balanceCheck.checkBalance( | ||
'BTC', | ||
(await wallet.getBalance()).confirmedBalance - 1, | ||
); | ||
}); | ||
|
||
test('should not throw when amount is equal balance', async () => { | ||
await balanceCheck.checkBalance( | ||
'BTC', | ||
(await wallet.getBalance()).confirmedBalance, | ||
); | ||
}); | ||
|
||
test('should throw when amount is more than balance', async () => { | ||
await expect( | ||
balanceCheck.checkBalance( | ||
'BTC', | ||
(await wallet.getBalance()).confirmedBalance + 1, | ||
), | ||
).rejects.toEqual(Errors.INSUFFICIENT_LIQUIDITY()); | ||
}); | ||
}); |
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