Programming Project - Phase 1

I would need to know what function you’re trying to call when getting the error.
Please give me some context and I will look into it.

1 Like

Hey @enrico

It is correct, you have to convert (or declare) the address as payable in order to send money to it.

You can declare an array of payable addresses: address payable [] users;

Or convert the address to address payable: address(uint160(users[0])).transfer(0.1 ether);

In Solidity 0.6 or > you can cast an address to payable just by doing payable(address).

Happy learning,
Dani

2 Likes

It is happening when i try to execute the doCoinFlip function, metamask opens and that message appears , if i confirm the transaction i get an error like that.

RPC Error: Error: [ethjs-rpc] rpc error with payload {“id”:1642721289067,“jsonrpc”:“2.0”,“params”:[“0xf893308504a817c800836170d4947eefc63a9321d28edc6dbb06ba3e2ee64d557e9d880de0b6b3a7640000a4e4959fd40000000000000000000000000000000000000000000000000000000000000000822d46a012c33a8fd17cdc61cc7cc47b3c607a74d945217230d32df52a1d2ca15cb2a96ca0224452c62afca7888449967bdcb6fa33a310c4e8a6772d64437e2fa64929b6f8”],“method”:“eth_sendRawTransaction”} [object Object] {code: -32603, message: “Error: [ethjs-rpc] rpc error with payload {“id”:16…method”:“eth_sendRawTransaction”} [object Object]", stack: “Error: Error: [ethjs-rpc] rpc error with payload {…method”:“eth_sendRawTransaction”} [object Object]"}
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
c @ inpage.js:1
(anonymous) @ inpage.js:1
Bt @ inpage.js:1
(anonymous) @ inpage.js:1
_runReturnHandlersUp @ inpage.js:1
(anonymous) @ inpage.js:1
n @ inpage.js:1
i @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
n @ inpage.js:1
i @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
c @ inpage.js:1
u @ inpage.js:1
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:1
a @ inpage.js:1
setTimeout (async)
(anonymous) @ inpage.js:15
write @ inpage.js:15
b @ inpage.js:15
(anonymous) @ inpage.js:15
v.write @ inpage.js:15
y @ inpage.js:15
h @ inpage.js:8
s.emit @ inpage.js:8
_ @ inpage.js:15
w @ inpage.js:15
b.push @ inpage.js:15
_write @ inpage.js:15
b @ inpage.js:15
(anonymous) @ inpage.js:15
v.write @ inpage.js:15
y @ inpage.js:15
h @ inpage.js:8
s.emit @ inpage.js:8
_ @ inpage.js:15
w @ inpage.js:15
b.push @ inpage.js:15
o._onMessage @ inpage.js:15
postMessage (async)
o._write @ contentscript.js:15
b @ contentscript.js:15
(anonymous) @ contentscript.js:15
v.write @ contentscript.js:15
y @ contentscript.js:15
h @ contentscript.js:8
s.emit @ contentscript.js:8
_ @ contentscript.js:15
w @ contentscript.js:15
b.push @ contentscript.js:15
_write @ contentscript.js:15
b @ contentscript.js:15
(anonymous) @ contentscript.js:15
v.write @ contentscript.js:15
y @ contentscript.js:15
h @ contentscript.js:8
s.emit @ contentscript.js:8
_ @ contentscript.js:15
w @ contentscript.js:15
b.push @ contentscript.js:15
_write @ contentscript.js:15
b @ contentscript.js:15
(anonymous) @ contentscript.js:15
v.write @ contentscript.js:15
y @ contentscript.js:8
h @ contentscript.js:8
s.emit @ contentscript.js:8
_ @ contentscript.js:8
w @ contentscript.js:8
b.push @ contentscript.js:8
o._onMessage @ contentscript.js:8
web3.min.js:1 Uncaught (in promise) {code: -32603, message: “Error: [ethjs-rpc] rpc error with payload {“id”:16…method”:“eth_sendRawTransaction”} [object Object]", stack: “Error: Error: [ethjs-rpc] rpc error with payload {…method”:“eth_sendRawTransaction”} [object Object]"}

