0

I'm trying to develop a javascript function to send BNB and a BEP20 token, at the same time, in one transaction, with web3.

let decimals = web3.utils.toBN(18); let bnb_amount = web3.utils.toBN(bnbinput); let bep20_amount = web3.utils.toBN(bep20input);

  // Get BEP20 Token contract instance
  let contract = new web3.eth.Contract(minABI, tokenAddress);

  // calculate token amount
  let bnb_value = web3.utils.toWei("1", "ether");
  let bep20_value = bep20_amount.mul(web3.utils.toBN(10).pow(decimals));

It works if I only try to send BNB, without the tokens (msg.value & no msg.data)

var rawTransaction = {
        "from": fromAddress,
        "to": toAddress,
        "value": bnb_value,
        "chainId": web3.utils.toHex(97)
      };

      web3.eth.sendTransaction(rawTransaction, function(log) {
        console.log(log)
      })

It also works if I only try to send tokens, without BNB (msg.data & no msg.value)

var rawTransaction = {
        "from": fromAddress,
        "to": toAddress,
        "data": contract.methods.transfer(toAddress, bep20_value).send({from: fromAddress}),
        "chainId": web3.utils.toHex(97)
      };

      web3.eth.sendTransaction(rawTransaction, function(log) {
        console.log(log)
      })

...but it don't work if I try to put everything together. Any idea?

My complete code is the following (not working):

var rawTransaction = {
        "from": fromAddress,
        "to": toAddress,
        "value": bnb_value,
        "data": contract.methods.transfer(toAddress, bep20_value).send({from: fromAddress}),
        "chainId": web3.utils.toHex(97)
      };

      web3.eth.sendTransaction(rawTransaction, function(log) {
        console.log(log)
      })

Is there any rule that states that is not possible to set msg.data and msg.value in a web3.eth.sendTransaction()?

Thanks

1
  • Anybody? I do not find a clear way to proceed.
    – AlexAcc
    Nov 29 '21 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.