Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 21:: Creating the Smart Contract

For the Remix IDE, ensure that you are loading it using http and not https.

This is because Ganache only accept connections to it using http. If you load the Remix IDE using https, you would have problems connecting to Ganache later on.

pragma solidity ^0.5.1; contract ProofOfExistence {

mapping (bytes32 => bool) private proofs;

// store a proof of existence in the contract state 
function storeProof(bytes32 proof) private

{

proofs[proof] = true;

}

// calculate and store the proof for a document 
function notarize(string memory document) public

{

storeProof(proofFor(document));

}

// helper function to get a document’s sha256 
function proofFor(string memory document) private 
pure returns (bytes32) {

return sha256(bytes(document));

}

// check if a document has been notarized

function checkDocument(string memory document) public 
view returns (bool) {

return proofs[proofFor(document)];

}

}

Deploying the Contract to Ganache

With the Smart Contract created, you shall now deploy it to Ganache. Under the Run tab in the Remix IDE, select Web3 Provider as the environment

Deploying the Smart Contract

The Smart Contract deployed with the two functions showing

Open the Remix IDE and select the account

Testing the Contract

Connecting MetaMask to Ganache

You will now see the Settings page. Scroll down the page, and

under the New Network header, enter http://127.0.0.1:7545.

This is the endpoint URL that Ganache (desktop version) is listening at. Once this is entered, click the “X” button to close the window

Importing an account in MetaMask

Entering the private key of the first account in Ganache

The account in Ganache imported into MetaMask