From e2c128feff8814d5ffd600a62077cb04a5d9e29d Mon Sep 17 00:00:00 2001 From: Riordan Panayides Date: Thu, 2 Dec 2021 18:13:19 +0000 Subject: [PATCH] Shuffle accounts array on load --- src/liquidator.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/liquidator.ts b/src/liquidator.ts index d3e5d1f..0322901 100644 --- a/src/liquidator.ts +++ b/src/liquidator.ts @@ -383,11 +383,14 @@ async function refreshAccounts( try { console.log('Refreshing accounts...'); console.time('getAllMangoAccounts'); - mangoAccounts.splice(0, mangoAccounts.length, ...(await client.getAllMangoAccounts( - mangoGroup, - undefined, - true, - ))); + + mangoAccounts.splice( + 0, + mangoAccounts.length, + ...(await client.getAllMangoAccounts(mangoGroup, undefined, true)), + ); + shuffleArray(mangoAccounts); + console.timeEnd('getAllMangoAccounts'); console.log(`Fetched ${mangoAccounts.length} accounts`); } catch (err) { @@ -1166,6 +1169,13 @@ async function closePositions( } } +function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } +} + function notify(content: string) { if (content && process.env.WEBHOOK_URL) { try {