storage.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // ethereum solc wants his pragma
  2. pragma abicoder v2;
  3. contract store {
  4. enum enum_bar { bar1, bar2, bar3, bar4 }
  5. struct struct_foo {
  6. enum_bar f1;
  7. bytes f2;
  8. int64 f3;
  9. bytes3 f4;
  10. string f5;
  11. inner_foo f6;
  12. }
  13. struct inner_foo {
  14. bool in1;
  15. string in2;
  16. }
  17. uint64 u64;
  18. uint32 u32;
  19. int16 i16;
  20. int256 i256;
  21. uint256 u256;
  22. string str;
  23. bytes bs = hex"b00b1e";
  24. bytes4 fixedbytes;
  25. enum_bar bar;
  26. struct_foo foo1;
  27. struct_foo foo2;
  28. function set_values() public {
  29. u64 = type(uint64).max;
  30. u32 = 0xdad0feef;
  31. i16 = 0x7ffe;
  32. i256 = type(int256).max;
  33. u256 = 102;
  34. str = "the course of true love never did run smooth";
  35. fixedbytes = "ABCD";
  36. bar = enum_bar.bar2;
  37. }
  38. function get_values1() public view returns (uint64, uint32, int16, int256) {
  39. return (u64, u32, i16, i256);
  40. }
  41. function get_values2() public view returns (uint256, string memory, bytes memory, bytes4, enum_bar) {
  42. return (u256, str, bs, fixedbytes, bar);
  43. }
  44. function do_ops() public {
  45. unchecked {
  46. // u64 will overflow to 1
  47. u64 += 2;
  48. u32 &= 0xffff;
  49. // another overflow
  50. i16 += 1;
  51. i256 ^= 1;
  52. u256 *= 600;
  53. str = "";
  54. bs[1] = 0xff;
  55. // make upper case
  56. fixedbytes |= 0x20202020;
  57. bar = enum_bar.bar4;
  58. }
  59. }
  60. function push_zero() public {
  61. bs.push();
  62. }
  63. function push(bytes1 b) public {
  64. bs.push(b);
  65. }
  66. function pop() public returns (byte) {
  67. // note: ethereum solidity bytes.pop() does not return a value
  68. return bs.pop();
  69. }
  70. function get_bs() public view returns (bytes memory) {
  71. return bs;
  72. }
  73. // function for setting the in2 field in either contract storage or memory
  74. function set_storage_in2(struct_foo storage f, string memory v) internal {
  75. f.f6.in2 = v;
  76. }
  77. // A memory struct is passed by memory reference (pointer)
  78. function set_in2(struct_foo memory f, string memory v) pure internal {
  79. f.f6.in2 = v;
  80. }
  81. function get_both_foos() public view returns (struct_foo memory, struct_foo memory) {
  82. return (foo1, foo2);
  83. }
  84. function get_foo(bool first) public view returns (struct_foo memory) {
  85. struct_foo storage f;
  86. if (first) {
  87. f = foo1;
  88. } else {
  89. f = foo2;
  90. }
  91. return f;
  92. }
  93. function set_foo2(struct_foo f, string v) public {
  94. set_in2(f, v);
  95. foo2 = f;
  96. }
  97. function set_foo1() public {
  98. foo1.f1 = enum_bar.bar2;
  99. foo1.f2 = "Don't count your chickens before they hatch";
  100. foo1.f3 = -102;
  101. foo1.f4 = hex"edaeda";
  102. foo1.f5 = "You can't have your cake and eat it too";
  103. foo1.f6.in1 = true;
  104. set_storage_in2(foo1, 'There are other fish in the sea');
  105. }
  106. function delete_foo(bool first) public {
  107. struct_foo storage f;
  108. if (first) {
  109. f = foo1;
  110. } else {
  111. f = foo2;
  112. }
  113. delete f;
  114. }
  115. function struct_literal() public {
  116. // declare a struct literal with fields. There is an
  117. // inner struct literal which uses positions
  118. struct_foo literal = struct_foo({
  119. f1: enum_bar.bar4,
  120. f2: "Supercalifragilisticexpialidocious",
  121. f3: 0xeffedead1234,
  122. f4: unicode'€',
  123. f5: "Antidisestablishmentarianism",
  124. f6: inner_foo(true, "Pseudopseudohypoparathyroidism")
  125. });
  126. // a literal is just a regular memory struct which can be modified
  127. literal.f3 = 0xfd9f;
  128. // now assign it to a storage variable; it will be copied to contract storage
  129. foo1 = literal;
  130. }
  131. }