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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
pragma solidity ^0.6.12;
interface VatLike { function move(address, address, uint256) external; function flux(bytes32, address, address, uint256) external; function ilks(bytes32) external returns (uint256, uint256, uint256, uint256, uint256); function suck(address, address, uint256) external; }
interface PipLike { function peek() external returns (bytes32, bool); }
interface SpotterLike { function par() external returns (uint256); function ilks(bytes32) external returns (PipLike, uint256); }
interface DogLike { function chop(bytes32) external returns (uint256); function digs(bytes32, uint256) external; }
interface ClipperCallee { function clipperCall(address, uint256, uint256, bytes calldata) external; }
interface AbacusLike { function price(uint256, uint256) external view returns (uint256); }
contract Clipper { mapping(address => uint256) public wards;
function rely(address usr) external auth { wards[usr] = 1; emit Rely(usr); }
function deny(address usr) external auth { wards[usr] = 0; emit Deny(usr); }
modifier auth() { require(wards[msg.sender] == 1, "Clipper/not-authorized"); _; }
bytes32 public immutable ilk; VatLike public immutable vat;
DogLike public dog; address public vow; SpotterLike public spotter; AbacusLike public calc;
uint256 public buf; uint256 public tail; uint256 public cusp; uint64 public chip; uint192 public tip; uint256 public chost;
uint256 public kicks; uint256[] public active;
struct Sale { uint256 pos; uint256 tab; uint256 lot; address usr; uint96 tic; uint256 top; }
mapping(uint256 => Sale) public sales;
uint256 internal locked;
uint256 public stopped = 0;
event Rely(address indexed usr); event Deny(address indexed usr);
event File(bytes32 indexed what, uint256 data); event File(bytes32 indexed what, address data);
event Kick( uint256 indexed id, uint256 top, uint256 tab, uint256 lot, address indexed usr, address indexed kpr, uint256 coin );
event Take( uint256 indexed id, uint256 max, uint256 price, uint256 owe, uint256 tab, uint256 lot, address indexed usr );
event Redo( uint256 indexed id, uint256 top, uint256 tab, uint256 lot, address indexed usr, address indexed kpr, uint256 coin );
event Yank(uint256 id);
constructor(address vat_, address spotter_, address dog_, bytes32 ilk_) public { vat = VatLike(vat_); spotter = SpotterLike(spotter_); dog = DogLike(dog_); ilk = ilk_; buf = RAY; wards[msg.sender] = 1; emit Rely(msg.sender); }
modifier lock() { require(locked == 0, "Clipper/system-locked"); locked = 1; _; locked = 0; }
modifier isStopped(uint256 level) { require(stopped < level, "Clipper/stopped-incorrect"); _; }
function file(bytes32 what, uint256 data) external auth lock { if (what == "buf") buf = data; else if (what == "tail") tail = data;
else if (what == "cusp") cusp = data;
else if (what == "chip") chip = uint64(data);
else if (what == "tip") tip = uint192(data);
else if (what == "stopped") stopped = data;
else revert("Clipper/file-unrecognized-param"); emit File(what, data); }
function file(bytes32 what, address data) external auth lock { if (what == "spotter") spotter = SpotterLike(data); else if (what == "dog") dog = DogLike(data); else if (what == "vow") vow = data; else if (what == "calc") calc = AbacusLike(data); else revert("Clipper/file-unrecognized-param"); emit File(what, data); }
uint256 constant BLN = 10 ** 9; uint256 constant WAD = 10 ** 18; uint256 constant RAY = 10 ** 27;
function min(uint256 x, uint256 y) internal pure returns (uint256 z) { z = x <= y ? x : y; }
function add(uint256 x, uint256 y) internal pure returns (uint256 z) { require((z = x + y) >= x); }
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) { require((z = x - y) <= x); }
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) { require(y == 0 || (z = x * y) / y == x); }
function wmul(uint256 x, uint256 y) internal pure returns (uint256 z) { z = mul(x, y) / WAD; }
function rmul(uint256 x, uint256 y) internal pure returns (uint256 z) { z = mul(x, y) / RAY; }
function rdiv(uint256 x, uint256 y) internal pure returns (uint256 z) { z = mul(x, RAY) / y; }
function getFeedPrice() internal returns (uint256 feedPrice) { (PipLike pip,) = spotter.ilks(ilk); (bytes32 val, bool has) = pip.peek(); require(has, "Clipper/invalid-price"); feedPrice = rdiv(mul(uint256(val), BLN), spotter.par()); }
function kick( uint256 tab, uint256 lot, address usr, address kpr ) external auth lock isStopped(1) returns (uint256 id) { require(tab > 0, "Clipper/zero-tab"); require(lot > 0, "Clipper/zero-lot"); require(usr != address(0), "Clipper/zero-usr"); id = ++kicks; require(id > 0, "Clipper/overflow");
active.push(id);
sales[id].pos = active.length - 1;
sales[id].tab = tab; sales[id].lot = lot; sales[id].usr = usr; sales[id].tic = uint96(block.timestamp);
uint256 top; top = rmul(getFeedPrice(), buf); require(top > 0, "Clipper/zero-top-price"); sales[id].top = top;
uint256 _tip = tip; uint256 _chip = chip; uint256 coin; if (_tip > 0 || _chip > 0) { coin = add(_tip, wmul(tab, _chip)); vat.suck(vow, kpr, coin); }
emit Kick(id, top, tab, lot, usr, kpr, coin); }
function redo( uint256 id, address kpr ) external lock isStopped(2) { address usr = sales[id].usr; uint96 tic = sales[id].tic; uint256 top = sales[id].top;
require(usr != address(0), "Clipper/not-running-auction");
(bool done,) = status(tic, top); require(done, "Clipper/cannot-reset");
uint256 tab = sales[id].tab; uint256 lot = sales[id].lot; sales[id].tic = uint96(block.timestamp);
uint256 feedPrice = getFeedPrice(); top = rmul(feedPrice, buf); require(top > 0, "Clipper/zero-top-price"); sales[id].top = top;
uint256 _tip = tip; uint256 _chip = chip; uint256 coin; if (_tip > 0 || _chip > 0) { uint256 _chost = chost; if (tab >= _chost && mul(lot, feedPrice) >= _chost) { coin = add(_tip, wmul(tab, _chip)); vat.suck(vow, kpr, coin); } }
emit Redo(id, top, tab, lot, usr, kpr, coin); }
function take( uint256 id, uint256 amt, uint256 max, address who, bytes calldata data ) external lock isStopped(3) { address usr = sales[id].usr; uint96 tic = sales[id].tic;
require(usr != address(0), "Clipper/not-running-auction");
uint256 price; { bool done; (done, price) = status(tic, sales[id].top);
require(!done, "Clipper/needs-reset"); }
require(max >= price, "Clipper/too-expensive");
uint256 lot = sales[id].lot; uint256 tab = sales[id].tab; uint256 owe;
{ uint256 slice = min(lot, amt);
owe = mul(slice, price);
if (owe > tab) { owe = tab; slice = owe / price; } else if (owe < tab && slice < lot) { uint256 _chost = chost; if (tab - owe < _chost) { require(tab > _chost, "Clipper/no-partial-purchase"); owe = tab - _chost; slice = owe / price; } }
tab = tab - owe; lot = lot - slice;
vat.flux(ilk, address(this), who, slice);
DogLike dog_ = dog; if (data.length > 0 && who != address(vat) && who != address(dog_)) { ClipperCallee(who).clipperCall(msg.sender, owe, slice, data); }
vat.move(msg.sender, vow, owe);
dog_.digs(ilk, lot == 0 ? tab + owe : owe); }
if (lot == 0) { _remove(id); } else if (tab == 0) { vat.flux(ilk, address(this), usr, lot); _remove(id); } else { sales[id].tab = tab; sales[id].lot = lot; }
emit Take(id, max, price, owe, tab, lot, usr); }
function _remove(uint256 id) internal { uint256 _move = active[active.length - 1]; if (id != _move) { uint256 _index = sales[id].pos; active[_index] = _move; sales[_move].pos = _index; } active.pop(); delete sales[id]; }
function count() external view returns (uint256) { return active.length; }
function list() external view returns (uint256[] memory) { return active; }
function getStatus(uint256 id) external view returns (bool needsRedo, uint256 price, uint256 lot, uint256 tab) { address usr = sales[id].usr; uint96 tic = sales[id].tic;
bool done; (done, price) = status(tic, sales[id].top);
needsRedo = usr != address(0) && done; lot = sales[id].lot; tab = sales[id].tab; }
function status(uint96 tic, uint256 top) internal view returns (bool done, uint256 price) { price = calc.price(top, sub(block.timestamp, tic)); done = (sub(block.timestamp, tic) > tail || rdiv(price, top) < cusp); }
function upchost() external { (,,,, uint256 _dust) = VatLike(vat).ilks(ilk); chost = wmul(_dust, dog.chop(ilk)); }
function yank(uint256 id) external auth lock { require(sales[id].usr != address(0), "Clipper/not-running-auction"); dog.digs(ilk, sales[id].tab); vat.flux(ilk, address(this), msg.sender, sales[id].lot); _remove(id); emit Yank(id); } }
|