Let me know once your repo is ready, I will be happy to check
contractInstance.methods.withdrawFunds(withdraw).send({from: accounts[0]});
doesn’t this mean that the money are sent from the owner address (account 0) to the contract (so the opposite of what I’d like to do)?
Nope, you are not sending money, you are just sending a transaction. You are just specifying the account no the value. send money -> {from:accounts[0], value: 100000}
contractInstance.methods.withdrawFunds(withdraw).send({from: accounts[X]});
how can I manage different players?
The accounts are taken by the provider (in your case Metamask).
If I use your dapp the accounts[0] will be my selected address in Metamask, if you use the dapp accounts[0] will be your address instead.
If you tested locally with ganache you have to change the sender address in your contract.
Hey @dan-i i have had an update on my repo… Could you run it and see where you encounter a problem?
Many thanks and Happy New ATHs today everybody.
Hey @Rob_McCourt I read the code and it seems ok.
Are you facing issues when running it? If not go to phase two, I will deploy and test it once done as that one is more complex and deserves attention
Happy coding,
Dani
thanks for your patience!
I tried to use
contractInstance.methods.withdrawFunds(withdraw).send({from: accounts[0], value: 100000});
but i get this error:
main.js:49 Uncaught ReferenceError: accounts is not defined
at HTMLButtonElement.startWithdraw (main.js:49)
at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2)
startWithdraw @ main.js:49
dispatch @ jquery-3.4.1.min.js:2
v.handle @ jquery-3.4.1.min.js:2
now i also noticed that when i try to make a bet with one account the tx shows 0 eth even though I bet 1 eth. Anyway the tx is going through normally and the balance is correct (if contract loose balance decrease of 2 eth for example)
don’t know if is related with the withdraw error
Hey @enrico
With this error message main.js:49 Uncaught ReferenceError: accounts is not defined
the compiler is telling you that accounts[0]
does not exist at that point in your code.
The library web3 helps you for sure, await web3.eth.getAccounts();
indeed returns an array of accounts (for the given provider) and from there you can use accounts[0].
You might consider to code a function such as the one below:
function getPlayerAddress () {
const playerAddr = await web3.eth.getAccounts();
if(playerAddr[0] !== undefined) {
return web3.utils.toChecksumAddress(playerAddr[0]);
}
}
Give it a try and feel free to modify / add / remove / copy code from it.
I was unable to add my screen recording but see some images and the repo url below.
https://github.com/james-davies/eth201-phase1
Added 4 ETH in migration script and you are only able to bet with a fourth of the contract balance. The reason behind it is that there should always be funds in the contract.
Hello friends this is my screenshots for my project phase 1. I had so may problems but now it works( i need to improve some code ). I learned a lotttt
My simple implementation of Flip Coin project - Part 1. Video recording and source code are available at link below:
Hi @dan-i
I get DeclarationError: Undeclared identifier.fier invocation: 0 arguments given but expected 1.
How to solve this?
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Bet is Ownable{
mapping(address => uint) playerBalance;
modifier playAmount(uint cost){
require(msg.value >= cost);
_;
}
event coinResult(bool result, uint coinFace);
function getPlayerBalance(address playerAddress) public returns(uint balance){
return playerBalance[playerAddress];
}
function addPlayerMoney(uint amount, address playerAddress) private{
playerBalance[playerAddress] += amount;
}
function withdrawPlayerMoney(uint amount, address playerAddress) private{
playerBalance[playerAddress] -= amount;
}
function flip() private returns(uint) {
return now % 2;
}
function deposit() public payable {
addPlayerMoney(msg.value, msg.sender);
}
function flipCoin(uint bet) public payable playAmount(cost) returns(bool) {
require(bet == 1 || bet ==0);
uint coinFace = flip();
bool result;
if (coinFace == bet) {
result = true;
addPlayerMoney(msg.value * 2, msg.sender);
emit coinResult(result, coinFace);
}
else {
result = false;
withdrawPlayerMoney(msg.value, msg.sender);
emit coinResult(result, coinFace);
}
}
}
You have to specify the amount to be passed to the modifier.
Example:
function flipCoin(uint bet) public payable playAmount(1 ether) returns(bool) {
I did up a straight version as described by you Filip… I did add a public donation function so if anyone wanted to they could donate to the file on Etherscan. There is no built in ownerOnly so owner has to win the dice game to claim the money.
hello,
sorry but what you said is not clear to me.
1
why the account0 doesn’t exist? it is in ganache and after the migration the balance it is updated. So the interaction works till when the contract is deployed (send eth from account 0 to contract) but it doesn’t work when i try to withdraw eth from the contract to account 0.
2
where should I add the function you gave me? what should it do?
I try to add it before the withdraw function but i get this error:
Uncaught SyntaxError: await is only valid in async function
thanks for your patience
Hi @dan-i
In the browser my deposit function does not work. I have connected metamask. How to solve this?
var web3 = new Web3(Web3.givenProvider);
var contractInstance;
$(document).ready(function() {
window.ethereum.enable().then(function(accounts){
contractInstance = new web3.eth.Contract(window.abi, "0xe257A59dBFFAf8A0032238c36b3b35E99935A94b", {from: accounts[0]});
console.log(contractInstance);
});
$("#deposit_money").click(depositMoney)
$("#play_money").click(playMoney)
$("#withdraw_money").click(withdrawMoney)
});
function depositMoney() {
var amount = $("#deposit_money").val();
contractInstance.methods.deposit().send({value: web3.utils.toWei(amount, "ether")})
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
})
}
Try to specify the sender’s account here:
contractInstance.methods.deposit().send({value: web3.utils.toWei(amount, "ether")})
Hello @dan-i
I changed the code but the buttons are not working. The entire code is on my github How to solve this?
Still got some work to go with the testing. Currently in limbo a few hours after starting development as I upgrade Ganache and now I am experiencing issues with contract storage with structs and mappings.
Also now having an issue with receiving slow promise data to console.log even though the data updates on the page (incorrectly or otherwise)
Hey @oneworldcoder
You are not importing the ABI in your index.html
If you open the console you will indeed see the error immediately.
Add <script type="text/javascript" src="./abi.js"></script>
to your html.
Then check this post I wrote few days ago about accounts.
function getPlayerAddress () {
const playerAddr = await web3.eth.getAccounts();
if(playerAddr[0] !== undefined) {
return web3.utils.toChecksumAddress(playerAddr[0]);
}
}
Happy learning