-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sellable.sol
119 lines (94 loc) · 3.01 KB
/
Sellable.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pragma solidity ^0.4.19;
/**
* @author Pablo Ruiz <[email protected]>
* @title Sellable
*/
/**
* @dev Sellable contract should be inherited by any other contract that
* wants to provide a mechanism for selling its ownership to another account
*/
contract Sellable {
// The owner of the contract
address public owner;
// Current sale status
bool public selling = false;
// Who is the selected buyer, if any.
// Optional
address public sellingTo;
// How much ether (wei) the seller has asked the buyer to send
uint public askingPrice;
//
// Modifiers
//
// Makes functions require the called to be the owner of the contract
modifier onlyOwner {
require(msg.sender == owner);
_;
}
// Add to functions that the owner wants to prevent being called while the
// contract is for sale.
modifier ifNotLocked {
require(!selling);
_;
}
event Transfer(uint _saleDate, address _from, address _to, uint _salePrice);
function Sellable() public{
owner = msg.sender;
Transfer(now,address(0),owner,0);
}
/**
* @dev initiateSale is called by the owner of the contract to start
* the sale process.
* @param _price is the asking price for the sale
* @param _to (OPTIONAL) is the address of the person that the owner
* wants to sell the contract to. If set to 0x0, anyone can buy it.
*/
function initiateSale(uint _price, address _to) onlyOwner public{
require(_to != address(this) && _to != owner);
require(!selling);
selling = true;
// Set the target buyer, if specified.
sellingTo = _to;
askingPrice = _price;
}
/**
* @dev cancelSale allows the owner to cancel the sale before someone buys
* the contract.
*/
function cancelSale() onlyOwner public{
require(selling);
// Reset sale variables
resetSale();
}
/**
* @dev completeSale is called buy the specified buyer (or anyone if sellingTo)
* was not set, to make the purchase.
* Value sent must match the asking price.
*/
function completeSale() public payable{
require(selling);
require(msg.sender != owner);
require(msg.sender == sellingTo || sellingTo == address(0));
require(msg.value == askingPrice);
// Swap ownership
address prevOwner = owner;
address newOwner = msg.sender;
uint salePrice = askingPrice;
owner = newOwner;
// Transaction cleanup
resetSale();
prevOwner.transfer(salePrice);
Transfer(now,prevOwner,newOwner,salePrice);
}
//
// Internal functions
//
/**
* @dev resets the variables related to a sale process
*/
function resetSale() internal{
selling = false;
sellingTo = address(0);
askingPrice = 0;
}
}