Solidity Alchemy – Course (ESCROW SMART CONTRACT)

RMAG news

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:

State Variables: Understanding and declaring variables that store the contract’s state.

Constructor: Initializing the contract with specific parameters.

Payable Constructor: Allowing the constructor to handle Ether transactions.

Functions: Implementing the contract’s logic through functions.

Function Security: Restricting function calls to specific addresses for security purposes.

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

}

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