hello! I was able to go further with my dapp. At this point is completed and it seem to work.
The only thing I can’t get to work is this:
after the bet I’d like to get on the page the new value of Balance for example without the need to refresh.
could you help me to get what is wrong?
for example in main.js file I define the getBalance() function
function getBalance(){
contractInstance.methods.balance().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#balance_output").text(res);
$("#max_bet").text(res/2.5);
})};
and then I call it after the bet function:
function inputData(){
var config = {
value: web3.utils.toWei("1", "ether")}
var amount = $("#amount").val();
contractInstance.methods.setBet(amount, bet).send({value: web3.utils.toWei(amount, "ether")})
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
alert("done");
})
getBalance()
}
but still it doesn’t update automatically on the page but only after refresh.
this is the main.js
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
$(document).ready(function() {
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(abi, "0x6623444594c3555F9652AD1ed273f8317a55b35f", {from: accounts[0]});
console.log(contractInstance);
getBalance()
getPlayerBalance()
getTotGames()
getTotMoneyBetted()
getGamesWon()
getMoneyWon()
getMinimumBet()
});
$("#bet0").click(function(){bet = 0;})
$("#bet1").click(function(){bet = 1;})
$("#confirm").click(inputData)
$("#get_data_button").click(fetchAndDisplay)
$("#withdraw").click(startWithdraw)
$("#withdrawPlayer").click(startWithdrawPlayer)
$("#deposit").click(startDepositFunds)
$("#set_minimumBet").click(setMinimumBet)
$("#getAddress").click(startGetAddress)
//$("#get_balance_button").click(function(){
//let res = contractInstance.methods.balance().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
//$("#balance_output").text(res);})});
});
function getTotGames(){
contractInstance.methods.TotGames().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#TotGames").text(res);
})};
function getTotMoneyBetted(){
contractInstance.methods.TotMoneyBetted().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#TotmoneyBetted").text(res);
})};
function getGamesWon(){
contractInstance.methods.TotGamesWon().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#TotGamesWon").text(res);
})};
function getMoneyWon(){
contractInstance.methods.TotMoneyWon().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#TotMoneyWon").text(res);
})}
function getBalance(){
contractInstance.methods.balance().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#balance_output").text(res);
$("#max_bet").text(res/2.5);
})};
function getMinimumBet(){
contractInstance.methods.minBet().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#min_bet").text(res);
})};
function getPlayerBalance(){
contractInstance.methods.getResult().call().then(function(res){ // quando uso un GETTER FUNCTION non devo creare un tx che poi deve essere minata, la blockchain non deve essere cambiata
// in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#playerBalance_output").text(res.playerBalance);
})}
function startDepositFunds(){
var deposit = $("#depositAmount").val();
contractInstance.methods.depositFunds(deposit).send({value: web3.utils.toWei(deposit, "ether")})
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
})
getBalance()
}
function startWithdraw(){
var withdraw = $("#withdrawAmount").val();
contractInstance.methods.withdrawFunds(withdraw).send()
.on("transactionHash", function(hash){ //istanza del contratto. methods(funzioni). funzione createPerson
console.log(hash); // .send dice di firmarla con metamask e inviarla al contratto all'indirizzo dentro contract instance
}) // in config specifico che devo mandare 1 ether per createPerson
.on("confirmation", function(confirmationNr){ // .on() funzione che quando riceve tx hash esegue una funzione (quando ci da txhash, block confirmation, receipt = quando tx è messa in un blocco per la prima volta)
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
alert("done");
})
getBalance()
};
function startWithdrawPlayer(){
var withdraw = $("#withdrawPlayerAmount").val();
contractInstance.methods.withdrawFundsPlayer(withdraw).send()
.on("transactionHash", function(hash){ //istanza del contratto. methods(funzioni). funzione createPerson
console.log(hash); // .send dice di firmarla con metamask e inviarla al contratto all'indirizzo dentro contract instance
}) // in config specifico che devo mandare 1 ether per createPerson
.on("confirmation", function(confirmationNr){ // .on() funzione che quando riceve tx hash esegue una funzione (quando ci da txhash, block confirmation, receipt = quando tx è messa in un blocco per la prima volta)
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
alert("done");
})
getPlayerBalance()
}
function setMinimumBet(){
var config = {value: web3.utils.toWei("1", "ether")} // definisco config del valore di 1 ether
var amount = $("#minBetAmount").val();
contractInstance.methods.minimumBet(amount).send() // metto input dentro funzione del CONTRATTO
.on("transactionHash", function(hash){ //istanza del contratto. methods(funzioni). funzione createPerson
console.log(hash); // .send dice di firmarla con metamask e inviarla al contratto all'indirizzo dentro contract instance
}) // in config specifico che devo mandare 1 ether per createPerson
.on("confirmation", function(confirmationNr){ // .on() funzione che quando riceve tx hash esegue una funzione (quando ci da txhash, block confirmation, receipt = quando tx è messa in un blocco per la prima volta)
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
alert("done");
})
getMinimumBet()
}
function startGetAddress(){
contractInstance.methods.TotUniquePlayers().call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#unique").text(res)})
var config = {value: web3.utils.toWei("1", "ether")} // definisco config del valore di 1 ether
var array = $("#address").val();
contractInstance.methods.getAddress(array).call().then(function(res){ // in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#addressResult").text(res);
contractInstance.methods.getStatistics(res).call().then(function(res){
$("#playerGames").text(res.games);
$("#playerMoneyBetted").text(res.moneyBetted);
$("#playerGamesWon").text(res.gamesWon);
$("#PlayerMoneyWon").text(res.moneyWon);
})
})
}
function inputData(){
var config = {
value: web3.utils.toWei("1", "ether")}
var amount = $("#amount").val();
contractInstance.methods.setBet(amount, bet).send({value: web3.utils.toWei(amount, "ether")})
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
alert("done");
})
getBalance()
}
function fetchAndDisplay(){ // deve prendere i dati dalla blockchain e poi deve mostrarli
contractInstance.methods.getResult().call().then(function(res){ // quando uso un GETTER FUNCTION non devo creare un tx che poi deve essere minata, la blockchain non deve essere cambiata
// in una blockchain vera createPerson è lenta, e getPerson è veloce
$("#amount_output").text(res.amount);
$("#bet_output").text(res.bet);
$("#message_output").text(res.message);
$("#result_output").text(res.result);
$("#playerBalance_output1").text(res.playerBalance);
$("#yourGames").text(res.games);
$("#yourMoneyBetted").text(res.moneyBetted);
$("#yourGamesWon").text(res.gamesWon);
$("#YourMoneyWon").text(res.moneyWon);
getBalance()
getPlayerBalance()
getGamesWon()
getMoneyWon()
})
}
this is the html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>People</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script type="text/javascript" src="./web3.min.js"></script>
<script type="text/javascript" src="./abi.js"></script>
<script type="text/javascript" src="./main.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">Welcome to Coinchain</h1>
<p class="lead">Make your bet safely through blockchain </p>
</div>
</div>
<div class="container">
<div>
Total games played: <span id="TotGames"></span>
</div>
<div>
Total money betted: <span id="TotmoneyBetted"></span>
</div>
<div>
Total wins: <span id="TotGamesWon"></span>
</div>
<div>
Total money won: <span id="TotMoneyWon"></span>
</div>
<br>
<br>
<h2>CONTRACT INFOS</h2>
<div>
Contract balance: <span id="balance_output"></span>
</div>
<div>
Maximum Bet: <span id="max_bet"></span>
</div>
<div>
Minimum Bet: <span id="min_bet"></span>
</div>
<div>
Player balance: <span id="playerBalance_output"></span>
</div>
<br>
<div>
<h2>Set your stake</h2>
<div class="input-group mb-3">
<input type="text" class="form-control" id="amount" placeholder="Set your stake" >
<input type="button" value="0" id="bet0" placeholder="0" >
<input type="button" value="1" id="bet1" placeholder="1" >
</div>
<button type="button" id="confirm" class="btn btn-primary">CONFIRM AMOUNT</button>
<div class="container">
<p class="lead">Make your bet safely through blockchain </p>
</div>
<div>
<h2>LAST BET</h2>
<div>
Amount: <span id="amount_output"></span>
</div>
<div>
What did you bet: <span id="bet_output"></span>
</div>
<div>
YOU: <span id="message_output"></span>
</div>
<div>
coin flip was: <span id="result_output"></span>
</div>
<div>
Your balance: <span id="playerBalance_output1"></span>
</div>
<h2>YOUR STATISTICS</h2>
<div>
Games played: <span id="yourGames"></span>
</div>
<div>
Money betted: <span id="yourMoneyBetted"></span>
</div>
<div>
Games won: <span id="yourGamesWon"></span>
</div>
<div>
Money won: <span id="YourMoneyWon"></span>
</div>
<button type="button" id="get_data_button" class="btn btn-primary">Get Data</button>
</div>
<br>
<div>
<input type="text" class="form-control" id="withdrawPlayerAmount" placeholder="amount to withdraw Player" >
<button type="button" id="withdrawPlayer" class="btn btn-primary">WITHDRAW PLAYER</button>
</div>
<br>
<div>
<h2>PLAYER STATISTICS</h2>
<div class="input-group mb-3">
<input type="text" class="form-control" id="address" placeholder="Input address" >
<button type="button" id="getAddress" class="btn btn-primary">GET ADDRESS</button>
</div>
<div>
Number of unique players: <span id="unique"></span>
<br>
Address: <span id="addressResult"></span>
</div>
<div>
Games: <span id="playerGames"></span>
</div>
<div>
Money betted: <span id="playerMoneyBetted"></span>
</div>
<div>
Games won: <span id="playerGamesWon"></span>
</div>
<div>
Money Won: <span id="PlayerMoneyWon"></span>
</div>
<br>
<div class="input-group mb-3">
<input type="text" class="form-control" id="minBetAmount" placeholder="Set minimum bet" >
<button type="button" id="set_minimumBet" class="btn btn-primary">SET MINIMUM BET</button>
</div>
<br>
<div>
<input type="text" class="form-control" id="depositAmount" placeholder="amount to deposit" >
<button type="button" id="deposit" class="btn btn-primary">DEPOSIT FUNDS</button>
</div>
<br>
<div>
<input type="text" class="form-control" id="withdrawAmount" placeholder="amount to withdraw" >
<button type="button" id="withdraw" class="btn btn-primary">WITHDRAW</button>
</div>
<br>
</body>
</html>
thanks a lot!