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