Skip to content

Commit 13b5184

Browse files
committed
Optimise BNB free checks
1 parent 519ac5a commit 13b5184

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

src/trader/apis/binance.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export async function fetchBalance(type: WalletType): Promise<ccxt.Balances> {
6868
// Use the cache
6969
logger.debug(`Using cached ${type} balances.`)
7070
return balances[type]
71+
} else {
72+
// Clear the old cached balances so that it doesn't get used below
73+
delete balances[type]
74+
delete balanceTimestamps[type]
7175
}
7276
}
7377

@@ -84,6 +88,12 @@ export async function fetchBalance(type: WalletType): Promise<ccxt.Balances> {
8488
await new Promise( resolve => setTimeout(resolve, delay) )
8589
}
8690

91+
// Other requests may have fetched the balances while we were waiting, so check the cache again
92+
if (balances.hasOwnProperty(type)) {
93+
logger.debug(`Using newly cached ${type} balances.`)
94+
return balances[type]
95+
}
96+
8797
return binanceClient.fetchBalance({type: type})
8898
.then((value) => {
8999
logger.silly(`Fetched balance: ${JSON.stringify(value)}`)

src/trader/trader.ts

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,12 @@ export async function executeTradingTask(
14361436
logger.silly("executeTradingTask->notifyAll: " + reason)
14371437
})
14381438

1439-
// Check that you still have enough BNB in each wallet
1440-
checkBNBThreshold().catch((reason) => {
1441-
logger.silly("executeTradingTask->checkBNBThreshold: " + reason)
1442-
})
1439+
if (tradeOpen.tradingType == TradingType.real) {
1440+
// Check that you still have enough BNB in this wallet
1441+
checkBNBThreshold(tradeOpen.wallet!).catch((reason) => {
1442+
logger.silly("executeTradingTask->checkBNBThreshold: " + reason)
1443+
})
1444+
}
14431445
}
14441446

14451447
// Notify NBT Hub that the trade has been executed
@@ -2363,43 +2365,38 @@ async function refreshMarkets() {
23632365
// Loads the balances from Binance and checks whether you still have sufficient BNB funds to cover fees and interest
23642366
// This also helps to pre-cache the balances ready for the next trade
23652367
const BNBState: Dictionary<string> = {}
2366-
async function checkBNBThreshold() {
2368+
async function checkBNBThreshold(wallet: WalletType) {
23672369
if (env().BNB_FREE_THRESHOLD >= 0) {
2368-
for (let wallet of Object.values(WalletType)) {
2369-
// Skip if margin trading is not in use
2370-
if (wallet == WalletType.MARGIN && !env().IS_TRADE_MARGIN_ENABLED) continue
2371-
2372-
// Initialise dictionary, assuming it is ok to start with
2373-
if (!(wallet in Object.keys(BNBState))) BNBState[wallet] = "ok"
2374-
2375-
// Fetch the BNB balance for this wallet
2376-
const balance = (await fetchBalance(wallet))["BNB"]
2377-
logger.debug(`${balance.free} BNB free in ${wallet}.`)
2378-
if (balance.free <= env().BNB_FREE_THRESHOLD) {
2379-
// Check if the low balance hasn't already been reported
2380-
if (BNBState[wallet] == "ok" || (BNBState[wallet] == "low" && balance.free <= 0)) {
2381-
// Log the low balance warning or error for empty balance
2382-
let notifyLevel = MessageType.WARN
2383-
let logMessage = `Your ${wallet} wallet only has ${balance.free} BNB free. You may need to top it up.`
2384-
if (balance.free <= 0) {
2385-
BNBState[wallet] = "empty"
2386-
notifyLevel = MessageType.ERROR
2387-
logMessage = `Your ${wallet} wallet has no free BNB. You will need to top it up now.`
2388-
logger.error(logMessage)
2389-
} else {
2390-
BNBState[wallet] = "low"
2391-
logger.warn(logMessage)
2392-
}
2393-
2394-
// Send as a notification
2395-
notifyAll({subject: notifyLevel, content: logMessage}).catch((reason) => {
2396-
logger.silly("checkBNBThreshold->notifyAll: " + reason)
2397-
})
2370+
// Initialise dictionary, assuming it is ok to start with
2371+
if (!(wallet in Object.keys(BNBState))) BNBState[wallet] = "ok"
2372+
2373+
// Fetch the BNB balance for this wallet
2374+
const balance = (await fetchBalance(wallet))["BNB"]
2375+
logger.debug(`${balance.free} BNB free in ${wallet}.`)
2376+
if (balance.free <= env().BNB_FREE_THRESHOLD) {
2377+
// Check if the low balance hasn't already been reported
2378+
if (BNBState[wallet] == "ok" || (BNBState[wallet] == "low" && balance.free <= 0)) {
2379+
// Log the low balance warning or error for empty balance
2380+
let notifyLevel = MessageType.WARN
2381+
let logMessage = `Your ${wallet} wallet only has ${balance.free} BNB free. You may need to top it up.`
2382+
if (balance.free <= 0) {
2383+
BNBState[wallet] = "empty"
2384+
notifyLevel = MessageType.ERROR
2385+
logMessage = `Your ${wallet} wallet has no free BNB. You will need to top it up now.`
2386+
logger.error(logMessage)
2387+
} else {
2388+
BNBState[wallet] = "low"
2389+
logger.warn(logMessage)
23982390
}
2399-
} else {
2400-
// Reset once the balance has exceeded the threshold again
2401-
BNBState[wallet] = "ok"
2391+
2392+
// Send as a notification
2393+
notifyAll({subject: notifyLevel, content: logMessage}).catch((reason) => {
2394+
logger.silly("checkBNBThreshold->notifyAll: " + reason)
2395+
})
24022396
}
2397+
} else {
2398+
// Reset once the balance has exceeded the threshold again
2399+
BNBState[wallet] = "ok"
24032400
}
24042401
}
24052402
}

0 commit comments

Comments
 (0)