What is Escrow Smart Contract?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
      
}

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 :

1


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