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

Pancake swap bot without 'paircreated' event

thekooldev1232 opened this issue · comments

const ethers = require('ethers');
const Web3 = require('web3');
const config = require('./config.json');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443')

const addresses = {
WBNB: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c',
MYTOKEN: '',
router: '0x10ED43C718714eb63d5aA57B78B54704E256024E',
recipient: '

',
}

//First address of this mnemonic must have enough BNB to pay for tx fess
const mnemonic = config.memo;
const provider = new ethers.providers.WebSocketProvider('your websocket url');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);

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
);

let tokenIn = addresses.WBNB, tokenOut = addresses.MYTOKEN;
const amountIn = ethers.utils.parseUnits('0.005', 'ether');

const tokenswap = async () => {
console.log('amountIn ######');
console.log(amountIn);
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
console.log('amounts $$$$$$');
console.log(amounts);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));
console.log(Buying new token ================= tokenIn: ${amountIn.toString()} ${tokenIn} (WBNB) tokenOut: ${amountOutMin.toString()} ${tokenOut});
console.log('##### gas #######');
console.log(ethers.BigNumber.from(web3.utils.toWei('20', 'gwei')).toHexString());

// Create signer
const wallet = new ethers.Wallet(
    Buffer.from(
        config.from.privkey, // paste your private key from metamask here
        "hex"
    )
)
const signer = wallet.connect(provider)
const pancakeswap = new ethers.Contract(
    addresses.router,
    ['function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'],
    signer
)

/* const txApprove = await wbnb.approve(
router.address,
amountIn
);
let receipt = await txApprove.wait();
console.log('receipt');
console.log(receipt); */
try {
console.log('here in try block');
const tx = await pancakeswap.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10, //10 minutes
{
nonce: 441,
gasLimit: web3.utils.toHex(500000),
gasPrice: web3.utils.toHex(web3.utils.toWei('15', 'gwei')),
}
);
const receipt1 = await tx.wait();
console.log('Transaction receipt of tx');
console.log(receipt1);
} catch (err) {
console.log('### Error ######')
console.log(err);
}
}

const init = async () => {
const txApprove = await wbnb.approve(
router.address,
amountIn
);
const receipt = await txApprove.wait();
console.log('Transaction receipt of bnb');
console.log(receipt);
tokenswap();
}

init();

Julian if you are seeing this, what's wrong with the code above?
Can anyone try and let me know?

which problem you have ?

I got this done! thanks
will delete the issue now