Hi @loksaha1310,
Thanks for reaching out!
The project really looks amazing.
When I clicked on the GitHub link I could only see README.md file.
There is no code source to review.
Hi @loksaha1310,
Thanks for reaching out!
The project really looks amazing.
When I clicked on the GitHub link I could only see README.md file.
There is no code source to review.
@PsiMach
Check this out:
function withdrawAll(uint toTransfer) public returns(uint) {
msg.sender.transfer(toTransfer);
return toTransfer;
}
You will need to pass amount as parameter while calling withdrawAll()
That is why you didn’t see the transfer happening
references: https://ethereum.stackexchange.com/questions/29234/how-to-create-withdrawal-function-from-my-contract
@CodyM
Great going!
Just check the require statement.
require(balance[msg.sender] >= msg.value, "The users bet cannot be more than users balance");
To send funds to the contract, you need a payable fallback function.
source: https://solidity.readthedocs.io/en/v0.5.3/contracts.html#fallback-function
Check out this example:
//payable fallback function
function() external payable {
}
//your flipCoin function
function flipCoin () public payable costs(100 wei) {
require(msg.value <= (houseBalance / 3), "The bet amount cannot exceed the payable jackpot!");
if (random() == 0) {
//Winning
result = true;
reward = msg.value;
msg.sender.transfer(reward * 2);
} else {
result = false;
//address(this) give the address of the current contract
address(this).transfer(msg.value*2)
}
houseBalance -= reward;
emit FlipCoinResult(msg.sender, result, reward);
}
As you can see this needs to be done with the contract address.
Meanwhile, you can concentrate on one solution and try to get the output.
Generally, we use function modifiers where it is expected to use same validations at two or more functions.
For example the onlyOwner
modifier.
There will be quite a few functions which can be only called by the owner of the contract hence the name.
This will give you some confidence and you will also see some progress
Happy coding.
Ahh ok! Your explanations are very easy to understand.
I think I can start writing contract codes now.
Thank you so much @gabba!
Thank you Taha,
I tied your suggestion but I’m receiving an error trying to get the function to accept the parameter as below
Here is the code in main.js
function WithdrawFunds() {
var depositAmount = $("#WithdrawalAmount_input").val();
var ether = web3.utils.toWei(String(depositAmount),"ether");
alert("Ether: " + ether);
contractInstance.methods.withdrawAll(ether).call().then(function (res){
var etherAmount = web3.utils.fromWei(res, "ether");
alert("Fund have been withdrawn: " + etherAmount);
});
}
Additionally here is the html from which this code pulls
<div class="input-group mb-3">
<input type="number" class="form-control" id="WithdrawalAmount_input" placeholder="Withdrawal">
</div>
I’ve confirmed via the alert that a parameter is being sent. I have also tried putting both a number and a string directly into the call.
Any ideas on what I might be doing wrong here?
O sorry sorry, I forgot to mention:sweat_smile:, I put the code files in the other branch. In the new_branch you can see the folder Ethler1.0 . Have put all the files there. I could better mention it in the readme! My bad:sweat_smile:
Hey @Taha – thanks again for the reply.
So would I need to poll web3.eth.getAccounts
in order to keep an up-to-date view of the accounts?
Is there some sort of “push” mechanism, like events, that would allow my dapp to know that the account(s) have changed?
Thanks,
David
Thanks @Taha
I’ve continued widdling away at phase 1 and this is my current code and where I am now stuck
*If the user loses how do I send users bet funds to the contract? Do we need to create a “house” account/address or can this be done using the address of the contract?
*Also, having a hard time grasping address(this) I cant seem to find this clearly explained. Is address(this) = to the contract address. If so how do i convert it to payable so i can send funds to it?
Code below: thank you for your assistance!
pragma solidity 0.5.12;
import "./SafeMath.sol";
import "./Ownable.sol";
contract CoinFlip is Ownable {
//mapping (address => uint) public balance;
uint contractBalance = address(this).balance;
function getContractBalance() public view returns(uint) {
return contractBalance;
}
function flipCoin (uint) public payable returns(uint) {
address bettingUser = msg.sender;
require(bettingUser.balance >= msg.value, "The users bet cannot be more than the users balance");
if (random() == 0) {
//Winning
uint win = SafeMath.mul(msg.value, 2);
msg.sender.transfer(win);
} else {
address(this).transfer(msg.value);
}
}
function () external payable { }
function withdrawAll() public onlyOwner returns(uint) {
uint toTransfer = contractBalance;
contractBalance = 0;
msg.sender.transfer(toTransfer);
return toTransfer;
}
function random() private view returns (uint) {
return block.timestamp % 2;
}
}
Lol just launched this contract and this is basically useless as is. the flipCoin function doesn’t require any ether be sent. I need to keep adding to this…
PROGRESS!!! @Taha
@loksaha1310 thanks for posting your github link, this was super helpful to get me on the right track!
Here is my current iteration. I may try to add some more features just to gain some more practice. Now onto the webpage and then the web3 implementation!
I do have one question about the costs modifier that is created, why is the _; needed at the end? I haven’t seen this until today.
pragma solidity 0.5.12;
import "./Ownable.sol";
contract CoinFlip is Ownable {
bool win;
uint public houseBalance = 10 ether;
uint reward;
modifier costs(uint value) {
require(msg.value >= value);
_;
}
function getContractBalance() public view returns(uint) {
return houseBalance;
}
function flipCoin () public payable costs(0.0001 ether) {
if (random() == 0) {
//Winning
win = true;
reward = msg.value;
msg.sender.transfer(reward * 2);
}
else {
win = false;
reward = -msg.value;
}
houseBalance -= reward;
}
function withdrawAll() public onlyOwner returns(uint toTransfer) {
toTransfer = houseBalance;
houseBalance = 0;
msg.sender.transfer(toTransfer);
return toTransfer;
}
function random() private view returns (uint) {
return block.timestamp % 2;
}
}
Hi @Daveed
Great job!
It will be nice if you could submit the complete assignment with Oracle API, will be better for review purpose.
Thanks
@CodyM
You are doing great progress with this project and I can clearly see you are going in the right direction.
Since, this is the test assignment its better you solve it first and then we give you guidance/hints for the best approach
@CodyM
Good to see you progress!
_;
basically means once the modifier conditions are satisfied, the remaining function should execute as programmed.
Try this:
function WithdrawFunds() {
var depositAmount = $("#WithdrawalAmount_input").val();
var ether = web3.utils.toWei(String(depositAmount),"ether");
alert("Ether: " + ether);
contractInstance.methods.withdrawAll(ether).then(function (res){
var etherAmount = web3.utils.fromWei(res, "ether");
alert("Fund have been withdrawn: " + etherAmount);
});
}
methods.myMethod.call()
is used for “constant” methods and not for calling different parameters.
Also try another browser.
Let me know
source: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-call
Yes!
There is no in-built “push” mechanism, if you need it you can try events
or using web3
as mentioned previously
@loksaha1310
You have written your smart contract nicely and can you also integrate oracle in your contract?
Yes,I’m trying to do that now😅
Yes, I will be turning that in this week.
Hi @Taha I’ve completed phase 1!
The only feature I wanted to add that I am not sure how to do in web3 is to use my FlipCoinResult event status, which is a boolean which is true when the user wins and false if they lose, to trigger a message saying you won or you lost. If you could point me in the right direction on that it would be awesome. Also here is a github link to my code for review. Thanks for all your assistance/input!