Skip to content

Instantly share code, notes, and snippets.

Created November 22, 2015 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6454599dcf8dc45eb414 to your computer and use it in GitHub Desktop.
Save anonymous/6454599dcf8dc45eb414 to your computer and use it in GitHub Desktop.
Purchase
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