Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active May 20, 2023 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianeon/a57c74af6ce5d4a6857c9873b9fe5ac5 to your computer and use it in GitHub Desktop.
Save julianeon/a57c74af6ce5d4a6857c9873b9fe5ac5 to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const fs = require('fs');
const gasPriceTokenApproval = '5.5'; // Set your desired gas price in Gwei for token approval
const gasPricePancakeSwap = '10';
// Connect to the BSC network using a provider URL
const providerUrl = 'https://bsc-dataseed.binance.org';
const web3 = new Web3(providerUrl);
// Important variables
const tokenContractAddress = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const walletPrivateKey = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
const walletAddress = '0xcccccccccccccccccccccccccccccccccccccccc';
// Set up the PancakeSwap contract
const pancakeswapContractAddress = '0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F';
const pancakeswapAbi = JSON.parse(fs.readFileSync('pancakeswap_abi.json', 'utf8'));
const pancakeswapContract = new web3.eth.Contract(pancakeswapAbi, pancakeswapContractAddress);
// Set up the token contract
const tokenAbi = JSON.parse(fs.readFileSync('token_abi.json', 'utf8'));
const tokenContract = new web3.eth.Contract(tokenAbi, tokenContractAddress);
// Set up the token information
const tokenDecimalFactor = 10 ** 18; // 18 decimal places
const tokenAmount = '1000000000000000000'; // Replace with your desired value
// Approve PancakeSwap to spend your tokens
const tokenApproveMethod = tokenContract.methods.approve(
pancakeswapContractAddress,
tokenAmount //* tokenDecimalFactor
);
let nonce = web3.eth.getTransactionCount(walletAddress);
const tokenApproveTx = {
gas: 500000,
gasPrice: web3.utils.toWei(gasPriceTokenApproval, 'gwei'), // Set gas price directly
from: walletAddress,
nonce: nonce,
data: tokenApproveMethod.encodeABI(),
to: tokenContractAddress
};
nonce += 1;
web3.eth.accounts.signTransaction(tokenApproveTx, walletPrivateKey)
.then((signedApproveTx) => {
web3.eth.sendSignedTransaction(signedApproveTx.rawTransaction)
.on('transactionHash', (txHash) => {
console.log('Token approval transaction hash:', txHash);
})
.on('receipt', (receipt) => {
// Check if the transaction was successful or reverted
if (receipt.status) {
// Purchase tokens on PancakeSwap
const latestBlock = web3.eth.getBlock('latest');
const pancakeSwapMethod = pancakeswapContract.methods.swapExactETHForTokens(
0,
[web3.utils.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'), tokenContractAddress],
walletAddress,
web3.eth.getBlock('latest').timestamp + 10 // The transaction will be valid for 10 more seconds
);
const pancakeSwapTx = {
gas: 500000,
gasPrice: web3.utils.toWei(gasPricePancakeSwap, 'gwei'), // Set gas price directly
from: walletAddress,
nonce: nonce,
value: web3.utils.toWei('0.005', 'ether'),
data: pancakeSwapMethod.encodeABI(),
to: pancakeswapContractAddress
};
web3.eth.accounts.signTransaction(pancakeSwapTx, walletPrivateKey)
.then((signedSwapTx) => {
web3.eth.sendSignedTransaction(signedSwapTx.rawTransaction)
.on('transactionHash', (txHash) => {
console.log('PancakeSwap transaction hash:', txHash);
})
.on('receipt', (receipt) => {
// Check if the transaction was successful or reverted
if (receipt.status) {
console.log('PancakeSwap transaction successful');
} else {
// Transaction reverted, extract and decode the revert reason
const revertReason = web3.utils.hexToAscii(receipt.receipt.rawLogs[0].data);
console.log('PancakeSwap transaction reverted:', revertReason);
}
})
.catch((error) => {
console.log('Failed to send PancakeSwap transaction:', error);
});
})
.catch((error) => {
console.log('Failed to sign PancakeSwap transaction:', error);
});
} else {
// Transaction reverted, extract and decode the revert reason
const revertReason = web3.utils.hexToAscii(receipt.receipt.rawLogs[0].data);
console.log('Token approval transaction reverted:', revertReason);
}
})
.catch((error) => {
console.log('Failed to send token approval transaction:', error);
});
})
.catch((error) => {
console.log('Failed to sign token approval transaction:', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment