Programming Project - Phase 2

Hey guys, does anyone know how to run unit testing against ropsten not against ganache? I would like to create unit tests and run them, but this contract is using oracle so it won’t work with ganache… @rostyslavdzhohola I’ve seen you have unit tests in your project, can you tell me how did you run them?

1 Like

@Ula I used a dummy function for generating random numbers and queryId on Ganache. Here is the video with the example.
https://academy.ivanontech.com/products/ethereum-smart-contract-programming-201/categories/2004134/posts/6702341.
When I want to migrate to Ropsten network, I change queryId to the provable_newRandomDSQuery() function

I don’t know if you can run tests on the Ropstean network, and even if you do, it will take longer time, because it takes time to mine each transaction.

2 Likes

Hey guys,
need help! Super confused with mappings and structs - not sure what I’m even doing. Any help is appreciated :smile:

https://github.com/Atheal9k/coinFlip/tree/master/contracts

disclaimer: my code may be confusing to read

@Ula I used a dummy function for generating random numbers and queryId on Ganache. Here is the video with the example.

Good suggestion here :slight_smile:

Hey @Atheal

I had a quick look at your code, it seems like you are using mappings and struct correctly.
What is exactly your question?

I also saw this line
uint256 randomNumber = uint256(keccak256(abi.encodePacked(_result))) % 100;
So basically you are creating a random number from 0 to 100, then you save it to latestNumber then you do %2, these seem to be unnecessary steps :slight_smile:

Let me know,
Dani

1 Like

That’s a good point!

Well the code compiles correctly, but it doesn’t work.

When I try and play it on remix, it doesn’t accept my calls for random() function (made sure its on ropsten as well).

And then sometimes it magically transacts but the callback for latestnumber function doesnt work.

My other functions work fine though.

I know the problem is kinda vague but I seem to have issue with translating how I want the logic into code form - especially when writing in this part - getting error hell when I try and call my structs and maps in this function:

function __callback(bytes32 _queryId, string memory _result, bytes memory _proof) public {
require(msg.sender == provable_cbAddress());

uint256 randomNumber = uint256(keccak256(abi.encodePacked(_result))) % 2;

latestNumber = randomNumber;
saveResults(_queryId,latestNumber);

winOrLose();

delete waiting[id];

emit generatedRandomNumber(randomNumber);

}

I have a test that works only halfway through. The first coinflipSet() function executes. Second coinflipGet() does not. Can someone check if I do a proper call to the second function in my test?

it("should increase or decrease the Coinflip value by the betting amount", async function(){
    console.log("The balance before is: " + await instance.balance());
    await instance.coinflipSet({from: accounts[1], value: smallBet});
    let balance = await instance.balance();
    let floatBalance = parseFloat(balance);
    let realBalance = await web3.eth.getBalance(instance.address);
    
await instance.coinflipGet(0x47baff04c1308acea62196db1ed9b745e174d9ffefb550f8c50d910d4acb5f5b ,{from: accounts[1]} );
    
//argument to the coinflipGet(bytes32 _queryId) 

console.log("Did you win? " + (((await instance.win_loose() ? true : false   )? "Yes" : "No")));
    console.log("The balance after is: " + await instance.balance());
    if(await instance.win_loose()){
      assert(floatBalance == web3.utils.toWei("0.877", "ether") && floatBalance == realBalance, "Amount didn't decrease ");
    } else {
      assert(floatBalance == web3.utils.toWei("1.123", "ether") && floatBalance == realBalance, "Amount didn't increase");
    }
    console.log("The balance after is: " + await instance.balance());
    console.log("Did you win? " + (((await instance.win_loose())? true : false   )? "Yes" : "No" + "\n"));
  });

Hey @Atheal

The 1st call to the oracle is free, but the others have a cost.
Does your contract have balance when you try to call the oracle? Let me know and if you can please send me the address of your contract on Ropsten.
I will have a look, if cannot figure it out will deploy your contract myself and check :slight_smile:

1 Like

This actually fixed my other issue.
Am I understanding correctly, that when I do Player memory newPlayer , I insert a new struct with default arguments? But when I use your way to add data, I change only specific arguments of the struct?

1 Like

Thank you @rostyslavdzhohola, true… Ropsten takes time and ether… I will play with ganache as well, good luck with your project!:slight_smile:

2 Likes

https://ropsten.etherscan.io/address/0x1c4e1ba46c8c60875065e27e8ea11fdeb1ebcb95

Haha, you’re right about the free oracle call. The first call seems to work but subsequent calls don’t - even if I attach a lot of ether to it.

