Thanks Filip! that does it. Works fine!
Now, I am trying to setup dapp outside superblocks. Iām using github
to post the code and get a webpage. So I created a repository, upload
the code from superblocks (the one you get from view tab when you click
Show source) and enable metamask using the hack āweb3.currentProvider.enable()ā
that @bashlund gave us.
HTML displays fine and I can send transactions to contract. Using superblocks and a block explorer I can see the flip function works fine but my problem is that the webpage does not update balance nor display the āyou wonā or āyou lossā text. Any ideas how to fix it, please?
Here is code from superblocks and below is the error from console in chrome.
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/jquery.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/web3.min.js"></script>
<script type="text/javascript">
/* Autogenerated - do not fiddle */
if(typeof(Contracts)==="undefined") var Contracts={};
(function(module, Contracts) {
var data={
address: "0x55e205dbdc84ef9bff23c88106acc94163bc06f4",
network: "rinkeby",
endpoint: "https://rinkeby.infura.io/",
abi: [{"constant":true,"inputs":[],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"player","type":"address"}],"name":"getLastFlip","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"flip","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
};
Contracts["Coinflip"]=data;
module.exports=data;
})((typeof(module)==="undefined"?{}:module), Contracts);
(function (Contract) {
var web3_instance;
var instance;
var accounts;
// in this version try to display in html if player wins or losses
function init(cb) {
web3_instance = new Web3(
(window.web3 && window.web3.currentProvider) ||
new Web3.providers.HttpProvider(Contract.endpoint));
accounts=web3.eth.accounts;
var contract_interface = web3_instance.eth.contract(Contract.abi);
instance = contract_interface.at(Contract.address);
cb();
}
function getBalance() {
instance.getBalance(function (error, result) {
if(error){
alert(error);
}
else{
$("#balance").html(result.toString());
}
});
}
function waitForReceipt(txHash,cb){
web3_instance.eth.getTransactionReceipt(txHash, function(error, receipt){
if(error){
elert(error);
}
else if(receipt !=null){
cb(receipt);
}
else{
window.setTimeout(function(){
waitForReceipt(txHash,cb);
},5000);
}
});
}
function getResult(){
instance.getLastFlip(accounts[0],function(error, result){
if(result){
$('#result').html("You won!");
}
else{
$('#result').html("You lost!");
}
});
}
function flip(){
let val=parseInt($("#bet").val());
instance.flip.sendTransaction({from: accounts[0], gas: 100000, value: val },
function(error, txHash){
if(error){
alert(error);
}
else{
waitForReceipt(txHash, function(receipt){
if(receipt.status === "0x1"){
getResult();
getBalance();
}
else{
alert("Receipt status fail");
}
});
}
});
}
$(document).ready(function () {
init(function () {
getBalance();
});
$("#submit").click(function(){
flip();
});
});
})(Contracts['Coinflip']);
</script>
<style type="text/css">
body {
background-color: midnightblue;
color: darksalmon;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
text-align: center;
}
</style>
</head>
<body>
<h1>Coinflip Game</h1>
<p> Welcome to coinflip contract </p>
<h2>Balance: <span id="balance"></span></h2>
<input id="bet" type="number"/>
<button id="submit"> Submit </button>
<h3 id="result"> </h3>
</body>
</html>
error message
inpage.js:1 Uncaught Error: Invalid number of arguments to Solidity function
at Object.InvalidNumberOfSolidityArgs (inpage.js:1)
at u.validateArgs (inpage.js:1)
at u.toPayload (inpage.js:1)
at u.call (inpage.js:1)
at u.execute (inpage.js:1)
at getResult (home.html:71)
at home.html:92
at Object.callback (home.html:58)
at inpage.js:1
at inpage.js:1