Skip to content

Commit

Permalink
fix: account balance calculation (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
habibalkhabbaz authored Sep 27, 2022
1 parent f4bd6f6 commit bb79bad
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 383 deletions.
12 changes: 6 additions & 6 deletions app/cronjob/trailingTrade/step/__tests__/cancel-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('cancel-order.js', () => {

let mockGetAPILimit;
let mockGetAndCacheOpenOrdersForSymbol;
let mockGetAccountInfo;
let mockGetAccountInfoFromAPI;

let mockDeleteManualOrder;

Expand All @@ -29,7 +29,7 @@ describe('cancel-order.js', () => {
binanceMock.client.cancelOrder = jest.fn().mockResolvedValue(true);
mockGetAPILimit = jest.fn().mockReturnValue(10);
mockGetAndCacheOpenOrdersForSymbol = jest.fn().mockResolvedValue([]);
mockGetAccountInfo = jest.fn().mockResolvedValue({
mockGetAccountInfoFromAPI = jest.fn().mockResolvedValue({
account: 'info'
});
PubSubMock.publish = jest.fn().mockResolvedValue(true);
Expand All @@ -46,7 +46,7 @@ describe('cancel-order.js', () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
getAPILimit: mockGetAPILimit,
getAndCacheOpenOrdersForSymbol: mockGetAndCacheOpenOrdersForSymbol,
getAccountInfo: mockGetAccountInfo
getAccountInfoFromAPI: mockGetAccountInfoFromAPI
}));

const step = require('../cancel-order');
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('cancel-order.js', () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
getAPILimit: mockGetAPILimit,
getAndCacheOpenOrdersForSymbol: mockGetAndCacheOpenOrdersForSymbol,
getAccountInfo: mockGetAccountInfo
getAccountInfoFromAPI: mockGetAccountInfoFromAPI
}));

const step = require('../cancel-order');
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('cancel-order.js', () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
getAPILimit: mockGetAPILimit,
getAndCacheOpenOrdersForSymbol: mockGetAndCacheOpenOrdersForSymbol,
getAccountInfo: mockGetAccountInfo
getAccountInfoFromAPI: mockGetAccountInfoFromAPI
}));

const step = require('../cancel-order');
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('cancel-order.js', () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
getAPILimit: mockGetAPILimit,
getAndCacheOpenOrdersForSymbol: mockGetAndCacheOpenOrdersForSymbol,
getAccountInfo: mockGetAccountInfo
getAccountInfoFromAPI: mockGetAccountInfoFromAPI
}));

