Created
March 20, 2022 19:59
-
-
Save vis-kid/da137edb5b5b255793de39e08fb36539 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
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.7.0 <0.9.0; | |
contract Escrow { | |
//Variables | |
enum State { NOT_INITIATED, AWAITING_PAYMENT, AWAITING_DELIVERY, COMPLETE } | |
State public currentState; | |
bool public isBuyerIn; | |
bool public isSellerIn; | |
uint public price; | |
address public buyer; | |
address payable public seller; | |
// Modifers | |
modifier onlyBuyer() { | |
require(msg.sender == buyer, "Not authorized!"); | |
_; | |
} | |
modifier escrowNotStarted() { | |
require(currentState == State.NOT_INITIATED); | |
_; | |
} | |
// Functions | |
constructor(address _buyer, address payable _seller, uint _price) { | |
buyer = _buyer; | |
seller = _seller; | |
price = _price *(1 ether); | |
} | |
function initContract() public escrowNotStarted { | |
if(msg.sender == buyer) { | |
isBuyerIn = true; | |
} | |
if(msg.sender == seller) { | |
isSellerIn = true; | |
} | |
if(isBuyerIn && isSellerIn) { | |
currentState = State.AWAITING_PAYMENT; | |
} | |
} | |
function depositFunds() public payable onlyBuyer { | |
require(currentState == State.AWAITING_PAYMENT, "Already paid!"); | |
require(msg.value == price, "Incorrect depoist amount!"); | |
currentState = State.AWAITING_DELIVERY; | |
} | |
function confirmDelivery() public payable onlyBuyer { | |
require(currentState == State.AWAITING_DELIVERY, "Cannot confirm delivery"); | |
seller.transfer(price); | |
currentState == State.COMPLETE; | |
} | |
function withdraw() public payable onlyBuyer { | |
require(currentState == State.AWAITING_DELIVERY, "Cannot withdraw at this stage!"); | |
payable(msg.sender).transfer(price); | |
currentState == State.COMPLETE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment