Last active
September 15, 2018 05:53
-
-
Save justinmeiners/b36a54a5315c20afb40d8890ac6a78b1 to your computer and use it in GitHub Desktop.
Dutch auction smart contract for a presentation.
This file contains hidden or 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
// Created By: Justin Meiners (2018) | |
pragma solidity ^0.4.24; | |
// https://en.wikipedia.org/wiki/Dutch_auction | |
contract DutchAuction { | |
uint public askingPrice; | |
address public auctioneer; | |
address public winner; | |
address public seller; | |
// indicates when the auction is complete | |
bool public finished; | |
mapping (address => uint) private escrow; | |
event AskingPriceChanged(uint amount); | |
event Finished(uint amount, address winner); | |
constructor(uint _askingPrice, address _auctioneer, address _seller) public { | |
askingPrice = _askingPrice; | |
auctioneer = _auctioneer; | |
seller = _seller; | |
finished = false; | |
} | |
function lowerAskingPrice(uint _askingPrice) public { | |
// only the auctioneer can adjust the askingPrice | |
require(msg.sender == auctioneer); | |
// auctioneer can only lower the askingPrice | |
require(_askingPrice < askingPrice); | |
require(!finished); | |
askingPrice = _askingPrice; | |
// notify | |
emit AskingPriceChanged(askingPrice); | |
} | |
function deposit() public payable { | |
// auctioneer cannot participate | |
require(msg.sender != auctioneer); | |
// you must register with some money | |
require(msg.value > 0); | |
// store money in their escrow account | |
escrow[msg.sender] += msg.value; | |
} | |
function withdraw() public { | |
// withdraw money from escrow account | |
// this can be done at anytime | |
uint amount = escrow[msg.sender]; | |
require(amount > 0); | |
escrow[msg.sender] = 0; | |
msg.sender.transfer(amount); | |
delete escrow[msg.sender]; | |
} | |
function call() public { | |
// auctioneer and seller cannot participate | |
require(msg.sender != auctioneer && msg.sender != seller); | |
require(escrow[msg.sender] >= askingPrice); | |
require(!finished); | |
// subtract from escrow | |
escrow[msg.sender] -= askingPrice; | |
winner = msg.sender; | |
finished = true; | |
// give it to the sender | |
escrow[seller] += askingPrice; | |
emit Finished(askingPrice, winner); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment