|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +from retry import retry |
| 5 | + |
| 6 | +from pycardano import * |
| 7 | + |
| 8 | +from .base import TestBase |
| 9 | + |
| 10 | + |
| 11 | +class TestDelegation(TestBase): |
| 12 | + @retry(tries=4, delay=6, backoff=2, jitter=(1, 3)) |
| 13 | + def test_stake_delegation(self): |
| 14 | + |
| 15 | + address = Address( |
| 16 | + self.payment_key_pair.verification_key.hash(), |
| 17 | + self.stake_key_pair.verification_key.hash(), |
| 18 | + self.NETWORK, |
| 19 | + ) |
| 20 | + |
| 21 | + utxos = self.chain_context.utxos(str(address)) |
| 22 | + |
| 23 | + if not utxos: |
| 24 | + giver_address = Address(self.payment_vkey.hash(), network=self.NETWORK) |
| 25 | + |
| 26 | + builder = TransactionBuilder(self.chain_context) |
| 27 | + |
| 28 | + builder.add_input_address(giver_address) |
| 29 | + builder.add_output(TransactionOutput(address, 440000000000)) |
| 30 | + |
| 31 | + signed_tx = builder.build_and_sign([self.payment_skey], giver_address) |
| 32 | + |
| 33 | + print("############### Transaction created ###############") |
| 34 | + print(signed_tx) |
| 35 | + print(signed_tx.to_cbor()) |
| 36 | + print("############### Submitting transaction ###############") |
| 37 | + self.chain_context.submit_tx(signed_tx.to_cbor()) |
| 38 | + |
| 39 | + time.sleep(3) |
| 40 | + |
| 41 | + stake_credential = StakeCredential( |
| 42 | + self.stake_key_pair.verification_key.hash() |
| 43 | + ) |
| 44 | + stake_registration = StakeRegistration(stake_credential) |
| 45 | + pool_hash = PoolKeyHash(bytes.fromhex(os.environ.get("POOL_ID").strip())) |
| 46 | + stake_delegation = StakeDelegation(stake_credential, pool_keyhash=pool_hash) |
| 47 | + |
| 48 | + builder = TransactionBuilder(self.chain_context) |
| 49 | + |
| 50 | + builder.add_input_address(address) |
| 51 | + builder.add_output(TransactionOutput(address, 35000000)) |
| 52 | + |
| 53 | + builder.certificates = [stake_registration, stake_delegation] |
| 54 | + |
| 55 | + signed_tx = builder.build_and_sign( |
| 56 | + [self.stake_key_pair.signing_key, self.payment_key_pair.signing_key], |
| 57 | + address, |
| 58 | + ) |
| 59 | + |
| 60 | + print("############### Transaction created ###############") |
| 61 | + print(signed_tx) |
| 62 | + print(signed_tx.to_cbor()) |
| 63 | + print("############### Submitting transaction ###############") |
| 64 | + self.chain_context.submit_tx(signed_tx.to_cbor()) |
| 65 | + |
| 66 | + time.sleep(8) |
| 67 | + |
| 68 | + builder = TransactionBuilder(self.chain_context) |
| 69 | + |
| 70 | + builder.add_input_address(address) |
| 71 | + |
| 72 | + stake_address = Address( |
| 73 | + staking_part=self.stake_key_pair.verification_key.hash(), |
| 74 | + network=self.NETWORK, |
| 75 | + ) |
| 76 | + |
| 77 | + builder.withdrawals = Withdrawals({bytes(stake_address): 0}) |
| 78 | + |
| 79 | + builder.add_output(TransactionOutput(address, 1000000)) |
| 80 | + |
| 81 | + signed_tx = builder.build_and_sign( |
| 82 | + [self.stake_key_pair.signing_key, self.payment_key_pair.signing_key], |
| 83 | + address, |
| 84 | + ) |
| 85 | + |
| 86 | + print("############### Transaction created ###############") |
| 87 | + print(signed_tx) |
| 88 | + print(signed_tx.to_cbor()) |
| 89 | + print("############### Submitting transaction ###############") |
| 90 | + self.chain_context.submit_tx(signed_tx.to_cbor()) |
0 commit comments