Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pancake swap bot without 'paircreated' event #65

Open
thekooldev1232 opened this issue May 18, 2021 · 3 comments
Open

Pancake swap bot without 'paircreated' event #65

thekooldev1232 opened this issue May 18, 2021 · 3 comments

Comments

@thekooldev1232
Copy link

thekooldev1232 commented May 18, 2021

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

@thekooldev1232
Copy link
Author

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

@ozgurk78
Copy link

which problem you have ?

@sbthak321
Copy link

I got this done! thanks
will delete the issue now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants