jklepatch / eattheblocks

Source code for Eat The Blocks, a screencast for Ethereum Dapp Developers

Home Page:https://eattheblocks.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Here is a working copy of the Pancakeswap bot

tuxforensics opened this issue · comments

put your wallet address in
put your private key in
put your wss provider in

add bnb and wbnb to your wallet and have fun buying all of the shit coins every 3 secs…

-Mike

const ethers = require(‘ethers’);

const addresses = {
WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’,
BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’,
factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’,
router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’,
recipient: ‘’
}

//First address of this mnemonic must have enough BNB to pay for tx fess

const privateKey = ‘’;

const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’);

const provider = new ethers.providers.WebSocketProvider(‘wss://muddy-young-frost.bsc.quiknode.pro/’);
const wallet = new ethers.Wallet(privateKey);
const account = wallet.connect(provider);

const factory = new ethers.Contract(
addresses.factory,
[‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’],
account
);
const router = new ethers.Contract(
addresses.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)’
],
account
);

const wbnb = new ethers.Contract(
addresses.WBNB,
[
‘function approve(address spender, uint amount) public returns(bool)’,
],
account
);

console.log(Before Approve);
const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’);
const init = async () => {
const tx = await wbnb.approve(
router.address,
valueToapprove,
{
gasPrice: mygasPrice,
gasLimit: ‘162445’
}
);
console.log(After Approve);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
}

factory.on(‘PairCreated’, async (token0, token1, pairAddress) => {
console.log(after factory.on:);
console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} );

//The quote currency needs to be WBNB (we will pay with WBNB)
let tokenIn, tokenOut;
if(token0 === addresses.WBNB) {
tokenIn = token0;
tokenOut = token1;
}

if(token1 == addresses.WBNB) {
tokenIn = token1;
tokenOut = token0;
}

//The quote currency is not WBNB
if(typeof tokenIn === ‘undefined’) {
return;
}

//We buy for 0.1 BNB of the new token
//ethers was originally created for Ethereum, both also work for BSC
//‘ether’ === ‘bnb’ on BSC
console.log(line 87);
const amountIn = ethers.utils.parseUnits(‘0.01’, ‘ether’);
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));

console.log(line 92);
console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} );
console.log(line 101);

const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
{
gasPrice: mygasPrice,
gasLimit: 162445
}
);
console.log(line 117);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
});

init();

Hi is it possible to somehow snipe specific projects instead of every project out there?

It is not working. Some console log messages are wrong in syntax, and there are some promise errors too...

hi @tuxforensics whenever i run node bot.js, i get this error

const ethers = require(‘ethers’);

