forked from JonnyLatte/MiscSolidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optionFactory.sol
149 lines (114 loc) · 4.01 KB
/
optionFactory.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
pragma solidity ^0.4.12;
import "github.com/JonnyLatte/MiscSolidity/ownedToken.sol";
contract optionFactory
{
using SafeMath for uint;
struct OPTION_INFO
{
address owner;
ERC20 sell_token;
uint sell_units;
ERC20 buy_token;
uint buy_units;
uint expiry;
ownedToken token;
}
uint public last_option_id;
mapping(uint => OPTION_INFO) option_info;
//--------------------------------------------------------------------------------
event mintEvent(uint id, uint unitLots);
event burnEvent(uint id, uint unitLots);
event exerciseEvent(uint id, uint unitLots);
event closeEvent(uint id);
event optionCreatedEvent(
uint id,
address indexed owner,
ERC20 indexed sell_token,
uint sell_units,
ERC20 indexed buy_token,
uint buy_units,
uint expiry,
ERC20 token);
//--------------------------------------------------------------------------------
function next_option_id() internal returns (uint)
{
return ++last_option_id;
}
function create_option(
ERC20 sell_token,
uint sell_units,
ERC20 buy_token,
uint buy_units,
uint time_to_expiry) returns (uint id, ownedToken token)
{
id = next_option_id();
token = new ownedToken();
if(address(token) == 0) throw;
var expiry = now + time_to_expiry;
option_info[id] = OPTION_INFO (
msg.sender,
sell_token,
sell_units,
buy_token,
buy_units,
expiry,
token);
optionCreatedEvent(id,msg.sender,sell_token, sell_units,buy_token,buy_units,expiry,token);
}
function optionInfo(uint id) constant returns(
address owner,
ERC20 sell_token,
uint sell_units,
ERC20 buy_token,
uint buy_units,
uint expiry,
uint time_to_expiry,
address token)
{
var option = option_info[id];
owner = option.owner;
sell_token = option.sell_token;
sell_units = option.sell_units;
buy_token = option.buy_token;
buy_units = option.buy_units;
expiry = option.expiry;
if(expiry > now) time_to_expiry = expiry - now;
token = option.token;
}
function mint(uint id, uint unitLots)
{
var option = option_info[id];
if(option.owner != msg.sender) throw;
if(!option.sell_token.transferFrom(msg.sender,this,unitLots.safeMul(option.sell_units))) throw;
option.token.mint(msg.sender,unitLots);
mintEvent(id,unitLots);
}
function burn(uint id, uint unitLots)
{
var option = option_info[id];
if(option.owner != msg.sender) throw;
if(!option.token.burn(msg.sender,unitLots)) throw;
if(!option.sell_token.transfer(msg.sender,unitLots.safeMul(option.sell_units))) throw;
burnEvent(id,unitLots);
}
function exercise(uint id, uint unitLots)
{
var option = option_info[id];
if(option.expiry > now) throw;
if(!option.token.burn(msg.sender,unitLots)) throw;
if(!option.buy_token.transferFrom (msg.sender,option.owner,unitLots.safeMul(option.buy_units))) throw;
if(!option.sell_token.transfer(msg.sender ,unitLots.safeMul(option.sell_units))) throw;
exerciseEvent(id,unitLots);
}
function close(uint id)
{
var option = option_info[id];
if(option.owner != msg.sender) throw;
if(option.expiry < now) throw;
var supply = option.token.totalSupply();
if(!option.sell_token.transfer(msg.sender,supply)) throw;
option.token.destroy();
delete option_info[id];
closeEvent(id);
}
}