Skip to content

Commit e8b88c4

Browse files
committed
Get cost model from context
1 parent a1a66cb commit e8b88c4

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

examples/plutus/forty_two/forty_two.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def get_env_val(key):
2626
payment_vkey = PaymentVerificationKey.from_signing_key(payment_skey)
2727

2828
chain_context = BlockFrostChainContext(
29-
project_id=get_env_val("BLOCKFROST_ID"), network=NETWORK
29+
project_id=get_env_val("BLOCKFROST_ID"),
30+
network=NETWORK,
31+
base_url="https://cardano-preview.blockfrost.io/api",
3032
)
3133

3234

@@ -67,7 +69,7 @@ def create_collateral(target_address, skey):
6769
script_hex = f.read()
6870
forty_two_script = cbor2.loads(bytes.fromhex(script_hex))
6971

70-
script_hash = plutus_script_hash(forty_two_script)
72+
script_hash = plutus_script_hash(PlutusV1Script(forty_two_script))
7173

7274
script_address = Address(script_hash, network=NETWORK)
7375

@@ -94,11 +96,20 @@ def create_collateral(target_address, skey):
9496
# Put integer 42 (the secret that unlocks the fund) in the redeemer.
9597
redeemer = Redeemer(RedeemerTag.SPEND, 42)
9698

97-
utxo_to_spend = chain_context.utxos(str(script_address))[0]
99+
utxo_to_spend = None
100+
for utxo in chain_context.utxos(str(script_address)):
101+
if utxo.output.datum_hash == datum_hash(datum):
102+
utxo_to_spend = utxo
103+
break
104+
105+
if utxo_to_spend is None:
106+
raise Exception("Can't find utxo to spend! Please try again later.")
98107

99108
builder = TransactionBuilder(chain_context)
100109

101-
builder.add_script_input(utxo_to_spend, forty_two_script, datum, redeemer)
110+
builder.add_script_input(
111+
utxo_to_spend, PlutusV1Script(forty_two_script), datum, redeemer
112+
)
102113

103114
# Send 5 ADA to taker address. The remaining ADA (~4.7) will be sent as change.
104115
take_output = TransactionOutput(taker_address, 5000000)

pycardano/backend/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ class ProtocolParameters:
9797

9898
coins_per_utxo_byte: int = None
9999

100+
cost_models: Dict[str, Dict[str, int]] = None
101+
"""A dict contains cost models for Plutus. The key will be "PlutusV1", "PlutusV2", etc.
102+
The value will be a dict of cost model parameters."""
103+
100104

101105
@typechecked
102106
class ChainContext:

pycardano/backend/blockfrost.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import argparse
21
import os
32
import tempfile
43
import time
@@ -28,13 +27,6 @@
2827
__all__ = ["BlockFrostChainContext"]
2928

3029

31-
def _namespace_to_dict(namespace: argparse.Namespace) -> dict:
32-
return {
33-
k: _namespace_to_dict(v) if isinstance(v, argparse.Namespace) else v
34-
for k, v in vars(namespace).items()
35-
}
36-
37-
3830
class BlockFrostChainContext(ChainContext):
3931
"""A `BlockFrost <https://blockfrost.io/>`_ API wrapper for the client code to interact with.
4032
@@ -119,8 +111,12 @@ def protocol_param(self) -> ProtocolParameters:
119111
max_val_size=int(params.max_val_size),
120112
collateral_percent=int(params.collateral_percent),
121113
max_collateral_inputs=int(params.max_collateral_inputs),
122-
coins_per_utxo_word=int(params.coins_per_utxo_word) or int(params.coins_per_utxo_size),
114+
coins_per_utxo_word=int(params.coins_per_utxo_word)
115+
or int(params.coins_per_utxo_size),
123116
coins_per_utxo_byte=int(params.coins_per_utxo_size),
117+
cost_models={
118+
k: v.to_dict() for k, v in params.cost_models.to_dict().items()
119+
},
124120
)
125121
return self._protocol_param
126122

pycardano/backend/ogmios.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ def protocol_param(self) -> ProtocolParameters:
121121
"coinsPerUtxoWord", ALONZO_COINS_PER_UTXO_WORD
122122
),
123123
coins_per_utxo_byte=result.get("coinsPerUtxoByte", 0),
124+
cost_models=result.get("costModels", {}),
124125
)
125126

127+
if "plutus:v1" in param.cost_models:
128+
param.cost_models["PlutusV1"] = param.cost_models.pop("plutus:v1")
129+
if "plutus:v2" in param.cost_models:
130+
param.cost_models["PlutusV2"] = param.cost_models.pop("plutus:v2")
131+
126132
args = {"query": "genesisConfig"}
127133
result = self._request(method, args)
128134
param.min_utxo = result["protocolParameters"]["minUtxoValue"]

pycardano/txbuilder.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,16 @@ def script_data_hash(self) -> Optional[ScriptDataHash]:
384384
if self.datums or self.redeemers:
385385
cost_models = {}
386386
for s in self.all_scripts:
387-
if isinstance(s, PlutusV1Script):
388-
cost_models[0] = PLUTUS_V1_COST_MODEL
387+
if isinstance(s, PlutusV1Script) or type(s) == bytes:
388+
cost_models[0] = (
389+
self.context.protocol_param.cost_models.get("PlutusV1")
390+
or PLUTUS_V1_COST_MODEL
391+
)
389392
if isinstance(s, PlutusV2Script):
390-
cost_models[1] = PLUTUS_V2_COST_MODEL
393+
cost_models[1] = (
394+
self.context.protocol_param.cost_models.get("PlutusV2")
395+
or PLUTUS_V2_COST_MODEL
396+
)
391397
return script_data_hash(
392398
self.redeemers, list(self.datums.values()), CostModels(cost_models)
393399
)

test/pycardano/backend/test_ogmios.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def test_protocol_param(self):
125125
max_collateral_inputs=5,
126126
coins_per_utxo_word=1,
127127
coins_per_utxo_byte=1,
128+
cost_models={},
128129
)
129130
== self.chain_context.protocol_param
130131
)

test/pycardano/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class FixedChainContext(ChainContext):
4545
max_collateral_inputs=3,
4646
coins_per_utxo_word=34482,
4747
coins_per_utxo_byte=4310,
48+
cost_models={},
4849
)
4950

5051
_genesis_param = GenesisParameters(

0 commit comments

Comments
 (0)