ERC20Wrapper.spec 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // STATUS - verified
  11. // totalsupply of wrapped should be less than or equal to underlying (assuming no external transfer) - solvency
  12. invariant whatAboutTotal(env e)
  13. totalSupply(e) <= underlyingTotalSupply()
  14. filtered { f -> f.selector != certorafallback_0().selector }
  15. {
  16. preserved {
  17. require underlyingBalanceOf(currentContract) <= underlyingTotalSupply();
  18. }
  19. preserved depositFor(address account, uint256 amount) with (env e2){
  20. require totalSupply(e) + amount <= underlyingTotalSupply();
  21. }
  22. }
  23. // STATUS - verified
  24. // totalsupply of wrapped should be less than or equal to the underlying balanceOf contract (assuming no external transfer) - solvency
  25. invariant underTotalAndContractBalanceOfCorrelation(env e)
  26. totalSupply(e) <= underlyingBalanceOf(currentContract)
  27. {
  28. preserved with (env e2) {
  29. require underlying() != currentContract;
  30. require e.msg.sender != currentContract;
  31. require e.msg.sender == e2.msg.sender;
  32. }
  33. }
  34. // STATUS - verified
  35. // Check that values are updated correctly by `depositFor()`
  36. rule depositForSpecBasic(env e){
  37. address account; uint256 amount;
  38. require e.msg.sender != currentContract;
  39. require underlying() != currentContract;
  40. uint256 wrapperTotalBefore = totalSupply(e);
  41. uint256 underlyingTotalBefore = underlyingTotalSupply();
  42. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  43. depositFor(e, account, amount);
  44. uint256 wrapperTotalAfter = totalSupply(e);
  45. uint256 underlyingTotalAfter = underlyingTotalSupply();
  46. uint256 underlyingThisBalanceAfter = underlyingBalanceOf(currentContract);
  47. assert wrapperTotalBefore == wrapperTotalAfter - amount, "wrapper total wrong update";
  48. assert underlyingTotalBefore == underlyingTotalAfter, "underlying total was updated";
  49. assert underlyingThisBalanceBefore == underlyingThisBalanceAfter - amount, "underlying this balance wrong update";
  50. }
  51. // STATUS - verified
  52. // Check that values are updated correctly by `depositFor()`
  53. rule depositForSpecWrapper(env e){
  54. address account; uint256 amount;
  55. require underlying() != currentContract;
  56. uint256 wrapperUserBalanceBefore = balanceOf(e, account);
  57. uint256 wrapperSenderBalanceBefore = balanceOf(e, e.msg.sender);
  58. depositFor(e, account, amount);
  59. uint256 wrapperUserBalanceAfter = balanceOf(e, account);
  60. uint256 wrapperSenderBalanceAfter = balanceOf(e, e.msg.sender);
  61. assert account == e.msg.sender => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  62. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  63. && wrapperUserBalanceBefore == wrapperUserBalanceAfter - amount, "wrapper balances wrong update";
  64. assert account != e.msg.sender => wrapperUserBalanceBefore == wrapperUserBalanceAfter - amount
  65. && wrapperSenderBalanceBefore == wrapperSenderBalanceAfter, "wrapper balances wrong update";
  66. }
  67. // STATUS - verified
  68. // Check that values are updated correctly by `depositFor()`
  69. rule depositForSpecUnderlying(env e){
  70. address account; uint256 amount;
  71. require e.msg.sender != currentContract;
  72. require underlying() != currentContract;
  73. uint256 underlyingSenderBalanceBefore = underlyingBalanceOf(e.msg.sender);
  74. uint256 underlyingUserBalanceBefore = underlyingBalanceOf(account);
  75. depositFor(e, account, amount);
  76. uint256 underlyingSenderBalanceAfter = underlyingBalanceOf(e.msg.sender);
  77. uint256 underlyingUserBalanceAfter = underlyingBalanceOf(account);
  78. assert account == e.msg.sender => underlyingSenderBalanceBefore == underlyingUserBalanceBefore
  79. && underlyingSenderBalanceAfter == underlyingUserBalanceAfter
  80. && underlyingSenderBalanceBefore == underlyingSenderBalanceAfter + amount, "underlying balances wrong update";
  81. assert account != e.msg.sender && account == currentContract => underlyingSenderBalanceBefore == underlyingSenderBalanceAfter + amount
  82. && underlyingUserBalanceBefore == underlyingUserBalanceAfter - amount, "underlying balances wrong update";
  83. assert account != e.msg.sender && account != currentContract => underlyingSenderBalanceBefore == underlyingSenderBalanceAfter + amount
  84. && underlyingUserBalanceBefore == underlyingUserBalanceAfter, "underlying balances wrong update";
  85. }
  86. // STATUS - verified
  87. // Check that values are updated correctly by `withdrawTo()`
  88. rule withdrawToSpecBasic(env e){
  89. address account; uint256 amount;
  90. require underlying() != currentContract;
  91. uint256 wrapperTotalBefore = totalSupply(e);
  92. uint256 underlyingTotalBefore = underlyingTotalSupply();
  93. withdrawTo(e, account, amount);
  94. uint256 wrapperTotalAfter = totalSupply(e);
  95. uint256 underlyingTotalAfter = underlyingTotalSupply();
  96. assert wrapperTotalBefore == wrapperTotalAfter + amount, "wrapper total wrong update";
  97. assert underlyingTotalBefore == underlyingTotalAfter, "underlying total was updated";
  98. }
  99. // STATUS - verified
  100. // Check that values are updated correctly by `withdrawTo()`
  101. rule withdrawToSpecWrapper(env e){
  102. address account; uint256 amount;
  103. require underlying() != currentContract;
  104. uint256 wrapperUserBalanceBefore = balanceOf(e, account);
  105. uint256 wrapperSenderBalanceBefore = balanceOf(e, e.msg.sender);
  106. withdrawTo(e, account, amount);
  107. uint256 wrapperUserBalanceAfter = balanceOf(e, account);
  108. uint256 wrapperSenderBalanceAfter = balanceOf(e, e.msg.sender);
  109. assert account == e.msg.sender => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  110. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  111. && wrapperUserBalanceBefore == wrapperUserBalanceAfter + amount, "wrapper user balance wrong update";
  112. assert account != e.msg.sender => wrapperSenderBalanceBefore == wrapperSenderBalanceAfter + amount
  113. && wrapperUserBalanceBefore == wrapperUserBalanceAfter, "wrapper user balance wrong update";
  114. }
  115. // STATUS - verified
  116. // cCheck that values are updated correctly by `withdrawTo()`
  117. rule withdrawToSpecUnderlying(env e){
  118. address account; uint256 amount;
  119. require e.msg.sender != currentContract;
  120. require underlying() != currentContract;
  121. uint256 underlyingSenderBalanceBefore = underlyingBalanceOf(e.msg.sender);
  122. uint256 underlyingUserBalanceBefore = underlyingBalanceOf(account);
  123. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  124. withdrawTo(e, account, amount);
  125. uint256 underlyingSenderBalanceAfter = underlyingBalanceOf(e.msg.sender);
  126. uint256 underlyingUserBalanceAfter = underlyingBalanceOf(account);
  127. uint256 underlyingThisBalanceAfter = underlyingBalanceOf(currentContract);
  128. assert account == e.msg.sender => underlyingSenderBalanceBefore == underlyingUserBalanceBefore
  129. && underlyingSenderBalanceAfter == underlyingUserBalanceAfter
  130. && underlyingUserBalanceBefore == underlyingUserBalanceAfter - amount, "underlying balances wrong update (acc == sender)";
  131. assert account != e.msg.sender && account == currentContract => underlyingUserBalanceBefore == underlyingUserBalanceAfter
  132. && underlyingSenderBalanceBefore == underlyingSenderBalanceAfter, "underlying balances wrong update (acc == contract)";
  133. assert account != e.msg.sender && account != currentContract => underlyingUserBalanceBefore == underlyingUserBalanceAfter - amount
  134. && underlyingSenderBalanceBefore == underlyingSenderBalanceAfter
  135. && underlyingThisBalanceBefore == underlyingThisBalanceAfter + amount, "underlying balances wrong update (acc != contract)";
  136. }
  137. // STATUS - verified
  138. // Check that values are updated correctly by `_recover()`
  139. rule recoverSpec(env e){
  140. address account; uint256 amount;
  141. uint256 wrapperTotalBefore = totalSupply(e);
  142. uint256 wrapperUserBalanceBefore = balanceOf(e, account);
  143. uint256 wrapperSenderBalanceBefore = balanceOf(e, e.msg.sender);
  144. uint256 underlyingThisBalanceBefore = underlyingBalanceOf(currentContract);
  145. mathint value = underlyingThisBalanceBefore - wrapperTotalBefore;
  146. _recover(e, account);
  147. uint256 wrapperTotalAfter = totalSupply(e);
  148. uint256 wrapperUserBalanceAfter = balanceOf(e, account);
  149. uint256 wrapperSenderBalanceAfter = balanceOf(e, e.msg.sender);
  150. assert wrapperTotalBefore == wrapperTotalAfter - value, "wrapper total wrong update";
  151. assert e.msg.sender == account => wrapperUserBalanceBefore == wrapperSenderBalanceBefore
  152. && wrapperUserBalanceAfter == wrapperSenderBalanceAfter
  153. && wrapperUserBalanceBefore == wrapperUserBalanceAfter - value, "wrapper balances wrong update";
  154. assert e.msg.sender != account => wrapperUserBalanceBefore == wrapperUserBalanceAfter - value
  155. && wrapperSenderBalanceBefore == wrapperSenderBalanceAfter, "wrapper balances wrong update";
  156. }