├── .gitattributes ├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── audits └── ChainSecurity_MakerDAO_Curve_LP__stETH_oracle_audit.pdf ├── src ├── CurveLPOracle.sol ├── CurveLPOracle.t.sol ├── CurveLPOracleFactory.t.sol ├── StethPrice.sol ├── StethPrice.t.sol └── steCRV.t.sol └── test.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sol linguist-language=Solidity 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /out 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/ds-test"] 2 | path = lib/ds-test 3 | url = https://github.com/dapphub/ds-test 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build 2 | 3 | all : build 4 | build :; DAPP_BUILD_OPTIMIZE=1 DAPP_BUILD_OPTIMIZE_RUNS=1000000 dapp --use solc:0.8.13 build 5 | clean :; dapp clean 6 | test : build 7 | ./test.sh $(match) 8 | deploy :; dapp create CurveLPOracle 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # curve-lp-oracle -------------------------------------------------------------------------------- /audits/ChainSecurity_MakerDAO_Curve_LP__stETH_oracle_audit.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/makerdao/curve-lp-oracle/302f5e6966fdbfebe0f7063c9d6f6bc1f6470f28/audits/ChainSecurity_MakerDAO_Curve_LP__stETH_oracle_audit.pdf -------------------------------------------------------------------------------- /src/CurveLPOracle.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | /// CurveLPOracle.sol 4 | 5 | // Copyright (C) 2021 Dai Foundation 6 | 7 | // This program is free software: you can redistribute it and/or modify 8 | // it under the terms of the GNU Affero General Public License as published by 9 | // the Free Software Foundation, either version 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU Affero General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU Affero General Public License 18 | // along with this program. If not, see . 19 | 20 | pragma solidity 0.8.13; 21 | 22 | interface AddressProviderLike { 23 | function get_registry() external view returns (address); 24 | } 25 | 26 | interface CurveRegistryLike { 27 | function get_n_coins(address) external view returns (uint256[2] calldata); 28 | } 29 | 30 | interface CurvePoolLike { 31 | function coins(uint256) external view returns (address); 32 | function get_virtual_price() external view returns (uint256); 33 | function lp_token() external view returns (address); 34 | function remove_liquidity(uint256, uint256[2] calldata) external returns (uint256); 35 | } 36 | 37 | interface OracleLike { 38 | function read() external view returns (uint256); 39 | } 40 | 41 | contract CurveLPOracleFactory { 42 | 43 | AddressProviderLike public immutable ADDRESS_PROVIDER; 44 | 45 | event NewCurveLPOracle(address owner, address orcl, bytes32 wat, address pool, bool nonreentrant); 46 | 47 | constructor(address addressProvider) { 48 | ADDRESS_PROVIDER = AddressProviderLike(addressProvider); 49 | } 50 | 51 | function build( 52 | address _owner, 53 | address _pool, 54 | bytes32 _wat, 55 | address[] calldata _orbs, 56 | bool _nonreentrant 57 | ) external returns (address orcl) { 58 | uint256 ncoins = CurveRegistryLike(ADDRESS_PROVIDER.get_registry()).get_n_coins(_pool)[1]; 59 | require(ncoins == _orbs.length, "CurveLPOracleFactory/wrong-num-of-orbs"); 60 | orcl = address(new CurveLPOracle(_owner, _pool, _wat, _orbs, _nonreentrant)); 61 | emit NewCurveLPOracle(_owner, orcl, _wat, _pool, _nonreentrant); 62 | } 63 | } 64 | 65 | contract CurveLPOracle { 66 | 67 | // --- Auth --- 68 | mapping (address => uint256) public wards; // Addresses with admin authority 69 | function rely(address _usr) external auth { wards[_usr] = 1; emit Rely(_usr); } // Add admin 70 | function deny(address _usr) external auth { wards[_usr] = 0; emit Deny(_usr); } // Remove admin 71 | modifier auth { 72 | require(wards[msg.sender] == 1, "CurveLPOracle/not-authorized"); 73 | _; 74 | } 75 | 76 | address public immutable src; // Price source, do not remove as needed for OmegaPoker 77 | 78 | // stopped, hop, and zph are packed into single slot to reduce SLOADs; 79 | // this outweighs the added bitmasking overhead. 80 | uint8 public stopped; // Stop/start ability to update 81 | uint16 public hop = 1 hours; // Minimum time in between price updates 82 | uint232 public zph; // Time of last price update plus hop 83 | 84 | // --- Whitelisting --- 85 | mapping (address => uint256) public bud; 86 | modifier toll { require(bud[msg.sender] == 1, "CurveLPOracle/not-whitelisted"); _; } 87 | 88 | struct Feed { 89 | uint128 val; // Price 90 | uint128 has; // Is price valid 91 | } 92 | 93 | Feed internal cur; // Current price (storage slot 0x3) 94 | Feed internal nxt; // Queued price (storage slot 0x4) 95 | 96 | address[] public orbs; // array of price feeds for pool assets, same order as in the pool 97 | 98 | address public immutable pool; // Address of underlying Curve pool 99 | bytes32 public immutable wat; // Label of token whose price is being tracked 100 | uint256 public immutable ncoins; // Number of tokens in underlying Curve pool 101 | bool public immutable nonreentrant; // Whether to prevent pool reentrancy 102 | 103 | // --- Events --- 104 | event Rely(address indexed usr); 105 | event Deny(address indexed usr); 106 | event Stop(); 107 | event Start(); 108 | event Step(uint256 hop); 109 | event Link(uint256 id, address orb); 110 | event Value(uint128 curVal, uint128 nxtVal); 111 | event Kiss(address a); 112 | event Diss(address a); 113 | 114 | // --- Init --- 115 | constructor(address _ward, address _pool, bytes32 _wat, address[] memory _orbs, bool _nonreentrant) { 116 | require(_pool != address(0), "CurveLPOracle/invalid-pool"); 117 | uint256 _ncoins = _orbs.length; 118 | pool = _pool; 119 | src = CurvePoolLike(_pool).lp_token(); 120 | wat = _wat; 121 | ncoins = _ncoins; 122 | nonreentrant = _nonreentrant; 123 | for (uint256 i = 0; i < _ncoins;) { 124 | require(_orbs[i] != address(0), "CurveLPOracle/invalid-orb"); 125 | orbs.push(_orbs[i]); 126 | unchecked { i++; } 127 | } 128 | require(_ward != address(0), "CurveLPOracle/ward-0"); 129 | wards[_ward] = 1; 130 | emit Rely(_ward); 131 | } 132 | 133 | function stop() external auth { 134 | stopped = 1; 135 | delete cur; 136 | delete nxt; 137 | zph = 0; 138 | emit Stop(); 139 | } 140 | 141 | function start() external auth { 142 | stopped = 0; 143 | emit Start(); 144 | } 145 | 146 | function step(uint16 _hop) external auth { 147 | uint16 old = hop; 148 | hop = _hop; 149 | if (zph > old) { // if false, zph will be unset and no update is needed 150 | zph = (zph - old) + _hop; 151 | } 152 | emit Step(_hop); 153 | } 154 | 155 | function link(uint256 _id, address _orb) external auth { 156 | require(_orb != address(0), "CurveLPOracle/invalid-orb"); 157 | require(_id < ncoins, "CurveLPOracle/invalid-orb-index"); 158 | orbs[_id] = _orb; 159 | emit Link(_id, _orb); 160 | } 161 | 162 | // For consistency with other oracles 163 | function zzz() external view returns (uint256) { 164 | if (zph == 0) return 0; // backwards compatibility 165 | return zph - hop; 166 | } 167 | 168 | function pass() external view returns (bool) { 169 | return block.timestamp >= zph; 170 | } 171 | 172 | // Marked payable to save gas. DO *NOT* send ETH to poke(), it will be lost permanently. 173 | function poke() external payable { 174 | 175 | // Ensure a single SLOAD while avoiding solc's excessive bitmasking bureaucracy. 176 | uint256 hop_; 177 | { 178 | 179 | // Block-scoping these variables saves some gas. 180 | uint256 stopped_; 181 | uint256 zph_; 182 | assembly { 183 | let slot1 := sload(1) 184 | stopped_ := and(slot1, 0xff ) 185 | hop_ := and(shr(8, slot1), 0xffff) 186 | zph_ := shr(24, slot1) 187 | } 188 | 189 | // When stopped, values are set to zero and should remain such; thus, disallow updating in that case. 190 | require(stopped_ == 0, "CurveLPOracle/is-stopped"); 191 | 192 | // Equivalent to requiring that pass() returns true; logic repeated to save gas. 193 | require(block.timestamp >= zph_, "CurveLPOracle/not-passed"); 194 | } 195 | 196 | uint256 val = type(uint256).max; 197 | for (uint256 i = 0; i < ncoins;) { 198 | uint256 price = OracleLike(orbs[i]).read(); 199 | if (price < val) val = price; 200 | unchecked { i++; } 201 | } 202 | val = val * CurvePoolLike(pool).get_virtual_price() / 10**18; 203 | require(val != 0, "CurveLPOracle/zero-price"); 204 | require(val <= type(uint128).max, "CurveLPOracle/price-overflow"); 205 | Feed memory cur_ = nxt; 206 | cur = cur_; 207 | nxt = Feed(uint128(val), 1); 208 | 209 | // The below is equivalent to: 210 | // zph = block.timestamp + hop 211 | // but ensures no extra SLOADs are performed. 212 | // 213 | // Even if _hop = (2^16 - 1), the maximum possible value, add(timestamp(), _hop) 214 | // will not overflow (even a 232 bit value) for a very long time. 215 | // 216 | // Also, we know stopped was zero, so there is no need to account for it explicitly here. 217 | assembly { 218 | sstore( 219 | 1, 220 | add( 221 | shl(24, add(timestamp(), hop_)), // zph value starts 24 bits in 222 | shl(8, hop_) // hop value starts 8 bits in 223 | ) 224 | ) 225 | } 226 | 227 | // This will revert if called during execution of a state-modifying pool function. 228 | if (nonreentrant) { 229 | uint256[2] calldata amounts; 230 | CurvePoolLike(pool).remove_liquidity(0, amounts); 231 | } 232 | 233 | emit Value(cur_.val, uint128(val)); 234 | 235 | // Safe to terminate immediately since no postfix modifiers are applied. 236 | assembly { stop() } 237 | } 238 | 239 | function peek() external view toll returns (bytes32,bool) { 240 | return (bytes32(uint256(cur.val)), cur.has == 1); 241 | } 242 | 243 | function peep() external view toll returns (bytes32,bool) { 244 | return (bytes32(uint256(nxt.val)), nxt.has == 1); 245 | } 246 | 247 | function read() external view toll returns (bytes32) { 248 | require(cur.has == 1, "CurveLPOracle/no-current-value"); 249 | return (bytes32(uint256(cur.val))); 250 | } 251 | 252 | function kiss(address _a) external auth { 253 | require(_a != address(0), "CurveLPOracle/no-contract-0"); 254 | bud[_a] = 1; 255 | emit Kiss(_a); 256 | } 257 | 258 | function kiss(address[] calldata _a) external auth { 259 | for(uint256 i = 0; i < _a.length;) { 260 | require(_a[i] != address(0), "CurveLPOracle/no-contract-0"); 261 | bud[_a[i]] = 1; 262 | emit Kiss(_a[i]); 263 | unchecked { i++; } 264 | } 265 | } 266 | 267 | function diss(address _a) external auth { 268 | bud[_a] = 0; 269 | emit Diss(_a); 270 | } 271 | 272 | function diss(address[] calldata _a) external auth { 273 | for(uint256 i = 0; i < _a.length;) { 274 | bud[_a[i]] = 0; 275 | emit Diss(_a[i]); 276 | unchecked { i++; } 277 | } 278 | } 279 | 280 | // Needed for Curve pool reentrancy checks, since remove_liquidity with call the caller to do an ETH transfer. 281 | // Technically we receive 0 Ether, but using a payable function saves a little gas. 282 | receive() external payable {} 283 | } 284 | -------------------------------------------------------------------------------- /src/CurveLPOracle.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | // Copyright (C) 2021 Dai Foundation 4 | 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU Affero General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU Affero General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Affero General Public License 16 | // along with this program. If not, see . 17 | 18 | pragma solidity 0.8.13; 19 | 20 | import "ds-test/test.sol"; 21 | 22 | import "./CurveLPOracle.sol"; 23 | 24 | interface Hevm { 25 | function warp(uint256) external; 26 | function load(address, bytes32 slot) external returns (bytes32); 27 | } 28 | 29 | contract MockCurvePool { 30 | uint256 public get_virtual_price; 31 | address public lp_token; 32 | bool public locked; 33 | function setVirtualPrice(uint256 _vp) external { 34 | get_virtual_price = _vp; 35 | } 36 | function lock() external { 37 | locked = true; 38 | } 39 | function remove_liquidity(uint256 _amount, uint256[2] calldata _min_amounts) external view { 40 | _amount; _min_amounts; // silence warnings 41 | require(!locked); 42 | } 43 | } 44 | 45 | contract MockOracle { 46 | uint256 private price; 47 | function read() external view returns (uint256) { 48 | return price; 49 | } 50 | function setPrice(uint256 _price) external { 51 | price = _price; 52 | } 53 | } 54 | 55 | contract CurveLPOracleTest is DSTest { 56 | uint256 constant WAD = 10**18; 57 | uint16 constant DEFAULT_HOP = 3600; // 1 hour in seconds 58 | 59 | Hevm hevm; 60 | MockCurvePool pool; 61 | address[] orbs; 62 | CurveLPOracle oracle; 63 | 64 | function setUp() public { 65 | hevm = Hevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D); 66 | pool = new MockCurvePool(); 67 | orbs.push(address(new MockOracle())); 68 | orbs.push(address(new MockOracle())); 69 | orbs.push(address(new MockOracle())); 70 | oracle = new CurveLPOracle(address(this), address(pool), "123CRV", orbs, false); 71 | oracle.step(DEFAULT_HOP); 72 | 73 | // set up some default price values 74 | MockOracle(orbs[0]).setPrice(100 * WAD); 75 | MockOracle(orbs[1]).setPrice(100 * WAD); 76 | MockOracle(orbs[2]).setPrice(100 * WAD); 77 | pool.setVirtualPrice(WAD); // 1 78 | 79 | // Get valid cur and nxt 80 | oracle.poke(); 81 | hevm.warp(oracle.zph()); 82 | oracle.poke(); 83 | } 84 | 85 | function test_constructor_and_public_fields() public { 86 | oracle = new CurveLPOracle(address(0x123), address(pool), "123CRV", orbs, false); 87 | assertEq(oracle.wards(address(0x123)), 1); 88 | assertTrue(oracle.pool() == address(pool)); 89 | assertTrue(oracle.wat() == "123CRV"); 90 | assertEq(oracle.ncoins(), orbs.length); 91 | assertTrue(!oracle.nonreentrant()); 92 | for (uint256 i = 0; i < orbs.length; i++) { 93 | assertTrue(oracle.orbs(i) == orbs[i]); 94 | } 95 | } 96 | 97 | function testFail_constructor_ward_zero() public { 98 | new CurveLPOracle(address(0), address(pool), "123CRV", orbs, false); 99 | } 100 | 101 | function testFail_constructor_pool_addr_zero() public { 102 | new CurveLPOracle(address(0x123), address(0), "123CRV", orbs, false); 103 | } 104 | 105 | function testFail_constructor_zero_orb() public { 106 | orbs[1] = address(0); 107 | new CurveLPOracle(address(0x123), address(pool), "123CRV", orbs, false); 108 | } 109 | 110 | function test_step() public { 111 | uint16 oldHop = oracle.hop(); 112 | uint16 newHop = oldHop + 1800; // newHop > oldHop 113 | uint232 zph = oracle.zph(); 114 | uint256 zzz = oracle.zzz(); 115 | assertTrue(zph >= oldHop); // we'll test the < case later 116 | 117 | // increase hop 118 | oracle.step(newHop); 119 | 120 | assertEq(oracle.hop(), newHop); 121 | assertEq(oracle.zph(), zph - oldHop + newHop); 122 | assertEq(oracle.zzz(), zzz); 123 | 124 | // decrease hop 125 | oracle.step(oldHop); 126 | 127 | assertEq(oracle.hop(), oldHop); 128 | assertEq(oracle.zph(), zph); // back to original value 129 | assertEq(oracle.zzz(), zzz); 130 | 131 | oracle.stop(); // sets zph to zero 132 | 133 | // Because block.timestamp is monotone and zph is always set to block.timestamp + hop in poke(), 134 | // zph == 0 is the only possible situation in which zph < oldHop can obtain. 135 | assertTrue(oracle.zph() < oldHop); 136 | assertEq(oracle.zph(), 0); 137 | assertEq(oracle.zzz(), 0); 138 | 139 | oracle.step(newHop); 140 | 141 | assertEq(oracle.hop(), newHop); 142 | assertEq(oracle.zph(), 0); // unset, so no change 143 | assertEq(oracle.zzz(), 0); 144 | 145 | oracle.step(0); 146 | assertEq(oracle.hop(), 0); 147 | assertEq(oracle.zph(), 0); // unset, so no change 148 | assertEq(oracle.zzz(), 0); 149 | 150 | oracle.step(1); // 1 != 0 151 | assertEq(oracle.hop(), 1); 152 | assertEq(oracle.zph(), 0); // unset, so no change 153 | assertEq(oracle.zzz(), 0); 154 | } 155 | 156 | function test_stop() public { 157 | oracle.kiss(address(this)); // whitelist for reading 158 | 159 | // Check that both current and pending values are non-zero and valid 160 | (bytes32 val, bool has) = oracle.peek(); 161 | assertGt(uint256(val), 0); 162 | assertTrue(has); 163 | (val, has) = oracle.peep(); 164 | assertGt(uint256(val), 0); 165 | assertTrue(has); 166 | 167 | assertGt(uint256(oracle.zph()), 0); 168 | assertGt(oracle.zzz(), 0); 169 | 170 | assertEq(oracle.stopped(), 0); 171 | oracle.stop(); 172 | assertEq(oracle.stopped(), 1); 173 | 174 | // Values should be zero and invalid 175 | (val, has) = oracle.peek(); 176 | assertEq(uint256(val), 0); 177 | assertTrue(!has); 178 | (val, has) = oracle.peep(); 179 | assertEq(uint256(val), 0); 180 | assertTrue(!has); 181 | 182 | assertEq(uint256(oracle.zph()), 0); 183 | assertEq(oracle.zzz(), 0); 184 | } 185 | 186 | function testFail_stop_not_authed() public { 187 | oracle.deny(address(this)); 188 | oracle.stop(); 189 | } 190 | 191 | function test_start() public { 192 | oracle.stop(); 193 | oracle.start(); 194 | assertEq(oracle.stopped(), 0); 195 | } 196 | 197 | function testFail_start_not_authed() public { 198 | oracle.stop(); 199 | oracle.deny(address(this)); 200 | oracle.start(); 201 | } 202 | 203 | function test_link() public { 204 | oracle.link(0, address(0x123)); 205 | assertTrue(oracle.orbs(0) == address(0x123)); 206 | oracle.link(2, address(0x321)); 207 | assertTrue(oracle.orbs(2) == address(0x321)); 208 | assertTrue(oracle.orbs(0) == address(0x123)); // should be unaffected 209 | } 210 | 211 | function testFail_link_zero_orb() public { 212 | oracle.link(0, address(0)); 213 | } 214 | 215 | function testFail_link_invalid_index() public { 216 | oracle.link(oracle.ncoins(), address(0x42)); 217 | } 218 | 219 | function test_poke_zph_zzz_pass_peep_peek_read() public { 220 | oracle.kiss(address(this)); 221 | 222 | MockOracle(orbs[0]).setPrice(100 * WAD); 223 | MockOracle(orbs[1]).setPrice(102 * WAD); 224 | MockOracle(orbs[2]).setPrice( 98 * WAD); 225 | pool.setVirtualPrice(105_831 * WAD / 10**5); // 1.05831 226 | 227 | uint256 hop = oracle.hop(); 228 | 229 | hevm.warp(oracle.zph()); 230 | assertTrue(oracle.pass()); 231 | 232 | oracle.poke(); 233 | 234 | assertEq(oracle.zph(), block.timestamp + hop); 235 | assertEq(oracle.zzz(), block.timestamp); 236 | assertTrue(!oracle.pass()); 237 | 238 | (bytes32 val, bool has) = oracle.peep(); 239 | assertTrue(has); 240 | uint256 firstPrice = (98 * WAD) * pool.get_virtual_price() / WAD; // minimum price used to value all assets 241 | assertEq(uint256(val), firstPrice); 242 | 243 | hevm.warp(oracle.zph() - 1); // warp to just before the next possible poke 244 | assertTrue(!oracle.pass()); 245 | 246 | hevm.warp(oracle.zph() + hop / 2); // warp somewhat beyond next possible poke 247 | assertTrue(oracle.pass()); 248 | 249 | // update values 250 | MockOracle(orbs[0]).setPrice(101 * WAD); 251 | MockOracle(orbs[1]).setPrice(105 * WAD); 252 | MockOracle(orbs[2]).setPrice(103 * WAD); 253 | pool.setVirtualPrice(106_792 * WAD / 10**5); // 1.06792 254 | 255 | oracle.poke(); 256 | 257 | assertEq(oracle.zph(), block.timestamp + hop); 258 | assertEq(oracle.zzz(), block.timestamp); 259 | assertTrue(!oracle.pass()); 260 | 261 | (val, has) = oracle.peek(); 262 | assertTrue(has); 263 | assertEq(uint256(val), firstPrice); 264 | assertEq(oracle.read(), val); 265 | 266 | (val, has) = oracle.peep(); 267 | assertTrue(has); 268 | uint256 secondPrice = (101 * WAD) * pool.get_virtual_price() / WAD; // minimum price used to value all assets 269 | assertEq(uint256(val), secondPrice); 270 | } 271 | 272 | function testFail_poke_stopped() public { 273 | hevm.warp(oracle.zph()); 274 | assertTrue(oracle.pass()); 275 | oracle.stop(); 276 | oracle.poke(); 277 | } 278 | 279 | function test_poke_restarted() public { 280 | hevm.warp(oracle.zph()); 281 | assertTrue(oracle.pass()); 282 | oracle.stop(); 283 | oracle.start(); 284 | oracle.poke(); 285 | } 286 | 287 | function test_kiss_single() public { 288 | assertTrue(oracle.bud(address(this)) == 0); // Verify caller is not whitelisted 289 | oracle.kiss(address(this)); // Whitelist caller 290 | assertTrue(oracle.bud(address(this)) == 1); // Verify caller is whitelisted 291 | oracle.kiss(address(this)); 292 | assertTrue(oracle.bud(address(this)) == 1); // Ensure idempotency 293 | } 294 | 295 | function testFail_kiss_single_not_authed() public { 296 | oracle.deny(address(this)); // Remove owner 297 | oracle.kiss(address(this)); // Attempt to whitelist caller 298 | } 299 | 300 | function testFail_kiss_single_zero_address() public { 301 | oracle.kiss(address(0)); // Attempt to whitelist 0 address 302 | } 303 | 304 | function test_diss_single() public { 305 | oracle.kiss(address(this)); // Whitelist caller 306 | assertTrue(oracle.bud(address(this)) == 1); // Verify caller is whitelisted 307 | oracle.diss(address(this)); // Remove caller from whitelist 308 | assertTrue(oracle.bud(address(this)) == 0); // Verify caller is not whitelisted 309 | oracle.diss(address(this)); 310 | assertTrue(oracle.bud(address(this)) == 0); // Ensure idempotency 311 | } 312 | 313 | function testFail_diss_single() public { 314 | oracle.deny(address(this)); // Remove owner 315 | oracle.diss(address(this)); // Attempt to remove caller from whitelist 316 | } 317 | 318 | function test_whiltelisting() public { 319 | oracle.kiss(address(this)); 320 | oracle.peek(); 321 | oracle.peep(); 322 | oracle.read(); 323 | } 324 | 325 | function testFail_peek_not_whitelisted() public { 326 | oracle.diss(address(this)); // Ensure caller not authorized to read prices 327 | oracle.peek(); 328 | } 329 | 330 | function testFail_peep_not_whitelisted() public { 331 | oracle.diss(address(this)); // Ensure caller not authorized to read prices 332 | oracle.peep(); 333 | } 334 | 335 | function testFail_read_not_whitelisted() public { 336 | oracle.diss(address(this)); // Ensure caller not authorized to read prices 337 | oracle.read(); 338 | } 339 | 340 | function test_kiss_and_diss_multiple() public { 341 | address[] memory guys = new address[](4); 342 | for (uint256 i = 0; i < guys.length; i++) { 343 | guys[i] = address(uint160(17 * i + 1)); 344 | assertEq(oracle.bud(guys[i]), 0); 345 | } 346 | oracle.kiss(guys); 347 | for (uint256 i = 0; i < guys.length; i++) { 348 | assertEq(oracle.bud(guys[i]), 1); 349 | } 350 | oracle.kiss(guys); // Idempotency 351 | for (uint256 i = 0; i < guys.length; i++) { 352 | assertEq(oracle.bud(guys[i]), 1); 353 | } 354 | oracle.diss(guys); 355 | for (uint256 i = 0; i < guys.length; i++) { 356 | assertEq(oracle.bud(guys[i]), 0); 357 | } 358 | oracle.diss(guys); // Idempotency 359 | for (uint256 i = 0; i < guys.length; i++) { 360 | assertEq(oracle.bud(guys[i]), 0); 361 | } 362 | } 363 | 364 | function testFail_kiss_multiple_not_authed() public { 365 | oracle.deny(address(this)); 366 | address[] memory guys = new address[](4); 367 | for (uint256 i = 0; i < guys.length; i++) { 368 | guys[i] = address(uint160(17 * i + 1)); 369 | assertEq(oracle.bud(guys[i]), 0); 370 | } 371 | oracle.kiss(guys); 372 | } 373 | 374 | function testFail_kiss_multiple_zero_address() public { 375 | address[] memory guys = new address[](4); 376 | for (uint256 i = 0; i < guys.length; i++) { 377 | guys[i] = address(uint160(17 * i + 1)); 378 | assertEq(oracle.bud(guys[i]), 0); 379 | } 380 | guys[1] = address(0); 381 | oracle.kiss(guys); 382 | } 383 | 384 | function testFail_diss_multiple_not_authed() public { 385 | oracle.deny(address(this)); 386 | address[] memory guys = new address[](4); 387 | for (uint256 i = 0; i < guys.length; i++) { 388 | guys[i] = address(uint160(17 * i + 1)); 389 | assertEq(oracle.bud(guys[i]), 0); 390 | } 391 | oracle.diss(guys); 392 | } 393 | 394 | 395 | // This test will fail if the value of `val` at peek does not match memory slot 0x3 396 | function testCurSlot0x3() public { 397 | //oracle.poke(); // Poke oracle (done in setUp()) 398 | hevm.warp(oracle.zzz() + oracle.hop() + 1); // Time travel into the future 399 | oracle.poke(); // Poke oracle again 400 | oracle.kiss(address(this)); // Whitelist caller 401 | (bytes32 val, bool has) = oracle.peek(); // Peek oracle price without caller being whitelisted 402 | assertTrue(has); // Verify oracle has value 403 | assertTrue(val != bytes32(0)); // Verify peep returned valid value 404 | 405 | // Load memory slot 0x3 406 | // Keeps `cur` slot parity with OSMs 407 | bytes32 curPacked = hevm.load(address(oracle), bytes32(uint256(3))); 408 | 409 | bytes16 memhas; 410 | bytes16 memcur; 411 | assembly { 412 | memhas := curPacked 413 | memcur := shl(128, curPacked) 414 | } 415 | 416 | assertTrue(uint256(uint128(memcur)) > 0); // Assert nxt has value 417 | assertEq(uint256(val), uint256(uint128(memcur))); // Assert slot value == cur 418 | assertEq(uint256(uint128(memhas)), 1); // Assert slot has == 1 419 | } 420 | 421 | // This test will fail if the value of `val` at peep does not match memory slot 0x4 422 | function testNxtSlot0x4() public { 423 | //oracle.poke(); // Poke oracle (done in setUp()) 424 | hevm.warp(oracle.zzz() + oracle.hop() + 1); // Time travel into the future 425 | oracle.poke(); // Poke oracle again 426 | oracle.kiss(address(this)); // Whitelist caller 427 | (bytes32 val, bool has) = oracle.peep(); // Peep oracle price without caller being whitelisted 428 | assertTrue(has); // Verify oracle has value 429 | assertTrue(val != bytes32(0)); // Verify peep returned valid value 430 | 431 | // Load memory slot 0x4 432 | // Keeps `nxt` slot parity with OSMs 433 | bytes32 nxtPacked = hevm.load(address(oracle), bytes32(uint256(4))); 434 | 435 | bytes16 memhas; 436 | bytes16 memnxt; 437 | assembly { 438 | memhas := nxtPacked 439 | memnxt := shl(128, nxtPacked) 440 | } 441 | 442 | assertTrue(uint256(uint128(memnxt)) > 0); // Assert nxt has value 443 | assertEq(uint256(val), uint256(uint128(memnxt))); // Assert slot value == nxt 444 | assertEq(uint256(uint128(memhas)), 1); // Assert slot has == 1 445 | } 446 | 447 | function test_constructor_nonreentrant() public { 448 | oracle = new CurveLPOracle(address(0x123), address(pool), "123CRV", orbs, true); 449 | assertTrue(oracle.nonreentrant()); 450 | } 451 | 452 | function doReentrantPokeOnFreshOracle(CurveLPOracle _oracle) internal { 453 | _oracle.step(DEFAULT_HOP); 454 | _oracle.kiss(address(this)); 455 | 456 | hevm.warp(_oracle.zph()); 457 | 458 | pool.lock(); 459 | assertTrue(_oracle.pass()); 460 | _oracle.poke(); 461 | } 462 | 463 | function test_not_nonreentrant() public { 464 | oracle = new CurveLPOracle(address(this), address(pool), "123CRV", orbs, false); 465 | doReentrantPokeOnFreshOracle(oracle); 466 | } 467 | 468 | function testFail_nonreentrant() public { 469 | oracle = new CurveLPOracle(address(this), address(pool), "123CRV", orbs, true); 470 | doReentrantPokeOnFreshOracle(oracle); 471 | } 472 | } 473 | -------------------------------------------------------------------------------- /src/CurveLPOracleFactory.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | // Copyright (C) 2021 Dai Foundation 4 | 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU Affero General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU Affero General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Affero General Public License 16 | // along with this program. If not, see . 17 | 18 | pragma solidity 0.8.13; 19 | 20 | import "ds-test/test.sol"; 21 | 22 | import "./CurveLPOracle.sol"; 23 | import { MockCurvePool } from "./CurveLPOracle.t.sol"; 24 | 25 | contract MockAddressProvider { 26 | address immutable REGISTRY; 27 | constructor(address registry) { 28 | REGISTRY = registry; 29 | } 30 | function get_registry() external view returns (address) { 31 | return REGISTRY; 32 | } 33 | } 34 | 35 | contract MockCurveRegistry { 36 | mapping (address => uint256) ncoins; 37 | function addPool(address _pool, uint256 _ncoins) external { 38 | ncoins[_pool] = _ncoins; 39 | } 40 | function get_n_coins(address _pool) external view returns (uint256[2] memory ncoins_) { 41 | ncoins_[0] = 42; // return nonsense value so it's obvious if this is accessed when it shouldn't be 42 | ncoins_[1] = ncoins[_pool]; 43 | } 44 | } 45 | 46 | contract CurveLPOracleFactoryTest is DSTest { 47 | address[] orbs; 48 | MockCurveRegistry registry; 49 | CurveLPOracleFactory factory; 50 | MockCurvePool pool; 51 | 52 | function setUp() public { 53 | registry = new MockCurveRegistry(); 54 | pool = new MockCurvePool(); 55 | registry.addPool(address(pool), 3); 56 | MockAddressProvider addressProvider = new MockAddressProvider(address(registry)); 57 | factory = new CurveLPOracleFactory(address(addressProvider)); 58 | orbs.push(address(0x1)); 59 | orbs.push(address(0x2)); 60 | orbs.push(address(0x3)); 61 | } 62 | 63 | function test_build() public { 64 | CurveLPOracle oracle = CurveLPOracle(payable(factory.build(address(0x123), address(pool), "CRVPOOL", orbs, false))); 65 | assertEq(oracle.wards(address(factory)), 0); 66 | assertEq(oracle.wards(address(0x123)), 1); 67 | assertTrue(oracle.pool() == address(pool)); 68 | assertTrue(oracle.wat() == "CRVPOOL"); 69 | assertEq(oracle.ncoins(), orbs.length); 70 | for (uint256 i = 0; i < orbs.length; i++) { 71 | assertTrue(orbs[i] == oracle.orbs(i)); 72 | } 73 | assertTrue(!oracle.nonreentrant()); 74 | 75 | oracle = CurveLPOracle(payable(factory.build(address(0x123), address(pool), "CRVPOOL", orbs, true))); 76 | assertTrue(oracle.nonreentrant()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/StethPrice.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | /// StethPrice.sol 4 | 5 | // Copyright (C) 2021 Dai Foundation 6 | 7 | // This program is free software: you can redistribute it and/or modify 8 | // it under the terms of the GNU Affero General Public License as published by 9 | // the Free Software Foundation, either version 3 of the License, or 10 | // (at your option) any later version. 11 | // 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU Affero General Public License for more details. 16 | // 17 | // You should have received a copy of the GNU Affero General Public License 18 | // along with this program. If not, see . 19 | 20 | pragma solidity 0.8.13; 21 | 22 | interface OracleLike { 23 | function peek() external view returns (uint256, bool); 24 | function read() external view returns (uint256); 25 | } 26 | 27 | interface StethLike { 28 | function getPooledEthByShares(uint256) external view returns (uint256); 29 | } 30 | 31 | // Implements a Median interface but is a pass-through to the wstETH Median. 32 | contract StethPrice { 33 | uint256 constant WAD = 10**18; 34 | 35 | StethLike immutable STETH; 36 | OracleLike immutable WSTETH_ORACLE; 37 | 38 | mapping(address => uint256) public wards; 39 | function rely(address usr) external auth {wards[usr] = 1; emit Rely(usr);} 40 | function deny(address usr) external auth {wards[usr] = 0; emit Deny(usr);} 41 | modifier auth { 42 | require(wards[msg.sender] == 1, "StethPrice/not-authorized"); 43 | _; 44 | } 45 | 46 | mapping (address => uint256) public bud; 47 | modifier toll { 48 | require(bud[msg.sender] == 1, "StethPrice/not-whitelisted"); 49 | _; 50 | } 51 | 52 | event Rely(address indexed usr); 53 | event Deny(address indexed usr); 54 | event Kiss(address a); 55 | event Diss(address a); 56 | 57 | constructor(address _steth, address _wstETHOracle) { 58 | STETH = StethLike(_steth); 59 | WSTETH_ORACLE = OracleLike(_wstETHOracle); 60 | wards[msg.sender] = 1; 61 | emit Rely(msg.sender); 62 | } 63 | 64 | function peek() external view toll returns (uint256 val, bool has) { 65 | (val, has) = WSTETH_ORACLE.peek(); 66 | val = val * WAD / STETH.getPooledEthByShares(1 ether); 67 | } 68 | 69 | function read() external view toll returns (uint256 price) { 70 | price = WSTETH_ORACLE.read() * WAD / STETH.getPooledEthByShares(1 ether); 71 | } 72 | 73 | function kiss(address _a) external auth { 74 | require(_a != address(0), "StethPrice/no-contract-0"); 75 | bud[_a] = 1; 76 | emit Kiss(_a); 77 | } 78 | 79 | function diss(address _a) external auth { 80 | bud[_a] = 0; 81 | emit Diss(_a); 82 | } 83 | 84 | function kiss(address[] calldata _a) external auth { 85 | for(uint256 i = 0; i < _a.length;) { 86 | require(_a[i] != address(0), "StethPrice/no-contract-0"); 87 | bud[_a[i]] = 1; 88 | emit Kiss(_a[i]); 89 | unchecked { i++; } 90 | } 91 | } 92 | 93 | function diss(address[] calldata _a) external auth { 94 | for(uint256 i = 0; i < _a.length;) { 95 | bud[_a[i]] = 0; 96 | emit Diss(_a[i]); 97 | unchecked { i++; } 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/StethPrice.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | // Copyright (C) 2021 Dai Foundation 4 | 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU Affero General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU Affero General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Affero General Public License 16 | // along with this program. If not, see . 17 | 18 | pragma solidity 0.8.13; 19 | 20 | import "ds-test/test.sol"; 21 | 22 | import "./StethPrice.sol"; 23 | 24 | interface Hevm { 25 | function store(address, bytes32, bytes32) external; 26 | } 27 | 28 | contract ETHstETHPoolTest is DSTest { 29 | 30 | uint256 constant WAD = 10**18; 31 | address constant STETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84; 32 | address constant WSTETH_ORACLE = 0x2F73b6567B866302e132273f67661fB89b5a66F2; 33 | 34 | Hevm hevm; 35 | StethPrice stethPrice; 36 | 37 | function setUp() public { 38 | hevm = Hevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D); 39 | 40 | stethPrice = new StethPrice(STETH, WSTETH_ORACLE); 41 | 42 | // Whitelist price converter to read from wstETH stethPrice 43 | hevm.store( 44 | WSTETH_ORACLE, 45 | keccak256(abi.encode(address(stethPrice), uint256(4))), 46 | bytes32(uint256(1)) 47 | ); 48 | 49 | // Whitelist this contract to read from the wstETH stethPrice 50 | hevm.store( 51 | WSTETH_ORACLE, 52 | keccak256(abi.encode(address(this), uint256(4))), 53 | bytes32(uint256(1)) 54 | ); 55 | } 56 | 57 | function test_read_and_peek() public { 58 | stethPrice.kiss(address(this)); 59 | 60 | uint256 expected = OracleLike(WSTETH_ORACLE).read() * WAD / StethLike(STETH).getPooledEthByShares(1 ether); 61 | assertEq(stethPrice.read(), expected); 62 | 63 | (, bool has) = OracleLike(WSTETH_ORACLE).peek(); 64 | (uint256 actual, bool haz) = stethPrice.peek(); 65 | assertEq(actual, expected); 66 | assertTrue(has == haz); 67 | } 68 | 69 | function test_kiss_single() public { 70 | assertTrue(stethPrice.bud(address(this)) == 0); // Verify caller is not whitelisted 71 | stethPrice.kiss(address(this)); // Whitelist caller 72 | assertTrue(stethPrice.bud(address(this)) == 1); // Verify caller is whitelisted 73 | stethPrice.kiss(address(this)); 74 | assertTrue(stethPrice.bud(address(this)) == 1); // Ensure idempotency 75 | } 76 | 77 | function testFail_kiss_single_not_authed() public { 78 | stethPrice.deny(address(this)); // Remove owner 79 | stethPrice.kiss(address(this)); // Attempt to whitelist caller 80 | } 81 | 82 | function testFail_kiss_single_zero_address() public { 83 | stethPrice.kiss(address(0)); // Attempt to whitelist 0 address 84 | } 85 | 86 | function test_diss_single() public { 87 | stethPrice.kiss(address(this)); // Whitelist caller 88 | assertTrue(stethPrice.bud(address(this)) == 1); // Verify caller is whitelisted 89 | stethPrice.diss(address(this)); // Remove caller from whitelist 90 | assertTrue(stethPrice.bud(address(this)) == 0); // Verify caller is not whitelisted 91 | stethPrice.diss(address(this)); 92 | assertTrue(stethPrice.bud(address(this)) == 0); // Ensure idempotency 93 | } 94 | 95 | function testFail_diss_single() public { 96 | stethPrice.deny(address(this)); // Remove owner 97 | stethPrice.diss(address(this)); // Attempt to remove caller from whitelist 98 | } 99 | 100 | function testFail_peek_not_whitelisted() public { 101 | stethPrice.diss(address(this)); // Ensure caller not authorized to read prices 102 | stethPrice.peek(); 103 | } 104 | 105 | function testFail_read_not_whitelisted() public { 106 | stethPrice.diss(address(this)); // Ensure caller not authorized to read prices 107 | stethPrice.read(); 108 | } 109 | 110 | function test_kiss_and_diss_multiple() public { 111 | address[] memory guys = new address[](4); 112 | for (uint256 i = 0; i < guys.length; i++) { 113 | guys[i] = address(uint160(17 * i + 1)); 114 | assertEq(stethPrice.bud(guys[i]), 0); 115 | } 116 | stethPrice.kiss(guys); 117 | for (uint256 i = 0; i < guys.length; i++) { 118 | assertEq(stethPrice.bud(guys[i]), 1); 119 | } 120 | stethPrice.kiss(guys); // Idempotency 121 | for (uint256 i = 0; i < guys.length; i++) { 122 | assertEq(stethPrice.bud(guys[i]), 1); 123 | } 124 | stethPrice.diss(guys); 125 | for (uint256 i = 0; i < guys.length; i++) { 126 | assertEq(stethPrice.bud(guys[i]), 0); 127 | } 128 | stethPrice.diss(guys); // Idempotency 129 | for (uint256 i = 0; i < guys.length; i++) { 130 | assertEq(stethPrice.bud(guys[i]), 0); 131 | } 132 | } 133 | 134 | function testFail_kiss_multiple_not_authed() public { 135 | stethPrice.deny(address(this)); 136 | address[] memory guys = new address[](4); 137 | for (uint256 i = 0; i < guys.length; i++) { 138 | guys[i] = address(uint160(17 * i + 1)); 139 | assertEq(stethPrice.bud(guys[i]), 0); 140 | } 141 | stethPrice.kiss(guys); 142 | } 143 | 144 | function testFail_kiss_multiple_zero_address() public { 145 | address[] memory guys = new address[](4); 146 | for (uint256 i = 0; i < guys.length; i++) { 147 | guys[i] = address(uint160(17 * i + 1)); 148 | assertEq(stethPrice.bud(guys[i]), 0); 149 | } 150 | guys[1] = address(0); 151 | stethPrice.kiss(guys); 152 | } 153 | 154 | function testFail_diss_multiple_not_authed() public { 155 | stethPrice.deny(address(this)); 156 | address[] memory guys = new address[](4); 157 | for (uint256 i = 0; i < guys.length; i++) { 158 | guys[i] = address(uint160(17 * i + 1)); 159 | assertEq(stethPrice.bud(guys[i]), 0); 160 | } 161 | stethPrice.diss(guys); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/steCRV.t.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: AGPL-3.0-or-later 2 | 3 | // Copyright (C) 2021 Dai Foundation 4 | 5 | // This program is free software: you can redistribute it and/or modify 6 | // it under the terms of the GNU Affero General Public License as published by 7 | // the Free Software Foundation, either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU Affero General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU Affero General Public License 16 | // along with this program. If not, see . 17 | 18 | pragma solidity 0.8.13; 19 | 20 | import "ds-test/test.sol"; 21 | 22 | import "./CurveLPOracle.sol"; 23 | 24 | interface Hevm { 25 | function store(address, bytes32, bytes32) external; 26 | } 27 | 28 | interface ERC20 { 29 | function balanceOf(address) external returns (uint256); 30 | function approve(address, uint256) external; 31 | } 32 | 33 | interface StethLike { 34 | function getPooledEthByShares(uint256) external view returns (uint256); 35 | function getSharesByPooledEth(uint256) external view returns (uint256); 36 | } 37 | 38 | // Using a different interface here to avoid polluting the CurvePoolLike 39 | // interface in CurveLPOracle.sol with functions only needed for testing. 40 | interface CurvePoolMutableLike { 41 | function add_liquidity(uint256[2] calldata, uint256) external payable returns (uint256); 42 | function remove_liquidity(uint256, uint256[2] calldata) external returns (uint256); 43 | } 44 | 45 | contract MockOracle { 46 | uint256 private price; 47 | function read() external view returns (uint256) { 48 | return price; 49 | } 50 | function setPrice(uint256 _price) external { 51 | price = _price; 52 | } 53 | } 54 | 55 | contract Evil { 56 | ERC20 constant STETH = ERC20(0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84); 57 | CurveLPOracle public immutable oracle; 58 | CurvePoolMutableLike public immutable pool; 59 | 60 | constructor(CurveLPOracle _oracle) payable { 61 | oracle = _oracle; 62 | address _pool = _oracle.pool(); 63 | pool = CurvePoolMutableLike(_pool); 64 | STETH.approve(_pool, type(uint256).max); 65 | } 66 | 67 | function attack() external { 68 | // We'll not worry about where we get ETH or stETH for the attack. 69 | // We just deposit everything we have. 70 | uint256[2] memory amounts; 71 | uint256 ethBal = address(this).balance; 72 | amounts[0] = ethBal; 73 | amounts[1] = STETH.balanceOf(address(this)); 74 | uint256 lpAmount = pool.add_liquidity{value: ethBal}(amounts, 0); // accept any slippage 75 | uint256[2] memory minAmountsOut; // leave zero to accept any slippage 76 | pool.remove_liquidity(lpAmount, minAmountsOut); // this will trigger the fallback function during execution 77 | } 78 | 79 | receive() external payable { 80 | oracle.poke(); 81 | } 82 | } 83 | 84 | contract ETHstETHPoolTest is DSTest { 85 | 86 | uint256 constant WAD = 10**18; 87 | address constant ADDRESS_PROVIDER = 0x0000000022D53366457F9d5E68Ec105046FC4383; 88 | address constant POOL = 0xDC24316b9AE028F1497c275EB9192a3Ea0f67022; 89 | address constant ETH_ORACLE = 0x64DE91F5A373Cd4c28de3600cB34C7C6cE410C85; 90 | address constant STECRV = 0x06325440D014e39736583c165C2963BA99fAf14E; 91 | address constant STETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84; 92 | 93 | Hevm hevm; 94 | CurveLPOracleFactory factory; 95 | address[] orbs; 96 | CurveLPOracle oracle; 97 | 98 | function setUp() public { 99 | hevm = Hevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D); 100 | factory = new CurveLPOracleFactory(ADDRESS_PROVIDER); 101 | orbs.push(ETH_ORACLE); 102 | orbs.push(address(new MockOracle())); 103 | oracle = CurveLPOracle(payable(factory.build(address(this), POOL, "steCRV", orbs, true))); 104 | oracle.kiss(address(this)); 105 | 106 | // Whitelist steCRV oracle to read from the ETH oracle 107 | hevm.store( 108 | ETH_ORACLE, 109 | keccak256(abi.encode(address(oracle), uint256(4))), 110 | bytes32(uint256(1)) 111 | ); 112 | 113 | // Whitelist this contract to read from the ETH oracle 114 | hevm.store( 115 | ETH_ORACLE, 116 | keccak256(abi.encode(address(this), uint256(4))), 117 | bytes32(uint256(1)) 118 | ); 119 | } 120 | 121 | function test_build() public { 122 | assertEq(oracle.wards(address(factory)), 0); 123 | assertEq(oracle.wards(address(this)), 1); 124 | assertTrue(oracle.pool() == POOL); 125 | assertEq(oracle.src(), STECRV); 126 | assertTrue(oracle.wat() == "steCRV"); 127 | assertEq(oracle.ncoins(), orbs.length); 128 | for (uint256 i = 0; i < orbs.length; i++) { 129 | assertTrue(orbs[i] == oracle.orbs(i)); 130 | } 131 | assertTrue(oracle.nonreentrant()); 132 | } 133 | 134 | function test_poke() public { 135 | uint256 p_ETH = OracleLike(orbs[0]).read(); 136 | uint256 p_steETH = 3_479 * WAD / 1000; 137 | MockOracle(orbs[1]).setPrice(p_steETH); 138 | uint256 min = p_ETH > p_steETH ? p_steETH : p_ETH; 139 | uint256 p_virt = CurvePoolLike(POOL).get_virtual_price(); 140 | uint256 expectation = p_virt * min / WAD; 141 | 142 | oracle.poke(); 143 | 144 | (bytes32 val, bool has) = oracle.peep(); 145 | assertTrue(has); 146 | assertEq(expectation, uint256(val)); 147 | } 148 | 149 | function testFail_reentrant_poke() public { 150 | uint256 endowment = oracle.pool().balance; // we'll double the amount of Ether in the pool 151 | Evil evil = new Evil{value: endowment}(oracle); 152 | 153 | // Give approximately the same amount of stETH 154 | uint256 shares = StethLike(STETH).getSharesByPooledEth(endowment); 155 | hevm.store( 156 | STETH, 157 | keccak256(abi.encode(address(evil), uint256(0))), 158 | bytes32(shares) 159 | ); 160 | assertEq(ERC20(STETH).balanceOf(address(evil)), StethLike(STETH).getPooledEthByShares(shares)); 161 | 162 | uint256 p_ETH = OracleLike(orbs[0]).read(); 163 | MockOracle(orbs[1]).setPrice(p_ETH); // for simplicity, assume price(stETH) == price(ETH) 164 | 165 | // uncomment to examine numerical effects if using unsafe implementation 166 | // uint256 p_virt = CurvePoolLike(POOL).get_virtual_price(); 167 | // uint256 expectation = p_virt * p_ETH / WAD; 168 | 169 | evil.attack(); 170 | 171 | // uncomment to examine numerical effects if using unsafe implementation 172 | // (bytes32 val,) = oracle.peep(); 173 | // assertEq(expectation, uint256(val)); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | [[ "$ETH_RPC_URL" && "$(seth chain)" == "ethlive" ]] || { echo "Please set a mainnet ETH_RPC_URL"; exit 1; } 5 | 6 | export DAPP_TEST_TIMESTAMP=$(seth block latest timestamp) 7 | export DAPP_TEST_NUMBER=$(seth block latest number) 8 | 9 | if [[ -z "$1" ]]; then 10 | LANG=C.UTF-8 hevm dapp-test --rpc="$ETH_RPC_URL" --json-file=out/dapp.sol.json --dapp-root=. --verbose 1 11 | else 12 | # If running with make test, one can trigger this logic via: 13 | # $ MATCH=some_string_to_match make test 14 | LANG=C.UTF-8 hevm dapp-test --rpc="$ETH_RPC_URL" --json-file=out/dapp.sol.json --dapp-root=. --verbose 2 --match "$1" 15 | fi 16 | --------------------------------------------------------------------------------