From d78d5cb5ba847ecf9d2c8732bd7b29710c1bf689 Mon Sep 17 00:00:00 2001 From: SCM Date: Thu, 17 Oct 2024 15:43:13 +0100 Subject: [PATCH 1/2] minting tests --- plutus_bench/protocol_params.py | 1 + tests/mint.py | 31 +++++++++++++++++++------------ tests/test_mint.py | 13 ++++++++++--- tests/test_mint_mockfrost.py | 20 ++++++++++++++------ 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/plutus_bench/protocol_params.py b/plutus_bench/protocol_params.py index d332d16..9e6e627 100644 --- a/plutus_bench/protocol_params.py +++ b/plutus_bench/protocol_params.py @@ -1,6 +1,7 @@ """ Default parameters copied from preview testnet """ + from fractions import Fraction from pycardano import ProtocolParameters, GenesisParameters diff --git a/tests/mint.py b/tests/mint.py index c6c3c12..6c93dd9 100644 --- a/tests/mint.py +++ b/tests/mint.py @@ -7,6 +7,7 @@ own_path = pathlib.Path(__file__) + def mint_coin_with_contract( token_name: str, amount: int, @@ -18,10 +19,12 @@ def mint_coin_with_contract( tn_bytes = bytes(token_name, encoding="utf-8") - - VerificationKey = pycardano.PaymentVerificationKey.from_signing_key(issuer_signing_key) - payment_address = pycardano.Address(payment_part=VerificationKey.hash(), network=network) - + VerificationKey = pycardano.PaymentVerificationKey.from_signing_key( + issuer_signing_key + ) + payment_address = pycardano.Address( + payment_part=VerificationKey.hash(), network=network + ) # get input utxo utxo_to_spend_or_burn = None @@ -31,6 +34,7 @@ def mint_coin_with_contract( utxo_to_spend_or_burn = utxo break else: + def f(pi: pycardano.ScriptHash, an: pycardano.AssetName, a: int) -> bool: return pi == script_hash and an.payload == tn_bytes and a >= -amount @@ -39,34 +43,37 @@ def f(pi: pycardano.ScriptHash, an: pycardano.AssetName, a: int) -> bool: utxo_to_spend_or_burn = utxo assert utxo_to_spend_or_burn is not None, "UTxO not found to spend!" - # Build script mint_script_path = own_path.parent / "contracts" / "signed_mint.py" pkh = required_key.hash() plutus_script = build(mint_script_path, pkh) - script_hash = pycardano.plutus_script_hash(plutus_script) # Build the transaction builder = pycardano.TransactionBuilder(context) builder.add_minting_script(script=plutus_script, redeemer=pycardano.Redeemer(0)) - builder.mint = pycardano.MultiAsset.from_primitive({bytes(script_hash):{tn_bytes: amount}}) + builder.mint = pycardano.MultiAsset.from_primitive( + {bytes(script_hash): {tn_bytes: amount}} + ) builder.add_input(utxo_to_spend_or_burn) if amount > 0: # if not burning builder.add_output( pycardano.TransactionOutput( - payment_address, amount = pycardano.Value(coin=amount, multi_asset=builder.mint) + payment_address, + amount=pycardano.Value(coin=amount, multi_asset=builder.mint), ) ) - #builder.required_signers = [VerificationKey,] + # builder.required_signers = [VerificationKey,] # sign the transation signed_tx = builder.build_and_sign( - signing_keys = [issuer_signing_key,], - change_address = payment_address, + signing_keys=[ + issuer_signing_key, + ], + change_address=payment_address, auto_required_signers=True, ) - context.submit_tx(signed_tx) \ No newline at end of file + context.submit_tx(signed_tx) diff --git a/tests/test_mint.py b/tests/test_mint.py index 849a848..f363360 100644 --- a/tests/test_mint.py +++ b/tests/test_mint.py @@ -19,8 +19,14 @@ def test_mint_contract(): minting_user = MockUser(api) minting_user.fund(100_000_000) + mint_coin_with_contract( + "My_token", + 100, + minting_user.signing_key, + minting_user.verification_key, + context, + ) - mint_coin_with_contract('My_token', 100, minting_user.signing_key, minting_user.verification_key, context) def test_wrong_signature_mint_contract(): api = MockFrostApi() @@ -32,13 +38,14 @@ def test_wrong_signature_mint_contract(): pytest.raises( TransactionFailedException, mint_coin_with_contract, - 'My_token', + "My_token", 100, minting_user.signing_key, other_user.verification_key, - context + context, ) + if __name__ == "__main__": test_mint_contract() test_wrong_signature_mint_contract() diff --git a/tests/test_mint_mockfrost.py b/tests/test_mint_mockfrost.py index afc9dc2..b68eaab 100644 --- a/tests/test_mint_mockfrost.py +++ b/tests/test_mint_mockfrost.py @@ -39,8 +39,15 @@ def test_mint_contract(server): minting_user = MockFrostUser(session) minting_user.fund(100_000_000) - mint_coin_with_contract('My_token', 100, minting_user.signing_key, minting_user.verification_key, context) - + mint_coin_with_contract( + "My_token", + 100, + minting_user.signing_key, + minting_user.verification_key, + context, + ) + + def test_wrong_signature_mint_contract(server): client = MockFrostClient(base_url="http://127.0.0.1:8000") session = client.create_session() @@ -53,14 +60,15 @@ def test_wrong_signature_mint_contract(server): pytest.raises( TransactionFailedException, mint_coin_with_contract, - 'My_token', + "My_token", 100, minting_user.signing_key, other_user.verification_key, - context + context, ) + if __name__ == "__main__": test_mint_contract() - #test_spend_from_gift_contract() - #test_other_user_spend_from_gift_contract() + # test_spend_from_gift_contract() + # test_other_user_spend_from_gift_contract() From bff1b6dc073baab0101c1a9ec549d45ba5fcba12 Mon Sep 17 00:00:00 2001 From: SCM Date: Thu, 17 Oct 2024 20:47:39 +0100 Subject: [PATCH 2/2] bug fix --- plutus_bench/tx_tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plutus_bench/tx_tools.py b/plutus_bench/tx_tools.py index f23a865..99f4d7a 100644 --- a/plutus_bench/tx_tools.py +++ b/plutus_bench/tx_tools.py @@ -349,7 +349,7 @@ def generate_script_contexts_resolved( for r in tx.transaction_witness_set.redeemer if r.index == i and r.tag == RedeemerTag.MINT ), - tx.transaction_witness_set, + tx.transaction_witness_set.redeemer, ) except StopIteration: raise ValueError(