I have a question, why deposit first is needed ? Just asking. Is this for security reasons ? I made that when a bet function is called then draw happens and if there a win then in the same time money is send back to player. Do you think this solution will work ? or its better to have deposit funds first.
Hey @Spartak
That means that the function you are trying to call is not in your ABI file.
Take a look and let me know.
Happy learning
That solution will still work. However, @filip mentions in one of the videos, maybe in the Phase 2 section, that you shouldnât send the winning bet at the same time for security. He mentioned that one function should only push or pull but not both.
I believe you can start the contract with an existing deposit if you include that information in your migration files as a {value: }. (not 100% sure as I did not do it though)
For me, the deposit function is needed if someone wins all of the money.
I did it that way first also then changed it later for security. Additionally, I am also doing it in this way to make this a liquidity pool operation later.
But, Yes. To finish the Phase 1 project, paying out at the same time will work.
Good Luck with your project!
Thank you for your answer. I see now. Will also make a deposit button. I have two other questions, maybe someone can help me. @filip
- How to mock the random function to test win or lose case ? Iâm trying to create a new contract that inherit from my base contract that has a random function hardcoded as 1 for example. https://dappsdev.org/hands-on/testing/solidity-mocks/ Do think its a good solution ?
- I understand that its better to have less code then more code, to have less gas fees. I saw that people use uint as head or tail option, I have used a string âheadâ or âtailâ so its more descriptive, but then I needed to write string compare functions like.
function compareStrings(string memory a, string memory b) private view returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
function isHead(string memory side) private view returns(bool) {
return compareStrings(side, "head");
}
function isTail(string memory side) private view returns(bool) {
return compareStrings(side, "tail");
}
which solution is better here ? using uint(less readable code) or string(more readable code)
Hey @dan-i i have migrate the files again and made new ABI.
Now i am getting this error "Returned values arenât valid, did it run Out of Gas? "
Tried with different versions of web3, downloaded the new web3.min.js file, and still doesnt work. Do you know any other fixes?
Thanks in advance
Here is my submission for phase 1.
I had to convert to gif for upload. Also windows screen capture doesnt pick up the metamask window unfortunately. However you still can see the contract balance changing when I confirm transactions.
working in action:
Hey there. Hereâs a short video of my pseudo-randomness coin flip dapp:
Not sure if it is helpful for anyone else, but I was stuck for a while trying to capture the event I had set up to say whether the person won or lost. I found that using web3 with an http provider and Ganache that the event was not firing at all. Gananche was showing no events and my transaction receipt was not showing my event parameters.
So I published to Kovan and suddenly I was receiving the events back. Hopefully this is helpful for anyone who ran into this.
Hi there, I also had trouble implementing this. In the end I just gave up - you can tell whether your bet won or lost by your balance changing but it would have been nice to capture the event then display the result. I might try it on kovan testnet too.
Hey I feel you, It was many days trying to resolve this! What I did was use remix to publish my contract, then used that contract address in my web3 code. It also took me a bit to work out how to capture the event data and then parse it to get my return variable and use that for the userâs response. Let me know if you get stuck on it.
There seems to be some issues with Ganache or Truffle. This thing with the events is one problem. I also had an issue when I was running my tests. Even though Ganache and Truffle both shows my contractâs address, when the actual TESTS were being run, they would use a different (newly generated) address every time the test file was run! It was weird. I posted in several forums but nobody could explain it. I think it is an issue on my local system because others were saying this was not happening for them. But not sure how to fix it.
Here is my work in progress:
https://gist.github.com/chrisdbarnett/9e08587dbf1f21c60d722a920e47122a
There are two images of the coin - heads and tails with the user being invited to click on one of them.
The smart contract is geared up to accept an argument for which side the use clicked on but at the moment a zero is passed to it and it will just ârandomlyâ decide whether you won or lost.
Itâs hardcoded to 1 Ether a bet at the moment and uses party.js to sprinkle the page with confetti if the user wins.
The smart contract does everything in one transaction in that the user gets their payout right back at them.
The smart contract assumes the funds where there in the first place and itâs down the creator to deposit enough money to cover any bets.
I had considered doing an IOU system and having the user withdraw the funds, but I need to push on through the course.
That said, I donât want to skimp. One thing Iâm not happy about, is that I seem to be losing more than winning, so I do wonder if the smart contract has been done right.
Metamask sometimes bombs out with an error - something to do with the transaction nonce.
Please let me know if mine is a complete disaster. Iâll probably get the whole lot and upload to Github. When I have time, I will improve it further.
Chris.
Hi, Iâm stuck and could use some help. I have problem that popup from metamask is not showing when I send a transaction.
Here is the deposit function that send transaction
https://github.com/dominno/coinflip/blob/main/frontend/main2.js#L62
Here video from problem I have, not sure what is wrong. I can read from my contract, but cant send a transaction.
https://www.dropbox.com/s/v50cr5islzqnv87/2020-11-10%2011-31-53.mov
Hey @Dominik_Szopa
I deployed your contract and followed the error given by the console, give it a check:
var config = {
value: web3.utils.toWei(valueEther, "ether"),
from: web3.currentProvider.selectedAddress
};
valueEther should be a string. value: web3.utils.toWei(valueEther.toString(), "ether"),
Also there is not such a function deposit
in your contract abi.
Migrate and recreate the abi file
cheers,
Dani
@dan-i you know what is causeing this error?
âCouldnât decode uint256 from ABI: 0xâ
I am on a right network, contracts deployed, even changed web3 version also not working.
thanks in advance
Hi @filip / @dan-i I have a question and am a bit stuck.
Never created a web app before so will have to take your âJavascript Programming for Blockchain Developersâ after this.
However, I have a maxCost function that only gets the available contract funds but cant get it to display on my page.
The js code is
function displayMaxBet(){
contractInstance.methods.maxCost().call().then(function(res){
$("#maxcost_output").text(res);
console.log(res);
})
}
solidity code:
function maxCost()
public
view
returns(uint)
{
// used division so there will always be money in the contract
return contractFunds.availableBalance / 4;
}
html:
<p type="text" id="maxcost_output"></p>
The betting part, deposit and withdraw works but cant get an output for this text.
Can you see what I am doing wrong? it doesnt output to console either in my console.log(res).
/J
Thank you, all works now. Will post the final code soon.
Hey @jbo
return contractFunds.availableBalance / 4;
Why are you using a dot notation? Can you please post the whole smart contract?
Thanks!
Dani
Here are screenshots of my first dapp for the phase 1.
I will continue improving and developing my dapp with oracles⌠and I am thinking now that the âReact web developmentâ course would be also interesting at this pointâŚ
Easy use - place your bet, select head or tail and flip!
All numbers (# of win / loss, balance) are being updated real-time (after each flip and withdrawal).
Winnings can be withdrawn.
And of course there are some checks (the bet is a positive number, only owner, withdrawal to the correct account etc.)
Finished working on the project.
https://www.dropbox.com/s/9o30br9382xz4hr/2020-11-11%2022-16-04.mov
source is here
https://github.com/dominno/coinflip
Please review and let me know what to improve.
In the movie I also have a question why when I restart the genache then I need to remove accounts in metamask and import them again, can you tell why ?