im new in blockchain and i´m playing with the web3 javascript library. I´m trying to approve the BUSD token to do a swap. My js code is something like this:
async function approveToken(web3, c, howMuchTokentoApprove, senderAddress) {
var abi = [ {
"constant": false,
"inputs": [
{ "internalType": "address", "name": "spender", "type": "address" },
{ "internalType": "uint256", "name": "amount", "type": "uint256" },
],
"name": "approve",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function",
},];
try {
var contract = new web3.eth.Contract(abi, c);
var nonceResponse = await web3.eth.getTransactionCount(senderAddress);
var nonce = nonceResponse;
var txParams = {
gas: web3.utils.toHex(gasLimit),
gasPrice: web3.utils.toHex(gasPrice),
nonce: web3.utils.toHex(nonce),
chainId: chainId,
to: c
};
txParams.data = await contract.methods.approve(senderAddress,
howMuchTokentoApprove).encodeABI();
var signedTx = await web3.eth.accounts.signTransaction(txParams, process.env.senderPrivateKey);
nonce += 1;
txParams.nonce = web3.utils.toHex(nonce);
await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log("Token spending approved. \n");
txAlreadyApprove = true;
return true
} catch (error) {
console.log('err', error);
}
}
c variable is this address (the busd token in bsc) https://bscscan.com/token/0xe9e7cea3dedca5984780bafc599bd69add087d56
senderAdddres is the wallet that wants to do the swap (my wallet)
But when i execute the code, the this is the result: https://bscscan.com/tx/0xd542a08538d39e54603b6aa40d6914f7240ba67519b4a9eebd8261f5d2eaf51e
The log show my wallet:
Approved BUSDFor Trade On0xb78314255250be3f4cfcba6205d1c1d02b06aba5
But, if i execute the operation with the pancake UI. This is the result: https://bscscan.com/tx/0x80a3008132ddcc8af290e9a62fbb8b21b8b33118d3da60a0392bae7d44847950
Approved BUSD For Trade OnPancakeSwap: Router v2
What i´m doing wrong?
Thank you! and happy new year!!