Skip to content

Commit 131816e

Browse files
authored
Merge pull request #42 from astrojarred/tx-output-enforce-value
Store Transaction Output amounts as Values
2 parents 975fb90 + db091b4 commit 131816e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

integration-test/test/test_all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def test_plutus(self):
274274

275275
non_nft_utxo = None
276276
for utxo in self.chain_context.utxos(str(taker_address)):
277-
if isinstance(utxo.output.amount, int):
277+
# multi_asset should be empty for collateral utxo
278+
if not utxo.output.amount.multi_asset:
278279
non_nft_utxo = utxo
279280
break
280281

pycardano/transaction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ def __le__(self, other: Union[Value, int]):
248248
def __lt__(self, other: Union[Value, int]):
249249
return self <= other and self != other
250250

251+
def to_shallow_primitive(self):
252+
if self.multi_asset:
253+
return super().to_shallow_primitive()
254+
else:
255+
return self.coin
256+
251257

252258
@dataclass(repr=False)
253259
class TransactionOutput(ArrayCBORSerializable):
@@ -257,6 +263,10 @@ class TransactionOutput(ArrayCBORSerializable):
257263

258264
datum_hash: DatumHash = field(default=None, metadata={"optional": True})
259265

266+
def __post_init__(self):
267+
if isinstance(self.amount, int):
268+
self.amount = Value(self.amount)
269+
260270
def validate(self):
261271
if isinstance(self.amount, int) and self.amount < 0:
262272
raise InvalidDataException(

test/pycardano/test_txbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def test_not_enough_input_amount(chain_context):
420420

421421
# Make output amount equal to the input amount
422422
tx_builder.add_input(input_utxo).add_output(
423-
TransactionOutput.from_primitive([sender, input_utxo.output.amount])
423+
TransactionOutput(Address.from_primitive(sender), input_utxo.output.amount)
424424
)
425425

426426
with pytest.raises(UTxOSelectionException):

0 commit comments

Comments
 (0)