DEV Community

Cover image for Solidity Alchemy - Course (ESCROW SMART CONTRACT)
Justice Sunday
Justice Sunday

Posted on

Solidity Alchemy - Course (ESCROW SMART CONTRACT)

The escrow smart contract is a system used for transferring funds upon the fulfillment of an agreement. Traditionally, this involves a third party, but in this case, the third party is an honest one facilitated by the smart contract. This contract involves three parties: the Depositor, the Arbiter, and the Beneficiary.

Depositor: The payer in the escrow agreement.
Beneficiary: The recipient of the funds from the escrow after the Arbiter confirms the transaction, typically for providing some service to the Depositor.
Arbiter: The trusted middleman responsible for approving the transaction. The Arbiter ensures the goods or services are received before releasing the funds.

From the lessons at Alchemy University, I learned to create an escrow smart contract with the following key components:

  1. State Variables: Understanding and declaring variables that store the contract's state.
  2. Constructor: Initializing the contract with specific parameters.
  3. Payable Constructor: Allowing the constructor to handle Ether transactions.
  4. Functions: Implementing the contract's logic through functions.
  5. Function Security: Restricting function calls to specific addresses for security purposes.
  6. Events: Emitting events to log significant actions in the contract.

Here's a sample implementation of an escrow smart contract in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;


contract Escrow {
    address public depositor;
    address public beneficiary;
    address public arbiter;

    constructor(address _arbiter, address _beneficiary)payable{
        depositor = msg.sender;
        arbiter = _arbiter;
        beneficiary = _beneficiary;
    }

    function approve()external  {
    //check if the caller of the function is the arbiter.
        require(msg.sender == arbiter, "Only arbiter can approve");
        uint balance = address(this).balance;
        payable(beneficiary).transfer(balance);
        emit Approved(balance);

        }

     event  Approved(uint);

}
Enter fullscreen mode Exit fullscreen mode

This contract ensures that only the Arbiter can approve the transfer of funds to the Beneficiary, adding a layer of security and trust to the transaction process.

Please, questions and contributions are welcome


Top comments (1)

Collapse
 
paul_ogemdiiheduru_2f570 profile image
Paul Ogemdi Iheduru

This is great man....