draft-InteroperableAddress.t.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.26;
  3. import {Test} from "forge-std/Test.sol";
  4. import {InteroperableAddress} from "../../contracts/utils/draft-InteroperableAddress.sol";
  5. contract InteroperableAddressTest is Test {
  6. using InteroperableAddress for bytes;
  7. function testFormatParse(bytes2 chainType, bytes calldata chainReference, bytes calldata addr) public view {
  8. vm.assume(chainReference.length > 0 || addr.length > 0);
  9. {
  10. (bytes2 chainType_, bytes memory chainReference_, bytes memory addr_) = InteroperableAddress
  11. .formatV1(chainType, chainReference, addr)
  12. .parseV1();
  13. assertEq(chainType, chainType_);
  14. assertEq(chainReference, chainReference_);
  15. assertEq(addr, addr_);
  16. }
  17. {
  18. (bool success, bytes2 chainType_, bytes memory chainReference_, bytes memory addr_) = InteroperableAddress
  19. .formatV1(chainType, chainReference, addr)
  20. .tryParseV1();
  21. assertTrue(success);
  22. assertEq(chainType, chainType_);
  23. assertEq(chainReference, chainReference_);
  24. assertEq(addr, addr_);
  25. }
  26. {
  27. (bytes2 chainType_, bytes memory chainReference_, bytes memory addr_) = this.parseV1Calldata(
  28. InteroperableAddress.formatV1(chainType, chainReference, addr)
  29. );
  30. assertEq(chainType, chainType_);
  31. assertEq(chainReference, chainReference_);
  32. assertEq(addr, addr_);
  33. }
  34. {
  35. (bool success, bytes2 chainType_, bytes memory chainReference_, bytes memory addr_) = this
  36. .tryParseV1Calldata(InteroperableAddress.formatV1(chainType, chainReference, addr));
  37. assertTrue(success);
  38. assertEq(chainType, chainType_);
  39. assertEq(chainReference, chainReference_);
  40. assertEq(addr, addr_);
  41. }
  42. }
  43. function testFormatParseEVM(uint256 chainid, address addr) public view {
  44. {
  45. (uint256 chainid_, address addr_) = InteroperableAddress.formatEvmV1(chainid, addr).parseEvmV1();
  46. assertEq(chainid, chainid_);
  47. assertEq(addr, addr_);
  48. }
  49. {
  50. (bool success, uint256 chainid_, address addr_) = InteroperableAddress
  51. .formatEvmV1(chainid, addr)
  52. .tryParseEvmV1();
  53. assertTrue(success);
  54. assertEq(chainid, chainid_);
  55. assertEq(addr, addr_);
  56. }
  57. {
  58. (uint256 chainid_, address addr_) = this.parseEvmV1Calldata(
  59. InteroperableAddress.formatEvmV1(chainid, addr)
  60. );
  61. assertEq(chainid, chainid_);
  62. assertEq(addr, addr_);
  63. }
  64. {
  65. (bool success, uint256 chainid_, address addr_) = this.tryParseEvmV1Calldata(
  66. InteroperableAddress.formatEvmV1(chainid, addr)
  67. );
  68. assertTrue(success);
  69. assertEq(chainid, chainid_);
  70. assertEq(addr, addr_);
  71. }
  72. }
  73. function parseV1Calldata(
  74. bytes calldata self
  75. ) external pure returns (bytes2 chainType, bytes calldata chainReference, bytes calldata addr) {
  76. return self.parseV1Calldata();
  77. }
  78. function tryParseV1Calldata(
  79. bytes calldata self
  80. ) external pure returns (bool success, bytes2 chainType, bytes calldata chainReference, bytes calldata addr) {
  81. return self.tryParseV1Calldata();
  82. }
  83. function parseEvmV1Calldata(bytes calldata self) external pure returns (uint256 chainid, address addr) {
  84. return self.parseEvmV1Calldata();
  85. }
  86. function tryParseEvmV1Calldata(
  87. bytes calldata self
  88. ) external pure returns (bool success, uint256 chainid, address addr) {
  89. return self.tryParseEvmV1Calldata();
  90. }
  91. }