SyntaxError: Invalid or unexpected token
←[90m at wrapSafe (node:internal/modules/cjs/loader:1024:16)←[39m
←[90m at Module._compile (node:internal/modules/cjs/loader:1072:27)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:973:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:813:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m

ıt not working ;

const ethers = require(‘ethers’);
SyntaxError: Invalid or unexpected token

hi @tuxforensics whenever i run node bot.js, i get this error

const ethers = require(‘ethers’);

SyntaxError: Invalid or unexpected token
←[90m at wrapSafe (node:internal/modules/cjs/loader:1024:16)←[39m
←[90m at Module._compile (node:internal/modules/cjs/loader:1072:27)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:973:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:813:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m

You have to install 'ethers' package. Reference here: https://www.npmjs.com/package/ethers

ıt not working ;

const ethers = require(‘ethers’);
SyntaxError: Invalid or unexpected token

You have to install 'ethers' package. Reference here: https://www.npmjs.com/package/ethers

I've found some errors in the code, you forgot the '' in all console.log but I have a problem with one of your const
const privateKey = '';
Should this not be the mnemonic phrase ?

yess
this must be privatekey, also orginal is mnemonic phrase
const privateKey = '';

yess
this must be privatekey, also orginal is mnemonic phrase
const privateKey = '';

Yes finally it's what I've done but still getting an error, the same in that one and the original one.
It comes from Ankr url.
Any good advice what to use for the websocket ?

I switched to a paid service at https://www.quicknode.com/

On Sun, May 23, 2021 at 12:46 PM Stéphane METZ @.***> wrote: yess this must be privatekey, also orginal is mnemonic phrase const privateKey = ''; Yes finally it's what I've done but still getting an error, the same in that one and the original one. It comes from Ankr url. Any good advice what to use for the websocket ? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#67 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52ODHWATK45WQEKKIQMLTPE5RBANCNFSM45HR6ZHA .

Ok I'll give it a try as they have a week trial, thanks

I switched to a paid service at https://www.quicknode.com/

On Sun, May 23, 2021 at 12:46 PM Stéphane METZ @.***> wrote: yess this must be privatekey, also orginal is mnemonic phrase const privateKey = ''; Yes finally it's what I've done but still getting an error, the same in that one and the original one. It comes from Ankr url. Any good advice what to use for the websocket ? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#67 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52ODHWATK45WQEKKIQMLTPE5RBANCNFSM45HR6ZHA .

Ok I'll give it a try as they have a week trial, thanks

Finally I tried it already but unfortunately the don't have the testnet.
Any idea where to look ?

Guys: Ankr is perfectly working. WHen you create the API endpoint, it is asking you either to have a classic or tokenized authentication. Choose tokenized, and you will have no problem with yous ANkr wss endpoint...

Guys: Ankr is perfectly working. WHen you create the API endpoint, it is asking you either to have a classic or tokenized authentication. Choose tokenized, and you will have no problem with yous ANkr wss endpoint...

Ankr isn't returning the proper cost from pancake but quicknode just works.

Guys: Ankr is perfectly working. WHen you create the API endpoint, it is asking you either to have a classic or tokenized authentication. Choose tokenized, and you will have no problem with yous ANkr wss endpoint...

It's what I have done, did you tested on mainnet or testnet ?
Anyway actually wss is dead

put your wallet address in

put your private key in

put your wss provider in

add bnb and wbnb to your wallet and have fun buying all of the shit coins every 3 secs…

-Mike

const ethers = require(‘ethers’);

const addresses = {

WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’,

BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’,

factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’,

router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’,

recipient: ‘’

}

//First address of this mnemonic must have enough BNB to pay for tx fess

const privateKey = ‘’;

const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’);

const provider = new ethers.providers.WebSocketProvider(‘wss://muddy-young-frost.bsc.quiknode.pro/’);

const wallet = new ethers.Wallet(privateKey);

const account = wallet.connect(provider);

const factory = new ethers.Contract(

addresses.factory,

[‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’],

account

);

const router = new ethers.Contract(

addresses.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)’

],

account

);

const wbnb = new ethers.Contract(

addresses.WBNB,

[

‘function approve(address spender, uint amount) public returns(bool)’,

],

account

);

console.log(Before Approve);

const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’);

const init = async () => {

const tx = await wbnb.approve(

router.address,

valueToapprove,

{

gasPrice: mygasPrice,

gasLimit: ‘162445’

}

);

console.log(After Approve);

const receipt = await tx.wait();

console.log(‘Transaction receipt’);

console.log(receipt);

}

factory.on(‘PairCreated’, async (token0, token1, pairAddress) => {

console.log(after factory.on:);

console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} );

//The quote currency needs to be WBNB (we will pay with WBNB)

let tokenIn, tokenOut;

if(token0 === addresses.WBNB) {

tokenIn = token0;

tokenOut = token1;

}

if(token1 == addresses.WBNB) {

tokenIn = token1;

tokenOut = token0;

}

//The quote currency is not WBNB

if(typeof tokenIn === ‘undefined’) {

return;

}

//We buy for 0.1 BNB of the new token

//ethers was originally created for Ethereum, both also work for BSC

//‘ether’ === ‘bnb’ on BSC

console.log(line 87);

const amountIn = ethers.utils.parseUnits(‘0.01’, ‘ether’);

const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);

//Our execution price will be a bit different, we need some flexbility

const amountOutMin = amounts[1].sub(amounts[1].div(10));

console.log(line 92);

console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} );

console.log(line 101);

const tx = await router.swapExactTokensForTokens(

amountIn,

amountOutMin,

[tokenIn, tokenOut],

addresses.recipient,

Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time

{

gasPrice: mygasPrice,

gasLimit: 162445

}

);

console.log(line 117);

const receipt = await tx.wait();

console.log(‘Transaction receipt’);

console.log(receipt);

});

init();

put your wallet address in
put your private key in
put your wss provider in
add bnb and wbnb to your wallet and have fun buying all of the shit coins every 3 secs…
-Mike
const ethers = require(‘ethers’);
const addresses = {
WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’,
BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’,
factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’,
router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’,
recipient: ‘’
}
//First address of this mnemonic must have enough BNB to pay for tx fess
const privateKey = ‘’;
const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’);
const provider = new ethers.providers.WebSocketProvider(‘wss://muddy-young-frost.bsc.quiknode.pro/’);
const wallet = new ethers.Wallet(privateKey);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
addresses.factory,
[‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’],
account
);
const router = new ethers.Contract(
addresses.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)’
],
account
);
const wbnb = new ethers.Contract(
addresses.WBNB,
[
‘function approve(address spender, uint amount) public returns(bool)’,
],
account
);
console.log(Before Approve);
const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’);
const init = async () => {
const tx = await wbnb.approve(
router.address,
valueToapprove,
{
gasPrice: mygasPrice,
gasLimit: ‘162445’
}
);
console.log(After Approve);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
}
factory.on(‘PairCreated’, async (token0, token1, pairAddress) => {
console.log(after factory.on:);
console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} );
//The quote currency needs to be WBNB (we will pay with WBNB)
let tokenIn, tokenOut;
if(token0 === addresses.WBNB) {
tokenIn = token0;
tokenOut = token1;
}
if(token1 == addresses.WBNB) {
tokenIn = token1;
tokenOut = token0;
}
//The quote currency is not WBNB
if(typeof tokenIn === ‘undefined’) {
return;
}
//We buy for 0.1 BNB of the new token
//ethers was originally created for Ethereum, both also work for BSC
//‘ether’ === ‘bnb’ on BSC
console.log(line 87);
const amountIn = ethers.utils.parseUnits(‘0.01’, ‘ether’);
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));
console.log(line 92);
console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} );
console.log(line 101);
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
{
gasPrice: mygasPrice,
gasLimit: 162445
}
);
console.log(line 117);
const receipt = await tx.wait();
console.log(‘Transaction receipt’);
console.log(receipt);
});
init();

