1

Here, I am trying to call a function(embrace) on a smart contract on Ropsten that needs two parameters and msg.value of 0.01 ether. But it's showing up the error that the msg.value is not sufficient. The rest of the functions(that don't need any msg.value attached to them) on the contract are working completely fine and also I am able to call and execute the function(embrace) form remix So, I am sure that there's no problem on the blockchain side code or the connection part.

what I understand is that I am not able to attach the value to the transaction properly. Please, can someone tell me what I am doing wrong here?

async function embrace(
  _value,
  _buyerName,
  _productID,
  passedpublickey,
  passedprivatekey
) {
  return new Promise(async (resolve, reject) => {
    const account = passedpublickey;
    const privateKey = Buffer.from(passedprivatekey, "hex");

    const contract = new web3js.eth.Contract(abi_assetMain, address_asset, {
      from: account,
      gasLimit: 3000000,
    });

    let valuethis = web3js.utils.toHex(web3js.utils.toWei("0.01", "ether"));
    console.log(
      "Contract Function:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
      valuethis,
      _value,
      _buyerName,
      _productID,
      passedpublickey,
      passedprivatekey
    );

    const contractFunction = contract.methods.embrace(_buyerName, _productID);
    const functionAbi = contractFunction.encodeABI();

    let estimatedGas;
    let nonce;

    contractFunction
      .estimateGas({ from: account })
      .then((gasAmount) => {
        estimatedGas = gasAmount.toString(16);

        web3js.eth.getTransactionCount(account).then((_nonce) => {
          nonce = _nonce.toString(16);

          console.log("Nonce: " + nonce);
          let networkChainId = 3;
          let value = web3js.utils.toHex(Web3.utils.toWei("0.01", "ether"));
          const details = {
          
            to: address_asset,
            data: functionAbi,
            from: account,
            value: value,
           
            gasLimit: web3js.utils.toHex(3000000),
            gasPrice: web3js.utils.toHex(web3js.utils.toWei("100", "gwei")),
            nonce: "0x" + nonce,
            chainId: networkChainId,
          };
          const transaction = new EthereumTx(details, { chain: networkChainId });
          transaction.sign(privateKey);

          const serializedTx = transaction.serialize();

          web3js.eth.sendSignedTransaction(
            "0x" + serializedTx.toString("hex"),
            (err, id) => {
              if (err) {
                console.log("---error---:", err);
                return resolve("Error Sending Transactions");
              } else if (id) {
                const url = etherscanAxplorUrl + id;
                console.log(url);
                return resolve(url);
              }
            }
          );
        });
      })
      .catch((err) => {
        reject(err);
        console.log("error errerrerrerrerrerr", err);
      });
  });
}

0

Your Answer

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

Browse other questions tagged or ask your own question.