You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//First address of this mnemonic must have enough BNB to pay for tx fess
const mnemonic = config.memo;
const provider = new ethers.providers.WebSocketProvider('your websocket url');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);
const ethers = require('ethers');
const Web3 = require('web3');
const config = require('./config.json');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443')
const addresses = {
',WBNB: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c',
MYTOKEN: '',
router: '0x10ED43C718714eb63d5aA57B78B54704E256024E',
recipient: '
}
//First address of this mnemonic must have enough BNB to pay for tx fess
const mnemonic = config.memo;
const provider = new ethers.providers.WebSocketProvider('your websocket url');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);
const router = new ethers.Contract(
addresses.router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
account
);
const wbnb = new ethers.Contract(
addresses.WBNB,
[
'function approve(address spender, uint amount) public returns(bool)',
],
account
);
let tokenIn = addresses.WBNB, tokenOut = addresses.MYTOKEN;
const amountIn = ethers.utils.parseUnits('0.005', 'ether');
const tokenswap = async () => {
console.log('amountIn ######');
console.log(amountIn);
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
console.log('amounts $$$$$$');
console.log(amounts);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));
console.log(
Buying new token ================= tokenIn: ${amountIn.toString()} ${tokenIn} (WBNB) tokenOut: ${amountOutMin.toString()} ${tokenOut}
);console.log('##### gas #######');
console.log(ethers.BigNumber.from(web3.utils.toWei('20', 'gwei')).toHexString());
/* const txApprove = await wbnb.approve(
router.address,
amountIn
);
let receipt = await txApprove.wait();
console.log('receipt');
console.log(receipt); */
try {
console.log('here in try block');
const tx = await pancakeswap.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10, //10 minutes
{
nonce: 441,
gasLimit: web3.utils.toHex(500000),
gasPrice: web3.utils.toHex(web3.utils.toWei('15', 'gwei')),
}
);
const receipt1 = await tx.wait();
console.log('Transaction receipt of tx');
console.log(receipt1);
} catch (err) {
console.log('### Error ######')
console.log(err);
}
}
const init = async () => {
const txApprove = await wbnb.approve(
router.address,
amountIn
);
const receipt = await txApprove.wait();
console.log('Transaction receipt of bnb');
console.log(receipt);
tokenswap();
}
init();
The text was updated successfully, but these errors were encountered: