// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0;
// Solidity program to deposit eth and get contract balance
contract contractInfo {
// Defining a function to deposit eth
function deposit(uint _amount) payable public{
require(msg.value == _amount);
}
// A smart contract can access its own balance as address(this).balance
// Given account's address, its current ether balance can be accessed as address.balance
function getBalance() public view returns(uint){
return address(this).balance;
}
}
- In ContractInfo.sol first we need have declared SPDX license that simply means that your smart contract is public and can be used by anyone without your permission.
- In the second line of code i.e. pragma solidity ^0.6.0; we have declared the compiler version.
-
Declaring a contract name contractInfo using keyword contract.
- Defining function to deposit eth to smart contract that takes only one parameter _amount of type uint having visibility set to public. We are making this function payable since we want make a transaction that will cost us ether. Inside the body of function we are checking if the required condition is fulfilled or not since it is costing us ether we need take extra care so we are adding this condition msg.value == _amount using keyword require which is fall back function that will return our ether if the transaction is not completed.
- Defining another function to get the balance of smart contract . We are setting the visibility to public and also have restricted the state mutability by adding view keyword. This function is returning the balance of contract to do that we have added keyword this which refers to balance of own contract.