Skip to content

Commit 95881a0

Browse files
authored
Simplifies the backend interface (use available classes!) (#206)
This is a minimal effort change to the backends to leverage the available classes in PyCardano for user-friendliness In particular, if a class for something is available then the backend should support it
1 parent 090879e commit 95881a0

File tree

16 files changed

+91
-56
lines changed

16 files changed

+91
-56
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ builder = TransactionBuilder(context)
105105
builder.add_input_address(address)
106106

107107
# Get all UTxOs currently sitting at this address
108-
utxos = context.utxos(str(address))
108+
utxos = context.utxos(address)
109109

110110
# We can also tell the builder to include a specific UTxO in the transaction.
111111
# Similarly, "add_input" could be called multiple times.
@@ -156,7 +156,7 @@ builder.add_output(
156156
signed_tx = builder.build_and_sign([psk], change_address=address)
157157

158158
# Submit signed transaction to the network
159-
context.submit_tx(signed_tx.to_cbor())
159+
context.submit_tx(signed_tx)
160160

161161
```
162162
</details>

examples/delegator_loyalty_rewards.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@
101101

102102
print("#### Transaction id ####")
103103
print(signed_tx.id)
104-
context.submit_tx(signed_tx.to_cbor())
104+
context.submit_tx(signed_tx)
105105
print("Transaction successfully submitted!")

examples/full_stack/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ def submit_tx():
7777
print(f"Transaction: \n {tx}")
7878
print(f"Transaction cbor: {tx.to_cbor()}")
7979
print(f"Transaction ID: {tx_id}")
80-
chain_context.submit_tx(tx.to_cbor())
80+
chain_context.submit_tx(tx)
8181
return {"tx_id": tx_id}

examples/native_token.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,4 @@ def load_or_create_key_pair(base_dir, base_name):
172172

173173
# Submit signed transaction to the network
174174
print("############### Submitting transaction ###############")
175-
chain_context.submit_tx(signed_tx.to_cbor())
175+
chain_context.submit_tx(signed_tx)

examples/plutus/forty_two/forty_two.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def submit_tx(tx):
4343
print(tx)
4444
print(tx.to_cbor())
4545
print("############### Submitting transaction ###############")
46-
chain_context.submit_tx(tx.to_cbor())
46+
chain_context.submit_tx(tx)
4747
wait_for_tx(str(tx.id))
4848

4949

@@ -91,15 +91,15 @@ def submit_tx(tx):
9191
utxo_to_spend = None
9292

9393
# Spend the utxo with datum 42 sitting at the script address
94-
for utxo in chain_context.utxos(str(script_address)):
94+
for utxo in chain_context.utxos(script_address):
9595
print(utxo)
9696
if utxo.output.datum:
9797
utxo_to_spend = utxo
9898
break
9999

100100
# Find the reference script utxo
101101
reference_script_utxo = None
102-
for utxo in chain_context.utxos(str(giver_address)):
102+
for utxo in chain_context.utxos(giver_address):
103103
if utxo.output.script and utxo.output.script == forty_two_script:
104104
reference_script_utxo = utxo
105105
break

examples/tx_builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
builder.add_input_address(address)
3232

3333
# Get all UTxOs currently sitting at this address
34-
utxos = context.utxos(str(address))
34+
utxos = context.utxos(address)
3535

3636
# We can also tell the builder to include a specific UTxO in the transaction.
3737
# Similarly, "add_input" could be called multiple times.
@@ -82,4 +82,4 @@
8282
signed_tx = builder.build_and_sign([psk], change_address=address)
8383

8484
# Submit signed transaction to the network
85-
context.submit_tx(signed_tx.to_cbor())
85+
context.submit_tx(signed_tx)

integration-test/test/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TestBase:
4545

4646
@retry(tries=TEST_RETRIES, delay=3)
4747
def assert_output(self, target_address, target_output):
48-
utxos = self.chain_context.utxos(str(target_address))
48+
utxos = self.chain_context.utxos(target_address)
4949
found = False
5050

5151
for utxo in utxos:
@@ -69,5 +69,5 @@ def fund(self, source_address, source_key, target_address, amount=5000000):
6969
print(signed_tx)
7070
print(signed_tx.to_cbor())
7171
print("############### Submitting transaction ###############")
72-
self.chain_context.submit_tx(signed_tx.to_cbor())
72+
self.chain_context.submit_tx(signed_tx)
7373
self.assert_output(target_address, target_output=output)

integration-test/test/test_certificate.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_stake_delegation(self):
1717
self.NETWORK,
1818
)
1919

20-
utxos = self.chain_context.utxos(str(address))
20+
utxos = self.chain_context.utxos(address)
2121

2222
if not utxos:
2323
giver_address = Address(self.payment_vkey.hash(), network=self.NETWORK)
@@ -33,7 +33,7 @@ def test_stake_delegation(self):
3333
print(signed_tx)
3434
print(signed_tx.to_cbor())
3535
print("############### Submitting transaction ###############")
36-
self.chain_context.submit_tx(signed_tx.to_cbor())
36+
self.chain_context.submit_tx(signed_tx)
3737

3838
time.sleep(3)
3939

@@ -60,7 +60,7 @@ def test_stake_delegation(self):
6060
print(signed_tx)
6161
print(signed_tx.to_cbor())
6262
print("############### Submitting transaction ###############")
63-
self.chain_context.submit_tx(signed_tx.to_cbor())
63+
self.chain_context.submit_tx(signed_tx)
6464

6565
time.sleep(8)
6666

@@ -86,4 +86,4 @@ def test_stake_delegation(self):
8686
print(signed_tx)
8787
print(signed_tx.to_cbor())
8888
print("############### Submitting transaction ###############")
89-
self.chain_context.submit_tx(signed_tx.to_cbor())
89+
self.chain_context.submit_tx(signed_tx)

integration-test/test/test_min_utxo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ class MyPlutusData(PlutusData):
102102

103103
# Submit signed transaction to the network
104104
print("############### Submitting transaction ###############")
105-
self.chain_context.submit_tx(signed_tx.to_cbor())
105+
self.chain_context.submit_tx(signed_tx)
106106

107107
self.assert_output(address, nft_output)

integration-test/test/test_mint.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def load_or_create_key_pair(base_dir, base_name):
136136

137137
# Submit signed transaction to the network
138138
print("############### Submitting transaction ###############")
139-
self.chain_context.submit_tx(signed_tx.to_cbor())
139+
self.chain_context.submit_tx(signed_tx)
140140

141141
self.assert_output(address, nft_output)
142142

@@ -162,7 +162,7 @@ def load_or_create_key_pair(base_dir, base_name):
162162

163163
# Submit signed transaction to the network
164164
print("############### Submitting transaction ###############")
165-
self.chain_context.submit_tx(signed_tx.to_cbor())
165+
self.chain_context.submit_tx(signed_tx)
166166

167167
self.assert_output(address, nft_to_send)
168168

@@ -233,7 +233,7 @@ def test_mint_nft_with_script(self):
233233
self.fund(address, self.payment_skey, address)
234234

235235
non_nft_utxo = None
236-
for utxo in self.chain_context.utxos(str(address)):
236+
for utxo in self.chain_context.utxos(address):
237237
# multi_asset should be empty for collateral utxo
238238
if not utxo.output.amount.multi_asset:
239239
non_nft_utxo = utxo
@@ -251,7 +251,7 @@ def test_mint_nft_with_script(self):
251251

252252
# Submit signed transaction to the network
253253
print("############### Submitting transaction ###############")
254-
self.chain_context.submit_tx(signed_tx.to_cbor())
254+
self.chain_context.submit_tx(signed_tx)
255255

256256
self.assert_output(address, nft_output)
257257

@@ -338,6 +338,6 @@ class MyPlutusData(PlutusData):
338338

339339
# Submit signed transaction to the network
340340
print("############### Submitting transaction ###############")
341-
self.chain_context.submit_tx(signed_tx.to_cbor())
341+
self.chain_context.submit_tx(signed_tx)
342342

343343
self.assert_output(address, nft_output)

integration-test/test/test_plutus.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_plutus_v1(self):
3737
print(signed_tx)
3838
print(signed_tx.to_cbor())
3939
print("############### Submitting transaction ###############")
40-
self.chain_context.submit_tx(signed_tx.to_cbor())
40+
self.chain_context.submit_tx(signed_tx)
4141
time.sleep(3)
4242

4343
# ----------- Fund taker a collateral UTxO ---------------
@@ -55,14 +55,14 @@ def test_plutus_v1(self):
5555
print(signed_tx)
5656
print(signed_tx.to_cbor())
5757
print("############### Submitting transaction ###############")
58-
self.chain_context.submit_tx(signed_tx.to_cbor())
58+
self.chain_context.submit_tx(signed_tx)
5959
time.sleep(3)
6060

6161
# ----------- Taker take ---------------
6262

6363
redeemer = Redeemer(42)
6464

65-
utxo_to_spend = self.chain_context.utxos(str(script_address))[0]
65+
utxo_to_spend = self.chain_context.utxos(script_address)[0]
6666

6767
taker_address = Address(self.extended_payment_vkey.hash(), network=self.NETWORK)
6868

@@ -75,7 +75,7 @@ def test_plutus_v1(self):
7575
builder.add_output(take_output)
7676

7777
non_nft_utxo = None
78-
for utxo in self.chain_context.utxos(str(taker_address)):
78+
for utxo in self.chain_context.utxos(taker_address):
7979
# multi_asset should be empty for collateral utxo
8080
if not utxo.output.amount.multi_asset:
8181
non_nft_utxo = utxo
@@ -89,7 +89,7 @@ def test_plutus_v1(self):
8989
print(signed_tx)
9090
print(signed_tx.to_cbor())
9191
print("############### Submitting transaction ###############")
92-
self.chain_context.submit_tx(signed_tx.to_cbor())
92+
self.chain_context.submit_tx(signed_tx)
9393

9494
self.assert_output(taker_address, take_output)
9595

@@ -121,7 +121,7 @@ def test_plutus_v2_datum_hash(self):
121121
print(signed_tx)
122122
print(signed_tx.to_cbor())
123123
print("############### Submitting transaction ###############")
124-
self.chain_context.submit_tx(signed_tx.to_cbor())
124+
self.chain_context.submit_tx(signed_tx)
125125
time.sleep(3)
126126

127127
# ----------- Taker take ---------------
@@ -131,7 +131,7 @@ def test_plutus_v2_datum_hash(self):
131131
utxo_to_spend = None
132132

133133
# Speed the utxo that doesn't have datum/datum_hash or script attached
134-
for utxo in self.chain_context.utxos(str(script_address)):
134+
for utxo in self.chain_context.utxos(script_address):
135135
if not utxo.output.script and (
136136
utxo.output.datum_hash == datum_hash(datum)
137137
or utxo.output.datum == datum
@@ -150,7 +150,7 @@ def test_plutus_v2_datum_hash(self):
150150
builder.add_output(take_output)
151151

152152
non_nft_utxo = None
153-
for utxo in self.chain_context.utxos(str(taker_address)):
153+
for utxo in self.chain_context.utxos(taker_address):
154154
# multi_asset should be empty for collateral utxo
155155
if not utxo.output.amount.multi_asset:
156156
non_nft_utxo = utxo
@@ -164,7 +164,7 @@ def test_plutus_v2_datum_hash(self):
164164
print(signed_tx)
165165
print(signed_tx.to_cbor())
166166
print("############### Submitting transaction ###############")
167-
self.chain_context.submit_tx(signed_tx.to_cbor())
167+
self.chain_context.submit_tx(signed_tx)
168168

169169
self.assert_output(taker_address, take_output)
170170

@@ -198,7 +198,7 @@ def test_plutus_v2_inline_script_inline_datum(self):
198198
print(signed_tx)
199199
print(signed_tx.to_cbor())
200200
print("############### Submitting transaction ###############")
201-
self.chain_context.submit_tx(signed_tx.to_cbor())
201+
self.chain_context.submit_tx(signed_tx)
202202
time.sleep(3)
203203

204204
# ----------- Taker take ---------------
@@ -208,7 +208,7 @@ def test_plutus_v2_inline_script_inline_datum(self):
208208
utxo_to_spend = None
209209

210210
# Speed the utxo that has both inline script and inline datum
211-
for utxo in self.chain_context.utxos(str(script_address)):
211+
for utxo in self.chain_context.utxos(script_address):
212212
if utxo.output.datum and utxo.output.script:
213213
utxo_to_spend = utxo
214214
break
@@ -225,9 +225,9 @@ def test_plutus_v2_inline_script_inline_datum(self):
225225

226226
print("############### Transaction created ###############")
227227
print(signed_tx)
228-
print(signed_tx.to_cbor())
228+
print(signed_tx)
229229
print("############### Submitting transaction ###############")
230-
self.chain_context.submit_tx(signed_tx.to_cbor())
230+
self.chain_context.submit_tx(signed_tx)
231231

232232
self.assert_output(taker_address, take_output)
233233

@@ -259,7 +259,7 @@ def test_plutus_v2_ref_script(self):
259259
print(signed_tx)
260260
print(signed_tx.to_cbor())
261261
print("############### Submitting transaction ###############")
262-
self.chain_context.submit_tx(signed_tx.to_cbor())
262+
self.chain_context.submit_tx(signed_tx)
263263
time.sleep(3)
264264

265265
# ----------- Send ADA to the same script address without datum or script ---------------
@@ -276,7 +276,7 @@ def test_plutus_v2_ref_script(self):
276276
print(signed_tx)
277277
print(signed_tx.to_cbor())
278278
print("############### Submitting transaction ###############")
279-
self.chain_context.submit_tx(signed_tx.to_cbor())
279+
self.chain_context.submit_tx(signed_tx)
280280
time.sleep(3)
281281

282282
# ----------- Taker take ---------------
@@ -286,7 +286,7 @@ def test_plutus_v2_ref_script(self):
286286
utxo_to_spend = None
287287

288288
# Spend the utxo that doesn't have datum/datum_hash or script attached
289-
for utxo in self.chain_context.utxos(str(script_address)):
289+
for utxo in self.chain_context.utxos(script_address):
290290
if not utxo.output.script and (
291291
utxo.output.datum_hash == datum_hash(datum)
292292
or datum_hash(utxo.output.datum) == datum_hash(datum)
@@ -308,6 +308,6 @@ def test_plutus_v2_ref_script(self):
308308
print(signed_tx)
309309
print(signed_tx.to_cbor())
310310
print("############### Submitting transaction ###############")
311-
self.chain_context.submit_tx(signed_tx.to_cbor())
311+
self.chain_context.submit_tx(signed_tx)
312312

313313
self.assert_output(taker_address, take_output)

pycardano/backend/base.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
from typeguard import typechecked
77

8+
from pycardano.address import Address
89
from pycardano.network import Network
910
from pycardano.plutus import ExecutionUnits
10-
from pycardano.transaction import UTxO
11+
from pycardano.transaction import UTxO, Transaction
1112

1213
__all__ = [
1314
"GenesisParameters",
@@ -136,7 +137,18 @@ def last_block_slot(self) -> int:
136137
"""Slot number of last block"""
137138
raise NotImplementedError()
138139

139-
def utxos(self, address: str) -> List[UTxO]:
140+
def utxos(self, address: Union[str, Address]) -> List[UTxO]:
141+
"""Get all UTxOs associated with an address.
142+
143+
Args:
144+
address (Union[str, Address]): An address, potentially bech32 encoded
145+
146+
Returns:
147+
List[UTxO]: A list of UTxOs.
148+
"""
149+
return self._utxos(str(address))
150+
151+
def _utxos(self, address: str) -> List[UTxO]:
140152
"""Get all UTxOs associated with an address.
141153
142154
Args:
@@ -147,7 +159,19 @@ def utxos(self, address: str) -> List[UTxO]:
147159
"""
148160
raise NotImplementedError()
149161

150-
def submit_tx(self, cbor: Union[bytes, str]):
162+
def submit_tx(self, tx: Transaction):
163+
"""Submit a transaction to the blockchain.
164+
165+
Args:
166+
tx (Transaction): The transaction to be submitted.
167+
168+
Raises:
169+
:class:`InvalidArgumentException`: When the transaction is invalid.
170+
:class:`TransactionFailedException`: When fails to submit the transaction to blockchain.
171+
"""
172+
return self.submit_tx_cbor(tx.to_cbor("bytes"))
173+
174+
def submit_tx_cbor(self, cbor: Union[bytes, str]):
151175
"""Submit a transaction to the blockchain.
152176
153177
Args:
@@ -159,7 +183,18 @@ def submit_tx(self, cbor: Union[bytes, str]):
159183
"""
160184
raise NotImplementedError()
161185

162-
def evaluate_tx(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
186+
def evaluate_tx(self, tx: Transaction) -> Dict[str, ExecutionUnits]:
187+
"""Evaluate execution units of a transaction.
188+
189+
Args:
190+
transaction (Transaction): The transaction to be evaluated.
191+
192+
Returns:
193+
List[ExecutionUnits]: A list of execution units calculated for each of the transaction's redeemers
194+
"""
195+
return self.evaluate_tx_cbor(tx.to_cbor("bytes"))
196+
197+
def evaluate_tx_cbor(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
163198
"""Evaluate execution units of a transaction.
164199
165200
Args:

0 commit comments

Comments
 (0)