Create Escrow Smart Contract

Create Escrow Smart Contract
6 min read

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, powered by smart contract development services, was introduced. An agreement between two or more parties that enforces its terms automatically is known as a smart escrow contract.

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 promotes a more safe and reliable transaction environment by guaranteeing that the vendor won't get paid unless the buyer is happy with the deal.

It takes a certain level of smart contract development expertise to create a P2P smart escrow contract. Self-executing contracts with the terms of the parties' agreement are known as smart contracts. They are stored on a blockchain network, making them immutable and transparent. Programming skills are necessary because smart contracts are written in code.

In order to execute this, you may choose to engage a reputable smart contract development company, specializing in smart contract development, to craft a robust P2P smart escrow contract tailored to your unique needs and preferences.

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

If you also have a project in mind want related to escrow smart contract development, connect with our smart contract developers to get stared.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Oodles Blockchain 68
Full-Stack Blockchain Development Services and Solutions for Startups and Enterprises for Business Excellence Transform your business processes into highly sec...
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up