Open In App

What is Wallet Smart Contract?

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Wallets are just like your bank account, through which we can receive money, send money, and can check the balance.

Important terms:

  1. uint- unsigned integer.
  2. address- It is a unique set of the string provided for the transaction.
  3. msg.sender- Address of the current user.
  4. msg.value- Cost put by the current user.
  5. public- Visibility of the function.
  6. view- This keyword is used when the function is read-only.
  7. modifier- modifier is just like a function, which is used at, the initial point of function so that it can check the required condition and prevent wastage of ether before function execution.

Depositing Money into the Contract Account owned by the owner

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate Wallet Smart Contract
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to depositing money into the contract account owned by the owner
contract wallet{
 
    // Specify the owner's address
    // which is publicly visible
    address payable public Owner;
 
    // mapping is created for mapping
    // address=>uint for transaction
    mapping(address=>uint) Amount; 
     
    // Defining a constructor
    // constructor is payable, which means
    // it cost ether during the deployment
    constructor() payable{
                      
       // msg.sender is the address of the
       // person who has currently deployed contract
        Owner = payable(msg.sender);
                    
        // msg.value is the value of the ether we are
        // giving during the deploying of contract
        Amount[Owner] = msg.value;
     
    }
 
    // modifier(onlyOwner) it will check
    // the required condition, here condition
    // is the only owner can call this function
    modifier onlyOwner(){
 
        // require is used whether the
        // owner is the person who has
        // deployed the contract or not
        require(Owner == msg.sender);
        _;
    }
 
   // Defining a function which is
   // used to send money
   function sendMoney(
      address payable receiver,  uint amount)
                       public payable onlyOwner {
 
         // receiver account balance should be>0
         require( receiver.balance>0);
           
        // amount should not be negative,
        // otherwise it will throw error
        require(amount>0);
                        
         Amount[Owner] -= amount;
         Amount[receiver] += amount;
    }
 
    // Defining function for Receiving money
    // to our smart contract account
    // not to an owners account
    function ReceiveMoney() public payable{
    }
  
    // This function will return the current
    // balance of the contract account owned
    // by the owner
    function CheckBalance_contractAccount()
      public view onlyOwner returns(uint){
         return address(this).balance;
    }
 
    // Defining a function that will return
   // the current balance of the owner's account
   function CheckBalance()
     public view onlyOwner returns(uint){
          return Amount[Owner];
   }
}


In the above code, money is deposited in the contract account, not in the owner’s account. Let us discuss a code in which we will see how to send ether directly into the owner’s account.

Depositing money into the Owner’s account instead of the Contract Account.

Solidity




// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; 
/// @title A contract for demonstrate Wallet Smart Contract
/// @author Jitendra Kumar
/// @notice For now, this contract just show how to depositing money into the Owner's account instead of the contract account.
contract wallet{
 
    // Specify the owner's address
    // which is publicly visible
    address payable public Owner;
 
    // Mapping is done from
    // address=>uint for transaction
    mapping(address => uint) Amount;
     
    // Constructor 'payable' is created,
    // which means it cost ether during the
    // deployment
    constructor() payable{
     
    // msg.sender is the address of the person
    // who has currently deployed contract
    Owner= payable(msg.sender);
     
    // msg.value is the value of the ether we
    // are giving during the deploying of contract
    Amount[Owner] = msg.value;
    }
             
    // Modifier is created
    modifier onlyOwner(){
             
        // require is used whether the owner is the
        // person who has deployed the contract or not
        require(Owner == msg.sender);
        _;
        }
 
    // Defining a function to send money,
    // we have used modifier(onlyOwner)
    // it will check the required condition,
    // here condition is the only owner can
    // call this function
    function sendMoney(
    address payable receiver, uint amount)
    public payable onlyOwner {
         
        // receiver account balance should be > 0
        require(receiver.balance>0);
             
        // amount should not be negative ,
        // otherwise it throw error
        require(amount >0);
             
        Amount[Owner] -= amount;
        Amount[receiver] += amount;
    }
     
        // Defining a function to receive money   
        function ReceiveMoney() public payable{
             
        // Receiving money in owners
        // account directly
        Amount[Owner] += msg.value;
    }
     
        // Defining a function to return
        // the balance of the contract
        // account owned by the owner
        function CheckBalance_contractAccount()
        public view onlyOwner returns(uint){
             
        // return the balance of contract
        // account owned by owner
        return address(this).balance;
        }
 
        // Defining a function to check the
        // current balance of the owner account
        function CheckBalance()
        public view onlyOwner returns(uint){
             
                // return the current balance
                // of the owner's account
                return Amount[Owner];
        }
}




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads