What is Wallet Smart Contract?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// pragma is the directive through
// which we write the smart contract  
pragma solidity 0.6.0;
                              
// Creating a contract
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() public  payable{
                      
       // msg.sender is the address of the
       // person who has currently deployed contract
        Owner = 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];
   }
    
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// pragma is the directive through which
// we write the smart contract
pragma solidity 0.6.0;
 
// Creating a contract
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() public payable{
      
     // msg.sender is the address of the person
     // who has currently deployed contract
     Owner = 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 onlyOwwner 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];
          }
}

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.