-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarket.sol
More file actions
260 lines (216 loc) · 7.29 KB
/
Market.sol
File metadata and controls
260 lines (216 loc) · 7.29 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
interface ICivicGate {
function check(bytes calldata proof, address user) external view returns (bool);
}
contract Market is ReentrancyGuard {
using SafeERC20 for IERC20;
enum Side { YES, NO }
string public question;
uint64 public endTime;
address public payoutToken;
bytes32 public civicRule;
address public creator;
address public civicGate;
address public dragonSwapManager;
bool public isResolved;
Side public winner;
mapping(address => mapping(Side => uint256)) public userShares;
mapping(address => uint256) public userDeposits;
uint256 public yesShares;
uint256 public noShares;
uint256 public totalLiquidity;
event Joined(
address indexed user,
Side side,
uint256 amount,
uint256 shares,
address viaChain
);
event Resolved(Side winner, uint256 timestamp);
event Redeemed(
address indexed user,
uint256 amount,
uint256 totalShares
);
modifier notExpired() {
require(block.timestamp <= endTime, "Market expired");
_;
}
modifier onlyAfterExpiry() {
require(block.timestamp > endTime, "Market not expired");
_;
}
modifier notResolved() {
require(!isResolved, "Market resolved");
_;
}
modifier onlyResolved() {
require(isResolved, "Market not resolved");
_;
}
modifier onlyCreatorOrManager() {
require(
msg.sender == creator || msg.sender == dragonSwapManager,
"Not authorized"
);
_;
}
constructor(
string memory _question,
uint64 _endTime,
address _payoutToken,
bytes32 _civicRule,
address _civicGate,
address _dragonSwapManager,
address _creator
) {
require(_endTime > block.timestamp, "Invalid end time");
require(_payoutToken != address(0), "Invalid payout token");
require(_creator != address(0), "Invalid creator");
require(_dragonSwapManager != address(0), "Invalid manager");
question = _question;
endTime = _endTime;
payoutToken = _payoutToken;
civicRule = _civicRule;
civicGate = _civicGate;
dragonSwapManager = _dragonSwapManager;
creator = _creator;
}
function join(
Side side,
uint256 amount,
bytes calldata civicProof
) external notExpired notResolved nonReentrant {
_join(msg.sender, side, amount, civicProof, address(0));
}
function joinFromZeta(
Side side,
uint256 amount,
address user,
bytes calldata civicProof
) external notExpired notResolved onlyCreatorOrManager nonReentrant {
_join(user, side, amount, civicProof, msg.sender);
}
function _join(
address user,
Side side,
uint256 amount,
bytes calldata civicProof,
address viaChain
) internal {
require(amount > 0, "Invalid amount");
require(user != address(0), "Invalid user");
// Check Civic gate if required
if (civicRule != bytes32(0) && civicGate != address(0)) {
require(
ICivicGate(civicGate).check(civicProof, user),
"Civic verification failed"
);
}
// Transfer tokens from user (or via chain caller)
address payer = viaChain != address(0) ? viaChain : user;
IERC20(payoutToken).safeTransferFrom(payer, address(this), amount);
// Calculate shares (simplified constant product for demo)
uint256 shares = amount; // 1:1 for simplicity
if (side == Side.YES) {
yesShares += shares;
} else {
noShares += shares;
}
userShares[user][side] += shares;
userDeposits[user] += amount;
totalLiquidity += amount;
emit Joined(user, side, amount, shares, viaChain);
}
function resolve(Side _winner)
external
onlyAfterExpiry
notResolved
onlyCreatorOrManager
{
isResolved = true;
winner = _winner;
emit Resolved(_winner, block.timestamp);
}
function redeem() external onlyResolved nonReentrant {
uint256 winningShares = userShares[msg.sender][winner];
require(winningShares > 0, "No winning shares");
uint256 totalWinningShares = winner == Side.YES ? yesShares : noShares;
require(totalWinningShares > 0, "No winning shares exist");
uint256 payout = (winningShares * totalLiquidity) / totalWinningShares;
// Clear user shares before transfer to prevent reentrancy
userShares[msg.sender][winner] = 0;
// Transfer payout using the payout token
IERC20(payoutToken).safeTransfer(msg.sender, payout);
emit Redeemed(msg.sender, payout, winningShares);
}
// Emergency function to unwind liquidity for payouts via DragonSwapManager
function unwindLiquidity(uint256 amount)
external
onlyResolved
returns (uint256 unwound)
{
require(msg.sender == dragonSwapManager, "Only manager can unwind");
require(amount <= totalLiquidity, "Insufficient liquidity");
totalLiquidity -= amount;
IERC20(payoutToken).safeTransfer(dragonSwapManager, amount);
return amount;
}
function getOdds() external view returns (uint256 yesPrice, uint256 noPrice) {
uint256 total = yesShares + noShares;
if (total == 0) {
return (50, 50); // 50/50 if no shares
}
yesPrice = (yesShares * 100) / total;
noPrice = 100 - yesPrice;
}
function getUserPosition(address user)
external
view
returns (uint256 yes, uint256 no, uint256 deposited)
{
return (
userShares[user][Side.YES],
userShares[user][Side.NO],
userDeposits[user]
);
}
function getMarketInfo()
external
view
returns (
string memory _question,
uint64 _endTime,
address _payoutToken,
bool _isResolved,
Side _winner,
uint256 _yesShares,
uint256 _noShares,
uint256 _totalLiquidity
)
{
return (
question,
endTime,
payoutToken,
isResolved,
winner,
yesShares,
noShares,
totalLiquidity
);
}
// Emergency withdrawal function for creator
function emergencyWithdraw() external {
require(msg.sender == creator, "Only creator");
require(block.timestamp > endTime + 30 days, "Too early for emergency");
uint256 balance = IERC20(payoutToken).balanceOf(address(this));
if (balance > 0) {
IERC20(payoutToken).safeTransfer(creator, balance);
}
}
}