Handling the fund function

To raise funds, we need to call the fund function, which is defined in our crowdfunding smart contract. In our web page, we use the HTML range input slider component to contribute fund amounts, as follows:

<form id="fund-form" method="post" role="form" style="display: block;">
<div class="form-group row">
<div class="row">
<div class="col-lg-12">
<input type="range" name="ageInputName" id="ageInputId" value="0" min="1" max="100" oninput="ageOutputId.value = ageInputId.value">
<div style="display: inline;"><output name="ageOutputName" id="ageOutputId">0</output> <span>ether</span></div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<button type="button" id="fundBtn" class="btn btn-primary pull-left">Submit</button>
</div>
</div>
</div>
</form>

The Crowdfunding fund function is a payable fallback function; therefore, we need to pass msg.sender and msg.value from the UI to call it, as follows.

   function fund() public atStage(Status.Fundraising) payable {
contributions.push(
Contribution({
addr: msg.sender,
amount: msg.value
})
);
……
}

You can define the sending address and value parameters as follows:

  handleFund: function(event) {
event.preventDefault();
var fundVal = $('#ageOutputId').val();
var selectAcct = $('#accts').find(":selected").val();
$("#displayMsg").html("");
App.contracts.CrowdFunding.deployed().then(function(instance) {
return instance.fund({ from: selectAcct, value:web3.toWei(fundVal, "ether"), gas:3500000});
}).then(function(result) {
App.loadProject();
}).catch(function(err) {
console.error(err);
$("#displayMsg").html(err);
});
},

Once we get the result back, we will call the loadProject function to refresh the project information. We can see that the current balance fund increased, as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.69.246