ERC20Wrapper.spec 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import "ERC20.spec"
  2. methods {
  3. underlying() returns(address) envfree
  4. underlyingTotalSupply() returns(uint256) envfree
  5. underlyingBalanceOf(address) returns(uint256) envfree
  6. depositFor(address, uint256) returns(bool)
  7. withdrawTo(address, uint256) returns(bool)
  8. _recover(address) returns(uint256)
  9. }
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // Invariants //
  12. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13. use invariant totalSupplyIsSumOfBalances
  14. // totalsupply of wrapped should be less than or equal to underlying (assuming no external transfer) - solvency
  15. invariant whatAboutTotal(env e)
  16. totalSupply() <= underlyingTotalSupply()
  17. filtered { f -> f.selector != certorafallback_0().selector && !f.isView }
  18. {
  19. preserved with (env e2) {
  20. require underlyingBalanceOf(currentContract) <= underlyingTotalSupply();
  21. requireInvariant totalSupplyIsSumOfBalances;
  22. require e.msg.sender == e2.msg.sender;
  23. }
  24. preserved depositFor(address account, uint256 amount) with (env e3){
  25. require totalSupply() + amount <= underlyingTotalSupply();
  26. }
  27. preserved _mint(address account, uint256 amount) with (env e4){
  28. require totalSupply() + amount <= underlyingTotalSupply();
  29. }
  30. preserved _burn(address account, uint256 amount) with (env e5){
  31. require balanceOf(account) >= amount;
  32. requireInvariant totalSupplyIsSumOfBalances;
  33. }
  34. }
  35. // totalsupply of wrapped should be less than or equal to the underlying balanceOf contract (assuming no external transfer) - solvency
  36. invariant underTotalAndContractBalanceOfCorrelation(env e)
  37. totalSupply() <= underlyingBalanceOf(currentContract)
  38. {
  39. preserved with (env e2) {
  40. require underlying() != currentContract;
  41. require e.msg.sender != currentContract;
  42. require e.msg.sender == e2.msg.sender;
  43. requireInvariant totalSupplyIsSumOfBalances;
  44. }
  45. preserved _mint(address account, uint256 amount) with (env e4){
  46. require totalSupply() + amount <= underlyingBalanceOf(currentContract);
  47. require underlying() != currentContract;
  48. }
  49. preserved _burn(address account, uint256 amount) with (env e5){
  50. require balanceOf(account) >= amount;
  51. requireInvariant totalSupplyIsSumOfBalances;
  52. }
  53. preserved depositFor(address account, uint256 amount) with (env e3){
  54. require totalSupply() + amount <= underlyingBalanceOf(currentContract);
  55. require underlyingBalanceOf(currentContract) + amount < max_uint256;
  56. require underlying() != currentContract;
  57. }
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. // Rules //
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. // Check that values are updated correctly by `depositFor()`
  63. rule depositForSpecBasic(env e) {
  64. address account;
  65. uint256 amount;
  66. require e.msg.sender != currentContract;
  67. require underlying() != currentContract;
  68. uint256 wrapperTotalBefore = totalSupply();
  69. uint256 underlyingTotalBefore = underlyingTotalSupply();
  70. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  71. depositFor(e, account, amount);
  72. uint256 wrapperTotalAfter = totalSupply();
  73. uint256 underlyingTotalAfter = underlyingTotalSupply();
  74. uint256 underlyingThisBalanceAfter = underlyingBalanceOf(currentContract);
  75. assert wrapperTotalBefore == to_uint256(wrapperTotalAfter - amount), "wrapper total wrong update";
  76. assert underlyingTotalBefore == underlyingTotalAfter, "underlying total was updated";
  77. assert underlyingThisBalanceBefore == to_uint256(underlyingThisBalanceAfter - amount), "underlying this balance wrong update";
  78. }
  79. // Check that values are updated correctly by `depositFor()`
  80. rule depositForSpecWrapper(env e) {
  81. address account;
  82. uint256 amount;
  83. require underlying() != currentContract;
  84. uint256 wrapperUserBalanceBefore = balanceOf(account);
  85. uint256 wrapperSenderBalanceBefore = balanceOf(e.msg.sender);
  86. depositFor(e, account, amount);
  87. uint256 wrapperUserBalanceAfter = balanceOf(account);
  88. uint256 wrapperSenderBalanceAfter = balanceOf(e.msg.sender);
  89. assert account == e.msg.sender => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  90. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  91. && wrapperUserBalanceBefore == to_uint256(wrapperUserBalanceAfter - amount)
  92. , "wrapper balances wrong update";
  93. assert account != e.msg.sender => wrapperUserBalanceBefore == to_uint256(wrapperUserBalanceAfter - amount)
  94. && wrapperSenderBalanceBefore == wrapperSenderBalanceAfter
  95. , "wrapper balances wrong update";
  96. }
  97. // Check that values are updated correctly by `depositFor()`
  98. rule depositForSpecUnderlying(env e) {
  99. address account;
  100. uint256 amount;
  101. require e.msg.sender != currentContract;
  102. require underlying() != currentContract;
  103. uint256 underlyingSenderBalanceBefore = underlyingBalanceOf(e.msg.sender);
  104. uint256 underlyingUserBalanceBefore = underlyingBalanceOf(account);
  105. depositFor(e, account, amount);
  106. uint256 underlyingSenderBalanceAfter = underlyingBalanceOf(e.msg.sender);
  107. uint256 underlyingUserBalanceAfter = underlyingBalanceOf(account);
  108. assert account == e.msg.sender => underlyingSenderBalanceBefore == underlyingUserBalanceBefore
  109. && underlyingSenderBalanceAfter == underlyingUserBalanceAfter
  110. && underlyingSenderBalanceBefore == to_uint256(underlyingSenderBalanceAfter + amount)
  111. , "underlying balances wrong update";
  112. assert account != e.msg.sender
  113. && account == currentContract => underlyingSenderBalanceBefore == to_uint256(underlyingSenderBalanceAfter + amount)
  114. && underlyingUserBalanceBefore == to_uint256(underlyingUserBalanceAfter - amount)
  115. , "underlying balances wrong update";
  116. assert account != e.msg.sender
  117. && account != currentContract => underlyingSenderBalanceBefore == to_uint256(underlyingSenderBalanceAfter + amount)
  118. && underlyingUserBalanceBefore == to_uint256(underlyingUserBalanceAfter)
  119. , "underlying balances wrong update";
  120. }
  121. // Check that values are updated correctly by `withdrawTo()`
  122. rule withdrawToSpecBasic(env e) {
  123. address account;
  124. uint256 amount;
  125. require underlying() != currentContract;
  126. uint256 wrapperTotalBefore = totalSupply();
  127. uint256 underlyingTotalBefore = underlyingTotalSupply();
  128. withdrawTo(e, account, amount);
  129. uint256 wrapperTotalAfter = totalSupply();
  130. uint256 underlyingTotalAfter = underlyingTotalSupply();
  131. assert wrapperTotalBefore == to_uint256(wrapperTotalAfter + amount), "wrapper total wrong update";
  132. assert underlyingTotalBefore == underlyingTotalAfter, "underlying total was updated";
  133. }
  134. // Check that values are updated correctly by `withdrawTo()`
  135. rule withdrawToSpecWrapper(env e) {
  136. address account; uint256 amount;
  137. require underlying() != currentContract;
  138. uint256 wrapperUserBalanceBefore = balanceOf(account);
  139. uint256 wrapperSenderBalanceBefore = balanceOf(e.msg.sender);
  140. withdrawTo(e, account, amount);
  141. uint256 wrapperUserBalanceAfter = balanceOf(account);
  142. uint256 wrapperSenderBalanceAfter = balanceOf(e.msg.sender);
  143. assert account == e.msg.sender => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  144. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  145. && wrapperUserBalanceBefore == to_uint256(wrapperUserBalanceAfter + amount)
  146. , "wrapper user balance wrong update";
  147. assert account != e.msg.sender => wrapperSenderBalanceBefore == to_uint256(wrapperSenderBalanceAfter + amount)
  148. && wrapperUserBalanceBefore == wrapperUserBalanceAfter
  149. , "wrapper user balance wrong update";
  150. }
  151. // STATUS - verified
  152. // Check that values are updated correctly by `withdrawTo()`
  153. rule withdrawToSpecUnderlying(env e) {
  154. address account; uint256 amount;
  155. require e.msg.sender != currentContract;
  156. require underlying() != currentContract;
  157. uint256 underlyingSenderBalanceBefore = underlyingBalanceOf(e.msg.sender);
  158. uint256 underlyingUserBalanceBefore = underlyingBalanceOf(account);
  159. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  160. withdrawTo(e, account, amount);
  161. uint256 underlyingSenderBalanceAfter = underlyingBalanceOf(e.msg.sender);
  162. uint256 underlyingUserBalanceAfter = underlyingBalanceOf(account);
  163. uint256 underlyingThisBalanceAfter = underlyingBalanceOf(currentContract);
  164. assert account == e.msg.sender => underlyingSenderBalanceBefore == underlyingUserBalanceBefore
  165. && underlyingSenderBalanceAfter == underlyingUserBalanceAfter
  166. && underlyingUserBalanceBefore == to_uint256(underlyingUserBalanceAfter - amount)
  167. , "underlying balances wrong update (acc == sender)";
  168. assert account != e.msg.sender && account == currentContract => underlyingUserBalanceBefore == underlyingUserBalanceAfter
  169. && underlyingSenderBalanceBefore == underlyingSenderBalanceAfter
  170. , "underlying balances wrong update (acc == contract)";
  171. assert account != e.msg.sender && account != currentContract => underlyingUserBalanceBefore == to_uint256(underlyingUserBalanceAfter - amount)
  172. && underlyingSenderBalanceBefore == underlyingSenderBalanceAfter
  173. && underlyingThisBalanceBefore == to_uint256(underlyingThisBalanceAfter + amount)
  174. , "underlying balances wrong update (acc != contract)";
  175. }
  176. // Check that values are updated correctly by `_recover()`
  177. rule recoverSpec(env e) {
  178. address account;
  179. uint256 amount;
  180. uint256 wrapperTotalBefore = totalSupply();
  181. uint256 wrapperUserBalanceBefore = balanceOf(account);
  182. uint256 wrapperSenderBalanceBefore = balanceOf(e.msg.sender);
  183. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  184. mathint value = underlyingThisBalanceBefore - wrapperTotalBefore;
  185. _recover(e, account);
  186. uint256 wrapperTotalAfter = totalSupply();
  187. uint256 wrapperUserBalanceAfter = balanceOf(account);
  188. uint256 wrapperSenderBalanceAfter = balanceOf(e.msg.sender);
  189. assert wrapperTotalBefore == to_uint256(wrapperTotalAfter - value), "wrapper total wrong update";
  190. assert e.msg.sender == account => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  191. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  192. && wrapperUserBalanceBefore == to_uint256(wrapperUserBalanceAfter - value)
  193. , "wrapper balances wrong update";
  194. assert e.msg.sender != account => wrapperUserBalanceBefore == to_uint256(wrapperUserBalanceAfter - value)
  195. && wrapperSenderBalanceBefore == wrapperSenderBalanceAfter
  196. , "wrapper balances wrong update";
  197. }