Nevermind, I figured it out! It had to do with the interference of Web3 extensions that I had installed on my chrome browser.
Could you post your code here?
Itâs pretty much what you posted in GitHub.
If itâs only pretty much the same I still need to see the code in order to figure out if there is an error in it.
Sounds good. Thanks btw for helping out.
Coinflip.sol
pragma solidity ^0.4.17;
contract Coinflip {
address owner;
mapping(address => bool) lastFlip;
function Coinflip() public{
owner = msg.sender;
}
function getBalance() constant public returns (uint){
return address(this).balance;
}
function getLastFlip(address player) constant public returns (bool){
return lastFlip[player];
}
function flip() payable public{
uint time = block.timestamp;
uint bet = msg.value;
if(time % 2 == 0){
msg.sender.transfer(bet*2);
lastFlip[msg.sender] = true;
}
else{
lastFlip[msg.sender] = false;
}
}
function deposit() payable public {
}
}
app.html
<!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>
<!-- JAVASCRIPT -->
<!-- STYLE -->
</head>
<body>
<h1>Hello World DApp</h1>
<p>Welcome to the 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>
app.css
> body { > background-color: midnightblue; > color: darksalmon; > font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; > text-align: center; > }
app.js
(function (Contract) {
var web3_instance;
var instance;
var accounts;
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(err, receipt){
if(error){
alert(err);
}
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â]);
Hello everyone, although itâs not directly related to the CoinFlip dapp, I have a question that the example doesnât explain. How do I use the âsendTranscationâ method for Solidity functions that need to receive inputs? And, more specifically, more than one input? Thanks in advance.
Thank you. I will be trying to debug this as soon as I have time. Iâm sorry that it has taken some time. Iâm doing my best
If we take the coinflip function as an example, you would add two arguments like this
instance.flip.sendTransaction(arg1, arg2, {from: accounts[0], gas:100000, value: val}, function(error, txHash)
I tried this already, so the problem is another one. Could I email you with the specific code iâm trying to work on?
I donât get any alerts - no alert about fail and no alert with the receipt even though my code is the same as in filipâs github, what can I do?
Send me a PM here on toshitimes and we can connect
Iâm looking into it. Will get back. Sorry for the slow response. I have been abroad.
Sorry for the delay, I have been traveling. I found the error. It was a very simple one, haha.
At the very bottom of app.js you write
})(Contracts[âCoinflipâ]);
The quotes around Coinflip is of the wrong type. You need to use â instead of â. The compiler canât interpret the later one. So the last line of app.js should be
})(Contracts['Coinflip']);
Wow, of course it would be that small.
Thanks!! Will try it now!
: https://github.com/filipmartinsson/Solidity/tree/master/Dapps/Coinflip%20dapp%20part5;
Hi. Fillip. Using the link and your video tutorials, the final part in my superblocks - the deposit function is missing. The rest like getLastFlip, getBalance and flip is present while doing the interact. Where did I go wrong?
Thanks for your help.
.
What does your solidity code look like? Where are you missing the deposit function, in the interact tab?
Hi, Filip. Itâs okay now. I checked with your summary found some difference and missing",;".
Thanks:
I try to make the dogcontract into DAPP, but some problem happened. I run the addDog function, metamask popup, but I canât add dog. Please help me
dogContract.sol
function addDog(string _name, uint _age) payable public {
uint id = dogList.push(Dogs(_name, _age));
ownerToDog[msg.sender] = id;
}
app.js
function addDog(){
var name = $("#dogName").val();
var age = $("#dogAge").val();
instance.addDog.sendTransaction(name, age, {from: accounts[0], gas:100000, value:100}, function(error, txHash){
if(error){
alert(error);
}
else{
waitForReceipt(txHash, function(receipt){
if(receipt.status === "0x1"){
alert("Dog is added successfully.");
}
else{
alert("Dog is failed to add.");
}
});
}
})
}
I see one error on the line
uint id = dogList.push(Dogs(_name, _age));
Dogs should be Dog. At least if the rest of your code is the same as mine. Meaning the struct is called Dog.
Hi Filip!
I had the same problem: Everythig works, except for the âyou wonâ and âyou lostâ part.
It was not working in my own intepretation of the code so I copied the whole code of yours (from all the files, .html, .js and .sol)
It still shows the same, everything works, except for the âyou wonâ/âyou lostâ part - with the code from gitHub
Any ideas?