0

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);

    }
} 

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!!

1 Answer 1

1

The difference is that in the first transaction you are approving yourself

MethodID: 0x095ea7b3
[0]:  000000000000000000000000b78314255250be3f4cfcba6205d1c1d02b06aba5
[1]:  0000000000000000000000000000000000000000000000003782dace9d900000

And the Pancake UI is approving the Pancake Router contract https://bscscan.com/address/0x10ed43c718714eb63d5aa57b78b54704e256024e#code

MethodID: 0x095ea7b3
[0]:  00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e
[1]:  ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

The value ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff is used for some tokens to indicate infinite approval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.