-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.sol
103 lines (80 loc) · 2.75 KB
/
Property.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
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Factory {
ERC20 private $token;
mapping (string => Property) private $properties;
constructor(ERC20 _token){
$token = _token;
}
function token() external view returns (ERC20) {
return $token;
}
event Create(string indexed name, address indexed creator);
function create(string calldata _name) external returns (Property) {
require($properties[_name] == Property(address(0)));
Property _property = new Property(_name, msg.sender, $token);
$properties[_name] = _property;
emit Create(_name, msg.sender);
return _property;
}
function get(string calldata _name) external view returns (Property) {
Property _property = $properties[_name];
require(address(_property) != address(0));
return _property;
}
}
contract Property is ERC20 {
address private $owner;
ERC20 private $token;
constructor(
string memory _name,
address _owner,
ERC20 _token
) ERC20(_name, _token.symbol()) {
$token = _token;
$owner = _owner;
}
function owner() external view returns (address) {
return $owner;
}
event Deposit(address indexed account, uint amount);
/**
* @dev Deposit some tokens and increase the price
**/
function deposit(uint _amount) external {
require(_amount > 0, "Property: invalid amount");
require($token.transferFrom(msg.sender, address(this), _amount));
_mint(msg.sender, _amount);
emit Deposit(msg.sender, _amount);
}
event Withdraw(address indexed account, uint amount);
/**
* @dev Withdraw some tokens and decrease the price
**/
function withdraw(uint _amount) external {
require(_amount > 0, "Property: invalid amount");
require($token.transfer(msg.sender, _amount));
_burn(msg.sender, _amount);
emit Withdraw(msg.sender, _amount);
}
event Buy(address indexed previous, address indexed next);
/**
* @dev Buy the property at the current price
**/
function buy() external {
require(msg.sender != $owner);
require($token.transferFrom(msg.sender, $owner, totalSupply()));
emit Buy($owner, msg.sender);
$owner = msg.sender;
}
event Send(address indexed previous, address indexed next);
/**
* @dev Send the property to someone
**/
function send(address _target) external {
require(msg.sender == $owner);
require(_target != address(0));
emit Buy($owner, _target);
$owner = _target;
}
}