diff --git a/pycardano/transaction.py b/pycardano/transaction.py index 6c060101..5d5c7c0e 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -107,7 +107,7 @@ def __add__(self, other: Asset) -> Asset: def __iadd__(self, other: Asset) -> Asset: new_item = self + other - self.update(new_item) + self.data = new_item.data return self.normalize() def __sub__(self, other: Asset) -> Asset: @@ -173,7 +173,7 @@ def __add__(self, other): def __iadd__(self, other): new_item = self + other - self.update(new_item) + self.data = new_item.data return self.normalize() def __sub__(self, other: MultiAsset) -> MultiAsset: diff --git a/pycardano/txbuilder.py b/pycardano/txbuilder.py index e03cc321..30e90597 100644 --- a/pycardano/txbuilder.py +++ b/pycardano/txbuilder.py @@ -547,8 +547,10 @@ def _calc_change( provided = Value() for i in inputs: provided += i.output.amount + if self.mint: provided.multi_asset += self.mint + if self.withdrawals: for v in self.withdrawals.values(): provided.coin += v diff --git a/pycardano/witness.py b/pycardano/witness.py index acceeb0f..e47ccf3b 100644 --- a/pycardano/witness.py +++ b/pycardano/witness.py @@ -8,17 +8,17 @@ from pycardano.key import ExtendedVerificationKey, VerificationKey from pycardano.nativescript import NativeScript from pycardano.plutus import ( + ExecutionUnits, PlutusV1Script, PlutusV2Script, PlutusV3Script, RawPlutusData, Redeemer, - Redeemers, - RedeemerMap, RedeemerKey, - RedeemerValue, - ExecutionUnits, + RedeemerMap, + Redeemers, RedeemerTag, + RedeemerValue, ) from pycardano.serialization import ( ArrayCBORSerializable, diff --git a/test/pycardano/test_txbuilder.py b/test/pycardano/test_txbuilder.py index cd05eff2..f3bb874a 100644 --- a/test/pycardano/test_txbuilder.py +++ b/test/pycardano/test_txbuilder.py @@ -1933,28 +1933,23 @@ def test_transaction_witness_set_no_redeemers(chain_context): assert witness_set.redeemer is None -def test_minting_and_burning_zero_quantity_assets(chain_context): +def test_burning_all_assets_under_single_policy(chain_context): """ - Test the minting and burning of multiple assets using the TransactionBuilder. + Test burning all assets under a single policy with TransactionBuilder. - This test ensures that assets are correctly minted and burned under the same policy ID. - Specifically, it verifies that after burning certain assets (AssetName1, AssetName2, and AssetName3), - they are removed from the multi-asset map, and the correct amount of the minted asset (AssetName4) remains. + This test ensures that burning multiple assets (AssetName1, AssetName2, AssetName3, AssetName4) + under policy_id_1 removes them from the multi-asset map. Steps: - 1. Define a policy ID and several assets (AssetName1, AssetName2, AssetName3, and AssetName4) using the AssetName class. - 2. Simulate minting of 2 units of AssetName4 and burning 1 unit each of AssetName1, AssetName2, and AssetName3. - 3. Add corresponding UTXOs for each asset as inputs. - 4. Add minting instructions to the TransactionBuilder. - 5. Build the transaction and verify that the burnt assets are removed from the multi-asset map. - 6. Check that the correct quantity of AssetName4 is minted and included in the transaction outputs. + 1. Define assets under policy_id_1 and simulate burning 1 unit of each. + 2. Add UTXOs for the assets and burning instructions. + 3. Build the transaction and verify that all burned assets are removed. Args: - chain_context: The blockchain context used for constructing and verifying the transaction. + chain_context: The blockchain context. Assertions: - - AssetName1, AssetName2, and AssetName3 are not present in the multi-asset map after burning. - - AssetName4 has exactly 2 units minted. + - AssetName1, AssetName2, AssetName3, and AssetName4 are removed after burning. """ tx_builder = TransactionBuilder(chain_context) @@ -1976,43 +1971,48 @@ def test_minting_and_burning_zero_quantity_assets(chain_context): ["d6cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 3] ) # Define a policy ID and asset names - policy_id = plutus_script_hash(PlutusV1Script(b"dummy script")) - multi_asset1 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName1": 1}}) - multi_asset2 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName2": 2}}) - multi_asset3 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName3": 1}}) - multi_asset4 = MultiAsset.from_primitive({policy_id.payload: {b"AssetName4": 3}}) + policy_id_1 = plutus_script_hash(PlutusV1Script(b"dummy script1")) + multi_asset1 = MultiAsset.from_primitive({policy_id_1.payload: {b"AssetName1": 1}}) + multi_asset2 = MultiAsset.from_primitive({policy_id_1.payload: {b"AssetName2": 1}}) + multi_asset3 = MultiAsset.from_primitive( + { + policy_id_1.payload: {b"AssetName3": 1}, + } + ) + multi_asset4 = MultiAsset.from_primitive( + { + policy_id_1.payload: {b"AssetName4": 1}, + } + ) # Simulate minting and burning of assets mint = MultiAsset.from_primitive( { - policy_id.payload: { + policy_id_1.payload: { b"AssetName1": -1, - b"AssetName2": -2, + b"AssetName2": -1, b"AssetName3": -1, - b"AssetName4": 2, + b"AssetName4": -1, } } ) # Set UTXO for the inputs utxo1 = UTxO( - tx_in1, TransactionOutput(Address(policy_id), Value(10000000, multi_asset1)) + tx_in1, TransactionOutput(Address(policy_id_1), Value(10000000, multi_asset1)) ) utxo2 = UTxO( - tx_in2, TransactionOutput(Address(policy_id), Value(10000000, multi_asset2)) + tx_in2, TransactionOutput(Address(policy_id_1), Value(10000000, multi_asset2)) ) utxo3 = UTxO( - tx_in3, TransactionOutput(Address(policy_id), Value(10000000, multi_asset3)) + tx_in3, TransactionOutput(Address(policy_id_1), Value(10000000, multi_asset3)) ) utxo4 = UTxO( - tx_in4, TransactionOutput(Address(policy_id), Value(10000000, multi_asset4)) + tx_in4, TransactionOutput(Address(policy_id_1), Value(10000000, multi_asset4)) ) # Add UTXO inputs - tx_builder.add_input(utxo1) - tx_builder.add_input(utxo2) - tx_builder.add_input(utxo3) - tx_builder.add_input(utxo4) + tx_builder.add_input(utxo1).add_input(utxo2).add_input(utxo3).add_input(utxo4) # Add the minting to the builder tx_builder.mint = mint @@ -2027,10 +2027,8 @@ def test_minting_and_burning_zero_quantity_assets(chain_context): for output in tx.outputs: multi_asset = output.amount.multi_asset - # Ensure that AssetName1, Node2, and Node3 were burnt (removed) - assert AssetName(b"AssetName1") not in multi_asset.get(policy_id, {}) - assert AssetName(b"AssetName2") not in multi_asset.get(policy_id, {}) - assert AssetName(b"AssetName3") not in multi_asset.get(policy_id, {}) - - # Ensure that AssetName4 has 5 units after minting - assert multi_asset.get(policy_id, {}).get(AssetName(b"AssetName4"), 0) == 5 + # Ensure that AssetName1, AssetName2, AssetName3 and AssetName4 were burnt (removed) + assert AssetName(b"AssetName1") not in multi_asset.get(policy_id_1, {}) + assert AssetName(b"AssetName2") not in multi_asset.get(policy_id_1, {}) + assert AssetName(b"AssetName3") not in multi_asset.get(policy_id_1, {}) + assert AssetName(b"AseetName4") not in multi_asset.get(policy_id_1, {})