Programming Project - Phase 2

@newavel

Your code is hitting a revert (), check your requires and you will see that something is wrong.

happy learning,
Dani

1 Like

Yes, it is different. It doesn’t make sense. The price on etherscan is different than what I have in my dapp shown.

A funny thing happened. Call to a coinflip() function stopped working, but withdrawAll() worked. Am I understanding correctly, that each time I send a call to the oracle, I have to subtract the gas price from my contract balance?

Provable give each contract one free call to the oracle then every call after that will deduct from your contract balance so you have to account for this in some way…
From memory the default amount gas price is 20 Gwei (which works out to be 4 finney) but you can use the provable_getPrice function listed in the documentation to ascertain the cost…
Have a read through the documentation in the link I sent through to get an understanding of it all and then figure out how to implement this in your contract so that balances on blockchain match what your DApp show

1 Like

I managed to get some ether thanks, now I’m having another issue, I’ve compiled my code successfully locally, but when I try to deploy my contract on ropsten, deployment fails with the following error : Error: ReferenceError: Ownable is not defined

My flip contract is inheriting from ownable and the ownable file compiles and deploys successfully.

Any ideas on how to go about the issue?

Since yesterday I haven’t been able to deploy my contract on the ropsten network. I’ve tried lowering and increasing gas, but it doesn’t help. What’s going on here?

hello @filip

I’ve developed the contract and now I’m following your guide about how to start trufle with the oracle.

at the point where I start the migration I get this error:

PS C:\Users\enrico\Desktop\coding\truffle\COIN_CONTRACT> truffle migrate --network ropsten
Error: Cannot find module '@truffle/hdwallet-provider'
Require stack:
- C:\Users\enrico\Desktop\coding\truffle\COIN_CONTRACT\truffle-config.js
- C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\node_modules\original-require\index.js
- C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\Users\enrico\Desktop\coding\truffle\COIN_CONTRACT\truffle-config.js:21:26)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at Object.require (internal/modules/cjs/helpers.js:72:18)
    at Function.load (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\config\dist\index.js:159:1)
    at Function.detect (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\config\dist\index.js:148:1)
    at Object.run (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:189:1)
    at Command.run (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\command.js:136:1)
    at Object.<anonymous> (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\cli.js:52:1)
    at __webpack_require__ (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\webpack\bootstrap ead030e4e76f43b08d0e:19:1)
    at C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\webpack:\webpack\bootstrap ead030e4e76f43b08d0e:62:1
    at Object.<anonymous> (C:\Users\enrico\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:68:10)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
