Skip to content

Commit 37f1b3b

Browse files
nielstroncffls
andauthored
Fix: negative minted amount is also requested (#264)
* Fix requesting negative amounts * Formatting * Fix in case of None mint * Do not add negative amounts to selected amount * Fix * Add test --------- Co-authored-by: Jerry <[email protected]>
1 parent 1e0f19b commit 37f1b3b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

pycardano/txbuilder.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,13 @@ def build(
934934
selected_amount += i.output.amount
935935

936936
if self.mint:
937-
selected_amount.multi_asset += self.mint
937+
# Add positive minted amounts to the selected amount (=source)
938+
for pid, m in self.mint.items():
939+
for tkn, am in m.items():
940+
if am > 0:
941+
selected_amount += Value(
942+
multi_asset=MultiAsset({pid: Asset({tkn: am})})
943+
)
938944

939945
if self.withdrawals:
940946
for v in self.withdrawals.values():
@@ -953,6 +959,15 @@ def build(
953959
for o in self.outputs:
954960
requested_amount += o.amount
955961

962+
if self.mint:
963+
# Add negative minted amounts to the requested amount (=sink)
964+
for pid, m in self.mint.items():
965+
for tkn, am in m.items():
966+
if am < 0:
967+
requested_amount += Value(
968+
multi_asset=MultiAsset({pid: Asset({tkn: -am})})
969+
)
970+
956971
# Include min fees associated as part of requested amount
957972
requested_amount += self._estimate_fee()
958973

test/pycardano/test_txbuilder.py

+40
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,46 @@ def test_tx_builder_mint_multi_asset(chain_context):
373373
assert expected == tx_body.to_primitive()
374374

375375

376+
def test_tx_builder_burn_multi_asset(chain_context):
377+
vk1 = VerificationKey.from_cbor(
378+
"58206443a101bdb948366fc87369336224595d36d8b0eee5602cba8b81a024e58473"
379+
)
380+
vk2 = VerificationKey.from_cbor(
381+
"58206443a101bdb948366fc87369336224595d36d8b0eee5602cba8b81a024e58475"
382+
)
383+
spk1 = ScriptPubkey(key_hash=vk1.hash())
384+
spk2 = ScriptPubkey(key_hash=vk2.hash())
385+
before = InvalidHereAfter(123456789)
386+
after = InvalidBefore(123456780)
387+
script = ScriptAll([before, after, spk1, ScriptAll([spk1, spk2])])
388+
policy_id = script.hash()
389+
390+
tx_builder = TransactionBuilder(chain_context)
391+
sender = "addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
392+
sender_address: Address = Address.from_primitive(sender)
393+
394+
# Add sender address as input
395+
to_burn = MultiAsset.from_primitive({policy_id.payload: {b"Token1": -1}})
396+
tx_input = TransactionInput.from_primitive([b"1" * 32, 123])
397+
tx_builder.potential_inputs.append(
398+
UTxO(
399+
tx_input,
400+
TransactionOutput.from_primitive(
401+
[sender, [2000000, {policy_id.payload: {b"Token1": 1}}]]
402+
),
403+
)
404+
)
405+
tx_builder.add_input_address(sender).add_output(
406+
TransactionOutput.from_primitive([sender, 3000000])
407+
).add_output(TransactionOutput.from_primitive([sender, 2000000]))
408+
409+
tx_builder.mint = to_burn
410+
411+
tx_body = tx_builder.build(change_address=sender_address)
412+
413+
assert tx_input in tx_body.inputs
414+
415+
376416
def test_tx_add_change_split_nfts(chain_context):
377417
# Set the max value size to be very small for testing purpose
378418
param = {"max_val_size": 50}

0 commit comments

Comments
 (0)