storage.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // u64 will overflow to 1
  46. u64 += 2;
  47. u32 &= 0xffff;
  48. // another overflow
  49. i16 += 1;
  50. i256 ^= 1;
  51. u256 *= 600;
  52. str = "";
  53. bs[1] = 0xff;
  54. // make upper case
  55. fixedbytes |= 0x20202020;
  56. bar = enum_bar.bar4;
  57. }
  58. function push_zero() public {
  59. bs.push();
  60. }
  61. function push(bytes1 b) public {
  62. bs.push(b);
  63. }
  64. function pop() public returns (byte) {
  65. // note: ethereum solidity bytes.pop() does not return a value
  66. return bs.pop();
  67. }
  68. function get_bs() public view returns (bytes memory) {
  69. return bs;
  70. }
  71. // function for setting the in2 field in either contract storage or memory
  72. function set_storage_in2(struct_foo storage f, string memory v) internal {
  73. f.f6.in2 = v;
  74. }
  75. // A memory struct is passed by memory reference (pointer)
  76. function set_in2(struct_foo memory f, string memory v) pure internal {
  77. f.f6.in2 = v;
  78. }
  79. function get_both_foos() public view returns (struct_foo memory, struct_foo memory) {
  80. return (foo1, foo2);
  81. }
  82. function get_foo(bool first) public view returns (struct_foo memory) {
  83. struct_foo storage f;
  84. if (first) {
  85. f = foo1;
  86. } else {
  87. f = foo2;
  88. }
  89. return f;
  90. }
  91. function set_foo2(struct_foo f, string v) public {
  92. set_in2(f, v);
  93. foo2 = f;
  94. }
  95. function set_foo1() public {
  96. foo1.f1 = enum_bar.bar2;
  97. foo1.f2 = "Don't count your chickens before they hatch";
  98. foo1.f3 = -102;
  99. foo1.f4 = hex"edaeda";
  100. foo1.f5 = "You can't have your cake and eat it too";
  101. foo1.f6.in1 = true;
  102. set_storage_in2(foo1, 'There are other fish in the sea');
  103. }
  104. function delete_foo(bool first) public {
  105. struct_foo storage f;
  106. if (first) {
  107. f = foo1;
  108. } else {
  109. f = foo2;
  110. }
  111. delete f;
  112. }
  113. function struct_literal() public {
  114. // declare a struct literal with fields. There is an
  115. // inner struct literal which uses positions
  116. struct_foo literal = struct_foo({
  117. f1: enum_bar.bar4,
  118. f2: "Supercalifragilisticexpialidocious",
  119. f3: 0xeffedead1234,
  120. f4: unicode'€',
  121. f5: "Antidisestablishmentarianism",
  122. f6: inner_foo(true, "Pseudopseudohypoparathyroidism")
  123. });
  124. // a literal is just a regular memory struct which can be modified
  125. literal.f3 = 0xfd9f;
  126. // now assign it to a storage variable; it will be copied to contract storage
  127. foo1 = literal;
  128. }
  129. }