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

PancakeSwap trading bot Problem :Unhandled 'error' event #63

Open
ozgurk78 opened this issue May 17, 2021 · 2 comments
Open

PancakeSwap trading bot Problem :Unhandled 'error' event #63

ozgurk78 opened this issue May 17, 2021 · 2 comments

Comments

@ozgurk78
Copy link

İf ı run node bot.js

I watch this problem . What can I do?

provider
wallet
account
factory
router
wbnb
PairCreated
Transaction receipt11
events.js:292
throw er; // Unhandled 'error' event
^

Error: Unexpected server response: 200
at ClientRequest. (C:\Users\ozgur\bot2\node_modules\ws\lib\websocket.js:580:7)
at ClientRequest.emit (events.js:315:20)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:641:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
at TLSSocket.socketOnData (_http_client.js:509:22)
at TLSSocket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at TLSSocket.Readable.push (internal/streams/readable.js:223:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on WebSocket instance at:
at abortHandshake (C:\Users\ozgur\bot2\node_modules\ws\lib\websocket.js:698:15)
at ClientRequest. (C:\Users\ozgur\bot2\node_modules\ws\lib\websocket.js:580:7)
[... lines matching original stack trace ...]
at TLSSocket.Readable.push (internal/streams/readable.js:223:10)

@ozgurk78
Copy link
Author

and my code:

console.log('provider');
//console.log(provider);
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log('wallet');
//console.log(wallet);
const account = wallet.connect(provider);
console.log('account');
//console.log(account);
const factory = new ethers.Contract(
addresses.factory,
['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
account
);
console.log('factory');
//console.log(factory);
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
);
console.log('router');
//console.log(router);
const wbnb = new ethers.Contract(
addresses.WBNB,
[
'function approve(address spender, uint amount) public returns(bool)',
],
account
);
console.log('wbnb');
//console.log(wbnb);
const init = async () => {
/*const tx = await wbnb.approve(
router.address,
'replace by amount covering several trades'
);
const receipt = await tx.wait(); */

console.log('Transaction receipt11');
//console.log(receipt);
}

console.log('PairCreated');

factory.on('error', function(err){
console.log('onerror');
console.log(err);
});

factory.on('PairCreated', async (token0, token1, pairAddress) => {
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
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(Buying new token ================= tokenIn: ${amountIn.toString()} ${tokenIn} (WBNB) tokenOut: ${amounOutMin.toString()} ${tokenOut});
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10 //10 minutes
);
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);
});

init();

@theterrapunk
Copy link

Maybe try putting the provider starting with "wss" instead of "https:" - that worked for me. Make sure to put it in quotes too.

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

2 participants