Programming Project - Phase 2

Source: provable/ethereum-api

Oraclize Support @oraclize-support Apr 27 23:13:
@BatiGencho we plan to get the ropsten instance back to a normal tomorrow or the day after, if you are in a rush you can test on a different testnet. Testnet services are completely free of charge and run experimental software. We are integrated on the following testnets: Goerli, Rinkeby, Ropsten and Kovan.
@marcelomorgado @gianchandania we will investigate most likely tomorrow, in the meantime you can test on Goerli/Rinkeby/Kovan! My apologies for the inconvenience

Lets just see what this day brings…

Did you try on Kovan ?

Really cool i love it ! :heartpulse:

1 Like

Yes, i tried Kovan. It behaves the same way. Spinning coins for ever xD.

I added support for kovan on the frontend. Switch between networks is not smooth yet. Just refresh the page. I have two questions:

  • How do you look at the emitted events on etherscan?
  • Is there a way to unsubscribe from an event subscription?
_contract.events.GameCreated().on('data', ....); 

Hi @mawize

You can see all the events emited on this page

https://ropsten.etherscan.io/address/0xE14434E433d27B95981Db181B0d59B83A696e034#events

There is a way when you are using the subscribe notation
https://web3js.readthedocs.io/en/v1.2.0/web3-eth-subscribe.html#id11

But i usually use Once

2 Likes

It is finally working and they fixed ropsten :heart_eyes:. My app now supports switching between networks and accounts smoothly (no reload).

Two minor things remain weird to me:

  • Somehow the same contract on kovan behaves differently. Events are not registered by web3.
  • I had to put a minimum bet of 0.01 ether because it seems that the oracle call is really expensive.

Can i calculate the cost for the oracle call before i call the provable_newRandomDSQuery() ?
I understand that the third parameter is the gas for the callback but contract balance gets reduced by much more.

:thinking: Try it out! :star_struck:

1 Like

The cost is usually 0.004 eth on ropsten but you can use this function to get the current price

provable_getPrice("RANDOM")
1 Like

Hi all!

I’m now also ready to show my little coinflip Dapp :partying_face:

So here are two screenshots, the first one showing the admin interface (shown only to the contract-owner) and the second one shows what a regular player sees.

The whole project can be found at https://github.com/Laserbach/CoinFlip_Dapp

The app works well (as far as I can tell :upside_down_face:) but there is one weird issue which I couldn’t figure out. It is a web3 problem:

There seems to be a difference when it comes to promise-handling if using .call() and .send() , take the following examples:

function getBalance() {
  contractInstance.methods.getBalance().call().then(function(result){
    var amount = web3.utils.fromWei(result, 'ether');
    $("#balance").html(amount);
  });
}

function getPlayerBalance() {
  contractInstance.methods.getPlayerBalance(address).call().then(function(result){
    var amount = web3.utils.fromWei(result, 'ether');
    $("#playerBalance").html(amount);
  });
}

Thse two .call() getter functions work exactly as they should, the smart contract in both cases returns an uint variable (balances in Wei), which I then transform into Ether and display.

Now here we have two .send() setter functions. Those will send funds and again will return me at the end the uint-value of the transfered funds.

function widthdrawFunds() {
  contractInstance.methods.withdrawAll().send({from: address}).then(function(result){
    var amount = web3.utils.fromWei(result, 'ether');
    alert("Balance withdrawn!")
    console.log(amount);
    getBalance();
  })
}

function widthdrawWinnings() {
  contractInstance.methods.withdrawPlayer().send({from: address}).then(function(result){
    var amount = web3.utils.fromWei(result, 'ether');
    alert("Here is your reward!")
    console.log(amount);
    getPlayerBalance();
  })
}

Now the weird thing with those .send() functions is, that web3.utils.fromWei will throw the following error:
web3-error

I know, the problem isnt really dramatic and I dont rely on the return-value which is delivered via the promise since I just wanted to use it for logging / testing. Also the smart-contract transaction actually goes through, so this error just breaks the function in the frontend (the getBalance() function wont be executed which leads to an outdated frontend page).

