forked from huyiya/pancake-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
99 lines (83 loc) · 3.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import 'dotenv/config'
import { Transaction } from 'web3-eth'
import { buyToken } from './src/helpers/exchangeToken'
import { getBUSDAddress, getPancakeRouterAddress, getWBNBAddress, getZeroAddress } from './src/utils/addressHelpers'
import { getBscscanUrl } from './src/utils/bscscan'
import decoder from './src/utils/decoder'
import { getLiquidity, getPair } from './src/utils/liquidity'
import { getBNBPath, getBUSDPath } from './src/utils/path'
import { getSymbolOfToken } from './src/utils/token'
import getWeb3 from './src/utils/web3'
import logger from './src/utils/logger'
const chainId = process.env.CHAIN_ID as string
const privateKey = process.env.PRIVATE_KEY as string
const targetToken = process.env.TARGET_TOKEN as string
const gasLimit = Number(process.env.GAS_LIMIT)
const amountBNB = Number(process.env.AMOUNT_BNB) || 0.01
const amountBUSD = Number(process.env.AMOUNT_BUSD) || 10
const liquidityInBNB = Boolean(process.env.LIQUIDITY_IN_BNB === 'true')
if (!privateKey || !targetToken) {
logger.error('Missing Private Key or Target Token')
process.exit(0)
}
const main = async () => {
const web3 = getWeb3()
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
const path = liquidityInBNB ? getBNBPath(targetToken) : getBUSDPath(targetToken)
const purchaseAmount = liquidityInBNB ? amountBNB : amountBUSD
const methodName = liquidityInBNB ? 'addLiquidityETH' : 'addLiquidity'
const liquidityToken = liquidityInBNB ? getWBNBAddress() : getBUSDAddress()
const liquidityTokenSymbol = liquidityInBNB ? 'BNB' : 'BUSD'
const targetTokenSymbol = await getSymbolOfToken(targetToken)
const lpPair = await getPair(targetToken, liquidityToken)
let liquidity: number
if (lpPair !== getZeroAddress()) {
liquidity = await getLiquidity(targetToken, liquidityToken)
}
web3.eth.subscribe('pendingTransactions')
.on('connected', async () => {
logger.info('Connected')
logger.info(`- Network: ${chainId}`)
logger.info(`- Buyer: ${account.address}`)
logger.info(`- Target Token: ${targetToken} - ${targetTokenSymbol}`)
logger.info(`- Liquidity in BNB: ${liquidityInBNB}`)
logger.info(`- LP Pair (${targetTokenSymbol}-${liquidityTokenSymbol}): ${lpPair}`)
if (liquidity > 0) {
logger.warn(`- Total Liquidity: ${liquidity.toFixed(3)} ${liquidityTokenSymbol}`)
}
logger.info(`- Purchase Amount: ${purchaseAmount} ${liquidityTokenSymbol}`)
})
.on('data', async (txHash: string) => {
const tx: Transaction = await web3.eth.getTransaction(txHash)
if (tx?.to?.toLowerCase() === getPancakeRouterAddress()) {
const gasPrice = Number(tx?.gasPrice)
const txInputDecoded = decoder(tx?.input)
// logger.info(`${txHash}: ${txInputDecoded?.name}`)
if (txInputDecoded?.name === methodName) {
// token2 exist if liquidity is not BNB
// if liquidity is BNB: token2 = amountTokenDesired
const [token, token2] = txInputDecoded.params
// TODO: optimize
const currentLiquidity = 0
// const currentLiquidity = liquidityInBNB
// ? await getLiquidity(token?.value as string, liquidityToken)
// : await getLiquidity(token?.value as string, token2?.value as string)
const checkTokenPair = liquidityInBNB
? path.includes(token?.value)
: path.includes(token?.value) && path.includes(token2?.value)
if (checkTokenPair && currentLiquidity === 0) {
logger.info(`- 🚀 Target Token was added: ${getBscscanUrl()}/tx/${tx.hash}`)
const result = await buyToken(account, path, gasPrice, gasLimit, purchaseAmount, liquidityInBNB)
result
? logger.info(`- 🥳 Buy success: ${getBscscanUrl()}/tx/${result}`)
: logger.error('- 🥺 Fail')
process.exit(0)
}
}
}
})
.on('error', err => {
logger.error(err)
})
}
main()