Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelSyntronic committed Mar 22, 2024
1 parent 11f0f12 commit e44c031
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
49 changes: 22 additions & 27 deletions client/Trader/SellToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,38 @@ async function sellAndConfirm(
let signature = ''
let txLanded = false
let sellError = ''
try {

let attempt = 1
while (!txLanded && attempt <= 6) {
console.log(`Buy attempt ${attempt}`)
let attempt = 1
while (!txLanded && attempt <= 20) {
console.log(`Sell attempt ${attempt}`)
try {
signature = await swapTokens(connection,
pool,
shitcoinAccountAddress,
mainTokenAccountAddress,
PAYER,
amountToSell)
txLanded = await confirmTransaction(connection, signature)
attempt += 1
}
} catch (e) {
console.error(`Failed to buy shitcoin with error ${e}. Retrying.`);
sellError = `${e}`
}

if (!txLanded) {
return { confirmedTxId: null, error: sellError }
}

let transactionConfirmed = false
let confirmationError = ''
try {
const transactionConfirmation = await retryAsyncFunction(getTransactionConfirmation, [connection, signature], 5, 300)
if (transactionConfirmation.err) {
confirmationError = `${transactionConfirmation.err}`
} else {
transactionConfirmed = true
} catch (e) {
console.error(`Failed to sell shitcoin with error ${e}. Retrying.`);
sellError = JSON.stringify(e)
}
} catch (e) {
confirmationError = `${e}`
attempt += 1
}

return { confirmedTxId: transactionConfirmed ? signature : null, error: confirmationError === '' ? null : confirmationError }
// let transactionConfirmed = false
// let confirmationError = ''
// try {
// const transactionConfirmation = await retryAsyncFunction(getTransactionConfirmation, [connection, signature], 5, 300)
// if (transactionConfirmation.err) {
// confirmationError = `${transactionConfirmation.err}`
// } else {
// transactionConfirmed = true
// }
// } catch (e) {
// confirmationError = `${e}`
// }

return { confirmedTxId: txLanded ? signature : null, error: txLanded ? null : sellError }
}

interface SPLTransferInfo {
Expand Down
14 changes: 13 additions & 1 deletion client/TurboBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class TurboBot {
}
}

private async updateRealWsolBalance() {
const newWalletBalance = (await this.connection.getTokenAccountBalance(SOL_SPL_TOKEN_ADDRESS)).value.uiAmount ?? 0
const totalProfit = (newWalletBalance - this.tradingWallet.startValue) / this.tradingWallet.startValue
this.tradingWallet = { ...this.tradingWallet, current: newWalletBalance, totalProfit }
console.log(`Wallet:\n${JSON.stringify(this.tradingWallet, null, 2)}`)
}

private async fetchInitialWalletSOLBalance() {
if (config.simulateOnly) { return }
console.log(`Fetching wallet balance`)
Expand Down Expand Up @@ -134,7 +141,12 @@ export class TurboBot {
} else {
console.log(`Couldn't sell`)
}
this.updateWSOLBalance(tradeResults)
if (config.simulateOnly) {
this.updateWSOLBalance(tradeResults)
} else {
await this.updateRealWsolBalance()
}

if (singleTrade) {
this.connection.removeOnLogsListener(this.onLogsSubscriptionId ?? 0)
this.onLogsSubscriptionId = null
Expand Down
4 changes: 2 additions & 2 deletions client/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export async function confirmTransaction(connection: Connection, txid: string):
try {
const confirmResult = await Promise.race([
getTransactionConfirmation(connection, txid),
timeout(10 * 1000)
timeout(20 * 1000)
])
const transactionFailed = confirmResult.err !== null;
if (transactionFailed) {
console.log(`Buying transaction ${chalk.bold(txid)} ${chalk.red('FAILED')}. Error: ${chalk.redBright(confirmResult.err)}`);
console.log(`Buying transaction ${chalk.bold(txid)} ${chalk.red('FAILED')}. Error: ${chalk.redBright(JSON.stringify(confirmResult.err))}`);
return false;
}
return true;
Expand Down

0 comments on commit e44c031

Please sign in to comment.