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
|
pragma solidity ^0.6.12;
contract Vat { mapping(address => uint256) public wards;
function rely(address usr) external auth { require(live == 1, "Vat/not-live"); wards[usr] = 1; }
function deny(address usr) external auth { require(live == 1, "Vat/not-live"); wards[usr] = 0; }
modifier auth() { require(wards[msg.sender] == 1, "Vat/not-authorized"); _; }
mapping(address => mapping(address => uint256)) public can;
function hope(address usr) external { can[msg.sender][usr] = 1; }
function nope(address usr) external { can[msg.sender][usr] = 0; }
function wish(address bit, address usr) internal view returns (bool) { return either(bit == usr, can[bit][usr] == 1); }
struct Ilk { uint256 Art; uint256 rate; uint256 spot; uint256 line; uint256 dust; }
struct Urn { uint256 ink; uint256 art; }
mapping(bytes32 => Ilk) public ilks;
mapping(bytes32 => mapping(address => Urn)) public urns; mapping(bytes32 => mapping(address => uint256)) public gem; mapping(address => uint256) public dai; mapping(address => uint256) public sin;
uint256 public debt; uint256 public vice; uint256 public Line; uint256 public live;
constructor() public { wards[msg.sender] = 1; live = 1; }
function _add(uint256 x, int256 y) internal pure returns (uint256 z) { z = x + uint256(y); require(y >= 0 || z <= x); require(y <= 0 || z >= x); }
function _sub(uint256 x, int256 y) internal pure returns (uint256 z) { z = x - uint256(y); require(y <= 0 || z <= x); require(y >= 0 || z >= x); }
function _mul(uint256 x, int256 y) internal pure returns (int256 z) { z = int256(x) * y; require(int256(x) >= 0); require(y == 0 || z / y == int256(x)); }
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 init(bytes32 ilk) external auth { require(ilks[ilk].rate == 0, "Vat/ilk-already-init"); ilks[ilk].rate = 10 ** 27; }
function file(bytes32 what, uint256 data) external auth { require(live == 1, "Vat/not-live"); if (what == "Line") Line = data; else revert("Vat/file-unrecognized-param"); }
function file(bytes32 ilk, bytes32 what, uint256 data) external auth { require(live == 1, "Vat/not-live"); if (what == "spot") ilks[ilk].spot = data; else if (what == "line") ilks[ilk].line = data; else if (what == "dust") ilks[ilk].dust = data; else revert("Vat/file-unrecognized-param"); }
function cage() external auth { live = 0; }
function slip(bytes32 ilk, address usr, int256 wad) external auth { gem[ilk][usr] = _add(gem[ilk][usr], wad); }
function flux(bytes32 ilk, address src, address dst, uint256 wad) external { require(wish(src, msg.sender), "Vat/not-allowed"); gem[ilk][src] = _sub(gem[ilk][src], wad); gem[ilk][dst] = _add(gem[ilk][dst], wad); }
function move(address src, address dst, uint256 rad) external { require(wish(src, msg.sender), "Vat/not-allowed"); dai[src] = _sub(dai[src], rad); dai[dst] = _add(dai[dst], rad); }
function either(bool x, bool y) internal pure returns (bool z) { assembly { z := or(x, y) } }
function both(bool x, bool y) internal pure returns (bool z) { assembly { z := and(x, y) } }
function frob(bytes32 i, address u, address v, address w, int256 dink, int256 dart) external { require(live == 1, "Vat/not-live");
Urn memory urn = urns[i][u]; Ilk memory ilk = ilks[i]; require(ilk.rate != 0, "Vat/ilk-not-init");
urn.ink = _add(urn.ink, dink); urn.art = _add(urn.art, dart); ilk.Art = _add(ilk.Art, dart);
int256 dtab = _mul(ilk.rate, dart); uint256 tab = _mul(ilk.rate, urn.art); debt = _add(debt, dtab);
require(either(dart <= 0, both(_mul(ilk.Art, ilk.rate) <= ilk.line, debt <= Line)), "Vat/ceiling-exceeded"); require(either(both(dart <= 0, dink >= 0), tab <= _mul(urn.ink, ilk.spot)), "Vat/not-safe");
require(either(both(dart <= 0, dink >= 0), wish(u, msg.sender)), "Vat/not-allowed-u"); require(either(dink <= 0, wish(v, msg.sender)), "Vat/not-allowed-v"); require(either(dart >= 0, wish(w, msg.sender)), "Vat/not-allowed-w");
require(either(urn.art == 0, tab >= ilk.dust), "Vat/dust");
gem[i][v] = _sub(gem[i][v], dink); dai[w] = _add(dai[w], dtab);
urns[i][u] = urn; ilks[i] = ilk; }
function fork(bytes32 ilk, address src, address dst, int256 dink, int256 dart) external { Urn storage u = urns[ilk][src]; Urn storage v = urns[ilk][dst]; Ilk storage i = ilks[ilk];
u.ink = _sub(u.ink, dink); u.art = _sub(u.art, dart); v.ink = _add(v.ink, dink); v.art = _add(v.art, dart);
uint256 utab = _mul(u.art, i.rate); uint256 vtab = _mul(v.art, i.rate);
require(both(wish(src, msg.sender), wish(dst, msg.sender)), "Vat/not-allowed");
require(utab <= _mul(u.ink, i.spot), "Vat/not-safe-src"); require(vtab <= _mul(v.ink, i.spot), "Vat/not-safe-dst");
require(either(utab >= i.dust, u.art == 0), "Vat/dust-src"); require(either(vtab >= i.dust, v.art == 0), "Vat/dust-dst"); }
function grab(bytes32 i, address u, address v, address w, int256 dink, int256 dart) external auth { Urn storage urn = urns[i][u]; Ilk storage ilk = ilks[i];
urn.ink = _add(urn.ink, dink); urn.art = _add(urn.art, dart); ilk.Art = _add(ilk.Art, dart);
int256 dtab = _mul(ilk.rate, dart);
gem[i][v] = _sub(gem[i][v], dink); sin[w] = _sub(sin[w], dtab); vice = _sub(vice, dtab); }
function heal(uint256 rad) external { address u = msg.sender; sin[u] = _sub(sin[u], rad); dai[u] = _sub(dai[u], rad); vice = _sub(vice, rad); debt = _sub(debt, rad); }
function suck(address u, address v, uint256 rad) external auth { sin[u] = _add(sin[u], rad); dai[v] = _add(dai[v], rad); vice = _add(vice, rad); debt = _add(debt, rad); }
function fold(bytes32 i, address u, int256 rate) external auth { require(live == 1, "Vat/not-live"); Ilk storage ilk = ilks[i]; ilk.rate = _add(ilk.rate, rate); int256 rad = _mul(ilk.Art, rate); dai[u] = _add(dai[u], rad); debt = _add(debt, rad); } }
|