Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 7: Program using Solidity to check Balance

1) Install the truffle

2) Open the Ganache

3)

5)

pragma solidity ^0.4.23;

contract Demo {

uint public balance;

// Initialize global variables 
constructor() public

{

balance = 0;

}

// The payable keyword allows this function to accept coins 
function contribute() public payable

{

// msg.value is the number of Wei (Ethereum unit) sent in a transaction 
balance += msg.value;

}

}

6) Compile the Solidity Program (Contract)

10) Create the initial file for deployment in migration folder 
const Demo = artifacts.require(“Demo”);

module.exports = function (deployer) {deployer.deploy(Demo);

};

11) Then update the truffle-config file for deployment

Then Compile

12) Deploy the Contract

0x507649263e48C9bB4387e1806c58D7C944D87cf2

Create the object of Demo contract by firing following command 
Demo.at(“0x507649263e48C9bB4387e1806c58D7C944D87cf2”).then(function(x) {dm = x});

Then Return the balance variable values

Demo.at(“0x507649263e48C9bB4387e1806c58D7C944D87cf2”).then(function(x) {dm = x});

14) add some ether to wallet

Wei is the smallest denomination of ether—the cryptocurrency coin used on the Ethereum network. 
One ether = 1,000,000,000,000,000,000 wei (1018).

Software Genre: Cryptocurrency How do you convert ether to Wei?

1 ether = 1 0000 0000 0000 0000 00 wei.

dm.contribute({from:”0x5093a1083E29e413d7DeA28d05EA551f7feFdD20”,value: 1000000000000000000}); Demo.at(“0x507649263e48C9bB4387e1806c58D7C944D87cf2”).then(function(x) {dm = x});

dm.contribute({from:”0x6b39907dD945b4FF048F910ee8620a44Efb17F14”,value: 1000000000000000000});