Sam 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Name – Saurabh Umbarkar

Roll no – 60
PRN no - 2043110151
Branch - CSE
Subject - Block Chain and Digital Currency

Experiment - 3

Aim - Write a smart contract on a test network, for Bank


account of a customer for following operations:-

1 - Deposit money
2 - Withdraw Money
3 - Show balance

Theory -

A "smart contract" is simply a program that runs on the Ethereum


blockchain. It's a

collection of code (its functions) and data (its state) that resides at
a specific address

on the Ethereum blockchain.

Smart contracts are a type of Ethereum account. This means they


have a balance and
can be the target of transactions. However they're not controlled
by a user, instead

they are deployed to the network and run as programmed. User


accounts can then

interact with a smart contract by submitting transactions that


execute a function

defined on the smart contract. Smart contracts can define rules,


like a regular

contract, and automatically enforce them via the code. Smart


contracts cannot be

deleted by default, and interactions with them are irreversible


Solidity

Solidity is an object-oriented, high-level language for


implementing smart contracts.

Smart contracts are programs which govern the behaviour of


accounts within the

Ethereum state.

Solidity is a curly-bracket language designed to target the


Ethereum Virtual Machine

(EVM). It is influenced by C++, Python and JavaScript. You can


find more details

about which languages Solidity has been inspired by in the


language
influences section.

Solidity is statically typed, supports inheritance, libraries and


complex user-defined

types among other features.

Step 1:

Open Remix IDE on any of your browsers, select on the

New File and click onSolidity to choose the environment.

Step 2:

Write the Smart contract in the code section, and click the

Compile button under the Compiler window to compile the


contract.
Unlock this documen
Step 3:

To execute the code, click on the Deploy button under Deploy and
Run Transactions window.
Step 4:

After deploying the code click on the method calls under the
drop-down of

deployed contracts to run the program, and for output, check to


click on the drop-

down on the console.

Step 5:

For debugging click on the

Debug button

corresponding to the method call in

the console. Here you can check each function call and variable
assignments.
U
Program -

Smart contract for the Bank account of the customer to do


operations like Deposit,

Withdraw and Show Balance


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract SimpleBank {

mapping (address => uint) private balances;

address owner;

// Constructor is "payable" so it can receive ether,

constructor() public payable {

/* Set the owner to the creator of this contract */


owner = msg.sender;

/// @notice Deposit ether into bank, requires method is "payable"

/// @return The balance of the user after the deposit is made

function deposit(uint depoamount) public payable returns (uint) {

balances[msg.sender] += depoamount;

payable(msg.sender).transfer(depoamount);

//emit LogDepositMade(msg.sender, msg.value);

return balances[msg.sender];

}
/// @notice Withdraw ether from bank

/// @return The balance remaining for the user

function withdraw(uint withdrawAmount) public returns (uint) {

// Check enough balance available, otherwise just return balance

if (withdrawAmount <= balances[msg.sender]) {

balances[msg.sender] -= withdrawAmount;

payable(msg.sender).transfer(withdrawAmount);

return balances[msg.sender];

}
function balance() public view returns (uint) {

return balances[msg.sender];

Output -
Deploy

Deposit Money

You might also like