|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +from retry import retry |
| 5 | + |
| 6 | +from pycardano import * |
| 7 | + |
| 8 | +from .base import TEST_RETRIES, TestBase |
| 9 | + |
| 10 | + |
| 11 | +class TestGovernanceAction(TestBase): |
| 12 | + @retry(tries=TEST_RETRIES, backoff=1.3, delay=2, jitter=(0, 10)) |
| 13 | + def test_governance_action_and_voting(self): |
| 14 | + # Create new stake key pair |
| 15 | + stake_key_pair = StakeKeyPair.generate() |
| 16 | + |
| 17 | + # Create addresses for testing |
| 18 | + address = Address( |
| 19 | + self.payment_vkey.hash(), |
| 20 | + stake_key_pair.verification_key.hash(), |
| 21 | + self.NETWORK, |
| 22 | + ) |
| 23 | + |
| 24 | + # Load pool cold key for signing |
| 25 | + # pool_cold_skey = PaymentSigningKey.load(self.pool_cold_key_path) |
| 26 | + |
| 27 | + # First, ensure we have enough funds |
| 28 | + utxos = self.chain_context.utxos(address) |
| 29 | + |
| 30 | + if not utxos: |
| 31 | + giver_address = Address(self.payment_vkey.hash(), network=self.NETWORK) |
| 32 | + |
| 33 | + builder = TransactionBuilder(self.chain_context) |
| 34 | + builder.add_input_address(giver_address) |
| 35 | + builder.add_output(TransactionOutput(address, 110000000000)) |
| 36 | + |
| 37 | + signed_tx = builder.build_and_sign([self.payment_skey], giver_address) |
| 38 | + print("############### Funding Transaction created ###############") |
| 39 | + print(signed_tx) |
| 40 | + print(signed_tx.to_cbor_hex()) |
| 41 | + print("############### Submitting funding transaction ###############") |
| 42 | + self.chain_context.submit_tx(signed_tx) |
| 43 | + time.sleep(5) |
| 44 | + |
| 45 | + # Step 1: Register as a DRep first |
| 46 | + drep_credential = DRepCredential(stake_key_pair.verification_key.hash()) |
| 47 | + anchor = Anchor( |
| 48 | + url="https://test-drep.com", |
| 49 | + data_hash=AnchorDataHash(bytes.fromhex("0" * 64)), |
| 50 | + ) |
| 51 | + |
| 52 | + drep_registration = RegDRepCert( |
| 53 | + drep_credential=drep_credential, |
| 54 | + coin=500000000, |
| 55 | + anchor=anchor, |
| 56 | + ) |
| 57 | + |
| 58 | + stake_credential = StakeCredential(stake_key_pair.verification_key.hash()) |
| 59 | + pool_hash = PoolKeyHash(bytes.fromhex(os.environ.get("POOL_ID").strip())) |
| 60 | + |
| 61 | + drep = DRep( |
| 62 | + DRepKind.VERIFICATION_KEY_HASH, |
| 63 | + stake_key_pair.verification_key.hash(), |
| 64 | + ) |
| 65 | + |
| 66 | + all_in_one_cert = StakeRegistrationAndDelegationAndVoteDelegation( |
| 67 | + stake_credential, pool_hash, drep, 1000000 |
| 68 | + ) |
| 69 | + |
| 70 | + # Create transaction for DRep registration |
| 71 | + builder = TransactionBuilder(self.chain_context) |
| 72 | + builder.add_input_address(address) |
| 73 | + builder.add_output(TransactionOutput(address, 35000000)) |
| 74 | + builder.certificates = [drep_registration, all_in_one_cert] |
| 75 | + |
| 76 | + signed_tx = builder.build_and_sign( |
| 77 | + [stake_key_pair.signing_key, self.payment_skey], |
| 78 | + address, |
| 79 | + ) |
| 80 | + print("############### DRep Registration Transaction created ###############") |
| 81 | + print(signed_tx) |
| 82 | + print(signed_tx.to_cbor_hex()) |
| 83 | + print("############### Submitting DRep registration ###############") |
| 84 | + self.chain_context.submit_tx(signed_tx) |
| 85 | + time.sleep(5) |
| 86 | + |
| 87 | + # Step 2: Create and submit parameter change action |
| 88 | + info_proposal = InfoAction() |
| 89 | + |
| 90 | + # Create transaction for parameter change |
| 91 | + builder = TransactionBuilder(self.chain_context) |
| 92 | + builder.add_input_address(address) |
| 93 | + builder.add_output(TransactionOutput(address, 35000000)) |
| 94 | + reward_account = Address( |
| 95 | + staking_part=stake_key_pair.verification_key.hash(), network=self.NETWORK |
| 96 | + ) |
| 97 | + builder.add_proposal( |
| 98 | + 100000000000, |
| 99 | + bytes(reward_account), |
| 100 | + info_proposal, |
| 101 | + Anchor( |
| 102 | + url="https://test-info.com", |
| 103 | + data_hash=AnchorDataHash(bytes.fromhex("0" * 64)), |
| 104 | + ), |
| 105 | + ) |
| 106 | + |
| 107 | + # Sign with both payment key and pool cold key for governance action |
| 108 | + signed_tx = builder.build_and_sign( |
| 109 | + [self.payment_skey, stake_key_pair.signing_key], |
| 110 | + address, |
| 111 | + ) |
| 112 | + print("############### Gov Action Transaction created ###############") |
| 113 | + print(signed_tx) |
| 114 | + print(signed_tx.to_cbor_hex()) |
| 115 | + print("############### Submitting gov action transaction ###############") |
| 116 | + self.chain_context.submit_tx(signed_tx) |
| 117 | + time.sleep(5) |
| 118 | + |
| 119 | + # Get the governance action ID from the transaction |
| 120 | + gov_action_id = GovActionId( |
| 121 | + transaction_id=signed_tx.id, |
| 122 | + gov_action_index=0, # First governance action in the transaction |
| 123 | + ) |
| 124 | + |
| 125 | + # Step 3: Vote for the action as a DRep |
| 126 | + drep_voter = Voter( |
| 127 | + credential=stake_key_pair.verification_key.hash(), |
| 128 | + voter_type=VoterType.DREP, |
| 129 | + ) |
| 130 | + |
| 131 | + # Step 4: Vote for the action as a stake pool |
| 132 | + pool_id = os.environ.get("POOL_ID").strip() |
| 133 | + |
| 134 | + # Create transaction for voting |
| 135 | + builder = TransactionBuilder(self.chain_context) |
| 136 | + builder.add_input_address(address) |
| 137 | + builder.add_output(TransactionOutput(address, 35000000)) |
| 138 | + |
| 139 | + # Add DRep vote using the helper method |
| 140 | + builder.add_vote( |
| 141 | + voter=drep_voter, |
| 142 | + gov_action_id=gov_action_id, |
| 143 | + vote=Vote.YES, |
| 144 | + anchor=Anchor( |
| 145 | + url="https://test-drep.com", |
| 146 | + data_hash=AnchorDataHash(bytes.fromhex("0" * 64)), |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + # Add pool vote using the helper method |
| 151 | + pool_voter = Voter( |
| 152 | + credential=VerificationKeyHash(bytes.fromhex(pool_id)), |
| 153 | + voter_type=VoterType.STAKING_POOL, |
| 154 | + ) |
| 155 | + builder.add_vote( |
| 156 | + voter=pool_voter, |
| 157 | + gov_action_id=gov_action_id, |
| 158 | + vote=Vote.YES, |
| 159 | + anchor=Anchor( |
| 160 | + url="https://test-pool.com", |
| 161 | + data_hash=AnchorDataHash(bytes.fromhex("0" * 64)), |
| 162 | + ), |
| 163 | + ) |
| 164 | + |
| 165 | + # Sign with all required keys |
| 166 | + signed_tx = builder.build_and_sign( |
| 167 | + [ |
| 168 | + stake_key_pair.signing_key, |
| 169 | + self.payment_skey, |
| 170 | + self.pool_cold_skey, |
| 171 | + ], |
| 172 | + address, |
| 173 | + ) |
| 174 | + print("############### Voting Transaction created ###############") |
| 175 | + print(signed_tx) |
| 176 | + print(signed_tx.to_cbor_hex()) |
| 177 | + print("############### Submitting voting transaction ###############") |
| 178 | + self.chain_context.submit_tx(signed_tx) |
| 179 | + |
| 180 | + print("############### Test completed successfully ###############") |
0 commit comments