ERC1155Supply.spec 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. methods {
  2. totalSupply(uint256) returns uint256 envfree
  3. balanceOf(address, uint256) returns uint256 envfree
  4. exists_wrapper(uint256) returns bool envfree
  5. }
  6. /// given two different token ids, if totalSupply for one changes, then
  7. /// totalSupply for other must not
  8. rule token_totalSupply_independence(method f)
  9. filtered {
  10. f -> f.selector != safeBatchTransferFrom(address,address,uint256[],uint256[],bytes).selector
  11. }
  12. {
  13. uint256 token1; uint256 token2;
  14. require token1 != token2;
  15. uint256 token1_before = totalSupply(token1);
  16. uint256 token2_before = totalSupply(token2);
  17. env e; calldataarg args;
  18. f(e, args);
  19. uint256 token1_after = totalSupply(token1);
  20. uint256 token2_after = totalSupply(token2);
  21. assert token1_after != token1_before => token2_after == token2_before,
  22. "methods must not change the total supply of more than one token";
  23. }
  24. /******************************************************************************/
  25. ghost mapping(uint256 => mathint) sumOfBalances {
  26. init_state axiom forall uint256 token . sumOfBalances[token] == 0;
  27. }
  28. hook Sstore _balances[KEY uint256 token][KEY address user] uint256 newValue (uint256 oldValue) STORAGE {
  29. sumOfBalances[token] = sumOfBalances[token] + newValue - oldValue;
  30. }
  31. /// The sum of the balances over all users must equal the total supply for a
  32. /// given token.
  33. invariant total_supply_is_sum_of_balances(uint256 token)
  34. sumOfBalances[token] == totalSupply(token)
  35. {
  36. preserved {
  37. requireInvariant balanceOfZeroAddressIsZero(token);
  38. }
  39. }
  40. /******************************************************************************/
  41. /// The balance of a token for the zero address must be zero.
  42. invariant balanceOfZeroAddressIsZero(uint256 token)
  43. balanceOf(0, token) == 0
  44. /// If a user has a token, then the token should exist.
  45. rule held_tokens_should_exist {
  46. address user; uint256 token;
  47. requireInvariant balanceOfZeroAddressIsZero(token);
  48. // This assumption is safe because of total_supply_is_sum_of_balances
  49. require balanceOf(user, token) <= totalSupply(token);
  50. // note: `exists_wrapper` just calls `exists`
  51. assert balanceOf(user, token) > 0 => exists_wrapper(token),
  52. "if a user's balance for a token is positive, the token must exist";
  53. }
  54. /******************************************************************************/
  55. /*
  56. rule sanity {
  57. method f; env e; calldataarg args;
  58. f(e, args);
  59. assert false;
  60. }
  61. */