Escrow

In general, an escrow refers to a financial arrangement where a third party holds and regulates payment of the funds required for two parties involved in a given transaction. The funds are held by the escrow service until the process of the transaction is completed or terminated.
In the context of smart contracts, an escrow smart contract refers to a program that enforces the rules of an escrow agreement in a decentralized and automated manner. It provides a secure way to exchange assets or funds between two parties without requiring the involvement of a centralized intermediary.
The use cases for escrow smart contracts are many, but some common examples include real estate transactions, freelance work, online purchases, and crowdfunding campaigns. By using an escrow smart contract, buyers and sellers can feel more secure about the exchange, as the smart contract ensures that the agreed-upon terms are met before releasing the funds.

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Escrow {
address payable public buyer;
address payable public seller;
address payable public arbiter;
uint public price;
bool public released;
bool public refunded;
constructor(address payable _buyer, address payable _seller, address payable _arbiter) payable {
require(msg.value > 0);
buyer = _buyer;
seller = _seller;
arbiter = _arbiter;
price = msg.value;
released = false;
refunded = false;
}
function releaseToSeller() public {
require(arbiter == msg.sender, "Only the arbiter can release funds.");
require(released == false, "Funds have already been released.");
seller.transfer(price);
released = true;
}
function refundToBuyer() public {
require(arbiter == msg.sender, "Only the arbiter can refund funds.");
require(refunded == false, "Funds have already been refunded.");
buyer.transfer(price);
refunded = true;
}
}
In this example, the Escrow contract acts as a mediator between a buyer, a seller, and an arbiter. The buyer sends the payment to the contract upon creation, and the funds are held in escrow until the arbiter determines that they should be released to the seller or refunded to the buyer.
The contract has a constructor that takes in the addresses of the buyer, seller, and arbiter, and sets the price to the amount sent to the contract. The releaseToSeller and refundToBuyer functions can be called by the arbiter to release the funds to the seller or refund them to the buyer, respectively.
It's worth noting that this is a very basic example, and in a real-world scenario, an escrow contract would likely have additional functionality to handle disputes and other edge cases.

Flowchart

+---------+
| Buyer |
+----+----+
|
1. Send ETH
|
+--------------+--------------+
| |
+----+----+ +----+----+
| Escrow | | Seller |
+----+----+ +----+----+
| |
2. Escrow holds ETH 3. Seller sends goods
| |
+----+----+ +----+----+
| Escrow | | Buyer |
+----+----+ +----+----+
| |
4. Buyer confirms receipt 5. Escrow releases ETH
| |
+--------------+--------------+
|
6. ETH sent to seller
|
+----+----+
| Seller |
+---------+
  1. 1.
    The buyer sends ETH to the escrow smart contract.
  2. 2.
    The escrow smart contract holds the ETH in escrow.
  3. 3.
    The seller sends the goods to the buyer.
  4. 4.
    The buyer confirms receipt of the goods.
  5. 5.
    The escrow smart contract releases the ETH to the seller.
  6. 6.
    The ETH is sent to the seller.
This is a simplified example and there may be additional steps and conditions involved depending on the specifics of the transaction and the smart contract implementation.