Created
November 22, 2015 10:47
-
-
Save anonymous/6454599dcf8dc45eb414 to your computer and use it in GitHub Desktop.
Purchase
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
contract Purchase { | |
address public seller; | |
address public buyer; | |
uint256 public price; | |
enum State { Created, BuyerConfirmed, ProductReturned, Finished } | |
State state; | |
function Purchase() { | |
seller = msg.sender; | |
price = msg.value / 2; | |
state = State.Created; | |
} | |
event BuyerConfirmed(address buyer); | |
function ConfirmPurchase() { | |
if (state != State.Created) throw; | |
if (msg.value != 2 * price) throw; | |
buyer = msg.sender; | |
state = State.BuyerConfirmed; | |
BuyerConfirmed(buyer); | |
} | |
event ProductReceivedOk(); | |
function ConfirmProductReceivedOk() { | |
if (state != State.BuyerConfirmed) throw; | |
if (buyer != msg.sender) throw; | |
seller.send(3 * price); | |
buyer.send(1 * price); | |
state = State.Finished; | |
ProductReceivedOk(); | |
} | |
event ProductReturned(); | |
function ConfirmProductReturned() { | |
if (state != State.BuyerConfirmed) throw; | |
if (buyer != msg.sender) throw; | |
state = State.ProductReturned; | |
ProductReturned(); | |
} | |
event Refunded(); | |
function ConfirmReturnedProductReceived() { | |
if (state != State.ProductReturned) throw; | |
if (seller != msg.sender) throw; | |
seller.send(2 * price); | |
buyer.send(2 * price); | |
state = State.Finished; | |
Refunded(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment