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.
Your spend = web3.utils.toChecksumAddress("0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7")
isn't a valid token address.
Likewise, your purchase token 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd
isn't a valid token either.
If you're trying to swap BUSD to WBNB, you need to change these variables to:
0xe9e7cea3dedca5984780bafc599bd69add087d56 (BUSD)
and
0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c (WBNB)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.