Sunday 11 November 2018

A step-by-step guide for deploying a simple Solidity contract on a test network

This post relates to a simple smart contract written in Solidity deployment on the Ropsten test network. It is a step-by-step deployment, giving as much detail as I could patiently gather..

METAMASK

"MetaMask is a bridge that allows you to visit the distributed web of tomorrow in your browser today. It allows you to run Ethereum dApps right in your browser without running a full Ethereum node.".
In a nutshell, it allows you to create an address and request ETH for testing. Install the extension for Chrome or Mozilla and set it up as follows. Start with the password creation (in my test I used 12345678910!)
You will now see this screenshot:
Then accept the various terms and conditions Finally, set up the secret words and back them up (spray talk duck avocado access put lens slice bench donor wise survey) - and the next screen put them back in the same order - and confirm. You will still see the following.
Now for the purpose of this exercise, select the "Ropsten Test Network" and click on "deposit" and select "Test Faucet" and click "GET ETHER" A new tab opens to https://faucet.metamask.io/ and select "reqest 1 ether from faucet" (or few times) A link like https://ropsten.etherscan.io/tx/0x2bc952af0236ef3960bdcd22b1a637f07e1369b1c0b200063c38104ab9a3611e will appear once the transaction(s) has(have) been validated and mined - you will see the number of ETH appear on your METAMASK main window for the "Ropsten Test Network"

SOLIDITY

Now on to the Solidity contract and coding/deployment/testing on the test network. Head towards https://remix.ethereum.org Under the tab 'run' select (if not already) 'environment' to be 'Injected Web' - and the test account we created should appear with the number of ETH requested by Faucet. We just need to write and deploy a contract... What about a simple contract to store the hash of a public document to validate the fact that it has not been modified?
pragma solidity ^0.4.7;

contract HashedDocsDemo {
    mapping (string => string) urlsDocHashMaps;
    
    function setHash(string url, string hash) public validStr(url) validStr(hash) {
        urlsDocHashMaps[url] = hash;
    }
    
    function checkHash(string url, string hash) public view validStr(url) validStr(hash) returns(bool) {
        string memory storedHash = urlsDocHashMaps[url];
        return keccak256(bytes(storedHash)) == keccak256(bytes(hash));
    }
    
    modifier validStr(string str) {
        bytes memory bstr = bytes(str); 
        require(bstr.length != 0, "invalid string");
        _;
    }
}
Once the contract has been pasted in the Remix IDE, go to "run" and click on deploy.
A window will popup and "click" confirm.
Another URL will be displayed (like https://ropsten.etherscan.io/tx/0xc551c6c2237c4faad697faf65bdd3d3a407914f1574b2a931871f3552154b93d) - same thing - wait for mining and effective contract deployment ("This transaction has been included into BlockNo #4400698 And will be reflected in a shortwhile")
We can finally interact with the contract - still in Remix - in the Run tab Enter any URL with any hash - then "transact" - same pop-up - confirm a new link is displayed: https://ropsten.etherscan.io/tx/0xe3c8af1e041405c53b2bf2ba300a947b3ba2a284f8f0ddce1187658f118557ce
Wait for confirmation - then you can look at the Remix ide, at the bottom, details of the transaction that went through and "decoded input" ....

No comments:

Blog Archive