Search code examples
javascriptsoliditysmartcontractsweb3js

Accepting parameters in javascript to send to solidity smart contract


Wrote this simple solidity contract where the functions accept a few parameters.

function fund(uint _dbIndex, uint _fundedAmount) public {
    uint totalAmount = projectWallets[_dbIndex].fundedAmount;
    totalAmount += _fundedAmount;
    projectWallets[_dbIndex].fundedAmount = totalAmount;
}

after creating this smart contract, i tried connecting the SC to a HTML page with javascript. I managed to get a function without parameters to work but couldnt get it to work if theres parameters in the function..

<input type="number" min="0" placeholder="Fund Amount" style="color: black;" id="fund">
<button id="fund_project">Fund Project</button>

above is the input and button to send the value into the smart contract.

$('#fund_project').click(function (e) {
  e.preventDefault();

  log("Calling fund function...");

  counter.fund(dbIndex, funds);
});

and this is a segment of the code where i take in the function with parameters. How do i get it to work?


Solution

  • A callback is a function that is called when the operation is complete. You need to use one, e.g.:

    counter.fund(dbIndex, funds, function (err, result) {
      if (err) {
        console.log('Error: ' + err);
      }
      else {
        console.log(result);
      }
    );