jklepatch / eattheblocks

Source code for Eat The Blocks, a screencast for Ethereum Dapp Developers
https://eattheblocks.com
4k stars 3.2k forks source link

Error: transaction failed on PancakeSwap trading bot #72

Open Genray100 opened 3 years ago

Genray100 commented 3 years ago

So I have been trying to adjust the bot to buy a specific token using swapExactETHForTokens instead of swapExactTokensForTokens since I heard that there is no need to approve when using the former.


const addresses = {
  WBNB: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
  factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73',
  router: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
  recipient: 'my_wallet_address'
}

const mnemonic = 'seed phrase left our for obvious reasons';

const provider = new ethers.providers.WebSocketProvider('wss://bsc-ws-node.nariox.org:443');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);

const router = new ethers.Contract(
    addresses.router,
    [
      'function swapExactETHForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
    ],
    account
  );

const init = async () => {
    const options = {gasPrice: 5000000000, gasLimit: 400000 };
    const amountIn = ethers.utils.parseUnits('0.1', 'ether');    
    const amountOutMin = 1
    const tx = await router.swapExactETHForTokens(
      amountIn,
      amountOutMin,
      [addresses.WBNB, '0xb3225ac90b741f762beca76dea1ead278ef26a96'], //just used a random token address, LOF in this case
      addresses.recipient,
      Date.now() + 1000 * 60 * 10, //10 minutes    
      options
    );
    console.log(`Transaction hash: ${tx.hash}`);
    const receipt = await tx.wait();
    console.log(`Transaction was mined in block ${receipt.blockNumber}`);
    }

init(); 

But I get the following error all the time:

(node:22256) UnhandledPromiseRejectionWarning: Error: transaction failed (transactionHash="0x35bb3f987f4f99fab4572db341886220a400f791cb68bce401dcf00fd5099396", transaction={"nonce":348,"gasPrice":{"type":"BigNumber","hex":"0x012a05f200"},"gasLimit":{"type":"BigNumber","hex":"0x061a80"},"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","value":{"type":"BigNumber","hex":"0x00"},"data":"0xa5be382e000000000000000000000000000000000000000000000000000009184e72a000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000b083653f11a5133868d56dacd723b837e27b7d17000000000000000000000000000000000000000000000000000001799a15a8690000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000b3225ac90b741f762beca76dea1ead278ef26a96","chainId":56,"v":148,"r":"0xb536f633a90b06f69a33467d24478dad64f868006a417ebbcd5525a372d6c9bf","s":"0x67c071f07c38723651c1b0e26dfa2bb42cc279587dc43326a2a4d52c81ed65d0","from":"it_shows_my_wallet_address_here_but_i_left_it_out","hash":"0x35bb3f987f4f99fab4572db341886220a400f791cb68bce401dcf00fd5099396"}, receipt={"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","from":"it_shows_my_wallet_address_here_but_i_left_it_out","contractAddress":null,"transactionIndex":324,"gasUsed":{"type":"BigNumber","hex":"0x5a8b"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x2ee5c5c282a605193f282aa34e31fa18c62ff3fdc6cd996e1592be49cdb60e03","transactionHash":"0x35bb3f987f4f99fab4572db341886220a400f791cb68bce401dcf00fd5099396","logs":[],"blockNumber":7663738,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x02bf7565"},"status":0,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.0.24)
    at Logger.makeError (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\logger\lib\index.js:180:21)
    at Logger.throwError (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\logger\lib\index.js:189:20)
    at WebSocketProvider.<anonymous> (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:1162:36)
    at step (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:48:23)
    at Object.next (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:29:53)
    at fulfilled (C:\Users\Genray\Desktop\trading-bot\node_modules\@ethersproject\providers\lib\base-provider.js:20:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:22256) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22256) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

When I look up the taransaction details at Bscscan it always says Status: Fail and after a little bit of time Warning! Error encountered during contract execution [execution reverted]

Could someone help me? I can not seem to locate where the error lies.

hbtj123 commented 3 years ago

same here for some reason do u know the reason by any chance?

adityawarmanfw commented 3 years ago

Same. Can't use that function. It works with Uniswap but not working with Pcs. Just use swapExactTokensForTokens + deposited WBNB.