这个机器人主要就是抢跑用的:机器人一次只能狙击一个令牌。
如何运行 1. 拷贝代码 2. $ npm 安装 3. 复制你的 env.example到 .env 4. 使用以下说明设置您的 .env:
WBNB_CONTRACT=0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
购买代币的 WBNB 合约
FACTORY=0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73
Pancake Factory 合约获得购买功能 ROUTER=0x10ED43C718714eb63d5aA57B78B54704E256024E
Pancake Factory 合约处理购买功能
YOUR_ADDRESS=
您来自 trustwallet 或其他钱包的 BSC (BEP20) 地址。
SLIPPAGE=5
在这里自定义您的滑点,不能小数。 (例如:1、5、10)。如果你买早期代币推荐 30+ Slippage
GWEI=5
在这里自定义您的 GWEI(gas 费),不能小数。 (例如:5、10、25)。如果买早期代币推荐15+ GWEI
GAS_LIMIT=345684
最低限度是 210000,越多越好。
MIN_LIQUIDITY_ADDED=3
设置您想购买的配对地址中添加多少最低流动性。设置在 BNB 中。 (例如:2、4、7)。 2 表示增加了 2 个 BNB 流动性。
YOUR_MNEMONIC=
在这里输入您的私钥,您可以从您的钱包隐私中获得。
AMOUNT_OF_WBNB=0.002
您想在WBNB中购买代币的金额。 TO_PURCHASE=0xe9e7cea3dedca5984780bafc599bd69add087d56
你想购买的代币地址。
下面就是具体的代码 :
import ethers from 'ethers';
import express from 'express';
import chalk from 'chalk';
import dotenv from 'dotenv';
import inquirer from 'inquirer';
const app = express();
dotenv.config();
const data = {
WBNB: process.env.WBNB_CONTRACT, //wbnb
to_PURCHASE: process.env.TO_PURCHASE, // token that you will purchase = BUSD for test '0xe9e7cea3dedca5984780bafc599bd69add087d56'
AMOUNT_OF_WBNB : process.env.AMOUNT_OF_WBNB, // how much you want to buy in WBNB
factory: process.env.FACTORY, //PancakeSwap V2 factory
router: process.env.ROUTER, //PancakeSwap V2 router
recipient: process.env.YOUR_ADDRESS, //your wallet address,
Slippage : process.env.SLIPPAGE, //in Percentage
gasPrice : ethers.utils.parseUnits(`${process.env.GWEI}`, 'gwei'), //in gwei
gasLimit : process.env.GAS_LIMIT, //at least 21000
minBnb : process.env.MIN_LIQUIDITY_ADDED //min liquidity added
}
let initialLiquidityDetected = false;
let jmlBnb = 0;
const bscMainnetUrl = 'https://bsc-dataseed1.defibit.io/' //https://bsc-dataseed1.defibit.io/ https://bsc-dataseed.binance.org/
const wss = 'wss://bsc-ws-node.nariox.org:443';
const mnemonic = process.env.YOUR_MNEMONIC //your memonic;
const tokenIn = data.WBNB;
const tokenOut = data.to_PURCHASE;
const provider = new ethers.providers.WebSocketProvider(wss);
const wallet = new ethers.Wallet(mnemonic);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
data.factory,
[
'event PairCreated(address indexed token0, address indexed token1, address pair, uint)',
'function getPair(address tokenA, address tokenB) external view returns (address pair)'
],
account
);
const router = new ethers.Contract(
data.router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
'function swapExactTokensForTokensSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
account
);
const erc = new ethers.Contract(
data.WBNB,
[{"constant": true,"inputs": [{"name": "_owner","type": "address"}],"name": "balanceOf","outputs": [{"name": "balance","type": "uint256"}],"payable": false,"type": "function"}],
account
);
const run = async () => {
await checkLiq();
}
let checkLiq = async() => {
const pairAddressx = await factory.getPair(tokenIn, tokenOut);
console.log(chalk.blue(`pairAddress: ${pairAddressx}`));
if (pairAddressx !== null && pairAddressx !== undefined) {
if (pairAddressx.toString().indexOf('0x0000000000000') > -1) {
console.log(chalk.cyan(`pairAddress ${pairAddressx} not detected. Auto restart`));
return await run();
}
}
const pairBNBvalue = await erc.balanceOf(pairAddressx);
jmlBnb = await ethers.utils.formatEther(pairBNBvalue);
console.log(`value BNB : ${jmlBnb}`);
if(jmlBnb > data.minBnb){
setTimeout(() => buyAction(), 5000);
}
else{
initialLiquidityDetected = false;
console.log(' run again...');
return await run();
}
}
let buyAction = async() => {
if(initialLiquidityDetected === true) {
console.log('not buy cause already buy');
return null;
}
console.log('ready to buy');
try{
initialLiquidityDetected = true;
let amountOutMin = 0.;
//We buy x amount of the new token for our wbnb
const amountIn = ethers.utils.parseUnits(`${data.AMOUNT_OF_WBNB}`, 'ether');
//if ( parseInt(data.Slippage) !== 0 ){
// const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
// amountOutMin = amounts[1].sub(amounts[1].div(`${data.Slippage}`));
// }
console.log(
chalk.green.inverse(`Start to buy \n`)
+
`Buying Token
=================
tokenIn: ${(amountIn * 1e-18).toString()} ${tokenIn} (BNB)
tokenOut: ${(amountOutMin / 1e-18).toString()} ${tokenOut}
`);
console.log('Processing Transaction.....');
console.log(chalk.yellow(`amountIn: ${(amountIn * 1e-18)} ${tokenIn} (BNB)`));
console.log(chalk.yellow(`amountOutMin: ${amountOutMin / 1e-18}`));
console.log(chalk.yellow(`tokenIn: ${tokenIn}`));
console.log(chalk.yellow(`tokenOut: ${tokenOut}`));
console.log(chalk.yellow(`data.recipient: ${data.recipient}`));
console.log(chalk.yellow(`data.gasLimit: ${data.gasLimit}`));
console.log(chalk.yellow(`data.gasPrice: ${data.gasPrice}`));
const tx = await router.swapExactTokensForTokensSupportingFeeOnTransferTokens( //uncomment this if you want to buy deflationary token
// const tx = await router.swapExactTokensForTokens( //uncomment here if you want to buy token
amountIn,
amountOutMin,
[tokenIn, tokenOut],
data.recipient,
Date.now() + 1000 * 60 * 5, //5 minutes
{
'gasLimit': data.gasLimit,
'gasPrice': data.gasPrice,
'nonce' : null //set you want buy at where position in blocks
});
const receipt = await tx.wait();
console.log(`Transaction receipt : https://www.bscscan.com/tx/${receipt.logs[1].transactionHash}`);
setTimeout(() => {process.exit()},4000);
}catch(err){
let error = JSON.parse(JSON.stringify(err));
console.log(`Error caused by :
{
reason : ${error.reason},
transactionHash : ${error.transactionHash}
message : Please check your BNB/WBNB balance, maybe its due because insufficient balance or approve your token manually on pancakeSwap
}`);
console.log(error);
inquirer.prompt([
{
type: 'confirm',
name: 'runAgain',
message: 'Do you want to run again thi bot?',
},
])
.then(answers => {
if(answers.runAgain === true){
console.log('= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =');
console.log('Run again');
console.log('= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =');
initialLiquidityDetected = false;
run();
}else{
process.exit();
}
});
}
}
run();
const PORT = 5001;
app.listen(PORT, console.log(chalk.yellow(`Listening for Liquidity Addition to token ${data.to_PURCHASE}`)));
主要执行交易的就是这个方法:swapExactTokensForTokensSupportingFeeOnTransferTokens
代码是测试过的,执行过程中有问题,可以留言与我沟通。