Create Escrow Smart Contract

Posted By : Jyoti

Apr 19, 2023

P2P Smart Escrow Contract: - 

A Secure and Trustworthy Way to Make Transactions Peer-to-peer (P2P) transactions are  becoming more popular than ever, but they come with their own set of challenges, such as lack of trust and security. To address these challenges, the concept of smart escrow contracts was introduced. A smart escrow contract is a self-executing contract that enforces the terms of an agreement between two or more parties.

 

A P2P smart escrow contract is a smart contract designed specifically for P2P transactions. It is a secure and trustworthy way to make transactions, ensuring that both parties fulfill their obligations. In a P2P smart escrow contract, a third-party escrow agent is used to hold the funds until the terms of the agreement are met. The smart contract is programmed to release the funds to the seller only when the buyer receives the goods or services and confirms their satisfaction. This ensures that the seller doesn't receive payment until the buyer is satisfied with the transaction, thereby creating a more secure and trustworthy transaction environment.

 

To create a P2P smart escrow contract, you'll need to have some understanding of smart contracts and programming. Smart contracts are self-executing contracts that contain the terms of the agreement between the parties. They are stored on a blockchain network, making them immutable and transparent. Programming skills are necessary because smart contracts are written in code.

 

Example: P2P smart escrow contract 

 

It is written in Solidity, a popular smart contract programming language:

 

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

 

contract P2PEscrow {

   // Address of the payer

   address payable public payerAddress;

   // Address of the payee

   address payable public payeeAddress;

   // Address of the escrow agent

   address payable public escrowAgent;

   // Amount of the contract

   uint public contractAmount;

   // Flag indicating if the payer approved the payment

   bool public payerApproved;

   // Flag indicating if the payee approved the payment

   bool public payeeApproved;

   // Flag indicating if the contract was canceled

   bool public contractCanceled;

 

   // Constructor function

   constructor(address payable _payerAddress, address payable _payeeAddress, address payable _escrowAgent, uint _contractAmount) payable {

       // Require the contract amount to be greater than zero

       require(_contractAmount > 0, "Contract amount must be greater than zero");

       // Require that the contract amount be sent with the creation of the contract

       require(msg.value == _contractAmount, "The contract amount must be sent with the creation of the contract");

 

       // Set the addresses and contract amount

       payerAddress = _payerAddress;

       payeeAddress = _payeeAddress;

       escrowAgent = _escrowAgent;

       contractAmount = _contractAmount;

   }

 

   // Approve payment function

   function approvePayment() public {

       if (msg.sender == payerAddress) {

           // Set the payer approval flag to true

           payerApproved = true;

       }

       else if (msg.sender == payeeAddress) {

           // Set the payee approval flag to true

           payeeApproved = true;

       }

 

       // If both parties have approved and the contract was not canceled, transfer the funds to the escrow agent

       if (payerApproved && payeeApproved && !contractCanceled) {

           escrowAgent.transfer(contractAmount);

       }

   }

 

   // Cancel payment function

   function cancelPayment() public {

       // Require that only the escrow agent can cancel the payment

       require(msg.sender == escrowAgent, "Only the escrow agent can cancel the payment");

       // Require that the payment has not been approved by either party

       require(!payerApproved && !payeeApproved, "Payment has already been approved by one of the parties");

 

       // Refund the contract amount to the payer

       payerAddress.transfer(contractAmount);

       // Set the contract canceled flag to true

       contractCanceled = true;

   }

 

   // Get contract status function

   function getContractStatus() public view returns (bool, bool, bool) {

       // Return the payer approval, payee approval, and contract canceled flags

       return (payerApproved, payeeApproved, contractCanceled);

   }

  

   // Get payer address function

   function getPayerAddress() public view returns (address payable) {

       return payerAddress;

   }

  

   // Get payee address function

   function getPayeeAddress() public view returns (address payable) {

       return payeeAddress;

   }

  

   // Get escrow agent function

   function getEscrowAgent() public view returns (address payable) {

       return escrowAgent;

   }

  

   // Get contract amount function

   function getContractAmount() public view returns (uint) {

       return contractAmount;

   }

}

 

The contract has four state variables:

  • payerAddress: The address of the person who is paying the funds into escrow.
  • payeeAddress: The address of the person who will receive the funds from escrow upon approval.
  • escrowAgent: The address of the escrow agent who will manage the funds held in escrow.
  • contractAmount: The amount of funds being held in escrow.


The constructor of the contract takes in four parameters: '_payerAddress', '_payeeAddress', '_escrowAgent', and '_contractAmount'. It requires that '_contractAmount' is greater than zero and that the exact amount of ether is sent with the creation of the contract. It sets the state variables accordingly.

 

The contract has three functions:

  • approvePayment(): This function is called by either the payer or the payee to indicate their approval for the release of funds from escrow. If the function is called by the payer, the payerApproved state variable is set to true. If it is called by the payee, the payeeApproved state variable is set to true. If both parties have approved and the contract has not been canceled, the full contract amount is transferred to the escrowAgent.
  • cancelPayment(): This function can only be called by the escrowAgent and only before either party has approved the release of funds. If called, the full contract amount is returned to the payer and the contractCanceled state variable is set to true.
  • getContractStatus(): This function returns the current state of the payerApproved, payeeApproved, and contractCanceled state variables.

 

In addition, there are four getter functions that return the values of the state variables: getPayerAddress(), getPayeeAddress(), getEscrowAgent(), and getContractAmount().

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

December 3, 2024 at 05:41 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text