Created
September 6, 2019 12:12
-
-
Save ericoporto/737257e4765ad453d9724940a7ef1c1a to your computer and use it in GitHub Desktop.
My first smart contract, done following a tutorial
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.0; | |
// MIT License | |
// | |
// Copyright (c) 2019 Érico Vieira Porto | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// Done following the live coding from BlockGeeks here | |
// https://www.youtube.com/watch?v=6RCtNrlzV28 | |
// Video tutorial by Niloo Ravaei | |
contract Escrow { | |
// using this and inState we provide basic state machine functionality | |
enum State { | |
eUninitiated, // initial state for the contract, before agreement | |
eAwaitingPayment, // after both buyer and seller deposit money in the contract | |
eAwaitingDelivery, // the buyer has paid | |
eComplete // the buyer has received | |
} | |
State currentState; | |
// the people involved on the contract transaction | |
address payable buyer; | |
address payable seller; | |
// the price defined at contract initialization | |
uint price; | |
bool buyer_in; | |
bool seller_in; | |
// costless functions to view the variables from the contract | |
function getBuyer() public view returns (address) { return buyer; } | |
function getSeller() public view returns (address) { return seller; } | |
function getPrice() public view returns (uint) { return price; } | |
function getCurrentState() public view returns (State) {return currentState; } | |
function getBuyerIn() public view returns (bool) { return buyer_in; } | |
function getSellerIn() public view returns (bool) { return seller_in; } | |
modifier inState(State expectedState) { | |
// functions modified by this has to be at this state | |
require(currentState == expectedState); _; | |
} | |
modifier correctPrice() { | |
// the correct price is defined when the contract is initialized | |
require(msg.value == price); _; | |
} | |
modifier buyerOnly() { | |
require(msg.sender == buyer); _; | |
} | |
constructor (address payable _buyer, address payable _seller, uint _price) public{ | |
// starts the contract, the state will be at eUnitiated at creation | |
buyer = _buyer; | |
seller = _seller; | |
price = _price; | |
} | |
//this method expects ether | |
//we want both buyer and seller to deposit some amount of money | |
//for better incentives, it's the price | |
function initiateContract() correctPrice inState(State.eUninitiated) payable public { | |
if(msg.sender == buyer && buyer_in == false) { | |
buyer_in = true; | |
} | |
if(msg.sender == seller && seller_in == false) { | |
seller_in = true; | |
} | |
if (buyer_in && seller_in){ | |
currentState = State.eAwaitingPayment; | |
} | |
} | |
// once buyer invokes this and pays the correct price, we can move to the next state | |
function confirmPayment() buyerOnly correctPrice inState(State.eAwaitingPayment) payable public { | |
currentState = State.eAwaitingDelivery; | |
} | |
// buyer has received the product and confirms delivery, we can return the escrow values. | |
function confirmDelivery() buyerOnly inState(State.eAwaitingDelivery) public { | |
seller.transfer(price * 2); | |
buyer.transfer(price); | |
currentState = State.eComplete; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment