@dan-i @filip Hey guys,
Iām making progress on the project. Iāve made and tested the solidity side using remix so I am currently at the stage connecting the contract to the dapp.
While testing interaction between the dapp and the contract, I run into a problem:
I call getBalance() from main.js in order to retrieve and display the contract balance to ensure my contract constructor indeed takes 10 eth upon deployment. This works, however when I move on to use the contractās placeBet() function, it gives me a type error saying itās not a function? I tried testing this with random() function in the contract as well simply trying to see if I can return the contractās result from random() in order to display it on the dapp but I receive the same errors.
Here are the console results when I click my Place Bet! then Refresh buttons:
![Screen Shot 2021-03-22 at 3.43.55 PM](https://forum.ivanontech.com/uploads/default/original/3X/2/1/219adadc67ab2bf44fd72c0a74fe05e7f257068b.png)
And here is my code from main.js and my betting.sol
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
var bet;
var config;
var result;
var winnings;
var losses;
$(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, "0x0d68d691a00E878fD1793F8Af858537B2F5d1112", {from: accounts[0]});
//update balance display
contractInstance.methods.getBalance().call().then(function(balance) {
$("#balance_output").text(web3.utils.fromWei(balance, "ether") + " ether");
console.log("Balance: " + balance);
})
winnings = 0;
losses = 0;
$("#win_output").text(winnings);
$("#lose_output").text(losses);
});
//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);
contractInstance.methods.placeBet().call().then(function(result) {
console.log("placeBet() called! result: " + result);
});
contractInstance.methods.random().call().then(function(result) {
console.log("random() called! result: "+ result);
});
}
//get balance from contract to display
function refresh() {
console.log("refresh button pressed");
contractInstance.methods.getBalance().call().then(function(balance) {
$("#balance_output").text(web3.utils.fromWei(balance, "ether") + " ether");
console.log("Balance: " + balance);
});
contractInstance.methods.random().call().then(function(result) {
console.log("result: " + result);
});
}```
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() == 1) {
win = true;
balance -= msg.value*2;
msg.sender.transfer(msg.value*2);
emit betPlaced(msg.value, win, balance);
return msg.value*2;
} else {
win = false;
emit betPlaced(msg.value, win, balance);
return 0;
}
}
function getBalance() public view returns (uint){
return balance;
}
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;
}
function testtest() public view returns (uint) {
return 0;
}
}
Thanks for the help!