Truffle v5.1.14-nodeLTS.0 (core: 5.1.13)
Node v12.18.3
PS C:\Users\enrico\Desktop\coding\truffle\COIN_CONTRACT>```

so it seems it doesnt' find the `module '@truffle/hdwallet-provider'`. 

When doing `npm install truffle-hdwallet-provider` I get this: 

  • [email protected]
    updated 1 package and audited 333 packages in 26.955s
    found 5 vulnerabilities (1 low, 4 high)
    run npm audit fix to fix them, or npm audit for details
maybe the vulnerabilities are the issues? if i do "npm audit fix" it doesn't fix anything saying it require a manual fixing.

i have no idea what to do, how can I proceed?

thanks

The error was indeed from a require. I rewrote the require but the error showed again. Can you please explain why the error is occurring? I want to understand what’s happening in the code. Thanks!

pragma solidity 0.5.12;

import "github.com/provable-things/ethereum-api/provableAPI.sol";

contract CoinFlip is usingProvable{
    address public owner;
    uint public contractBalance;

    uint256 constant NUM_RANDOM_BYTES_REQUESTED = 1;
    uint public latestNumber;
    
    struct betConfig {
        address player;
        uint coinSide;
        uint bettingValue;
    }
    
    struct callbackMapping{
        uint randomNumber;
        uint side;
        address payable playerAddress;
        uint betValue;
        bool winOrLose;
    }
    
    mapping (address => betConfig) public betInfo;
    mapping (bytes32 => callbackMapping) public waiting;
    
    //Events ---------
    event LogNewProvableQuery(string description);
    event generatedRandomNumber(uint256 randomNumber);
    
    event mappingUpdated(string description);
    event playerInfoAdded(string description);
    
    event result(string description);
    
    constructor() public payable{
        
    }
    
    //Game proper ---------
    
    function bet(uint _coinSide) public payable {
        require(msg.value > 5 wei, "Bet something larger than 5 wei.");
        uint valueToBet = msg.value;
        
        uint256 QUERY_EXECUTION_DELAY = 0;
        uint256 GAS_FOR_CALLBACK = 200000;
        
        bytes32 queryId = provable_newRandomDSQuery(
            QUERY_EXECUTION_DELAY,
            NUM_RANDOM_BYTES_REQUESTED,
            GAS_FOR_CALLBACK
            );
            emit LogNewProvableQuery("Provable query was sent, standing by for the answer");
        
        waiting[queryId].playerAddress = msg.sender;
        waiting[queryId].side = _coinSide;
        waiting[queryId].betValue = valueToBet;
        
            emit mappingUpdated("Mapping updated. Waiting for random number.");
    }
    
    function __callback(bytes32 _queryId, string memory _result, bytes memory _proof) public {
        require(msg.sender == provable_cbAddress(), "Callback error.");

        uint256 randomNumber = uint256(keccak256(abi.encodePacked(_result))) % 2;
        latestNumber = randomNumber;
        
        emit generatedRandomNumber(randomNumber);
        
        processResults(_queryId, latestNumber);
    }
    
    function processResults(bytes32 id, uint number) private {
        waiting[id].randomNumber = number;
        
        if (waiting[id].randomNumber == 1){
            waiting[id].winOrLose == true;
                uint prize =  waiting[id].betValue * 3;
                waiting[id].playerAddress.transfer(prize);
                
                emit result("You won!");
        }
        if (waiting[id].randomNumber == 0){
            waiting[id].winOrLose == false;
            
                emit result("You lost.");
        }
    }
}

It happens when Ropsten is down.
Which it is today.

Hey @Mucha_Julius

There is probably an error in how you’re importing the ownable contract. Double check that there are no typos.
If does not work push your repo on github and give me the link

Hi @enrico

Mind that you’re talking about two different modules:
Error: Cannot find module '@truffle/hdwallet-provider'
[email protected]

run npm i @truffle/hdwallet-provider

1 Like

Hey @newavel

I am happy to check but you need to give me a bit of context :slight_smile:
Which network have you deployed your contract on? (testnet/ropsten etc…)
Which function are you calling when you get that error?

1 Like

Hey guys… I have been trying to deploy the exact code Filip used with the consistently same ā€œGas Estimation Failedā€ notification. I have been looking through this entire forum but to no avail. I am trying as Filip did, to deploy on the Ropsten network through Remix. I have connected my Metamask which has a 5 eth balance. I’ve tried putting the gas fees up into the millions, or down to a few hundred thousand.

I’m not sure if if matters, though I have not deployed it ever, meaning it shouldn’t be affected by the free first time call.

I can understand from previous posts, that there may be a problem with a require() being reverted so I even tried to comment that out from Filip’s code. I’m really not sure what’s up… any insight will help.

Thanks.

P.S. - I also just tried it on the Rinkeby testnet too - same result. I am definitely missing something… :thinking:

I figured it out :sweat_smile: I had a deployer.deploy (Ownable) in my flip migration. Here’s my repo https://github.com/mnaibei/coinflip.git., my dapp is failing to deploy with

ā€œCoinflipā€ ran out of gas (using a value you set in your network config or deployment parameters.)

  • Block limit: 0x203c3f0
  • Gas sent: 1050000

Please check it out and advise

---- ---- ---- UPDATE ---- ---- ---- No more Gas Estimation Failed
It seems like you simply have to deploy all the associated contracts (or like 3 of them worked) but doing it one at a time… then it will allow you to deploy the random.sol main contract… I suppose it was trying to allocate enough gas for all deployment… i’m not sure… but it does work if you try to do all the provable.sol contracts, then deploy the random.sol one as Filip did. Man, I feel dumb. haha. A new day, fresh eyes.

1 Like

Gabba, thank you for the getPrice() information… I am wondering if I understand it correctly. Will this .004 eth only be the cost of the call for the random number? Meaning the 200,000 in GAS_FOR_CALLBACK will additionally be taken from the contract balance, OR will that gas fee be part of the price of the call?

I appreciate any insight people can give.

I’m having trouble with my event listeners. It’s working fine but the problem is that it doesnt display the result properly, any ideas?

image
image


image
image

Github: https://github.com/Atheal9k/coinFlip

Thanks!

Hey @CrazyCanadia

The gas fee and the price of the oracle are two different things.
You use gas to run and change state of the blockchain, you pay the fee to the oracle to use it’s service.

Happy coding,
Dani

1 Like

I will post this here as it is related.
As my second project, I am making a collectible which inherits the ERC721 contract. As some contracts in it use pragma 0.6.2, I couldn’t compile until I updated Truffle and specified in truffle-config.js to use 0.6.2.
So, now my contract compiles, but when trying to migrate I get this:

So far, I have tried:

  • updating Ganache,
  • deleting package.json and re-run truffle init,
  • downgrading node.js to 10.21.

I don’t see any more ideas on the internet. Maybe someone else has had this? :thinking:

1 Like

Now I was able to proceed thanks to your help! :slight_smile:

when trying to deploy as shown in the video I should have eth in my metamask in the ropsten testnet.

I tried to get them as shown from faucet.metamask.io but I always get the same error:

{"error":"[ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"message\":\"Too Many Requests\",\"data\":{\"originalError\":{}},\"stack\":\"Error: Too Many Requests\\n at eval (/www/node_modules/web3-provider-engine/subproviders/rpc.js:52:23)\\n at Proxy.eval (/www/node_modules/web3-provider-engine/subproviders/rpc.js:54:11)\\n at Object.eval [as apply] (LavaMoat/core/kernel:939:17)\\n at Object.eval [as apply] (LavaMoat/core/kernel:939:17)\\n at Request.self.callback (/www/node_modules/request/request.js:186:22)\\n at Object.eval [as apply] (LavaMoat/core/kernel:939:17)\\n at Proxy.emit (events.js:310:20)\\n at Object.eval [as apply] (LavaMoat/core/kernel:939:17)\\n at Request.eval (/www/node_modules/request/request.js:1155:10)\\n at Object.eval [as apply] (LavaMoat/core/kernel:939:17)\"}}'"}

I tried with two different metamsk in different browsers and different ip.

How could I get over this? Are there other faucet compatible with what I need for the project?

thanks :slight_smile:

1 Like