Skip to content

Instantly share code, notes, and snippets.

@adityawarmanfw
Last active August 2, 2022 22:08
Show Gist options
  • Select an option

  • Save adityawarmanfw/2fed30899638e48a02aa27d98827c8aa to your computer and use it in GitHub Desktop.

Select an option

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

despro7 commented Jul 10, 2021

Copy link
Copy Markdown

Hi, mate! I have a problem with this script, I can't figure out what the problem is. Can you please tell me how I can fix this error? Thanks in advance!

(node:2496) UnhandledPromiseRejectionWarning: Error: transaction failed (transactionHash="0x3153f1a031988e9fa033b82148671d0ac7077d7e4172c69eba5964de958cddcc", transaction={"nonce":1,"gasPrice":{"type":"BigNumber","hex":"0x02540be400"},"gasLimit":{"type":"BigNumber","hex":"0x0493e0"},"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","value":{"type":"BigNumber","hex":"0x038d7ea4c68000"},"data":"0x7ff36ab5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000004d2b9322a823d0ba88a114e01b069fd59bb2db760000000000000000000000000000000000000000000000000000000060e9a4f70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000c748673057861a797275cd8a068abb95a902e8de","chainId":56,"v":147,"r":"0x222c92dcf7c6e3c4525fc02b7b10d526d01013be65f2568744081a4442aa3a1d","s":"0x4419976d946840757ae9db32c3df34557e845a400d388ad5ffe1db85abbd4ef1","from":"0x4d2b9322a823d0ba88a114E01B069fd59BB2dB76","hash":"0x3153f1a031988e9fa033b82148671d0ac7077d7e4172c69eba5964de958cddcc","type":null}, receipt={"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","from":"0x4d2b9322a823d0ba88a114E01B069fd59BB2dB76","contractAddress":null,"transactionIndex":1,"gasUsed":{"type":"BigNumber","hex":"0x0485ad"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0x838e34d30817fb48187df00484414685d0ac2c5ed93b5984e84d0768423e4477","transactionHash":"0x3153f1a031988e9fa033b82148671d0ac7077d7e4172c69eba5964de958cddcc","logs":[],"blockNumber":9035480,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x0560ad"},"status":0,"type":0,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.4.1)
at Logger.makeError (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\logger\lib\index.js:187:21)
at Logger.throwError (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\logger\lib\index.js:196:20)
at WebSocketProvider. (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\providers\lib\base-provider.js:1320:36)
at step (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\providers\lib\base-provider.js:48:23)
at Object.next (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\providers\lib\base-provider.js:29:53)
at fulfilled (C:\Users\Xiaomi\snipe_bsc\node_modules@ethersproject\providers\lib\base-provider.js:20:58)
(Use node --trace-warnings ... to show where the warning was created)
(node:2496) 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:2496) [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.

@adityawarmanfw

Copy link
Copy Markdown
Author

Hi, mate! I have a problem with this script, I can't figure out what the problem is. Can you please tell me how I can fix this error? Thanks in advance!

I'm not sure. Try buying other token?

@despro7

despro7 commented Jul 10, 2021

Copy link
Copy Markdown

I just tried to buy another token, it worked, thanks bro! Can you please tell me how to modify this script so that I can change the amount in the terminal before buying?

@kbeprogramming

Copy link
Copy Markdown

Hi, mate... thanks for you effort on this project what you shared to other people... but im retard lmao and i cant understand some thinks like i dont even know what means "mnemonic code" or i did install nodejs but idk how to use it soo... may you recommend me some video on youtube for newbies, plz? i really appreciate it man

@tomekozlowski

Copy link
Copy Markdown

ERROR: BIP39 Wordlist for en (English) FAILED

i give u half babymooncumdogcatshitcoin for halp :(((((((

@tomekozlowski

Copy link
Copy Markdown

i am guessing i should not type the mnemonic phrase, instead the key - however trust wallet (unlike metamask) dosn’t show private keys.

WHAT SHOULD I TYPE IN THERE?
and if it doesn’t show on trustwallet, do i have to switch to metamask or is there a way to extract that data/key?

@adityawarmanfw

adityawarmanfw commented Jul 13, 2021 via email

Copy link
Copy Markdown
Author

@adityawarmanfw

adityawarmanfw commented Jul 13, 2021 via email

Copy link
Copy Markdown
Author

@tomekozlowski

Copy link
Copy Markdown

oh i see, thank you, so well - i typed it out just straight up with single spaces, and it gave the error, then i tried with commas, and the same thing. have you got any idea what the error is about and how to fix it:/ ?

@tomekozlowski

Copy link
Copy Markdown

Error: BIP39 Wordlist for en (English) FAILED
at loadWords (/root/node_modules/@ethersproject/wordlists/lib/lang-en.js:31:15)
at LangEn.getWordIndex (/root/node_modules/@ethersproject/wordlists/lib/lang-en.js:44:9)
at mnemonicToEntropy (/root/node_modules/@ethersproject/hdnode/lib/index.js:275:30)
at Function.HDNode.fromMnemonic (/root/node_modules/@ethersproject/hdnode/lib/index.js:219:38)
at Function.Wallet.fromMnemonic (/root/node_modules/@ethersproject/wallet/lib/index.js:238:43)
at Object. (/root/pcsbuy.js:19:30)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)

@tomekozlowski

Copy link
Copy Markdown

„ if (wordlist_1.Wordlist.check(lang) !== "0x3c8acc1e7b08d8e76f9fda015ef48dc8c710a73cb7e0f77b2c18a9b5a7adde60") {
wordlist = null;
throw new Error("BIP39 Wordlist for en (English) FAILED");

this is how they made the badboy error happen in the language files in ethers.js
what it means, wtf? it looks like someone’s wallet 🤡

tried different input methods of the words but no avail

@tomekozlowski

Copy link
Copy Markdown

well, i know fuck all about all this tbh now im angry cuz what my smooth brain gets from this is that the guy writing that part of code was like:
„ok, NOW, let’s play a game: if the wordlist doesn’t match my wallet address, I’m throwing a brand new error at your face, bitch.”

what a meanie! how much does he want???? how much is enough????

@adityawarmanfw

adityawarmanfw commented Jul 13, 2021

Copy link
Copy Markdown
Author

@tomekozlowski Well I'm not sure why that's happened. Is your mnemonic using a non english language? Anyway you can try to use privateKey instead of mnemonic by changing these:

const mnemonic = 'PUT YOUR MNEMONIC HERE';
const wallet = ethers.Wallet.fromMnemonic(mnemonic);

to this:

const privateKey = 'PUT YOUR PRIVATE KEY HERE';
const wallet = new ethers.Wallet(privateKey);

@tomekozlowski

tomekozlowski commented Jul 15, 2021

Copy link
Copy Markdown

Thank you a lot for help. I managed to set it up now.
I have a problem - same or similar as the first person who commented, transactions fail, and there is this wall of text, that looks very similar to his problem. You told him to buy another token - problem is this popped up for me even with tether, and sometimes I need to buy really a specific token to kill noobs at launch who want to outsmart me... and need a reliable bot/script to do so, is there a way to make it work through this error? There isn't like anything specific in the error, as to why it occurs... goes like this:

"node_modules@ethersproject\logger\lib\index.js:187
var error = new Error(message);
^

Error: transaction failed (transactionHash="0x28 [...]"

and after is a wall of text basically describing the transaction details... wtf : /

If you know, could you tell me what that wall of text error is about? How to understand it, why it occurs, and what it means when this error occurs? Thanks. I will tip you half a cumshitdogecoin anyways, as a show of immense gratitude.

@Prgrmnb

Prgrmnb commented Jul 15, 2021

Copy link
Copy Markdown

Hi does the script work for dxsale presale too?

@tomekozlowski

Copy link
Copy Markdown

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

harakiriblade commented Jul 22, 2021

Copy link
Copy Markdown

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

Azi1234 commented Jul 27, 2021

Copy link
Copy Markdown

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
Copy Markdown

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

@arsenal120496

arsenal120496 commented Nov 20, 2021

Copy link
Copy Markdown

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
Copy Markdown

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

Eredyn commented Nov 27, 2021

Copy link
Copy Markdown

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
Copy Markdown

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