Skip to content

Commit f5e03cc

Browse files
committed
Add fake fee to fake transaction when fee is 0
1 parent 599acf3 commit f5e03cc

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

pycardano/txbuilder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
UTxO,
4242
Value,
4343
)
44-
from pycardano.utils import fee, min_lovelace, script_data_hash
44+
from pycardano.utils import fee, max_tx_fee, min_lovelace, script_data_hash
4545
from pycardano.witness import TransactionWitnessSet, VerificationKeyWitness
4646

4747
__all__ = ["TransactionBuilder"]
@@ -539,6 +539,12 @@ def _build_fake_witness_set(self) -> TransactionWitnessSet:
539539

540540
def _build_full_fake_tx(self) -> Transaction:
541541
tx_body = self._build_tx_body()
542+
543+
if tx_body.fee == 0:
544+
# When fee is not specified, we will use max possible fee to fill in the fee field.
545+
# This will make sure the size of fee field itself is taken into account during fee estimation.
546+
tx_body.fee = max_tx_fee(self.context)
547+
542548
witness = self._build_fake_witness_set()
543549
tx = Transaction(tx_body, witness, True, self.auxiliary_data)
544550
size = len(tx.to_cbor("bytes"))

test/pycardano/test_txbuilder.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
from test.pycardano.test_key import SK
33
from test.pycardano.util import chain_context
44

5-
import cbor2
65
import pytest
7-
from nacl.encoding import RawEncoder
8-
from nacl.hash import blake2b
96

107
from pycardano.address import Address
118
from pycardano.coinselection import RandomImproveMultiAsset
@@ -14,7 +11,6 @@
1411
InvalidTransactionException,
1512
UTxOSelectionException,
1613
)
17-
from pycardano.hash import SCRIPT_HASH_SIZE, ScriptHash
1814
from pycardano.key import VerificationKey
1915
from pycardano.nativescript import (
2016
InvalidBefore,
@@ -37,6 +33,7 @@
3733
Value,
3834
)
3935
from pycardano.txbuilder import TransactionBuilder
36+
from pycardano.utils import fee
4037
from pycardano.witness import VerificationKeyWitness
4138

4239

@@ -170,9 +167,8 @@ def test_tx_builder_raises_utxo_selection(chain_context):
170167

171168
with pytest.raises(UTxOSelectionException) as e:
172169
tx_body = tx_builder.build(change_address=sender_address)
173-
assert "Unfulfilled amount:" in e.value.args[0]
174-
# The unfulfilled amount includes requested (991000000) and estimated fees (161101)
175-
assert "'coin': 991161101" in e.value.args[0]
170+
# The unfulfilled amount includes requested (991000000) and estimated fees (161277)
171+
assert "Unfulfilled amount:\n {'coin': 991161277" in e.value.args[0]
176172
assert "{AssetName(b'NewToken'): 1}" in e.value.args[0]
177173

178174

@@ -609,15 +605,15 @@ def test_tx_builder_exact_fee_no_change(chain_context):
609605
TransactionOutput.from_primitive([sender, input_amount - tx_body.fee])
610606
)
611607

612-
tx_body = tx_builder.build()
608+
tx = tx_builder.build_and_sign([SK])
613609

614610
expected = {
615611
0: [[b"11111111111111111111111111111111", 3]],
616612
1: [
617-
# First output
618-
[sender_address.to_primitive(), 9836391],
613+
[sender_address.to_primitive(), 9836215],
619614
],
620-
2: 163609,
615+
2: 163785,
621616
}
622617

623-
assert expected == tx_body.to_primitive()
618+
assert expected == tx.transaction_body.to_primitive()
619+
assert tx.transaction_body.fee >= fee(chain_context, len(tx.to_cbor("bytes")))

0 commit comments

Comments
 (0)