Create a gist now

Instantly share code, notes, and snippets.

anonymous /purchase.sol
Created Nov 22, 2015

What would you like to do?
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