GenerateGovernanceVAAs.t.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "forge-std/Test.sol";
  4. import "./utils/WormholeTestUtils.t.sol";
  5. import "./utils/PythTestUtils.t.sol";
  6. import "../contracts/pyth/PythGovernanceInstructions.sol";
  7. contract GenerateGovernanceVAAs is Test, WormholeTestUtils, PythTestUtils, PythGovernanceInstructions {
  8. uint16 constant TEST_GOVERNANCE_CHAIN_ID = 1;
  9. bytes32 constant TEST_GOVERNANCE_EMITTER = 0x0000000000000000000000000000000000000000000000000000000000000011;
  10. uint16 constant TARGET_CHAIN_ID = 2;
  11. function setUp() public {
  12. // Initialize wormhole with 1 guardian to match the working tests
  13. setUpWormholeReceiver(1);
  14. }
  15. function testGenerateSetFeeInTokenVAA() public view {
  16. bytes memory setFeeInTokenMessage = abi.encodePacked(
  17. MAGIC,
  18. uint8(GovernanceModule.Target),
  19. uint8(GovernanceAction.SetFeeInToken),
  20. TARGET_CHAIN_ID,
  21. uint64(5), // value
  22. uint64(3), // exponent
  23. uint8(20), // token address length
  24. hex"7e5f4552091a69125d5dfcb7b8c2659029395bdf" // token address
  25. );
  26. bytes memory vaa = encodeAndSignMessage(
  27. setFeeInTokenMessage,
  28. TEST_GOVERNANCE_CHAIN_ID,
  29. TEST_GOVERNANCE_EMITTER,
  30. 1
  31. );
  32. console.log("test_set_fee_in_token VAA:");
  33. console.logBytes(vaa);
  34. }
  35. function testGenerateSetWormholeAddressVAA() public view {
  36. bytes memory setWormholeAddressMessage = abi.encodePacked(
  37. MAGIC,
  38. uint8(GovernanceModule.Target),
  39. uint8(GovernanceAction.SetWormholeAddress),
  40. TARGET_CHAIN_ID,
  41. hex"7e5f4552091a69125d5dfcb7b8c2659029395bdf" // new wormhole address
  42. );
  43. bytes memory vaa = encodeAndSignMessage(
  44. setWormholeAddressMessage,
  45. TEST_GOVERNANCE_CHAIN_ID,
  46. TEST_GOVERNANCE_EMITTER,
  47. 1
  48. );
  49. console.log("test_set_wormhole_address VAA:");
  50. console.logBytes(vaa);
  51. }
  52. function testGenerateAuthorizeGovernanceDataSourceTransferVAA() public view {
  53. // For AuthorizeGovernanceDataSourceTransfer, the claim_vaa is the remaining payload
  54. // Based on governance_structs.rs lines 167-172, it expects claim_vaa = payload[cursor..]
  55. bytes memory claimVaa = abi.encodePacked(
  56. hex"be7e5f4552091a69125d5dfcb7b8c2659029395bdf", // 21 bytes: prefix + address
  57. uint64(100), // 8 bytes: sequence
  58. uint64(3) // 8 bytes: index
  59. );
  60. bytes memory authorizeMessage = abi.encodePacked(
  61. MAGIC,
  62. uint8(GovernanceModule.Target),
  63. uint8(GovernanceAction.AuthorizeGovernanceDataSourceTransfer),
  64. TARGET_CHAIN_ID,
  65. claimVaa
  66. );
  67. bytes memory vaa = encodeAndSignMessage(
  68. authorizeMessage,
  69. TEST_GOVERNANCE_CHAIN_ID,
  70. TEST_GOVERNANCE_EMITTER,
  71. 1
  72. );
  73. console.log("test_authorize_governance_data_source_transfer VAA:");
  74. console.logBytes(vaa);
  75. }
  76. function testGenerateSetTransactionFeeVAA() public view {
  77. bytes memory setTransactionFeeMessage = abi.encodePacked(
  78. MAGIC,
  79. uint8(GovernanceModule.Target),
  80. uint8(GovernanceAction.SetTransactionFee),
  81. TARGET_CHAIN_ID,
  82. uint64(100), // value
  83. uint64(3) // exponent
  84. );
  85. bytes memory vaa = encodeAndSignMessage(
  86. setTransactionFeeMessage,
  87. TEST_GOVERNANCE_CHAIN_ID,
  88. TEST_GOVERNANCE_EMITTER,
  89. 1
  90. );
  91. console.log("test_set_transaction_fee VAA:");
  92. console.logBytes(vaa);
  93. }
  94. function testGenerateWithdrawFeeVAA() public view {
  95. // For WithdrawFee, based on governance_structs.rs lines 348-384:
  96. // target_address (20 bytes) + value (8 bytes) + expo (8 bytes)
  97. bytes memory withdrawFeeMessage = abi.encodePacked(
  98. MAGIC,
  99. uint8(GovernanceModule.Target),
  100. uint8(GovernanceAction.WithdrawFee),
  101. TARGET_CHAIN_ID,
  102. hex"7e5f4552091a69125d5dfcb7b8c2659029395bdf", // target_address (20 bytes)
  103. uint64(100), // value (8 bytes)
  104. uint64(3) // expo (8 bytes)
  105. );
  106. bytes memory vaa = encodeAndSignMessage(
  107. withdrawFeeMessage,
  108. TEST_GOVERNANCE_CHAIN_ID,
  109. TEST_GOVERNANCE_EMITTER,
  110. 1
  111. );
  112. console.log("test_withdraw_fee VAA:");
  113. console.logBytes(vaa);
  114. }
  115. function encodeAndSignMessage(
  116. bytes memory data,
  117. uint16 emitterChainId,
  118. bytes32 emitterAddress,
  119. uint64 sequence
  120. ) internal view returns (bytes memory) {
  121. return generateVaa(
  122. uint32(1), // timestamp = 1 (same as working VAAs)
  123. emitterChainId,
  124. emitterAddress,
  125. sequence,
  126. data,
  127. 1 // Number of guardians
  128. );
  129. }
  130. }