Skip to content

Commit 12e4545

Browse files
committed
Refactor integration tests
1 parent 03c74b2 commit 12e4545

File tree

4 files changed

+256
-228
lines changed

4 files changed

+256
-228
lines changed

integration-test/test/base.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""An example that demonstrates low-level construction of a transaction."""
2+
3+
import os
4+
import time
5+
6+
from retry import retry
7+
8+
from pycardano import *
9+
10+
11+
@retry(tries=10, delay=4)
12+
def check_chain_context(chain_context):
13+
print(f"Current chain tip: {chain_context.last_block_slot}")
14+
15+
16+
class TestBase:
17+
# Define chain context
18+
NETWORK = Network.TESTNET
19+
20+
OGMIOS_WS = "ws://localhost:1337"
21+
22+
KUPO_URL = "http://localhost:1442/v1/matches"
23+
24+
chain_context = OgmiosChainContext(OGMIOS_WS, Network.TESTNET, kupo_url=KUPO_URL)
25+
26+
check_chain_context(chain_context)
27+
28+
payment_key_path = os.environ.get("PAYMENT_KEY")
29+
extended_key_path = os.environ.get("EXTENDED_PAYMENT_KEY")
30+
if not payment_key_path or not extended_key_path:
31+
raise Exception(
32+
"Cannot find payment key. Please specify environment variable PAYMENT_KEY and extended_key_path"
33+
)
34+
payment_skey = PaymentSigningKey.load(payment_key_path)
35+
payment_vkey = PaymentVerificationKey.from_signing_key(payment_skey)
36+
extended_payment_skey = PaymentExtendedSigningKey.load(extended_key_path)
37+
extended_payment_vkey = PaymentExtendedVerificationKey.from_signing_key(
38+
extended_payment_skey
39+
)
40+
41+
payment_key_pair = PaymentKeyPair.generate()
42+
stake_key_pair = StakeKeyPair.generate()
43+
44+
@retry(tries=4, delay=1)
45+
def assert_output(self, target_address, target_output):
46+
time.sleep(1)
47+
utxos = self.chain_context.utxos(str(target_address))
48+
found = False
49+
50+
for utxo in utxos:
51+
output = utxo.output
52+
if output == target_output:
53+
found = True
54+
55+
assert found, f"Cannot find target UTxO in address: {target_address}"
56+
57+
@retry(tries=4, delay=6, backoff=2, jitter=(1, 3))
58+
def fund(self, source_address, source_key, target_address, amount=5000000):
59+
builder = TransactionBuilder(self.chain_context)
60+
61+
builder.add_input_address(source_address)
62+
output = TransactionOutput(target_address, amount)
63+
builder.add_output(output)
64+
65+
signed_tx = builder.build_and_sign([source_key], source_address)
66+
67+
print("############### Transaction created ###############")
68+
print(signed_tx)
69+
print(signed_tx.to_cbor())
70+
print("############### Submitting transaction ###############")
71+
self.chain_context.submit_tx(signed_tx.to_cbor())
72+
self.assert_output(target_address, target_output=output)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)