I am trying to use pancakeswap contract method to simply swap BUSD to WBNB token, from metamask wallet. pancakeswap contract method is:
var web3 = new Web3(new Web3.providers.HttpProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'))
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
tokenToBuy = web3.utils.toChecksumAddress("0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd")
spend = web3.utils.toChecksumAddress("0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7")
contract = new web3.eth.Contract(panabi, panRouterContractAddress, { from: sender_address });
var swap = contract.methods.swapExactTokensForTokens(
web3.utils.toWei("0.002", "ether"), 0, [spend, tokenToBuy], sender_address, web3.utils.toHex(Math.round(Date.now() / 1000) + 60 * 20)
)
var encodedABI = swap.encodeABI()
nonc = await web3.eth.getTransactionCount(sender_address);
ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: sender_address,
to: panRouterContractAddress,
value: web3.utils.toHex(web3.utils.toWei("0.002", "ether")),
gasPrice: web3.utils.toHex(web3.utils.toWei("15", "gwei")),
gas: web3.utils.toHex(300000),
nonce: web3.utils.toHex(nonc),
data: encodedABI
},
],
})
.then((txHash) => { console.log(txHash); })
.then(() => console.log('Transaction sent!'))
.catch((error) => console.error);
I am sure I am missing something.