Anyways I would be interested to know what the actual problem here is. I tried many different things like .toFixed() or .toString() as it was suggested in some forums but nothing would fix this issue. And whats annoying the most is the fact that in the .call() functions it perfectly works this way, using the same return values and promise handling… :exploding_head:

Does anybody here have an idea on how to fix it?

Also @gabba I would be glad if you could roast my code :grin: :smiling_imp:

Best - Lars

3 Likes

Hi @Laserbach

Well done :+1:
You get an error because you try to pass an object as parameter to the fromWei() function.
When you are using send() you will create a transaction so the return value will be the transaction receipt.
Try to display the result value you will get something like this:

If you want to get an information after calling a method with send you need to emit an event in your solidity method.
Also don’t forget to call the provable_getPrice(“RANDOM”) function to update the balance of your smart contract after each call. Otherwise you will have an issue at some point.

2 Likes

Hey Gabba!

Thanks for your reply, as always on point! =)

Thats so interesting, so would it be correct to say that any smart-contract functions which include transactions and are called with send() dont work with return values?
I have implemented 2 withdraw functions, one for the admin and one for the players, would there be any way to access their return values in the frontend? Or would using events be the only workaround in this case? This would be an important rule for me to keep in mind for future projects.

Also thank you very much for the provable_getPrice(“RANDOM”) function, thats indeed an important update!

Best regards and happy halving!!

2 Likes

Yes indeed you will never be able to get the return value of a method using send.
You can set a variable in you smart contract and use a getter to access it, but calling an event is the proper way to do and it’s cheaper you ll pay less gas this way.
Happy Halving too :wink:

2 Likes

@gabba @filip

Receiving error msg after: coinflip % truffle migrate --network ropsten
Error: Cannot find module ‘@truffle/hdwallet-https://ropsten.infura.io/v3/00ce8b2c615e4c1da84101d3e70490ba
Require stack:

truffle-config.js is correct

const HDWalletProvider = require('@truffle/hdwallet-provider');

const infuraKey = "...";

const fs = require('fs');

const mnemonic = fs.readFileSync(".secret").toString().trim();
provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/'secret`),
         network_id: 3,       // Ropsten's id
          gas: 5500000,        // Ropsten has a lower block limit than mainnet
            confirmations: 2,    // # of confs to wait between deployments. (default: 0)
          timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
          skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
     }
truffle migrate --network ropsten    
Error: Cannot find module '@truffle/hdwallet-provider'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/cherrybluemoon/Coinflip/truffle-config.js:21:26)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Function.Config.load (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/packages/truffle-config/index.js:381:1)
    at Function.Config.detect (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/packages/truffle-config/index.js:370:1)
    at Object.run (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/commands/migrate.js:194:1)
    at Command.run (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/command.js:113:1)
    at Object.<anonymous> (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/packages/truffle-core/cli.js:55:1)
    at __webpack_require__ (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/webpack/bootstrap fe83fc804e18bb49e1de:19:1)
    at /Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/webpack:/webpack/bootstrap fe83fc804e18bb49e1de:65:1
    at Object.<anonymous> (/Users/cherrybluemoon/.nvm/versions/node/v10.18.0/lib/node_modules/truffle/build/cli.bundled.js:71:10)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

A post was merged into an existing topic: Programming Project - Phase 1

A post was merged into an existing topic: Programming Project - Phase 1

Hi @cherrybluemoon

Can you share the content of your package.json file ?

@gabba

{
  "name": "coinflip",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "truffle-hdwallet-provider": "^1.0.17"
  }
}

Hi @cherrybluemoon

You are not using the correct package, you are requiring @truffle/hdwallet-provider
but in you had installed truffle-hdwallet-provider.

truffle-hdwallet-provider is deprecated

use @truffle/hdwallet-provider instead

Hi @gabba
That work! Appreciate you helping me. What a difference between a - and / made.

It’s not the name npm repository if you look at this one

truffle-hdwallet-provider

It’s deprecated

1 Like

I’ve uploaded my solution on: https://github.com/fuchsi21/CoinFlipDapp

The Web implementation is far from being finished. Animations and error handling are missing. The website should only reflect the functionality of the Smart Contract.

in the Smart Contract I have also not yet managed to verify the callback. Does anyone know if this works in the testnet?

best regards and many thanks
daniel

1 Like