Skip to content

Commit 0288497

Browse files
committedMar 13, 2022
[Bug fix] Fail transaction builder when there is not enough input amount to pay for fees
1 parent f75a093 commit 0288497

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎pycardano/txbuilder.py

+8
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def _calc_change(self, fees, inputs, outputs, address) -> List[TransactionOutput
161161
if self.mint:
162162
provided.multi_asset += self.mint
163163

164+
if not requested < provided:
165+
raise InvalidTransactionException(
166+
f"The input UTxOs cannot cover the transaction outputs and tx fee. \n"
167+
f"Inputs: {inputs} \n"
168+
f"Outputs: {outputs} \n"
169+
f"fee: {fees}"
170+
)
171+
164172
change = provided - requested
165173

166174
# Remove any asset that has 0 quantity

‎test/pycardano/test_txbuilder.py

+16
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,19 @@ def test_tx_builder_mint_multi_asset(chain_context):
198198
}
199199

200200
assert expected == tx_body.to_primitive()
201+
202+
203+
def test_not_enough_input_amount(chain_context):
204+
tx_builder = TransactionBuilder(chain_context)
205+
sender = "addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
206+
sender_address = Address.from_primitive(sender)
207+
input_utxo = chain_context.utxos(sender)[0]
208+
209+
# Make output amount equal to the input amount
210+
tx_builder.add_input(input_utxo).add_output(
211+
TransactionOutput.from_primitive([sender, input_utxo.output.amount])
212+
)
213+
214+
with pytest.raises(InvalidTransactionException):
215+
# Tx builder must fail here because there is not enough amount of input ADA to pay tx fee
216+
tx_body = tx_builder.build(change_address=sender_address)

0 commit comments

Comments
 (0)