@@ -4,39 +4,29 @@ import constants from '../Config/constants.js';
44const delay = ms => new Promise ( res => setTimeout ( res , ms ) ) ;
55
66const sellToken = async ( account , tokenContract , gasLimit , gasPrice , value = 99 ) => {
7- let attempts = 10 ; // max attempts
8- let slippage = constants . Slippage ; // starting slippage
7+ let attempts = 10 ; // max attempts
8+ let slippage = constants . Slippage ; // starting slippage
99
10- while ( attempts > 0 ) {
11- try {
12- const sellTokenContract = new constants . ethers . Contract (
13- tokenContract ,
14- constants . swapAbi ,
15- account
16- ) ;
17-
18- const contract = new constants . ethers . Contract ( constants . PANROUTERADDRESS , constants . abi , account ) ;
19- const accountAddress = account . address ;
20- const tokenBalance = await constants . erc20 ( account , tokenContract ) . balanceOf (
21- accountAddress
22- ) ;
23- let amountOutMin = 0 ;
24- const amountIn = tokenBalance . mul ( value ) . div ( 100 ) ;
25- const amounts = await constants . router ( account ) . getAmountsOut ( amountIn , [
26- tokenContract ,
27- constants . BNBCONTRACT
28- ] ) ;
10+ while ( attempts > 0 ) {
11+ try {
12+ const contract = new constants . ethers . Contract ( constants . PANROUTERADDRESS , constants . abi , account ) ;
13+ const accountAddress = account . address ;
14+ const tokenBalance = await constants . erc20 ( account , tokenContract ) . balanceOf (
15+ accountAddress
16+ ) ;
17+ let amountOutMin = 0 ;
18+ const amountIn = tokenBalance . mul ( value ) . div ( 100 ) ;
2919
30- // increment slippage by 2% for each attempt
31- slippage += 2 ;
20+ const sellTokenContract = new constants . ethers . Contract (
21+ tokenContract ,
22+ constants . swapAbi ,
23+ account
24+ ) ;
25+ const allowance = await sellTokenContract . allowance ( account . address , constants . PANROUTERADDRESS ) ;
3226
33- if ( parseInt ( slippage ) !== 0 ) {
34- amountOutMin = amounts [ 1 ] . sub ( amounts [ 1 ] . mul ( slippage . toString ( ) ) . div ( 100 ) ) ;
35- } else {
36- amountOutMin = amounts [ 1 ] ;
37- }
38-
39- const approve = await sellTokenContract . approve ( constants . PANROUTERADDRESS , amountIn ) ;
27+ // If allowance is less than double the amount we want to sell, approve double the spending
28+ if ( allowance . lt ( amountIn . mul ( 2 ) ) ) {
29+ const approve = await sellTokenContract . approve ( constants . PANROUTERADDRESS , amountIn . mul ( 2 ) ) ;
4030 const receipt_approve = await approve . wait ( ) ;
4131 if (
4232 receipt_approve &&
@@ -46,79 +36,92 @@ const sellToken = async (account, tokenContract, gasLimit, gasPrice, value = 99)
4636 const message = `INFO - Approved https://bscscan.com/tx/${ receipt_approve . transactionHash } ${ constants . chalk . green ( '✅' ) } ` ;
4737 console . log ( constants . chalk . green ( message ) ) ;
4838 constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
49- const swap_txn =
50- await contract . swapExactTokensForETHSupportingFeeOnTransferTokens (
51- amountIn ,
52- amountOutMin ,
53- [ tokenContract , constants . BNBCONTRACT ] ,
54- accountAddress ,
55- Date . now ( ) + 1000 * 60 * 10 ,
56- {
57- gasLimit : gasLimit ,
58- gasPrice : gasPrice ,
59- }
60- ) ;
61- const receipt = await swap_txn . wait ( ) ;
62- if ( receipt && receipt . blockNumber && receipt . status === 1 ) {
63- // 0 - failed, 1 - success
64- const message = `INFO - Transaction https://bscscan.com/tx/${ receipt . transactionHash } mined, status success ${ constants . chalk . green ( '✅' ) } ` ;
65- console . log ( constants . chalk . green ( message ) ) ;
66- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
67- // if the sell transaction is successful, break the loop
68- break ;
69- } else if ( receipt && receipt . blockNumber && receipt . status === 0 ) {
70- const message = `ERROR - Transaction https://bscscan.com/tx/${ receipt . transactionHash } mined, status failed ${ constants . chalk . red ( '❌' ) } ` ;
71- console . log ( constants . chalk . red ( message ) ) ;
72- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
73- } else {
74- const message = `WARNING - Transaction https://bscscan.com/tx/${ receipt . transactionHash } not mined ${ constants . chalk . yellow ( '⚠️' ) } ` ;
75- console . log ( constants . chalk . yellow ( message ) ) ;
76- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
77- }
7839 }
40+ }
7941
80- // decrement the value by 10 each time
81- value -= 10 ;
82- attempts -- ;
42+ const amounts = await constants . router ( account ) . getAmountsOut ( amountIn , [
43+ tokenContract ,
44+ constants . BNBCONTRACT
45+ ] ) ;
8346
84- // delay execution for 5 second (5000 milliseconds) between each sell attempt
85- await delay ( 5000 ) ;
86- } catch ( error ) {
87- let errorMessage = error . message ;
88- const errorReasonIndex = errorMessage . indexOf ( "(reason=" ) ;
89-
90- // Extract error reason if it exists
91- if ( errorReasonIndex !== - 1 ) {
92- errorMessage = errorMessage . substring ( errorReasonIndex + 9 , errorMessage . indexOf ( "," ) ) ;
93- errorMessage = `Error reason: ${ errorMessage } ` ;
94- }
95-
96- const message = `ERROR - Error while selling token: ${ errorMessage } ${ constants . chalk . red ( '❌' ) } ` ;
97- console . error ( constants . chalk . red ( message ) ) ;
98- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
99- attempts -- ;
100-
101- // delay execution for 5 second (5000 milliseconds) between each sell attempt
102- await delay ( 5000 ) ;
47+ // increment slippage by 2% for each attempt
48+ slippage += 2 ;
49+
50+ if ( parseInt ( slippage ) !== 0 ) {
51+ amountOutMin = amounts [ 1 ] . sub ( amounts [ 1 ] . mul ( slippage . toString ( ) ) . div ( 100 ) ) ;
52+ } else {
53+ amountOutMin = amounts [ 1 ] ;
10354 }
104-
105- } // closing brace for while loop
106- // If all attempts have been made and the token couldn't be sold, add to blacklist
107- if ( attempts === 0 ) {
108- constants . blacklist . push ( tokenContract ) ;
109- constants . fs . writeFile ( './Lists/blacklist.json' , JSON . stringify ( constants . blacklist ) , ( err ) => {
110- if ( err ) {
111- const message = `ERROR - Error while updating blacklist: ${ err } ${ constants . chalk . red ( '❌' ) } ` ;
112- console . error ( constants . chalk . red ( message ) ) ;
113- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
114- } else {
115- const message = `INFO - Token ${ tokenContract } added to blacklist after unsuccessful sell attempts ${ constants . chalk . green ( '✅' ) } ` ;
116- console . log ( constants . chalk . green ( message ) ) ;
117- constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
55+
56+ const swap_txn = await contract . swapExactTokensForETHSupportingFeeOnTransferTokens (
57+ amountIn ,
58+ amountOutMin ,
59+ [ tokenContract , constants . BNBCONTRACT ] ,
60+ accountAddress ,
61+ Date . now ( ) + 1000 * 60 * 10 ,
62+ {
63+ gasLimit : gasLimit ,
64+ gasPrice : gasPrice ,
11865 }
119- } ) ;
66+ ) ;
67+ const receipt = await swap_txn . wait ( ) ;
68+ if ( receipt && receipt . blockNumber && receipt . status === 1 ) {
69+ // 0 - failed, 1 - success
70+ const message = `INFO - Transaction https://bscscan.com/tx/${ receipt . transactionHash } mined, status success ${ constants . chalk . green ( '✅' ) } ` ;
71+ console . log ( constants . chalk . green ( message ) ) ;
72+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
73+ // if the sell transaction is successful, break the loop
74+ break ;
75+ } else if ( receipt && receipt . blockNumber && receipt . status === 0 ) {
76+ const message = `ERROR - Transaction https://bscscan.com/tx/${ receipt . transactionHash } mined, status failed ${ constants . chalk . red ( '❌' ) } ` ;
77+ console . log ( constants . chalk . red ( message ) ) ;
78+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
79+ } else {
80+ const message = `WARNING - Transaction https://bscscan.com/tx/${ receipt . transactionHash } not mined ${ constants . chalk . yellow ( '⚠️' ) } ` ;
81+ console . log ( constants . chalk . yellow ( message ) ) ;
82+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
83+ }
84+ // decrement the value by 10 each time
85+ value -= 10 ;
86+ attempts -- ;
87+
88+ // delay execution for 5 second (5000 milliseconds) between each sell attempt
89+ await delay ( 5000 ) ;
90+ } catch ( error ) {
91+ let errorMessage = error . message ;
92+ const errorReasonIndex = errorMessage . indexOf ( "(reason=" ) ;
93+
94+ // Extract error reason if it exists
95+ if ( errorReasonIndex !== - 1 ) {
96+ errorMessage = errorMessage . substring ( errorReasonIndex + 9 , errorMessage . indexOf ( "," ) ) ;
97+ errorMessage = `Error reason: ${ errorMessage } ` ;
98+ }
99+
100+ const message = `ERROR - Error while selling token: ${ errorMessage } ${ constants . chalk . red ( '❌' ) } ` ;
101+ console . error ( constants . chalk . red ( message ) ) ;
102+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
103+ attempts -- ;
104+
105+ // delay execution for 1 second (1000 milliseconds) between each sell attempt
106+ await delay ( 1000 ) ;
120107 }
108+
109+ } // closing brace for while loop
110+ // If all attempts have been made and the token couldn't be sold, add to blacklist
111+ if ( attempts === 0 ) {
112+ constants . blacklist . push ( tokenContract ) ;
113+ constants . fs . writeFile ( './Lists/blacklist.json' , JSON . stringify ( constants . blacklist ) , ( err ) => {
114+ if ( err ) {
115+ const message = `ERROR - Error while updating blacklist: ${ err } ${ constants . chalk . red ( '❌' ) } ` ;
116+ console . error ( constants . chalk . red ( message ) ) ;
117+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
118+ } else {
119+ const message = `INFO - Token ${ tokenContract } added to blacklist after unsuccessful sell attempts ${ constants . chalk . green ( '✅' ) } ` ;
120+ console . log ( constants . chalk . green ( message ) ) ;
121+ constants . fs . appendFileSync ( './error_logs/log.txt' , `${ new Date ( ) . toLocaleString ( ) } - ${ message } \n` ) ;
122+ }
123+ } ) ;
124+ }
121125}
122126
123127export default sellToken ;
124-
0 commit comments