-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathFixedProductMarketMaker.sol
386 lines (324 loc) · 14.1 KB
/
FixedProductMarketMaker.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
pragma solidity ^0.5.1;
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { IERC20 } from "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import { ConditionalTokens } from "@gnosis.pm/conditional-tokens-contracts/contracts/ConditionalTokens.sol";
import { CTHelpers } from "@gnosis.pm/conditional-tokens-contracts/contracts/CTHelpers.sol";
import { ERC1155TokenReceiver } from "@gnosis.pm/conditional-tokens-contracts/contracts/ERC1155/ERC1155TokenReceiver.sol";
import { ERC20 } from "./ERC20.sol";
library CeilDiv {
// calculates ceil(x/y)
function ceildiv(uint x, uint y) internal pure returns (uint) {
if(x > 0) return ((x - 1) / y) + 1;
return x / y;
}
}
contract FixedProductMarketMaker is ERC20, ERC1155TokenReceiver {
event FPMMFundingAdded(
address indexed funder,
uint[] amountsAdded,
uint sharesMinted
);
event FPMMFundingRemoved(
address indexed funder,
uint[] amountsRemoved,
uint collateralRemovedFromFeePool,
uint sharesBurnt
);
event FPMMBuy(
address indexed buyer,
uint investmentAmount,
uint feeAmount,
uint indexed outcomeIndex,
uint outcomeTokensBought
);
event FPMMSell(
address indexed seller,
uint returnAmount,
uint feeAmount,
uint indexed outcomeIndex,
uint outcomeTokensSold
);
using SafeMath for uint;
using CeilDiv for uint;
uint constant ONE = 10**18;
ConditionalTokens public conditionalTokens;
IERC20 public collateralToken;
bytes32[] public conditionIds;
uint public fee;
uint internal feePoolWeight;
uint[] outcomeSlotCounts;
bytes32[][] collectionIds;
uint[] positionIds;
mapping (address => uint256) withdrawnFees;
uint internal totalWithdrawnFees;
function getPoolBalances() private view returns (uint[] memory) {
address[] memory thises = new address[](positionIds.length);
for(uint i = 0; i < positionIds.length; i++) {
thises[i] = address(this);
}
return conditionalTokens.balanceOfBatch(thises, positionIds);
}
function generateBasicPartition(uint outcomeSlotCount)
private
pure
returns (uint[] memory partition)
{
partition = new uint[](outcomeSlotCount);
for(uint i = 0; i < outcomeSlotCount; i++) {
partition[i] = 1 << i;
}
}
function splitPositionThroughAllConditions(uint amount)
private
{
for(uint i = conditionIds.length - 1; int(i) >= 0; i--) {
uint[] memory partition = generateBasicPartition(outcomeSlotCounts[i]);
for(uint j = 0; j < collectionIds[i].length; j++) {
conditionalTokens.splitPosition(collateralToken, collectionIds[i][j], conditionIds[i], partition, amount);
}
}
}
function mergePositionsThroughAllConditions(uint amount)
private
{
for(uint i = 0; i < conditionIds.length; i++) {
uint[] memory partition = generateBasicPartition(outcomeSlotCounts[i]);
for(uint j = 0; j < collectionIds[i].length; j++) {
conditionalTokens.mergePositions(collateralToken, collectionIds[i][j], conditionIds[i], partition, amount);
}
}
}
function collectedFees() external view returns (uint) {
return feePoolWeight.sub(totalWithdrawnFees);
}
function feesWithdrawableBy(address account) public view returns (uint) {
uint rawAmount = feePoolWeight.mul(balanceOf(account)) / totalSupply();
return rawAmount.sub(withdrawnFees[account]);
}
function withdrawFees(address account) public {
uint rawAmount = feePoolWeight.mul(balanceOf(account)) / totalSupply();
uint withdrawableAmount = rawAmount.sub(withdrawnFees[account]);
if(withdrawableAmount > 0){
withdrawnFees[account] = rawAmount;
totalWithdrawnFees = totalWithdrawnFees.add(withdrawableAmount);
require(collateralToken.transfer(account, withdrawableAmount), "withdrawal transfer failed");
}
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal {
if (from != address(0)) {
withdrawFees(from);
}
uint totalSupply = totalSupply();
uint withdrawnFeesTransfer = totalSupply == 0 ?
amount :
feePoolWeight.mul(amount) / totalSupply;
if (from != address(0)) {
withdrawnFees[from] = withdrawnFees[from].sub(withdrawnFeesTransfer);
totalWithdrawnFees = totalWithdrawnFees.sub(withdrawnFeesTransfer);
} else {
feePoolWeight = feePoolWeight.add(withdrawnFeesTransfer);
}
if (to != address(0)) {
withdrawnFees[to] = withdrawnFees[to].add(withdrawnFeesTransfer);
totalWithdrawnFees = totalWithdrawnFees.add(withdrawnFeesTransfer);
} else {
feePoolWeight = feePoolWeight.sub(withdrawnFeesTransfer);
}
}
function addFunding(uint addedFunds, uint[] calldata distributionHint)
external
{
require(addedFunds > 0, "funding must be non-zero");
uint[] memory sendBackAmounts = new uint[](positionIds.length);
uint poolShareSupply = totalSupply();
uint mintAmount;
if(poolShareSupply > 0) {
require(distributionHint.length == 0, "cannot use distribution hint after initial funding");
uint[] memory poolBalances = getPoolBalances();
uint poolWeight = 0;
for(uint i = 0; i < poolBalances.length; i++) {
uint balance = poolBalances[i];
if(poolWeight < balance)
poolWeight = balance;
}
for(uint i = 0; i < poolBalances.length; i++) {
uint remaining = addedFunds.mul(poolBalances[i]) / poolWeight;
sendBackAmounts[i] = addedFunds.sub(remaining);
}
mintAmount = addedFunds.mul(poolShareSupply) / poolWeight;
} else {
if(distributionHint.length > 0) {
require(distributionHint.length == positionIds.length, "hint length off");
uint maxHint = 0;
for(uint i = 0; i < distributionHint.length; i++) {
uint hint = distributionHint[i];
if(maxHint < hint)
maxHint = hint;
}
for(uint i = 0; i < distributionHint.length; i++) {
uint remaining = addedFunds.mul(distributionHint[i]) / maxHint;
require(remaining > 0, "must hint a valid distribution");
sendBackAmounts[i] = addedFunds.sub(remaining);
}
}
mintAmount = addedFunds;
}
require(collateralToken.transferFrom(msg.sender, address(this), addedFunds), "funding transfer failed");
require(collateralToken.approve(address(conditionalTokens), addedFunds), "approval for splits failed");
splitPositionThroughAllConditions(addedFunds);
_mint(msg.sender, mintAmount);
conditionalTokens.safeBatchTransferFrom(address(this), msg.sender, positionIds, sendBackAmounts, "");
// transform sendBackAmounts to array of amounts added
for (uint i = 0; i < sendBackAmounts.length; i++) {
sendBackAmounts[i] = addedFunds.sub(sendBackAmounts[i]);
}
emit FPMMFundingAdded(msg.sender, sendBackAmounts, mintAmount);
}
function removeFunding(uint sharesToBurn)
external
{
uint[] memory poolBalances = getPoolBalances();
uint[] memory sendAmounts = new uint[](poolBalances.length);
uint poolShareSupply = totalSupply();
for(uint i = 0; i < poolBalances.length; i++) {
sendAmounts[i] = poolBalances[i].mul(sharesToBurn) / poolShareSupply;
}
uint collateralRemovedFromFeePool = collateralToken.balanceOf(address(this));
_burn(msg.sender, sharesToBurn);
collateralRemovedFromFeePool = collateralRemovedFromFeePool.sub(
collateralToken.balanceOf(address(this))
);
conditionalTokens.safeBatchTransferFrom(address(this), msg.sender, positionIds, sendAmounts, "");
emit FPMMFundingRemoved(msg.sender, sendAmounts, collateralRemovedFromFeePool, sharesToBurn);
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
)
external
returns (bytes4)
{
if (operator == address(this)) {
return this.onERC1155Received.selector;
}
return 0x0;
}
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
)
external
returns (bytes4)
{
if (operator == address(this) && from == address(0)) {
return this.onERC1155BatchReceived.selector;
}
return 0x0;
}
function calcBuyAmount(uint investmentAmount, uint outcomeIndex) public view returns (uint) {
require(outcomeIndex < positionIds.length, "invalid outcome index");
uint[] memory poolBalances = getPoolBalances();
uint investmentAmountMinusFees = investmentAmount.sub(investmentAmount.mul(fee) / ONE);
uint buyTokenPoolBalance = poolBalances[outcomeIndex];
uint endingOutcomeBalance = buyTokenPoolBalance.mul(ONE);
for(uint i = 0; i < poolBalances.length; i++) {
if(i != outcomeIndex) {
uint poolBalance = poolBalances[i];
endingOutcomeBalance = endingOutcomeBalance.mul(poolBalance).ceildiv(
poolBalance.add(investmentAmountMinusFees)
);
}
}
require(endingOutcomeBalance > 0, "must have non-zero balances");
return buyTokenPoolBalance.add(investmentAmountMinusFees).sub(endingOutcomeBalance.ceildiv(ONE));
}
function calcSellAmount(uint returnAmount, uint outcomeIndex) public view returns (uint outcomeTokenSellAmount) {
require(outcomeIndex < positionIds.length, "invalid outcome index");
uint[] memory poolBalances = getPoolBalances();
uint returnAmountPlusFees = returnAmount.mul(ONE) / ONE.sub(fee);
uint sellTokenPoolBalance = poolBalances[outcomeIndex];
uint endingOutcomeBalance = sellTokenPoolBalance.mul(ONE);
for(uint i = 0; i < poolBalances.length; i++) {
if(i != outcomeIndex) {
uint poolBalance = poolBalances[i];
endingOutcomeBalance = endingOutcomeBalance.mul(poolBalance).ceildiv(
poolBalance.sub(returnAmountPlusFees)
);
}
}
require(endingOutcomeBalance > 0, "must have non-zero balances");
return returnAmountPlusFees.add(endingOutcomeBalance.ceildiv(ONE)).sub(sellTokenPoolBalance);
}
function buy(uint investmentAmount, uint outcomeIndex, uint minOutcomeTokensToBuy) external {
uint outcomeTokensToBuy = calcBuyAmount(investmentAmount, outcomeIndex);
require(outcomeTokensToBuy >= minOutcomeTokensToBuy, "minimum buy amount not reached");
require(collateralToken.transferFrom(msg.sender, address(this), investmentAmount), "cost transfer failed");
uint feeAmount = investmentAmount.mul(fee) / ONE;
feePoolWeight = feePoolWeight.add(feeAmount);
uint investmentAmountMinusFees = investmentAmount.sub(feeAmount);
require(collateralToken.approve(address(conditionalTokens), investmentAmountMinusFees), "approval for splits failed");
splitPositionThroughAllConditions(investmentAmountMinusFees);
conditionalTokens.safeTransferFrom(address(this), msg.sender, positionIds[outcomeIndex], outcomeTokensToBuy, "");
emit FPMMBuy(msg.sender, investmentAmount, feeAmount, outcomeIndex, outcomeTokensToBuy);
}
function sell(uint returnAmount, uint outcomeIndex, uint maxOutcomeTokensToSell) external {
uint outcomeTokensToSell = calcSellAmount(returnAmount, outcomeIndex);
require(outcomeTokensToSell <= maxOutcomeTokensToSell, "maximum sell amount exceeded");
conditionalTokens.safeTransferFrom(msg.sender, address(this), positionIds[outcomeIndex], outcomeTokensToSell, "");
uint feeAmount = returnAmount.mul(fee) / (ONE.sub(fee));
feePoolWeight = feePoolWeight.add(feeAmount);
uint returnAmountPlusFees = returnAmount.add(feeAmount);
mergePositionsThroughAllConditions(returnAmountPlusFees);
require(collateralToken.transfer(msg.sender, returnAmount), "return transfer failed");
emit FPMMSell(msg.sender, returnAmount, feeAmount, outcomeIndex, outcomeTokensToSell);
}
}
// for proxying purposes
contract FixedProductMarketMakerData {
mapping (address => uint256) internal _balances;
mapping (address => mapping (address => uint256)) internal _allowances;
uint256 internal _totalSupply;
bytes4 internal constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
mapping(bytes4 => bool) internal _supportedInterfaces;
event FPMMFundingAdded(
address indexed funder,
uint[] amountsAdded,
uint sharesMinted
);
event FPMMFundingRemoved(
address indexed funder,
uint[] amountsRemoved,
uint collateralRemovedFromFeePool,
uint sharesBurnt
);
event FPMMBuy(
address indexed buyer,
uint investmentAmount,
uint feeAmount,
uint indexed outcomeIndex,
uint outcomeTokensBought
);
event FPMMSell(
address indexed seller,
uint returnAmount,
uint feeAmount,
uint indexed outcomeIndex,
uint outcomeTokensSold
);
ConditionalTokens internal conditionalTokens;
IERC20 internal collateralToken;
bytes32[] internal conditionIds;
uint internal fee;
uint internal feePoolWeight;
uint[] internal outcomeSlotCounts;
bytes32[][] internal collectionIds;
uint[] internal positionIds;
mapping (address => uint256) internal withdrawnFees;
uint internal totalWithdrawnFees;
}