Programming Project - Phase 2

Hi @xactant

Their is a lot of different issues . I ll not go back to all of them because you can see the posts in this topic but it really depend on the node/ truffle/ npm version you are using.

Just don’t use the truffle-hdwallet-provider which is not supported anymore, you have to use @truffle/hdwallet-provider

Those versions seems to be fine

Truffle v5.1.0-next.1 (core: 5.1.0-next.1)
Solidity - 0.5.12 (solc-js)
Node v11.12.0
Web3.js v1.2.1

This is the oldest i was able to run without problem, i just use nvm to switch version.

As they could fix those issues in the futur don’t hesitate to share with us if you are able to run more recent versions :slight_smile:

1 Like

Hello Guys, I am completely ready with the solidity code, but I have a problem with the Javascript listener in the front end I tried everything but:


var someEvent = contractInstance.SomeEvent(); // <= do not recognize the event!

someEvent.watch(function(error, result){

if(!error){

.....
}else{
.....
}

})

Is this the way that I need to make it happen?

I will appreciate your help!

Thanks.

Hi @filip, I managed to make the Dapp running without watching your “Are you stuck and need help getting started” video. I watched it after I got the main functionality right, In my approach, I did not use events. It works on ropsten network (without simulating it with multiply users). You can see my approach below:

Solidity:
struct Player {
address addr;
uint betValue;
bool updating;
bool won;
}

mapping(bytes32 => Player) public players;
mapping(address => bytes32) public playersId;

In order to retrieve information whether the result is available from frontend, I call two functions:

function getPlayerId() public view returns (bytes32) {
return playersId[msg.sender];
}

function getPlayerResult(bytes32 _queryId) public view returns (bool) {
return players[_queryId].won;
}
In main.js, I have a function checkResult(), that is called in bet() function .on(“receipt”).

The checkFunction() below. Note that I use fetchedQuery variable in this function to track that only unique Ids are processed,

function checkResult() {
contractInstance.methods.getPlayerId().call().then(function(res) {
if (fetchedQuery == res || res == 0) {
console.log(“Processing…”);
return;
} else {
fetchedQuery = res;
contractInstance.methods.getPlayerResult(res).call().then(function(res2) {
console.log(res2);
if (res2 == true) {
$("#flip_coin_unknown").attr(“src”, “./pic/bitcoin_head.jpg”);
$("#result_output").text(“You won!”);
} else {
$("#flip_coin_unknown").attr(“src”, “./pic/bitcoin_tail.jpg”);
$("#result_output").text(“You lost!”);
}
});
}
});
getContractBalance();
}

I am not submitting whole code because I am still working on this project. At this point, I want to ask you if this approach is recommended? I managed to make it work, but I am open to the idea that listening for events may be more beneficial. If yes, can you help to explain why is better to listen for events than calling for getFunctions from smart contract?

Best, Kamil

Hi @ProgPredator

You can use the getPastEvents api if you want to catch the last event emit.

await contract.getPastEvents(['SomeEvents'],
                             {fromBlock: 'latest', toBlock: 'latest'},
                             async (err, events) => {
                                  console.log(events[events.length-1].returnValues);
                             });
1 Like

Hi @KamilK
Using events and an event listener in your frontend is more user friendly, because when you are using an oracle the response can take times to be process and your user will have to click multiples time on the getPlayerResult,without knowing if the request when through, what happen if an error occur ?
By using an event listener your frontend will be update automatically when the event will be store on the blockchain, you can also listen for an error event in case the oracle call failed for any reason.

1 Like

Thanks @gabba , I still have issue to use the JS listener, but I made it waiting enough for the oracle callback. So this is my project:

1 Like

Below is the link for my completed project

https://we.tl/t-r83qABZBtG

Hello everyone, do you guys also experience that oracle is not calling back on ropsten network?

