Skip to content

Instantly share code, notes, and snippets.

@goodjoon
Created March 27, 2016 13:26
Show Gist options
  • Select an option

  • Save goodjoon/0c76fcaf5a103eb7a24f to your computer and use it in GitHub Desktop.

Select an option

Save goodjoon/0c76fcaf5a103eb7a24f to your computer and use it in GitHub Desktop.
contract Purchase {
uint public value;
address public seller;
address public buyer;
enum State { Created, Locked, Inactive }
State public state;
function Purchase() {
seller = msg.sender;
value = msg.value/2;
if (2*value != msg.value) throw;
}
modifier require(bool _condition) {
if (!_condition) throw;
_
}
modifier onlyBuyer() {
if (msg.sender != buyer) throw;
_
}
modifier onlySeller() {
if (msg.sender != seller) throw;
_
}
modifier inState(State _state) {
if (state != _state) throw;
_
}
event aborted();
event purchaseConfirmed();
event itemReceived();
// 구매를 취소하고 ether 를 refund 한다
// Seller 만 취소할 수 있으며 contract 가 lock 되기 전에만 가능
function abort()
onlySeller
inState(State.Created) {
aborted();
seller.send(this.balance);
state = State.Locked;
}
function confirmReceived()
onlyBuyer
inState(State.Locked) {
itemReceived();
buyer.send(value);
seller.send(this.balance);
state = State.Inactive;
}
function() {throw;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment