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