abi_encode_v2.sol 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // RUN: --target polkadot --emit cfg
  2. pragma abicoder v2;
  3. contract C {
  4. struct S {
  5. uint256 a;
  6. uint256[] b;
  7. }
  8. // CHECK: C::function::f0
  9. function f0() public pure returns (bytes memory) {
  10. return abi.encode();
  11. }
  12. function f1() public pure returns (bytes memory) {
  13. return abi.encode(1, 2);
  14. }
  15. function f2() public pure returns (bytes memory) {
  16. string memory x = "abc";
  17. return abi.encode(1, x, 2);
  18. }
  19. function f3() public pure returns (bytes memory r) {
  20. // test that memory is properly allocated
  21. string memory x = "abc";
  22. r = abi.encode(1, x, 2);
  23. bytes memory y = "def";
  24. require(y[0] == "d");
  25. y[0] = "e";
  26. require(y[0] == "e");
  27. }
  28. S s;
  29. function f4() public returns (bytes memory r) {
  30. string memory x = "abc";
  31. s.a = 7;
  32. s.b.push(2);
  33. s.b.push(3);
  34. r = abi.encode(1, x, s, 2);
  35. bytes memory y = "def";
  36. require(y[0] == "d");
  37. y[0] = "e";
  38. require(y[0] == "e");
  39. }
  40. }
  41. // ----
  42. // f0() -> 0x20, 0x0
  43. // f1() -> 0x20, 0x40, 0x1, 0x2
  44. // f2() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
  45. // f3() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
  46. // f4() -> 0x20, 0x160, 0x1, 0x80, 0xc0, 0x2, 0x3, "abc", 0x7, 0x40, 0x2, 0x2, 0x3
  47. // gas irOptimized: 112630
  48. // gas legacy: 114794
  49. // gas legacyOptimized: 112572