Always made sure I had balance in the contract.

Thanks Dani!

1 Like

Hey guys, @filip is anyone else experiencing ropsten faucet error when buying test ether?
Is there another way to get test ether or to use a different testnet?

1 Like

Just to be clear about the .gitignore file that I’ve seen a few times in this forum (Originally I was just going to just upload everything but this file as I’m a little paranoid about someone gaining access to my wallet - but I’m assuming it is easier for someone to fork the repo and check my work/DApp without having to create a .secret file of their own and go changing things to get it working??).
Anyway, I was unsure about how to create this .gitignore file and found this link… https://stackoverflow.com/questions/10744305/how-to-create-a-gitignore-file

  1. Create the text file gitignore.txt
  2. Open it in a text editor and add your rules, then save and close
  3. Hold SHIFT, right click the folder you’re in, then select Open command window here
  4. Then rename the file in the command line, with ren gitignore.txt .gitignore

As I wasn’t sure how to create the gitignore file - I followed these steps and it appears to have worked.
Then from the gitignore github:
https://github.com/github/gitignore/blob/master/Go.gitignore
I can see this as an example of how to list files to be ignored.

Binaries for programs and plugins

*.exe

So in my gitignore file, I have
*.secret
Is that all I need to do to ensure I can upload the entire DApp file (instead of having to upload each file individually) and not risk exposing my Metamask seed phrase? Or have I missed something…please advise @Taha @dan-i Cheers, Justin

Here is the coin flip part of my DApp working - I have tried to make it entertaining as well, I hope you enjoy.
Part 1 (large gif file, so split into 2)
blockPunt phase 2 - Flip Out

Part 2

Here’s a tour of the entire blockPunt DApp (showing all game pages, main page, rules page and owner page (which has password protections for page access and to destroy contract)…

My github link to view all of my contracts and the current state of the front end (still having issues with RollOut catching the events in the js file) is below. I posted around a month ago asking for advice on why the events were not being recognised, but didn’t get a response, I thought I’d solved it with my FlipOut file by changing where the events listeners were situated in the file as it started working after I changed where they were, but have discovered that I somehow just fluked it or something as it started working, but the same thing is not working in the Rollout js file and I have also seen posts since then stating that my assumptions about the position of the event listeners is incorrect. I haven’t even touched the DrawOut js file since phase 1 of this project as my next step is to fix the front end of the RollOut game.

Anyway, the brief was to produce a coinflip contract and DApp, so check and check, but I really want to get it working with all games, so could @Taha or @dan-i please advise on why my events listeners aren’t working properly.

Here is my github link - https://github.com/cryptocrom/blockPunt

This is the issue when I try the RollOut game in the front end…

If anyone wants to see the Crypto Commandments I created to keep patrons entertained during betting (which pops up briefly in the gif attachments above), here it is…

3 Likes

This code stopped working. It used to work in ganache, but now it does not. What could be wrong?

function withdrawAll() public onlyOwner returns(uint256){
    uint256 toTransfer = balance;
    balance = 0;
    msg.sender.transfer(toTransfer);
    return toTransfer;
  }

Have you checked the amount in etherscan to see if it is accurately representing the sum of your balances? Look up the contract ropsten.etherscan https://ropsten.etherscan.io/ and check the balance. If it is less than what you think there should be it will be failing. Then check all of your functions that change balances etc. but it is most likely the price of the query to provable that is getting deducted from your contract, you will need to account for this if you haven’t already… here is the Provable documentation - check out Ethereum - Best Practises, precalculating the query price
http://docs.provable.xyz/#ethereum-best-practices

Good luck

1 Like

Hello,

I was testing my code today on Remix but I am getting this error:

It works well the last time I was doing the code; I didn’t change anything so far. I also tried compiling and deploying those sample contracts (the ones named "1_Storage.sol, etc) on Remix and it’s showing the same error. What could be the cause?

Thanks.

Hey @rostyslavdzhohola

Am I understanding correctly, that when I do Player memory newPlayer , I insert a new struct with default arguments?

You are just creating an instance of the struct (empty). You will have to fill it and push it to the Struct.

Cheers,
Dani

1 Like

Happy I was helpful @Atheal

If you dive into the Oracle documentation, you will see a function that returns the fees required by the oracle.
I think it could be a good idea to take the fee from the user bet. In that way your contract does not require a balance to pay the fees.

have fun learning,
Dani

Hey @Mucha_Julius

Sometimes faucets are down unfortunately, just google and you will find many of them :slight_smile:

Happy coding,
Dani