First, thanks @lucas for taking a look at my dapp, the event listener finally works
Here is my version:
The repo doesnât contain node modules folder as it is huge, and the .secret file.
When the page is first loaded, it makes sure that Metamask is connected and brings it up if it is not.
When a user enters the desired amount and presses BET, a transaction is created:
On confirmation, the user gets an alert:
In Ropsten Etherscan we can see the received transaction:
Under Internal Txns, we can see that the contract has made a request to the oracle:
When the contract receives a random number, the user gets an alert:
The same message is added to the table body:
Looks like I am lucky today
In Etherscan, we can see that the oracle has responded and the user has been paid:
As the owner, I can also check the new balance of the contract:
As defined in Coinflip_deploy.js, 0.1 ETH is deposited when deploying. There is a deposit function to increase balance, and it works
The withdraw, though, doesnât work. I donât want to make it âwithdraw allâ, because that wouldnât be optimal in real life. We want to withdraw profit, but keep some balance to continue operating.
To do that, my withdraw function takes an argument:
It works fine when I just enter the amount in wei and pass it to the function like that.
If, however, I try this:
let withAmount = $("#withdraw").val();
let withConfig = Number({value: web3.utils.toWei(withAmount,"ether")});
I get this:
And, if I try this:
let withAmount = $("#withdraw").val();
let withConfig = Number(withAmount*1e18);
I get this:
Thanks @dan-i for pointing out that I didnât convert it to number before, but apparently I am still passing a wrong data type.
Other improvement points:
-
The contract doesnât take a new bet from address until the current one is fully executed. If, however, the player tried to place another bet, they just get an error in MetaMask. It would be nice to create a listener for pendingBet variable and bring up an alert, but I didnât really figure it and I have already been stuck on this project for some time
-
The same issue applies if the bet amount is too high.
-
When a player refreshes the webpage, the win/lose history is cleared.
-
If the player is not the owner, they shouldnât see the owner buttons, but I havenât looked for a solution to that yet.