Last active
March 14, 2019 22:07
-
-
Save stupeters187/e521f0f5bc940bd841a441957feb297e to your computer and use it in GitHub Desktop.
Escrow Smart Contract Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.5.2; | |
contract Escrow { | |
//state variables | |
address payable buyer; | |
address payable seller; | |
address arbiter; | |
uint public amountInWei; | |
//solidity events | |
event FundingReceived(uint _timestamp); | |
event SellerPaid(uint _timestamp); | |
event BuyerRefunded(uint _timestamp); | |
//constructor function; used to set initial state of contract | |
constructor(address payable _buyer, address _arbiter, uint _amountInWei) public { | |
seller = msg.sender; | |
buyer = _buyer; | |
arbiter = _arbiter; | |
amountInWei = _amountInWei; | |
} | |
//function for buyer to fund; payable keyword must be used | |
function fund() public payable { | |
require(msg.sender == buyer && //conditional checks to make sure only the buyer's address | |
msg.value == amountInWei//can send the correcr amount to the contract | |
); | |
emit FundingReceived(now); //emit FundingReceived() event | |
} | |
//function for buyer to payout seller | |
function payoutToSeller() public { | |
require(msg.sender == buyer || msg.sender == arbiter); //only buyer or arbiter can execute this function | |
seller.transfer(address(this).balance); //using the solidity's built in transfer function, the funds are sent to the seller | |
emit SellerPaid(now); //emit SellerPaid() event | |
} | |
//function for seller to refund the buyer | |
function refundBuyer() public { | |
require(msg.sender == seller || msg.sender == arbiter);//only buyer or arbiter can execute this function | |
buyer.transfer(address(this).balance); //using the solidity's built in transfer function, the funds are returned to the buyer | |
emit BuyerRefunded(now);//emit BuyerRefunded() event | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment