Skip to content

Commit 7289bff

Browse files
committed
Added safety check for partial funded fees
1 parent fa37e6c commit 7289bff

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

src/trader/trader.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,14 +1262,31 @@ async function executeTradeAction(
12621262
if (result.status == "closed") {
12631263
// If you do not have enough BNB to cover fees you may not receive the full amount
12641264
// Only need to check on buy trades as the wallet buffer will cover sell shortfalls
1265-
if (action == ActionType.BUY && result.fee.currency != "BNB") {
1265+
if (action == ActionType.BUY) {
1266+
var logMessage = ""
12661267
const market = tradingMetaData.markets[tradeOpen.symbol]
1267-
var logMessage = `${getLogName(tradeOpen)} trade used ${result.fee.cost} ${result.fee.currency} to pay fees instead of BNB.`
1268+
var feeQty = new BigNumber(0)
12681269

1269-
// Check if the fee was paid from the base quantity
1270-
if (result.fee.currency == market.base) {
1270+
if (result.fee) {
1271+
if (result.fee.currency != "BNB") {
1272+
logMessage = `${getLogName(tradeOpen)} trade used ${result.fee.cost} ${result.fee.currency} to pay fees instead of BNB.`
1273+
1274+
// Check if the fee was paid from the base asset
1275+
if (result.fee.currency == market.base) {
1276+
feeQty = new BigNumber(result.fee.cost)
1277+
}
1278+
}
1279+
} else {
1280+
// This usually occurs when the fees were paid with some BNB and some of the base asset
1281+
// Have to assume the worst and calculate the whole fee in the base asset
1282+
feeQty = tradeOpen.quantity.multipliedBy(env().TAKER_FEE_PERCENT / 100)
1283+
1284+
logMessage = `${getLogName(tradeOpen)} trade result did not contain fee information, it probably used some ${market.base} for fees. Trade size will be reduced by ${feeCost.toFixed()} ${market.base} just in case.`
1285+
}
1286+
1287+
if (feeQty.isGreaterThan(0)) {
12711288
// Calculate the new quantity by deducting the fee
1272-
const qty = tradeOpen.quantity.minus(result.fee.cost)
1289+
const qty = tradeOpen.quantity.minus(feeQty)
12731290

12741291
if (tradeOpen.positionType == PositionType.LONG) {
12751292
// Deducted fees will likely result in an illegal quantity, so recalculate
@@ -1290,13 +1307,16 @@ async function executeTradeAction(
12901307
}
12911308
}
12921309
}
1293-
logMessage += ` Check that you have enough BNB to cover trading fees.`
1294-
logger.error(logMessage)
12951310

1296-
// Send notifications that fees were not paid in BNB
1297-
notifyAll(getNotifierMessage(MessageType.ERROR, undefined, signal, undefined, logMessage)).catch((reason) => {
1298-
logger.silly("executeTradeAction->notifyAll: " + reason)
1299-
})
1311+
if (logMessage) {
1312+
logMessage += ` Check that you have enough BNB to cover trading fees.`
1313+
logger.error(logMessage)
1314+
1315+
// Send notifications that fees were not paid in BNB
1316+
notifyAll(getNotifierMessage(MessageType.ERROR, undefined, signal, undefined, logMessage)).catch((reason) => {
1317+
logger.silly("executeTradeAction->notifyAll: " + reason)
1318+
})
1319+
}
13001320
}
13011321

13021322
// Check if the price and cost is different than we expected (it usually is)

0 commit comments

Comments
 (0)