How to call contract’s write function using web3Js

I am creating a dapp where I have to call a custom write function.
Read function are getting called smoothly in the dapp.
But facing issues with write function.

I need guidance in that in possible please share articles links, videos links anything.

Thank you

Hi @Owai2627

Can you show you code?

The error which you are seeing could happen if you are not connected to the correct chain in your wallet.

async function transferTokensToContract(contractAddress, amount) {

        web3 = new Web3(web3.currentProvider); // Assuming you're in a browser context

        const contract = new web3.eth.Contract(contractAbi, contractAddress);

        try {

            const accounts = await ethereum.request({ method: 'eth_requestAccounts' });

            const senderAddress = accounts[0];

            //const convertedValue = Number(convertToWBNB(amount));

            const gasLimit = 20000000; // You can adjust this value based on your needs

             // Call the burn function on the contract with the sender's address and amount

            await contract.methods.burn(amount).send({ from: senderAddress, gas: gasLimit  });                

           

            console.log(`${amount} tokens burned to address ${senderAddress}`);

        } catch (error) {

            console.error("Error transferring tokens:", error);

        }

    }

Here are few things to check

  • Check if you are using correct contract address and you are connected to the correct wallet network where the contract is deployed

  • Is seems you are calling the burn function so check if the wallet which you are using has permissions to call the burn functions.

All the above things are correct.

https://testnet.bscscan.com/vmtrace?txhash=0xe144435371fc2556a927f6defb1475dc34360603921884e7f9c6fd95de89ef17&type=gethtrace2

It is showing type: call

Is it correct, As I used (.send) function in code

I am not sure what else can go wrong here.

Incase if you are owner of the contract you can try calling the contract functions from remix. It if works on the remix then we should debug the js code further on what is causing the issue.

Yeah it is working on remix.

Can you send me your telegram username, so I could forward you whole code and screenshots of error.

If possible please share the related code here.

Or if you want to keep the code private use the tickets channel in our discord to share the code.