It was working fine for me fine 3 days ago. 2 days ago I started to work on code improvement and realized that never get the response from oracle. I thought that I messed up with my code and try back and fourth deploying new contracts (lost whole day… :frowning: ). Today I tried to call contracts that I deployed on Friday and were working that day. I find out that I don`t get response from oracle also on those contracts. See below screenshot as an example (Q = query, C = Callback)

The last transaction on screenshot was FAIL due to REVERT. Variable “updating” is still true, and was never updated to false, because __callback function was never called.

A day ago I uploaded a link to my project here. If anyone could look on my solidity code and confirm that everything is fine there I would be very grateful!

Best, Kamil

Hello, I have the same issue! No callback.

Is there a way to know if the oracle provider is usable on certain networks?

Hi @ProgPredator, @KamilK
@dan-i and i, had the same issue last week. It is not clear yet why the callback is sometime delayed, i talked about it to @filip but yes It is a bit annoying… As it is running on a
testnet they could have issue with spams or maybe their oracle don’t have random number to send back. I wasn’t able to find info about their service online last time, even an oracle status (to make sure it works or not)

hi @ProgPredator

I look at your contract and your code today, i really like your flashy colors :slight_smile: .

Regarding your contract issue with the oracle your problem didn’t come from the callback, the first call is free but then every call cost you gas and your contract was out ether so out of gas.
When your user is locked it’s impossible to unlock it because you check if his address is false (which is the correct way to do it) but if your contract failed to pay the provable api the user is locked out.

In you smart contract you should use the require before the call to provable_randomDS_proofVerify__returnCode () otherwise you will never check if the call is true of false.

    require (msg.sender == provable_cbAddress());
    if (provable_randomDS_proofVerify__returnCode(...)

Regarding the ui when i first logged the address was 0x000 , the same thing happen if i switch for an other user.
Why are you not only working with eth in the front and convert them to wei with web3js.utils.
It’s confusing to deposit 1 which is 0.01 Eth and bet 1 which is 0.000000000000000001 Eth

const weiValue = Web3.utils.toWei('1', 'ether');
console.log(weiValue);
// => 1000000000000000000
const etherValue = Web3.utils.fromWei('1000000000000000000', 'ether');
console.log(etherValue);
// => 1

Their is a security issue in your contract you can’t check the amount bet checking the parameter value :

function flip (uint size, uint side) payable public  {
      ....
     betting[msg.sender].size = size;

You need to check the msg.value, because someone can use a proxy or directly call your contract without using your web interface and send 0.000001 Eth (as msg.value) and write 10 Eth in your function parameter. He will be able to withdraw all the fund in your contract easily.

Edit: There is a small type in you confirmation function, it should be “confirmation” otherwise your console log will never be display

function inputBet()
...
.on("confirmation", function(conformationNr)
...

Ps: Try to install your package locally with npm, like that when someone clone your project he will just have to do an npm install, some dependencies were missing in your package.json file

2 Likes

@KamilK @ProgPredator @dan-i

Regarding PROVABLE API they have officially some issues:

Oraclize Support @oraclize-support Mar 03 07:03
I can confirm we are experiencing some issues on Ropsten, we are investigating, the Ropsten service should be back to normal shortly

Do not hesitate to ping them on their gist if you have issues again with their service

1 Like

Hello @gabba ,

Thank you very much for the detailed answer! I am taking this course very seriously and really appreciate your answer! I will edit the project after I finish whole Ethereum Smart Contract Security course, because I think that I need to add many functions and libraries from there!

Thanks Again , Bye! :slight_smile:

No worries, send me a pm if you want me to check your project again after your modifications.
Bye :wink:

1 Like

Any solutions for this i’m having the same issue

I cannot deploy my Dapp on the testnet using truffle. Can anybody help. Before truffle migrate --network ropsten I did npm install truffle-hdwallet-provider, but forgot to add it to the screenshot btw.

Did you try to install git ?
If it doesn’t work try to downgrade your node version, their is many post talking about how to do it in this topic

Hi @illyushin
Your error message is self explanatory, you are using @truffle/hdwallet-provider so you need to do an npm install @truffle/hdwallet-provider.

truffle-hdwallet-provider is an older version

1 Like

I’ve tried installing git through the git website ; using npm install git and npm install git-install as well as reinstalling various versions of npm and node to no avail I have no idea what to do next doesn’t look like the issue thread is active either any way to install it without using npm or yarn?