Skip to content

Commit

Permalink
bug: Fixed order not found issue (#218)
Browse files Browse the repository at this point in the history
* Fixed order not found issue
  • Loading branch information
chrisleekr authored Jun 5, 2021
1 parent 14fb2ba commit 4f97d53
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,65 @@ describe('ensure-manual-buy-order.js', () => {
});
});
});

describe('when binance.client.getOrder throws an error', () => {
beforeEach(async () => {
mockGetLastBuyPrice = jest.fn().mockResolvedValue({
lastBuyPrice: 30,
quantity: 3
});

cacheMock.hgetall = jest.fn().mockResolvedValue({
159653829: JSON.stringify({
symbol: 'CAKEUSDT',
orderId: 159653829,
origQty: '1.00000000',
executedQty: '1.00000000',
cummulativeQuoteQty: '19.54900000',
status: 'NEW',
type: 'LIMIT',
side: 'BUY',
nextCheck: moment()
.subtract(1, 'minute')
.format('YYYY-MM-DDTHH:mm:ssZ')
})
});

binanceMock.client.getOrder = jest
.fn()
.mockRejectedValue(new Error('Order is not found.'));

const step = require('../ensure-manual-buy-order');

rawData = {
symbol: 'CAKEUSDT',
isLocked: false,
symbolConfiguration: {
system: {
checkManualBuyOrderPeriod: 10
}
}
};

result = await step.execute(loggerMock, rawData);
});

it('does not trigger saveLastBuyPrice', () => {
expect(mockSaveLastBuyPrice).not.toHaveBeenCalled();
});

it('does not trigger cache.hdel', () => {
expect(cacheMock.hdel).not.toHaveBeenCalled();
});

it('triggers cache.hset', () => {
expect(cacheMock.hset).toHaveBeenCalledWith(
`trailing-trade-manual-buy-order-CAKEUSDT`,
159653829,
expect.any(String)
);
});
});
});
});
});
41 changes: 37 additions & 4 deletions app/cronjob/trailingTrade/step/ensure-manual-buy-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,43 @@ const execute = async (logger, rawData) => {

if (moment(nextCheck) < moment()) {
// Check orders whether it's filled or not
const orderResult = await binance.client.getOrder({
symbol,
orderId: buyOrder.orderId
});
let orderResult;
try {
orderResult = await binance.client.getOrder({
symbol,
orderId: buyOrder.orderId
});
} catch (e) {
logger.error(
{ e },
'The order could not be found or error occurred querying the order.'
);
const updatedNextCheck = moment().add(
checkManualBuyOrderPeriod,
'seconds'
);

logger.info(
{
e,
buyOrder,
checkManualBuyOrderPeriod,
nextCheck: updatedNextCheck
},
'The order could not be found or error occurred querying the order.'
);

await cache.hset(
`trailing-trade-manual-buy-order-${symbol}`,
buyOrder.orderId,
JSON.stringify({
...buyOrder,
nextCheck: updatedNextCheck
})
);

return data;
}

// If filled, then calculate average cost and quantity and save new last buy pirce.
if (orderResult.status === 'FILLED') {
Expand Down

0 comments on commit 4f97d53

Please sign in to comment.