This is my main.js file:

var web3 = new Web3(Web3.givenProvider);
var contractInstance;

$(document).ready(function() {
	window.ethereum.enable().then(function(accounts) {
		contractInstance = new web3.eth.Contract(abi, "0x7EeFC63a9321d28EdC6dbB06Ba3E2Ee64D557E9D", {from: accounts[0]});
		console.log(contractInstance);
		
	});
	$("#add_data_button").click(inputData);
	$("#toggle_button").click(fetchAndChangeFace);
	$("#fillup_button").click(fillUpp);

});


function inputData(){
	var betHead;
	if( $("coinface_input").text() === "Head" ) {
		betHead = true;
	}
	else {
		betHead = false;
	}

	var betSize = $("#betSize_input").val();
	var config = { value: web3.utils.toWei(betSize,"ether")};

	contractInstance.methods.doCoinFlip(betHead).send(config)
	.on("transactionHash", function(hash){
		console.log(hash);
	})
	.on("confirmation", function(confirmationNr){
		console.log(confirmationNr);
	})
	.on("receipt", function(receipt){
		console.log(receipt);
	})
	contractInstance.events.gamePlayed(function (error,result){
		console.log(result);
		console.log("Here it is" + " " + result.returnValues[0])
		console.log("events picked up!!")
	})
	eventListener();
}

function eventListener(){
	contractInstance.events.gamePlayed(function (error,result){
		let playb = result.returnValues[0];
		let resultb = result.returnValues[1];
		let win = result.returnValues[2];
		let payout = result.returnValues[3];
		placeResult ( playb, resultb, win, payout);
	})
}


function placeResult(playb, resultb, win, pay){
	let playbet = playb;
	let resultbet = resultb;
	let winning = win;
	let payout = pay;

	if( playbet === true ){
		$("#youbet_output").text("Head");
	}
	else {
		$("#youbet_output").text("Tail");
	}
	if(resultbet === true ){
		$("#result_output").text("Head");
	}
	else{
		$("#result_output").text("Tail");
	}
	if(winning === true ){
		$("winloose_output").text("You won!");
	}
	else{
		$("winloose_output").text("You lost!");
	}
	$("#payout_output").text(web3.utils.fromWei(payout, "ether"));
}

function fetchAndChangeFace(){
	let face = $("#coinface_input").text()
	console.log("coinface: " + face);
	if( face === ""){
		$("#coinface_input").text("Head");
	}
	else if (face === "Head"){
		$("#coinface_input").text("Tail");
	}
	else {
		$("#coinface_input").text("Head");
	}
}

function fillUpp(){

	var betSize = $("#fillupp_input").val();
	var config = { value: web3.utils.toWei(betSize,"ether")};

	contractInstance.methods.incBalance().send(config)
	.on("transactionHash", function(hash){
		console.log(hash);
	})
	.on("confirmation", function(confirmationNr){
		console.log(confirmationNr);
	})
	.on("receipt", function(receipt){
		console.log(receipt);
	})
}

@filip

Hello! I developed the coin flip contract (COIN.sol) which I tried in remix where it works.

When I try to make the dapp I get some errors and doubts:

  1. i have the website COIN.html and in the main.js I call the functions of COIN.sol

I’ve defined the function which save the bet amount and the bet (0 or 1) in the following way:

  function setBet(uint amount, uint flip) public payable costs( (players[msg.sender].amount)*1000000000000000000 )

when I click the button which should start the setBet function it gives me this error:

Uncaught Error: Invalid number of parameters for "setBet". Got 2 expected 1! at Object.InvalidNumberOfParams (web3.min.js:1) at Object.o._createTxObject (web3.min.js:1) at HTMLButtonElement.inputData (main.js:23) at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2) at HTMLButtonElement.v.handle (jquery-3.4.1.min.js:2) InvalidNumberOfParams @ web3.min.js:1 o._createTxObject @ web3.min.js:1 inputData @ main.js:23 dispatch @ jquery-3.4.1.min.js:2 v.handle @ jquery-3.4.1.min.js:2

I don’t get why because the setBet function has two parameters.

  1. setBet() should send the amount defined from the player to the contract. in remix I’ve defined this as follow and it works:
(players[msg.sender].amount)*1000000000000000000

where players is a mapping which is related to the amount played from the player.

I’m not sure how to set the amount to send in the main.js file. I did it like this. Is it correct?

contractInstance.methods.setBet(amount, flip).send((players[msg.sender].amount)*1000000000000000000)

I’ve added in github my project (COIN folder for the contract and COINDAPP for the dapp): https://github.com/enricofabris/betContract

thank you :slight_smile:

Hey guys. I’m having some difficulty finding clear instructions on how to pass values from Javascript to my smart contract. I’m trying to pass values from a JS object into a Solidity struct. Can Solidity listen for Javascript events? Can anyone here point me to some documentation that explains this clearly?

Hey there,
it would be amazing if you could check out my version of heads or tails.
https://youtu.be/dvaXf9uF7Bo
M

1 Like

After long summer break I finally managed to make everything work as I imagined… To make this random true/false thing a little bit more interesting… instead of flipping coin, in my Dapp, you bet on fight between Mortal Kombat characters; Scorpion and SubZero.
So… here are some screen captures of MK Dapp in action… Unfortunately, in these gifs you cannot hear announcer when you choose a fighter, start or win the fight, but it’s there :wink:

Bet on Scorpion:


Bet on SubZero:

Also, I’d like to thank all of you who helped me, knowingly and unknowingly, when I got stuck…I learned a lot, especially how much I don’t know :sweat_smile: Cheers!

4 Likes

I have no clue where to start. Should i use the template to help me or make everything from scratch? What should i code first? Stressed because i don’t remember how to do everything :frowning:

That’s so amazing :smiley: I have no idea how to even start ahaha

I don’t know why this is happening but I can’t for the life of me get web3 working with my site. I even tried downloading a few other students’ projects to see if I was doing something wrong and Metamask wasn’t firing on theirs either. What am I doing wrong here?

good lord!!! I’ve been on this for like a week… I’m so lost LOL

@filip @dan-i @MrGanD

Hey guys., I’m running into another error. On my webpage when I tr and execute a function, say flip coin, I get a typeError in my main.js file

“main.js:49 Uncaught TypeError: contractInstance.methods.fund is not a function”

and yet in my conttract the above is declared as a function.

share your main.js file

It took me longer than anticipated but learned a lot on how to make a simple webpage and how to integrate it with a smart contract :smiley:.

This is result:

1 Like

Can someone help me with the coding for the flip resulting in sending double the input amount, kind of stuck :frowning: https://github.com/lucusra/CoinToss.

1 Like

For some reason when I call the placeBet function the playerWin result is always false. What am I missing here?

It works when I step thru the functions one by one on remix but when I try to call them one after another in the placeBet function it breaks. Guessing it’s because I’m using the state variables before they’re actually being updated by the other functions. Just not sure how to fix it.

Hey @Spartak

In order to fix this error you have to reset Metamask:

Settings -> Advanced -> Reset Account.

Schermata 2020-10-21 alle 11.12.30

Happy coding,
Dani

1 Like

Is there any other way of doing this?
require(_setBet == 0 || 1, "please choose either 1 or 0")
I noticed I’m unable to use the || operator :frowning:
@dan-i

Ciao Enrico,

I looked at your code.
In your migration file 2_COIN_deploy.js you are migrating COIN, while you contract is called Helloworld.
Modify your migration as follows:


module.exports = function(deployer) {
  deployer.deploy(HelloWorld,{value:2000000000000000000});
};

Migrate your project and take the correct ABI.

Will work perfectly :slight_smile:

Happy learning,
Dani

2 Likes

Well done @MrGanD , post you code once you finish phase two!

Keep going,
Dani

1 Like