Programming Project - Phase 2

Just completed my Flip Dapp 2.0.
Video : https://drive.google.com/file/d/1xNGJrG-X9nLmm3KBuePkLTqctclWgE1j/view?usp=sharing
Github: https://github.com/kabelSalat17/flipDapp2.0
I’m open to any suggestions or feedback.

Thanks in advance

Matteo

1 Like

I have found a resolution to the issue of Contract balance and internal balance difference. However, I don’t really like it. I haven’t found the root of the problem. I don’t know where goes 0.004 Ether each time I execute the betting function.

Below I have a solution I have found

function coinflipSet() public payable costs(10000000000 wei){
    require(isPlaying[msg.sender] == false, "Current address is in paly");
    require(msg.value >= 100000000000 wei);
    require(msg.value <= balance);
    balance -= 4000000000000000;
    isPlaying[msg.sender] = true;
    uint256 betBalance = msg.value;
    emit coinFlipped("CoinflipSet() function successfuly executed");
    update(betBalance, msg.sender);
  }

As you can see I am simply subtracting 0.004 Eth each time the function coinflipSet() is executed. Are there two different balances? I still can’t figure out why my balance argument inside the Coinflip contract was different from the balance shown on the Etherscan.

https://github.com/RostyslavDzhohola/Coinflip-test-Project

This problem had been solved, but I don’t know how? After a couple of days, it just disappeared.

1 Like

I really like the idea of it being against a liquidity pool. You have inspired me to attempt implementing that in my own dApp. I am also a fan of having a non-centralized source of random numbers but unfortunately I cannot comment on how effective your implementation is… I will have to look at things more to understand better. Thanks for posting your ideas. :+1:

1 Like

Hi,

I was trying to migrate my contract but I’m not sure why the compiling process just stops there. I couldn’t update the js file because no contract address is generated…

Try this: truffle migrate --reset --network ropsten

1 Like

This is where the 4 finney is going. If you want to make it more clear in your contract, assign the value to a variable name that makes sense and deduct that so that anyone reading can make sense of what you are deducting and why…
e.g.

function coinflipSet() public payable costs(10000000000 wei){
    require(isPlaying[msg.sender] == false, "Current address is in paly");
    require(msg.value >= 100000000000 wei);
    require(msg.value <= balance);
    uint costOfOracleQuery = 4000000000000000;    //obviously the name is quite long, but just using this as example
    balance -= costOfOracleQuery;   //then this line makes sense to anyone reading your code
    isPlaying[msg.sender] = true;
    uint256 betBalance = msg.value;
    emit coinFlipped("CoinflipSet() function successfuly executed");
    update(betBalance, msg.sender);
  }

…Also if you read the documentation (linked in my earlier reply) you will see a function to get the price of the query instead of just setting a fixed amount of 0.004 eth like you have done (which may result in errors in the future if the oracle price ever changes for some reason)…

Anyways, looks like you’ve got it working, these are just ideas to make it easy on yourself and anyone trying to understand your code down the track.

1 Like

Still not working unfortunately :frowning: any ideas?

1 Like

Double check everything:

  • check the migration file - make sure everything is correct and that you are migrating the right contract and your migrations files are named correctly and in the right order.
  • check all of your inheritance and so on in your contracts

If it is not the migrations file and you can’t get it working, link your github project so Dani can take a look and set you on the right track

1 Like

Hey @MarcisB

Give it a try using ganache-cli.

In your console:

truffle develop
migrate --reset

keep me posted,
Dani

1 Like

Hey @enrico

These errors do not depend on you and there is no way for us to fix them.
You have to temporary use another faucted.

Cheers,
Dani

Ciao Matteo,

I took a look at your project.
Neat code, clean and without useless steps well done.

I would remove this one (line 93) latestNumber = randomNumber;
Just use randomNumber instead, it’s already declared and there is no need to initialise another variable (keep in mind that each var requires gas).

Good that you are are verifying the sender of __callback ().

Just something for you to think about, what happens if:

  • The contract has 0 ether;
  • I create a new bet (1 eth) and I win.

What you are checking at the moment is

require(msg.value <= contractBalance, "Not enough funds to pay out.Should pay to play first");

Is that enoguh?

Let me know,
Dani

@newavel

what suggested by @cryptocrom is correct.

truffle migrate --reset --network ropsten

If still does not work, please post a screenshot of your terminal.

Happy learning,
Dani

Are there better examples of how to use web3.js besides their documentation? Their examples are very poor, and no obvious way to replicate them. To be honest, I have no idea what I am doing in the main.js file. I need a couple of code examples that show how to update data on the web page when the event is fired and how to get that event.

Ciao Dani,
thanks for your feedback. It was really helpful.

I could do something like this:

require(contractBalance!=0, "Nothing to win")
require(msg.value/2 <= contractBalance, "Not enough funds to pay out");

What do you think ?

Matteo

1 Like

Hey Matteo good morning!

require(msg.value/2 <= contractBalance, “Not enough funds to pay out”);

That does not look correct :slight_smile:

If I bet 1 ether and I win, your contact has to send me back 2 ether.
So msg.value should not be divided by two, it should be multiplied by two.

Let me know if you need other info.

Happy coding,
Dani

Hey @rostyslavdzhohola

I do find the web3 docs really exhaustive, which command is not clear to you?

Cheers,
Dani

That worked.

Thanks!

1 Like

Right, yes. I wanted to divide the contract balance actually :woozy_face:

Thankss

1 Like

Their examples look like they were written for more advanced people.
Here is the code from the page https://web3js.readthedocs.io/en/v1.3.0/web3-eth-contract.html?highlight=events

myContract.events.MyEvent({
    filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
    fromBlock: 0
}, function(error, event){ console.log(event); })
.on("connected", function(subscriptionId){
    console.log(subscriptionId);
})
.on('data', function(event){
    console.log(event); // same results as the optional callback above
})
.on('changed', function(event){
    // remove event from local database
})
.on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
    ...
});

// event output example
> {
    returnValues: {
        myIndexedParam: 20,
        myOtherIndexedParam: '0x123456789...',
        myNonIndexParam: 'My String'
    },
    raw: {
        data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
        topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
    },
    event: 'MyEvent',
    signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
    blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    blockNumber: 1234,
    address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
}

I’ve tried using this to get a particular event but to no avail. However, I found a workaround after watching a bunch of youtube videos.