Ethereum coinflip dapp discussion

How to deploy a React uport App like created here https://www.youtube.com/watch?v=h7J-SYUOqeA to be able to get there from a normal url?

Use truffle and deploy to the mainnet. Then use web3js in the frontend. Deploy frontend just like any other webpage.

I really appreciate for your reply. Sorry for the late reply tho:)

I don’t know how to do what happens at 00:40 where you get a popup and have a user uploading the file and then it get stored and cryptographic signature is receive, how to create that functionality? https://vimeo.com/239155784

I asked on a few foums but what they said was something around the lines of
You want to include the hash of the files contents in your signed message to the blockhain

eg, on OSX terminal

$ md5 fileToNotarize.doc
MD5 (fileToNotarize.doc) = e789c8f38a10b495614ae07665c8db79

Then include the file hash with a transaction message to the blockchain

But I still don’t know how to really do it, do you know any more comprehensive sources?

Well, there are many parts to achieving that. And it also depends on what kind of server you are running. This is pretty difficult stuff, and it will take time to build. Just a heads up.

  1. You have to create the file upload functionality. This has nothing to do with blockchain. Here is guide with HTML and PHP, if you want to use PHP. https://www.taniarascia.com/how-to-upload-files-to-a-server-with-plain-javascript-and-php/

  2. You need to hash the file and store the file and hash in some sort of database. SQL perhaps. Here is a function in php which will do the trick of hashing the file. http://php.net/manual/en/function.hash-file.php

  3. You need to build a smart contract that holds the hashes of the files. I’m not sure what the purpose it, so maybe you need even more functionality.

  4. Then you need to send a transaction to the smart contract from your server with the hash. If you are using php then here is an api. https://github.com/digitaldonkey/ethereum-php.

All of this can also be done with nodejs, if you want to work in javascript.

Hi filip,

general question about solidity: could you explain the difference between brackets vs periods and/or parenthesis and the theory behind best cases to use those?

I can actually see how periods access variables within a struct, etc. and how parenthesis are used for arguments. Just not too sure about brackets.

Thanks in advance!
@filip

Sure. Brackets are used when creating contracts, structs or functions for example. They form a block of code that encapsulates the code that belong to a certain function for example. So when we define a function, brackets are used to set the boundaries for when the function code starts and when it ends.

Periods are most often used when accessing structs or objects. So if I have a struct that was defined like this:

struct Person {
    string name;
    uint age;
}

When I have created a Person (let’s call it Filip) from that struct I can then access the properties of that person through
Filip.name or Filip.age

Do you understand it better now? Let me know if you have further questions.

Thanks filip! I apologize not being specific enough. I am actually puzzled by the use of square brackets […]

Ok, sorry about that.

Brackets are used to access elements within arrays and declare arrays. When declaring/creating arrays we put the brackets after the type just to tells us and the compiler that this is an array. So I can declare/create an array by doing either:

uint[] myArray; which creates an empty array with elements of type uint

or

uint[2] myArray = [2, 5]; which does the same thing but sets it to length 2 and initializes it with values as well.

Brackets are also used to access already created arrays. So say I have the array declared above, I can then access an element in it by doing the following.

uint sum = myArray[0] + myArray[1] which would add the first and second element of the array together. Sum would be 7.

What about in the following instance:

// the last line of code references – ownerToDog[owner] = id; – This was not a defined array.

function addDog(string _name, uint _age) {
address owner = msg.sender;
uint id = dogs.push(Dog(_name, _age));
ownerToDog[owner] = id;
}
// last line of code seems to access a certain functionality before reaching the period – return dogs[id-1].name; –

function getDog() view returns(string) {
    address owner = msg.sender; 
    uint id = ownerToDog[owner];
    return dogs[id-1].name;
}

I’m sorry. I forgot to mention mapping. Mappings also use brackets. So when we have a defined mapping we use mappings to grab an element from that mapping.

So in the ownerToDog mapping, if we want to get the dog of the owner. We input the owner into the mapping using brackets and the output is the dog. Is it clearer now?

