Skip to content

Instantly share code, notes, and snippets.

@adityawarmanfw
Last active August 2, 2022 22:08
Show Gist options
  • Save adityawarmanfw/2fed30899638e48a02aa27d98827c8aa to your computer and use it in GitHub Desktop.
Save adityawarmanfw/2fed30899638e48a02aa27d98827c8aa to your computer and use it in GitHub Desktop.
PancakeSwap SwapExactETHForTokens
const ethers = require('ethers');
const prompt = require('prompt-sync')({sigint: true});
const addresses = {
WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
router: "0x10ed43c718714eb63d5aa57b78b54704e256024e",
target: "0x8129686c77E63C22bE6a7F06F9C61f135BD0a0CF" // Change this to your address ELSE YOU GONNA SEND YOUR BEANS TO ME
}
const BNBAmount = ethers.utils.parseEther('0.1').toHexString();
const gasPrice = ethers.utils.parseUnits('10', 'gwei');
const gas = {
gasPrice: gasPrice,
gasLimit: 300000
}
const mnemonic = 'PUT YOUR MNEMONIC HERE';
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 amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)'
],
account
);
const snipe = async (token) => {
const tx = await router.swapExactETHForTokens(
0, // Degen ape don't give a fuxk about slippage
[addresses.WBNB, token],
addresses.target,
Math.floor(Date.now() / 1000) + 60 * 10, // 10 minutes from now
{
...gas,
value: BNBAmount
}
);
console.log(`Swapping BNB for tokens...`);
const receipt = await tx.wait();
console.log(`Transaction hash: ${receipt.transactionHash}`);
}
const token = prompt('Input token address:');
(async () => {
await snipe(token);
})();
@shottaboss
Copy link

Can someone do a tutorial video of how to set this up?

@arsenal120496
Copy link

arsenal120496 commented Nov 20, 2021

can i test this code on testnet bro ? do you ever try this ? and which package i need to install if i want to swap BNB to token ?

@tomekozlowski
Copy link

i came back to the script, need to make a few modifications here to use this awesome piece, in my tiny project:

  1. it has to read the >token to buy address< variable from a json file i provide
  2. execute immediately without any user input delay

basically instead for taking user input - reading a variable from json file, and executing on its own…
what modification to make? sounds easy but I really don’t know much about js yet and need to get it running.

@Eredyn
Copy link

Eredyn commented Nov 27, 2021

Does this script work with tokens have buy/sell taxes? I notice it's using swapExactETHForTokens instead of swapExactETHForTokensSupportingFeeOnTransferTokens. Seems like a lot of projects nowadays use the token taxes so I assume a version with swapExactETHForTokensSupportingFeeOnTransferTokens is required for those instead?

@skeety123
Copy link

I'm getting Uncaught SyntaxError: Unexpected identifier

basically I have the whole code in, but it can't find the code itself lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment