@qso-soft/shared
Version:
Shared library for QSO-soft
85 lines • 3.33 kB
JavaScript
import { OKX_WL_ERROR } from '../../constants';
import { getClientByNetwork, getExpectedBalance, getRandomNumber, getTopUpOptions, sleep, transactionWorker, } from '../../helpers';
import { Okx } from '../../managers/okx';
export const executeOkxWithdraw = async (params) => {
const { wallet, okxWithdrawNetwork, tokenToWithdraw, minAndMaxAmount, minNativeBalance, logger, expectedBalance, amount, minAmount, } = params;
return makeOkxWithdraw({
wallet,
okxWithdrawNetwork,
tokenToWithdraw,
minAndMaxAmount,
minNativeBalance,
logger,
expectedBalance,
amount,
minAmount,
});
};
export const makeOkxWithdraw = async ({ okxWithdrawNetwork, wallet, expectedBalance, logger, minNativeBalance, minAndMaxAmount, tokenToWithdraw, amount, minAmount, withdrawSleep, hideExtraLogs = false, }) => {
const logTemplate = {
action: 'execWithdraw',
status: 'in progress',
};
const { currentExpectedBalance, isTopUpByExpectedBalance } = getExpectedBalance(expectedBalance);
const client = getClientByNetwork(okxWithdrawNetwork, wallet.privKey, logger);
const walletAddress = wallet.walletAddress;
const { currentAmount, shouldTopUp, isDone, successMessage, prevNativeBalance } = await getTopUpOptions({
client,
currentExpectedBalance,
isTopUpByExpectedBalance,
minNativeBalance,
minAndMaxAmount,
tokenToWithdraw,
amount,
network: okxWithdrawNetwork,
});
if (!currentAmount && typeof isDone === 'boolean' && isDone) {
return {
status: 'passed',
message: successMessage,
};
}
if (shouldTopUp) {
try {
const okx = new Okx({
logger,
amount: currentAmount,
});
await okx.execWithdraw({
walletAddress,
token: tokenToWithdraw,
network: okxWithdrawNetwork,
minAmount,
});
let currentNativeBalance = await client.getNativeBalance();
while (!(currentNativeBalance.int > prevNativeBalance)) {
const currentSleep = withdrawSleep ? getRandomNumber(withdrawSleep) : 20;
await sleep(currentSleep);
currentNativeBalance = await client.getNativeBalance();
if (!hideExtraLogs) {
logger.info('Tokens are still on the way to wallet...', logTemplate);
}
}
return {
status: 'success',
message: `Your wallet was successfully topped up from OKX. Current balance is [${currentNativeBalance.int} ${tokenToWithdraw}]`,
};
}
catch (err) {
const errorMessage = err.message;
if (errorMessage === OKX_WL_ERROR) {
throw new Error(OKX_WL_ERROR);
}
throw err;
}
}
return {
status: 'error',
};
};
export const execOkxWithdraw = async (params) => transactionWorker({
...params,
startLogMessage: `Execute make OKX withdraw of ${params.tokenToWithdraw} from ${params.okxWithdrawNetwork}...`,
transactionCallback: executeOkxWithdraw,
});
//# sourceMappingURL=make-okx-withdraw.js.map