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

Prgrmnb commented Jul 15, 2021

Hi does the script work for dxsale presale too?

@tomekozlowski
Copy link

it’s working perfectly now for me :)

how to get the delay to nanoseconds? what makes delay more (for me 3-5 seconds before SMASHING DA ENTER BUTTON AND TRANSACTION COMPLETED ON BLOCKCHAIN) to a micro-nano-chad-sniper-seconds levels?
i taste sick gains… just need supercomputer delay lvls…

@harakiriblade
Copy link

harakiriblade commented Jul 22, 2021

Getting this error in every token awhile back it was working

(node:12252) UnhandledPromiseRejectionWarning: Error: transaction failed (transactionHash="0xc30c4a15f6331a411824aee6d322a8682f59a028b8811c9e7d7e4410c290c743", transaction={"nonce":46,"gasPrice":{"type":"BigNumber","hex":"0x012a05f200"},"gasLimit":{"type":"BigNumber","hex":"0x0493e0"},"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","value":{"type":"BigNumber","hex":"0x2386f26fc10000"},"data":"0xb6f9de9500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000f7be0a124518bc225b6dbb7e72a86a6613221b940000000000000000000000000000000000000000000000000000000060f91eaa0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c0000000000000000000000001512ff29f8c5df54988066bed6d6e31a0c100c03","chainId":56,"v":148,"r":"0x58435842f43a1c685f1c378fe7ac4abe1679a7ad70824ac7bb8ccc5f7e1c26be","s":"0x6918ca07f92a23ae6e859e1e9200694718d1fe975120adafa8afa964828c4b1f","from":"0xF7BE0a124518bc225b6dbb7E72a86a6613221b94","hash":"0xc30c4a15f6331a411824aee6d322a8682f59a028b8811c9e7d7e4410c290c743","type":null}, receipt={"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","from":"0xF7BE0a124518bc225b6dbb7E72a86a6613221b94","contractAddress":null,"transactionIndex":160,"gasUsed":{"type":"BigNumber","hex":"0x047b41"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x6910973dc27dfb2f3c3c4fd8310f0878f1b7732f6496a324b45d8ac242980229","transactionHash":"0xc30c4a15f6331a411824aee6d322a8682f59a028b8811c9e7d7e4410c290c743","logs":[],"blockNumber":9373400,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x017b17ba"},"status":0,"type":0,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.4.1)
at Logger.makeError (D:\Bot\simplebot\node_modules@ethersproject\logger\lib\index.js:187:21)
at Logger.throwError (D:\Bot\simplebot\node_modules@ethersproject\logger\lib\index.js:196:20)
at WebSocketProvider. (D:\Bot\simplebot\node_modules@ethersproject\providers\lib\base-provider.js:1320:36)
at step (D:\Bot\simplebot\node_modules@ethersproject\providers\lib\base-provider.js:48:23)
at Object.next (D:\Bot\simplebot\node_modules@ethersproject\providers\lib\base-provider.js:29:53)
at fulfilled (D:\Bot\simplebot\node_modules@ethersproject\providers\lib\base-provider.js:20:58)
(Use node --trace-warnings ... to show where the warning was created)
(node:12252) 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:12252) [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.

Getting this error in every token awhile back it was working

@Azi1234
Copy link

Azi1234 commented Jul 27, 2021

events.js:352
throw er; // Unhandled 'error' event
^

Error: Unexpected server response: 503
at ClientRequest. (C:\Users\Orietta Berti\Desktop\tradingbot\node
_modules\ws\lib\websocket.js:604:7)
at ClientRequest.emit (events.js:375:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:647:27
)
at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
at TLSSocket.socketOnData (_http_client.js:515:22)
at TLSSocket.emit (events.js:375:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at TLSSocket.Readable.push (internal/streams/readable.js:204:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on WebSocket instance at:
at abortHandshake (C:\Users\Orietta Berti\Desktop\tradingbot\node_modules\ws
\lib\websocket.js:731:15)
at ClientRequest. (C:\Users\Orietta Berti\Desktop\tradingbot\node
_modules\ws\lib\websocket.js:604:7)
[... lines matching original stack trace ...]
at TLSSocket.Readable.push (internal/streams/readable.js:204:10)

@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