const step = require('../cancel-order');
Expand Down
74 changes: 22 additions & 52 deletions app/cronjob/trailingTrade/step/__tests__/handle-open-orders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('handle-open-orders.js', () => {

let mockCancelOrder;
let mockRefreshOpenOrdersAndAccountInfo;
let mockUpdateAccountInfo;
let mockGetAccountInfoFromAPI;
let mockSaveOverrideAction;

let mockIsExceedingMaxOpenTrades;
Expand All @@ -34,7 +34,7 @@ describe('handle-open-orders.js', () => {
mockCancelOrder = jest.fn().mockResolvedValue(true);

mockSaveOverrideAction = jest.fn().mockResolvedValue(true);
mockUpdateAccountInfo = jest.fn().mockResolvedValue({
mockGetAccountInfoFromAPI = jest.fn().mockResolvedValue({
account: 'updated'
});

Expand All @@ -44,7 +44,7 @@ describe('handle-open-orders.js', () => {
describe('when symbol is locked', () => {
beforeEach(async () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('handle-open-orders.js', () => {
describe('when action is not not-determined', () => {
beforeEach(async () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('handle-open-orders.js', () => {
describe('when order is not STOP_LOSS_LIMIT', () => {
beforeEach(async () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -316,13 +316,13 @@ describe('handle-open-orders.js', () => {

describe('when order is buy', () => {
describe('when open trade limit is reached', () => {
describe('when cancelling order is failedd', () => {
describe('when cancelling order is failed', () => {
beforeEach(async () => {
mockIsExceedingMaxOpenTrades = jest.fn().mockResolvedValue(true);
mockCancelOrder = jest.fn().mockResolvedValue(false);

jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -409,13 +409,13 @@ describe('handle-open-orders.js', () => {
});
});

describe('when cancelling order is succeedd', () => {
describe('when cancelling order is succeeded', () => {
beforeEach(async () => {
mockIsExceedingMaxOpenTrades = jest.fn().mockResolvedValue(true);
mockCancelOrder = jest.fn().mockResolvedValue(true);

jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -478,18 +478,8 @@ describe('handle-open-orders.js', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).not.toHaveBeenCalled();
});

it('triggers updateAccountInfo', () => {
expect(mockUpdateAccountInfo).toHaveBeenCalledWith(
loggerMock,
[
{
asset: 'USDT',
free: 50.0179958,
locked: 0
}
],
expect.any(String)
);
it('triggers getAccountInfoFromAPI', () => {
expect(mockGetAccountInfoFromAPI).toHaveBeenCalledWith(loggerMock);
});

it('returns expected value', () => {
Expand Down Expand Up @@ -536,7 +526,7 @@ describe('handle-open-orders.js', () => {
mockCancelOrder = jest.fn().mockResolvedValue(false);

jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -629,8 +619,8 @@ describe('handle-open-orders.js', () => {
);
});

it('does not trigger updateAccountInfo', () => {
expect(mockUpdateAccountInfo).not.toHaveBeenCalled();
it('does not trigger getAccountInfoFromAPI', () => {
expect(mockGetAccountInfoFromAPI).not.toHaveBeenCalled();
});

it('returns expected value', () => {
Expand Down Expand Up @@ -668,7 +658,7 @@ describe('handle-open-orders.js', () => {
describe('when cancelling order is succeed', () => {
beforeEach(async () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -743,18 +733,8 @@ describe('handle-open-orders.js', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).not.toHaveBeenCalled();
});

it('triggers updateAccountInfo', () => {
expect(mockUpdateAccountInfo).toHaveBeenCalledWith(
loggerMock,
[
{
asset: 'USDT',
free: 50.0179958,
locked: 0
}
],
expect.any(String)
);
it('triggers getAccountInfoFromAPI', () => {
expect(mockGetAccountInfoFromAPI).toHaveBeenCalledWith(loggerMock);
});

it('returns expected value', () => {
Expand Down Expand Up @@ -795,7 +775,7 @@ describe('handle-open-orders.js', () => {
mockIsExceedingMaxOpenTrades = jest.fn().mockResolvedValue(false);

jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -897,7 +877,7 @@ describe('handle-open-orders.js', () => {
mockCancelOrder = jest.fn().mockResolvedValue(false);

jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -1009,7 +989,7 @@ describe('handle-open-orders.js', () => {
describe('when cancel order is succeed', () => {
beforeEach(async () => {
jest.mock('../../../trailingTradeHelper/common', () => ({
updateAccountInfo: mockUpdateAccountInfo,
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
saveOverrideAction: mockSaveOverrideAction,
isExceedingMaxOpenTrades: mockIsExceedingMaxOpenTrades,
cancelOrder: mockCancelOrder,
Expand Down Expand Up @@ -1085,18 +1065,8 @@ describe('handle-open-orders.js', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).not.toHaveBeenCalled();
});

it('triggers updateAccountInfo', () => {
expect(mockUpdateAccountInfo).toHaveBeenCalledWith(
loggerMock,
[
{
asset: 'BTC',
free: 0.00001,
locked: 0
}
],
expect.any(String)
);
it('triggers getAccountInfoFromAPI', () => {
expect(mockGetAccountInfoFromAPI).toHaveBeenCalledWith(loggerMock);
});

it('returns expected value', () => {
Expand Down
Loading

0 comments on commit bb79bad

Please sign in to comment.