Ethereum Asked by Himanshu on January 11, 2021
I have tried using events.eventname.returnValues[index] but I am not getting any response. Below is the smart contract code –
pragma solidity 0.5.12;
contract Dflip{
uint contractBalance;
address public owner;
constructor() public {
owner = msg.sender;
}
modifier costs (uint cost){
require(msg.value >= cost,"The minimum value to place bet is 0.01 ether");
_;
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
event flipCoinData(uint betValue,bool result);
event withdrawFundData(uint amount);
event fundContractData(address funder,uint fundAmount);
function flipCoin() public payable costs(0.1 ether) returns(bool) {
require(msg.value <= contractBalance,"The contract doesn't have enough balance");
bool result;
uint chance;
chance = now % 2;
if (chance == 0){
contractBalance += msg.value;
result = false;
}
else if ( chance == 1){
contractBalance -= msg.value;
msg.sender.transfer(msg.value*2);
result = true;
}
return result;
emit flipCoinData(msg.value,result);
}
function getContractBalance() public view returns(uint){
return address(this).balance;
}
function withdrawAll() public onlyOwner {
emit withdrawFundData(contractBalance);
msg.sender.transfer(contractBalance);
}
function fundContract() public payable {
require(msg.value != 0);
contractBalance += msg.value;
emit fundContractData(msg.sender,msg.value);
}
}
And this is the mainJS file to connect with web3JS
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
$(document).ready(function(){
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(abi,"0x28811FA3bC95f2776918871BAb6332662f15cd43",{from:accounts[0]})
console.log(contractInstance);
})
$("#flip_button").click(inputData);
$("#get_data_button").click(contractBalance);
$("#fund_contract_button").click(fundContract);
$("#withdraw_fund_button").click(withdrawFund);
})
// extra function starts here
function withdrawFund(){
contractInstance.methods.withdrawAll().send();
}
function fundContract(){
var fundAmount = $("#fund_contract_value").val();
var config = {
value: web3.utils.toWei(fundAmount,"ether")
}
contractInstance.methods.fundContract().send(config)
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
})
}
function inputData(){
var betAmount = $("#bet_input").val();
var config = {
value: web3.utils.toWei(betAmount,"ether")
}
contractInstance.methods.flipCoin().send(config)
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
if(receipt.events.flipCoinData.returnValues[1] === false){
alert("You have lost");
}
})
}
function contractBalance(){
contractInstance.methods.getContractBalance().call().then(function(res){
$("#jackpot_output").text(web3.utils.fromWei(res) + " Ether");
})
}
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP