Skip to content
Related Articles

Related Articles

What is Escrow Smart Contract?
  • Last Updated : 10 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

Escrow is the third party which holds the asset(asset can be money, bond, stocks) on the presence of two parties. Escrow will release the fund when certain conditions are met.
For Example, “A” is a seller and wants to sell his car, “B” is a buyer who wants to buy “A”‘s car so they will contact Escrow “C”(an arbiter) which hold the asset until “B” receives the car. When this condition will be met, Escrow will release the fund to “A”. This solves the issue of trust and prevents any discrepancy.
Lets the write a smart contract for the Escrow using solidity language. 
Here Smart contract will hold the asset, which will be released on when conditions are fulfilled.
 

Solidity




pragma solidity 0.6.0;     
   
// Defining a Contract
contract escrow{
  
    // Declaring the state variables
    address payable public buyer;
    address payable public seller;
    address payable public arbiter;
    mapping(address => uint) TotalAmount;
  
    // Defining a enumerator 'State'
    enum State{
         
        // Following are the data members
        awate_payment, awate_delivery, complete 
    }
  
    // Declaring the object of the enumerator
    State public state;
      
    // Defining function modifier 'instate'
    modifier instate(State expected_state){
          
        require(state == expected_state);
        _;
    }
  
   // Defining function modifier 'onlyBuyer'
    modifier onlyBuyer(){
        require(msg.sender == buyer || 
                msg.sender == arbiter);
        _;
    }
  
    // Defining function modifier 'onlySeller'
    modifier onlySeller(){
        require(msg.sender == seller);
        _;
    }
      
    // Defining a constructor
    constructor(address payable _buyer, 
                address payable _sender) public{
        
        // Assigning the values of the 
        // state variables
        arbiter = msg.sender;
        buyer = _buyer;
        seller = _sender;
        state = State.awate_payment;
    }
      
    // Defining function to confirm payment
    function confirm_payment() onlyBuyer instate(
      State.awate_payment) public payable{
      
        state = State.awate_delivery;
          
    }
      
    // Defining function to confirm delivery
    function confirm_Delivery() onlyBuyer instate(
      State.awate_delivery) public{
          
        seller.transfer(address(this).balance);
        state = State.complete;
    }
  
    // Defining function to return payment
    function ReturnPayment() onlySeller instate(
      State.awate_delivery)public{
        
         
       buyer.transfer(address(this).balance);
    }
      
}

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :