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