Hey @dan-i,
So my dapp and contract are functioning based on the requirements of making and placing bets. But I have some questions as there are some extra functionalities I still want to experiment with.
-
When I place a bet using placeBet(), itās working as expected, except for what it returns. In my .sol contract, I have it defined to return 0 or double the bet value in order to keep track of the results on the frontend. However, when I try to console.log() the returned result, I get the following (screenshot). How can I write this so on the frontend I can receive a uint?
-
In my $(document).ready() function, I am able to get the metamask account balance and address, as well as the contract balance and address. I am using the refresh() function with the refresh button in order to retrieve updated account balances of both the contract and metamask accounts to check the results after placing a bet. However, this.web3.eth.getBalance() works in my document.ready() function but returns the following error when called from the refresh function. Why is this and how can I fix this?
Other than these two questions, everything is working!
Here is my code (Iāve commented out the this.web3.eth error in the refresh() for functionality for now):
main.js
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
var bet;
var config;
var result;
var winnings;
var losses;
var roundsWon;
var roundsLost;
var roundsPlayed;
var winFlag;
$(document).ready(function() {
//brings up metamask ask for permission from browser, then passes accounts in metamask to promise function
window.ethereum.enable().then(function(accounts) {
//contract instance of abi template containing function definitions, address of contract, and default sender (metamask account 0)
contractInstance = new web3.eth.Contract(abi, "0x7961a7dDde5D3B90d4FA4f8c1eA1B62DAE275Ddf", {from: accounts[0]});
//Get player's Metamask account balance and address
this.web3.eth.getBalance(accounts[0], (err, balance) => {
balance = web3.utils.fromWei(balance, "ether");
$("#metamask_balance_output").text(balance + " ETH");
console.log("Metamask account balance: " + balance + " ETH");
});
contractInstance.methods.getSenderAddress().call().then(function(address) {
$("#metamask_address_output").text(address);
console.log("Metamask account (sender) address: " + address);
});
//Get Betting contract account balance and address
contractInstance.methods.getBalance().call().then(function(balance) {
balance = web3.utils.fromWei(balance, "ether");
$("#contract_balance_output").text(balance + " ETH");
console.log("Betting contract balance: " + balance + " ETH");
});
contractInstance.methods.getContractAddress().call().then(function(address) {
$("#contract_address_output").text(address);
console.log("Betting contract address: " + address);
});
$("#round_output").text("Unplayed!");
winnings = 0;
losses = 0;
roundsWon = 0;
roundsLost = 0;
roundsPlayed = 0;
$("#winnings_output").text(winnings);
$("#losses_output").text(losses);
$("#rounds_won_output").text(roundsWon);
$("#rounds_lost_output").text(roundsLost);
$("#rounds_played_output").text(roundsPlayed);
});
//on click of # buttons, call functions
$("#bet_button").click(placeBet);
$("#refresh_button").click(refresh);
});
//on add data button click,
function placeBet() {
bet = $("#bet_input").val();
console.log("***bet button pressed. bet received from form is: " + bet + " ***");
config = {
value: web3.utils.toWei(bet, "ether")
};
result = contractInstance.methods.placeBet().send(config).then(async function(result) {
await console.log(result);
});
console.log(result);
}
//get balance from contract to display
function refresh() {
console.log("***refresh button pressed***");
/*
this.web3.eth.getBalance(accounts[0], (err, balance) => {
balance = web3.utils.fromWei(balance, "ether");
$("#metamask_balance_output").text(balance + " ETH");
console.log("Metamask account balance: " + balance + " ETH");
});
*/
//Get Betting contract account balance and address
contractInstance.methods.getBalance().call().then(function(balance) {
balance = web3.utils.fromWei(balance, "ether");
$("#contract_balance_output").text(balance + " ETH");
console.log("Betting contract balance: " + balance + " ETH");
});
contractInstance.methods.getWin().call().then(function(win) {
if(win == true) {
$("#round_output").text("You won the last round.");
console.log("Previous round win flag: " + win);
} else {
$("#round_output").text("You lost the last round.");
console.log("Previous round win flag: " + win);
}
});
}
Betting.sol
pragma solidity 0.5.12;
contract Betting {
uint public balance;
bool public win;
modifier costs(uint cost){
require(msg.value >= cost, "Minimum bet or donation is 1 ether!");
_;
}
modifier deploymentCosts(uint deploymentCost){
require(msg.value >= deploymentCost, "Deployment costs 10 ether to initialize betting pot!");
_;
}
modifier notEmpty() {
require(balance > 0, "The pot is empty! Must wait for refill or donations!");
_;
}
constructor() public payable deploymentCosts(10 ether) {
require(msg.value >= 10 ether);
balance += msg.value;
win = false;
}
event betPlaced(uint bet, bool result, uint currentBalance);
//init pot to 10 ether
function init() public payable costs(10 ether) {
require(msg.value >= 10 ether);
balance += msg.value;
win = false;
}
function placeBet() public payable costs(1 ether) notEmpty returns (uint) {
//minimum bet is 1 ether
//require(msg.value >= 1 ether);
balance += msg.value;
//if random returns 1, user wins, set flag, transfer double of sent bet
if(random() == 1) {
win = true;
balance -= msg.value*2;
msg.sender.transfer(msg.value*2);
emit betPlaced(msg.value, win, balance);
return msg.value*2;
//if random return 0, user loses, set flag, contract keeps bet
} else {
win = false;
emit betPlaced(msg.value, win, balance);
return 0;
}
}
function getBalance() public view returns (uint){
return balance;
}
function getSenderAddress() public view returns (address) {
return msg.sender;
}
function getContractAddress() public view returns (address) {
return address(this);
}
function getWin() public view returns (bool) {
return win;
}
function donate() public payable costs(1 ether) {
balance += msg.value;
}
//returns pseudo random 1 or 0
function random() public view returns (uint) {
return now % 2;
}
}