Getting `Unexpected end of JSON input`
See original GitHub issueconst ethers = require('ethers');
const addresses = {
WBNB: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73',
router: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
recipient: ''
}
const privateKey = '';
const mygasPrice = ethers.utils.parseUnits('5', 'gwei');
const provider = new ethers.providers.WebSocketProvider('wss://apis-sj.ankr.com/.............');
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.01', 18);
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();
I’m getting this error:
Before Approve
After Approve
undefined:1
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at WebSocketProvider._this._websocket.onmessage (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\@ethersproject\providers\lib\websocket-provider.js:106:31)
at WebSocket.onMessage (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:375:28)
at Receiver.receiverOnMessage (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\websocket.js:834:20)
at Receiver.emit (events.js:375:28)
at Receiver.dataMessage (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\receiver.js:437:14)
at Receiver.getData (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\receiver.js:367:17)
at Receiver.startLoop (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\receiver.js:143:22)
at Receiver._write (C:\Users\Kadiem Alqazzaz\Desktop\psbot\node_modules\ws\lib\receiver.js:78:10)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Uncaught SyntaxError: Unexpected end of JSON input
A common error encountered by JavaScript programmers is the Uncaught SyntaxError: Unexpected end of JSON input. This is usually observed when the coder...
Read more >Unexpected end of JSON input Error in JavaScript - Stack Diary
The "Unexpected end of JSON input" error is a syntax error that occurs when the JSON you're trying to parse is incomplete or...
Read more >Uncaught SyntaxError: Unexpected end of JSON input at ...
When you try to parse it as JSON it is converted to a string ( "" ), which is empty, so you reach...
Read more >Unexpected end of JSON input Error in JavaScript | bobbyhadz
The "Unexpected end of JSON input" error occurs when trying to parse invalid JSON using the JSON.parse or $.parseJSON methods. Trying to parse...
Read more >How to fix "SyntaxError: Unexpected token < in JSON at ...
This error occurs when you are trying to parse a string to JSON and the string is not parsable. In other words, it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
same issue with ankr web socket
For anyone looking for a temporary fix, it looks like it’s an Ankr problem from my testing. I haven’t had that problem at all using Moralis