Skip to content

Commit 733f52f

Browse files
xxAVOGADROxxnielstron
authored andcommitted
Added unit test
1 parent 7727c72 commit 733f52f

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

test/pycardano/test_txbuilder.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from pycardano import RedeemerKey, RedeemerMap, RedeemerValue
11+
from pycardano import AssetName, RedeemerKey, RedeemerMap, RedeemerValue
1212
from pycardano.address import Address
1313
from pycardano.certificate import (
1414
PoolRegistration,
@@ -1931,3 +1931,106 @@ def test_transaction_witness_set_no_redeemers(chain_context):
19311931
tx_builder = TransactionBuilder(chain_context)
19321932
witness_set = tx_builder.build_witness_set()
19331933
assert witness_set.redeemer is None
1934+
1935+
1936+
def test_minting_and_burning_zero_quantity_assets(chain_context):
1937+
"""
1938+
Test the minting and burning of multiple assets using the TransactionBuilder.
1939+
1940+
This test ensures that assets are correctly minted and burned under the same policy ID.
1941+
Specifically, it verifies that after burning certain assets (AssetName1, AssetName2, and AssetName3),
1942+
they are removed from the multi-asset map, and the correct amount of the minted asset (AssetName4) remains.
1943+
1944+
Steps:
1945+
1. Define a policy ID and several assets (AssetName1, AssetName2, AssetName3, and AssetName4) using the AssetName class.
1946+
2. Simulate minting of 2 units of AssetName4 and burning 1 unit each of AssetName1, AssetName2, and AssetName3.
1947+
3. Add corresponding UTXOs for each asset as inputs.
1948+
4. Add minting instructions to the TransactionBuilder.
1949+
5. Build the transaction and verify that the burnt assets are removed from the multi-asset map.
1950+
6. Check that the correct quantity of AssetName4 is minted and included in the transaction outputs.
1951+
1952+
Args:
1953+
chain_context: The blockchain context used for constructing and verifying the transaction.
1954+
1955+
Assertions:
1956+
- AssetName1, AssetName2, and AssetName3 are not present in the multi-asset map after burning.
1957+
- AssetName4 has exactly 2 units minted.
1958+
"""
1959+
tx_builder = TransactionBuilder(chain_context)
1960+
1961+
# Create change address
1962+
sender = "addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
1963+
sender_address = Address.from_primitive(sender)
1964+
1965+
# Create four transaction inputs
1966+
tx_in1 = TransactionInput.from_primitive(
1967+
["a6cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 0]
1968+
)
1969+
tx_in2 = TransactionInput.from_primitive(
1970+
["b6cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 1]
1971+
)
1972+
tx_in3 = TransactionInput.from_primitive(
1973+
["c6cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 2]
1974+
)
1975+
tx_in4 = TransactionInput.from_primitive(
1976+
["d6cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 3]
1977+
)
1978+
# Define a policy ID and asset names
1979+
policy_id = plutus_script_hash(PlutusV1Script(b"dummy script"))
1980+
multi_asset1 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName1": 1}})
1981+
multi_asset2 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName2": 2}})
1982+
multi_asset3 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName3": 1}})
1983+
multi_asset4 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName4": 3}})
1984+
1985+
# Simulate minting and burning of assets
1986+
mint = MultiAsset.from_primitive(
1987+
{
1988+
policy_id.payload: {
1989+
b"AssetName1": -1,
1990+
b"AssetName2": -2,
1991+
b"AssetName3": -1,
1992+
b"AssetName4": 2,
1993+
}
1994+
}
1995+
)
1996+
1997+
# Set UTXO for the inputs
1998+
utxo1 = UTxO(
1999+
tx_in1, TransactionOutput(Address(policy_id), Value(10000000, multi_asset1))
2000+
)
2001+
utxo2 = UTxO(
2002+
tx_in2, TransactionOutput(Address(policy_id), Value(10000000, multi_asset2))
2003+
)
2004+
utxo3 = UTxO(
2005+
tx_in3, TransactionOutput(Address(policy_id), Value(10000000, multi_asset3))
2006+
)
2007+
utxo4 = UTxO(
2008+
tx_in4, TransactionOutput(Address(policy_id), Value(10000000, multi_asset4))
2009+
)
2010+
2011+
# Add UTXO inputs
2012+
tx_builder.add_input(utxo1)
2013+
tx_builder.add_input(utxo2)
2014+
tx_builder.add_input(utxo3)
2015+
tx_builder.add_input(utxo4)
2016+
2017+
# Add the minting to the builder
2018+
tx_builder.mint = mint
2019+
2020+
# Build the transaction
2021+
tx = tx_builder.build(change_address=sender_address)
2022+
2023+
# Check that the transaction has outputs
2024+
assert tx.outputs
2025+
2026+
# Loop through the transaction outputs to verify the multi-asset quantities
2027+
for output in tx.outputs:
2028+
multi_asset = output.amount.multi_asset
2029+
2030+
# Ensure that AssetName1, Node2, and Node3 were burnt (removed)
2031+
assert AssetName(b"AssetName1") not in multi_asset.get(policy_id, {})
2032+
assert AssetName(b"AssetName2") not in multi_asset.get(policy_id, {})
2033+
assert AssetName(b"AssetName3") not in multi_asset.get(policy_id, {})
2034+
2035+
# Ensure that AssetName4 has 5 units after minting
2036+
assert multi_asset.get(policy_id, {}).get(AssetName(b"AssetName4"), 0) == 5

0 commit comments

Comments
 (0)