But : I have this error :

[‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’],

SyntaxError: Invalid or unexpected token
←[90m at wrapSafe (internal/modules/cjs/loader.js:979:16)←[39m
←[90m at Module._compile (internal/modules/cjs/loader.js:1027:27)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:769:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m at internal/main/run_main_module.js:17:47←[39m

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

gas limit is the maximum gas you ready to pay. It's not mean that every transaction takes 500.000.
btw, can you send me the script buying tokens?
My email: datlilkayz11@gmail.com
Thanks

Following your code, I got the issue
UnhandledPromiseRejectionWarning: Error: insufficient funds for intrinsic transaction cost
maybe the issue come from the gasPrice and gasLimit as well

And is there a way how to predict gas used for tx?

My code is here btw: ethers-io/ethers.js#1635

Hi, do you also know how to sell tokens that is bought in nodejs?

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

The optimal setting would be 400.000 and 6 wei.

Could you please share the script for selling? I am able to buy, but not to sell

Thanks in advance!

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

The optimal setting would be 400.000 and 6 wei.

Could you please share the script for selling? I am able to buy, but not to sell

Thanks in advance!

Maybe you need 2 changes.

  1. Change the approved address from WBNB to your token which you want to sell
  2. Change the index of token in swapExactTokensForTokens. For buying a token, the index is [WBNB, token address], for selling maybe it's [tokenaddress,WBNB].

I'm not sure that my solution will work or not. But from the view of a developer, I think it work.

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

The optimal setting would be 400.000 and 6 wei.
Could you please share the script for selling? I am able to buy, but not to sell
Thanks in advance!

Maybe you need 2 changes.

1. Change the approved address from WBNB to your token which you want to sell

2. Change the index of token in **swapExactTokensForTokens**. For buying a token, the index is [WBNB, token address], for selling maybe it's [tokenaddress,WBNB].

I'm not sure that my solution will work or not. But from the view of a developer, I think it work.

Hey, thanks for your reply!

I will try that. But my major concern is that not all tokens have the same amount of decimals. WBNB has 18, safemoon for example has 9 decimals. Slippage would be 12% or more in this case but my script does not work.

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

Can you give me your source code?

I already fixed it. Thanks. But when I use gas limit can I use always 500,000 or more? Because some of my transactions are with 250-300,000 gas used... or is there a way how to set optimal gas limit for all transactions? Thanks!

The optimal setting would be 400.000 and 6 wei.
Could you please share the script for selling? I am able to buy, but not to sell
Thanks in advance!

Maybe you need 2 changes.

1. Change the approved address from WBNB to your token which you want to sell

2. Change the index of token in **swapExactTokensForTokens**. For buying a token, the index is [WBNB, token address], for selling maybe it's [tokenaddress,WBNB].

I'm not sure that my solution will work or not. But from the view of a developer, I think it work.

Hey, thanks for your reply!

I will try that. But my major concern is that not all tokens have the same amount of decimals. WBNB has 18, safemoon for example has 9 decimals. Slippage would be 12% or more in this case but my script does not work.

Don't have any way to set specific slippage for specific token like SafeMoon. decimal value and slippage must be added manually.

#67 (comment)

Hey, when I want to sell some tokens to WBNB are there any changes? Because I have a script for buying tokens that is working correctly but when I try to switch addresses (so output address is WBNB) then trade fails.

hey @Tomix3D can you share you git repo?

Hey, sorry I can't.. but my swap code is above.

Anyone else knows why I'm getting this in the console?
"this should not happen"
It's getting stuck after getting approved and I get to see the hash and the transaction comes through but never buys the token. and at the end states that message. "this should not happen". It's not written anywhere in my code. Thanks.

// Approves the transaction and then gives out a receipt check bscscan.com
console.log('After Approve');
const receipt = await tx.wait(); -- Get's stuck here?
console.log('Transaction receipt');
console.log(receipt);
}

Hi,
Can anyone help me on the following?
When I run this code I always get the error "insufficient funds for gas * price + value".
My wallet has 0.2 WBNB and if I tried to do the swap in my Metamask wallet it works so I'm not sure what I'm doing wrong.

Thanks!

Same problem as Matthias.
I've got this message "this should not happen" when calling Approve or swapExactTokensForTokens.
I'm pretty sure all my parameters are ok and in the right unit
(using 6e9 for gasPrice and 30000 for GasLimit).
I just don't get it and it's driving me nuts.
If someone has an idea or can help me on that, thanks in advance!