|
1 | 1 | import chai, { expect } from 'chai'; |
2 | 2 | import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import sinon from 'sinon'; |
3 | 4 | import { OrderSide, Owner, SwapClientType } from '../../lib/constants/enums'; |
4 | 5 | import p2pErrors from '../../lib/p2p/errors'; |
5 | 6 | import Service from '../../lib/service/Service'; |
6 | 7 | import Xud from '../../lib/Xud'; |
7 | 8 | import { getTempDir } from '../utils'; |
| 9 | +import { ServiceOrderSidesArrays } from '../../lib/service/types'; |
8 | 10 |
|
9 | 11 | chai.use(chaiAsPromised); |
10 | 12 |
|
@@ -186,4 +188,63 @@ describe('API Service', () => { |
186 | 188 | }); |
187 | 189 | await expect(shutdownPromise).to.be.fulfilled; |
188 | 190 | }); |
| 191 | + |
| 192 | + describe('Max Quantity Calculation', () => { |
| 193 | + before(async () => { |
| 194 | + const map = new Map<string, ServiceOrderSidesArrays>(); |
| 195 | + map.set('BTC/DAI', { |
| 196 | + buyArray: [], |
| 197 | + sellArray: [ |
| 198 | + { quantity: 0.01, price: 20000, pairId: 'BTC/DAI', id: 'test_1', createdAt: 1, side: OrderSide.Sell, |
| 199 | + isOwnOrder: false, nodeIdentifier: { nodePubKey: 'some_key' } }, |
| 200 | + { quantity: 0.01, price: 50000, pairId: 'BTC/DAI', id: 'test_2', createdAt: 1, side: OrderSide.Sell, |
| 201 | + isOwnOrder: false, nodeIdentifier: { nodePubKey: 'some_key2' } }, |
| 202 | + { quantity: 0.05, price: 100000, pairId: 'BTC/DAI', id: 'test_2', createdAt: 1, side: OrderSide.Sell, |
| 203 | + isOwnOrder: false, nodeIdentifier: { nodePubKey: 'some_key2' } }, |
| 204 | + ], |
| 205 | + }); |
| 206 | + |
| 207 | + sinon.createSandbox().stub(service, 'listOrders').returns(map); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should return `0` for 0 balance mkt', async () => { |
| 211 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 0); |
| 212 | + await expect(number).to.equal(0); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should return `0.005` for 100 balance mkt', async () => { |
| 216 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 100); |
| 217 | + await expect(number).to.equal(0.005); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should return `0.01` for 200 balance mkt', async () => { |
| 221 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 200); |
| 222 | + await expect(number).to.equal(0.01); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should return `0.016` for 500 balance mkt', async () => { |
| 226 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 500); |
| 227 | + await expect(number).to.equal(0.016); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should return `0.02` for 700 balance mkt', async () => { |
| 231 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 700); |
| 232 | + await expect(number).to.equal(0.02); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should return `0.021` for 800 balance mkt', async () => { |
| 236 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 800); |
| 237 | + await expect(number).to.equal(0.021); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should return `0.07` for 5700 balance mkt', async () => { |
| 241 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 5700); |
| 242 | + await expect(number).to.equal(0.07); |
| 243 | + }); |
| 244 | + |
| 245 | + it('should return `0.07` for 10000 balance mkt', async () => { |
| 246 | + const number = service['calculateBuyMaxMarketQuantity']('BTC/DAI', 0, 10000); |
| 247 | + await expect(number).to.equal(0.07); |
| 248 | + }); |
| 249 | + }); |
189 | 250 | }); |
0 commit comments