1 Like

Hi Filip,

accounts[0] does not appear to point to my metamask account. This is true in both instance.getLastFlip(accounts[0], function(error, result){ …
and instance.flip.sendTransaction({from: accounts[0], gas:100000, value:
val}, function(error, txHash){ …

If I replace accounts[0] with the address of the metamask account it works. If I use accounts[0] in instance.flip.sendTransaction then metamask is not invoked to confirm the transaction and nothing happens when I hit submit. I fix it by substituting the metamask address for accounts[0].

Here is my app.js
Please let me know what you think I have wrong.

(function (Contract) {
var web3_instance;
var instance;
var accounts;

function init(cb) {
    web3_instance = new Web3(
        (window.web3 && window.web3.currentProvider) ||
        new Web3.providers.HttpProvider(Contract.endpoint));

    accounts = web3.eth.accounts;

    var contract_interface = web3_instance.eth.contract(Contract.abi);
    instance = contract_interface.at(Contract.address);
    cb();
}

function getBalance() {
    instance.getBalance(function (error, result) {
        if(error){
          alert(error);
        }
        else{
          // .html is used to write balance to web page. tostring takes 
          // number and converts to string
          $("#balance").html(result.toString());
        }
    });
}

// web3.eth.getTransactionReceipt returns an object containing various 
// information about a transaction which is being placed in receipt
// receipt.status is the status code returned by 
// web3.eth.getTransactionReceipt 

function waitForReceipt(txHash, cb){
web3_instance.eth.getTransactionReceipt(txHash, function(error, receipt){
if(error){
alert(error);
}
else if(receipt !== null){
cb(receipt);
}
else{
window.setTimeout(function(){
waitForReceipt(txHash, cb);
}, 5000);
}
})
}

function getResult(){
// “0x84e792e9915126D25A7b52cd4072A7C658333018”
instance.getLastFlip(“0x84e792e9915126D25A7b52cd4072A7C658333018”, function(error, result){
// var boolresult = bool.toString(result);
alert(“result is”, result);
if(result){
$("#result").html(“You won!”);
}
else{
$("#result").html(“You lost!”);
}
});
}

// txHash which is returned by instance.flip.sendTransaction then 
// becomes the argument for function waitForReceipt
function flip(){
  // parseInt converts string to integer
  let val = parseInt($("#bet").val());
  // in next line from: accounts[0] should work but doesn't seem to so 
  // I hardcoded the external address account 
 // "0x84e792e9915126D25A7b52cd4072A7C658333018"

  instance.flip.sendTransaction({from: "0x84e792e9915126D25A7b52cd4072A7C658333018", gas:100000, value:
  val}, function(error, txHash){
    if(error){
      alert(error);
    }
    else{
 //      alert("SUCCESS");  replaced by the statements in purple
         waitForReceipt(txHash, function(receipt){
         if(receipt.status === "0x1"){
 //        alert(JSON.stringify(receipt));
           getResult();
           getBalance();
         }
         else{
           alert("receipt status fail");
          }
        });
    }
  })
}

$(document).ready(function () {
      init(function () {
          getBalance();
      });
      $("#submit").click(function(){
        flip();
      })
  });

})(Contracts[‘Coinflip’]);

What is the output if you run alert(JSON.stringify(accounts)) in the beginning of the flip function?

If I run alert(JSON.stringify(accounts)) in the beginning of the flip function I get []

Just a left bracket followed by a right bracket

Thanks. I’m looking into this now. Seems to be a bigger problem than I thought. Something is acting up with web3.js and metamask.

Filip Thank you. If you need me to run any tests to help diagnose the problem please let me know.

Yes, Thank you Filip!

In the meantime you can continue by entering your address like you did before. Hopefully I will find a solution soon.

Seems to be related to this https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

I have been in contact with superblocks to see if there is anything they need to resolve on their end. They hoped to be able to release an update within a day or two.

It should work now. Could you try again?