-
Notifications
You must be signed in to change notification settings - Fork 0
/
Purchase.sol
60 lines (47 loc) · 1.51 KB
/
Purchase.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pragma solidity ^0.5.8;
contract Purchase {
uint public value;
address payable public seller;
address payable public buyer;
enum State { Created, Locked, Inactive } State public state;
constructor() public payable {
seller = msg.sender;
value = msg.value / 2;
require((2 * value) == msg.value, "Value has to be even.");
}
modifier condition(bool _condition) {
require(_condition);
_;
}
modifier onlyBuyer() {
require(buyer == msg.sender , "Only buyer can call this.");
_;
}
modifier onlySeller() {
require(seller == msg.sender , "Only seller can call this.");
_;
}
modifier inState(State _state) {
require(state == _state, "Invalid state.");
_;
}
event Aborted();
event PurchaseConfirmed();
event ItemReceived();
function abort() public onlySeller inState(State.Created) payable{
emit Aborted();
state = State.Inactive;
seller.transfer(address(this).balance);
}
function confirmPurchase() public inState(State.Created) condition(msg.value == (2 * value)) payable {
emit PurchaseConfirmed();
buyer = msg.sender;
state = State.Locked;
}
function confirmReceived() public onlyBuyer inState(State.Locked) payable{
emit ItemReceived();
state = State.Inactive;
buyer.transfer(value);
seller.transfer(address(this).balance);
}
}