-
Notifications
You must be signed in to change notification settings - Fork 0
/
Purchase.sol
57 lines (48 loc) · 1.46 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
pragma solidity ^0.4.22;
contract Purchase{
uint public value;
address public seller;
address 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(msg.sender == buyer,"Only buyer can call this.");
_;
}
modifier onlySeller() {
require(msg.sender == seller,"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){
emit Aborted();
state = State.Inactive;
seller.transfer(address(this).balance);
}
function comfirmPurchase() 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){
emit ItemReceived();
state = State.Inactive;
buyer.transfer(value);
seller.transfer(address(this).balance);
}
}