abi.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity;
  3. #[test]
  4. fn packed() {
  5. let mut vm = build_solidity(
  6. r#"
  7. struct s {
  8. int32 f1;
  9. uint8 f2;
  10. string f3;
  11. uint16[2] f4;
  12. }
  13. contract bar {
  14. function test() public {
  15. uint16 a = 0xfd01;
  16. assert(abi.encodePacked(a) == hex"01fd");
  17. uint32 b = 0xaabbccdd;
  18. assert(abi.encodePacked(true, b, false) == hex"01ddccbbaa00");
  19. }
  20. function test2() public {
  21. string b = "foobar";
  22. bytes c = abi.encodePacked(b);
  23. assert(abi.encodePacked(b) == "foobar");
  24. assert(abi.encodePacked("foobar") == "foobar");
  25. assert(abi.encodePacked("foo", "bar") == "foobar");
  26. }
  27. function test3() public {
  28. s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ 4, 5 ] });
  29. assert(abi.encodePacked(x) == hex"ff010000f774657374696504000500");
  30. }
  31. function test4() public {
  32. uint8[] vec = new uint8[](2);
  33. vec[0] = 0xca;
  34. vec[1] = 0xfe;
  35. assert(abi.encodePacked(vec) == hex"cafe");
  36. }
  37. }"#,
  38. );
  39. let account = vm.initialize_data_account();
  40. vm.function("new")
  41. .accounts(vec![("dataAccount", account)])
  42. .call();
  43. vm.function("test").call();
  44. vm.function("test2").call();
  45. vm.function("test3").call();
  46. vm.function("test4").call();
  47. }
  48. #[test]
  49. fn inherited() {
  50. let mut vm = build_solidity(
  51. r#"
  52. contract foo {
  53. function test() public {
  54. }
  55. }
  56. contract bar is foo { }"#,
  57. );
  58. let data_account = vm.initialize_data_account();
  59. vm.function("new")
  60. .accounts(vec![("dataAccount", data_account)])
  61. .call();
  62. vm.function("test").call();
  63. let mut vm = build_solidity(
  64. r#"
  65. contract foo {
  66. int public test;
  67. }
  68. contract bar is foo { }"#,
  69. );
  70. let data_account = vm.initialize_data_account();
  71. vm.function("new")
  72. .accounts(vec![("dataAccount", data_account)])
  73. .call();
  74. vm.function("test")
  75. .accounts(vec![("dataAccount", data_account)])
  76. .call();
  77. }