hi @dan-i
Please see below the main.js code
contractFunded is the event
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
$(document).ready(function(){
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(abi, web3.utils.toChecksumAddress("0xBd8Faa10595d86c39a89AD0e21041ee0E0cF0202"), {from : accounts[0]});
console.log(contractInstance);
console.log(`Use Contract address: ${contractInstance._address}`)
});
$("#flip_button").click(flipCoin);
$("#get_balance").click(fetchAndDisplay);
$("#fund_contract_button").click(fundContract);
$("#withdraw_funds").click(withdrawAll);
});
function flipCoin(){
var bet = $("#bet_input").val();
var config = {
value: web3.utils.toWei(bet,"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.betPlaced.returnValues[2] === false){
alert("You lost " + bet + " Ether!");
}
else if(receipt.events.betPlaced.returnValues[2] === true){
alert("You won " + bet + " Ether!");
}
})
};
function fetchAndDisplay(){
contractInstance.methods.getBalance().call().then(function(res){
$("#jackpot_output").text("The Contract has : " + web3.utils.fromWei(res[1], "ether") + "Ether");
})
};
function fundContract(){
var fund = $("#fund_contract").val();
var config = {
value : web3.utils.toWei(fund, "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);
receipt.events.contractFunded(function(error, result){
alert("The Contract has now been funded by :" + result.returnValues.amount(web3.utils.fromWei(amount, "ether")) + "Ether");
})
})
};
function withdrawAll(){
contractInstance.methods.withdrawAll().send();
};
also, see below the solidity code:
pragma solidity 0.5.12;
contract Coinflip {
address public contractOwner;
constructor() public {
contractOwner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == contractOwner, "You are not the owner");
_;
}
uint public contractBalance;
address public contractAddress;
modifier costs(uint cost) {
require(msg.value >= cost, "Minimum amount >= 0.01 ether");
_;
}
event betPlaced(address user, uint bet, bool);
event contractFunded(address contractOwner, uint amount);
//Flip the Coin and check whether user won or lost;
function flipCoin() public payable costs(0.01 ether) returns(bool success) {
require(address(this).balance >= msg.value, "The contract doesnt have enough balance to play right now. Come Back later");
if (now % 2 == 0) {
contractBalance += msg.value;
success = false;
}
else if (now % 2 == 1){
contractBalance -= msg.value;
msg.sender.transfer(msg.value * 2);
success = true;
}
//event to be emitted
emit betPlaced(msg.sender, msg.value, success);
return success;
}
function getBalance() public view returns(address, uint, uint) {
return(address(this), address(this).balance, contractBalance);
}
// withdraw all funds possible only though contractOwner address;
function withdrawAll() public onlyOwner returns(uint){
msg.sender.transfer(address(this).balance);
assert(address(this).balance == 0);
return address(this).balance;
}
function fundContract() public payable onlyOwner returns(uint) {
require(msg.value != 0);
emit contractFunded(msg.sender, msg.value);
return msg.value;
}
}
What am I doing wrong here ??
thanks and Regards
su.kal Crypto