Skip to content

Commit 20626b7

Browse files
Merge pull request #2398 from valory-xyz/jmoreira-valory/predict-665-add-tests-for-pr-2394
Tests for gas multiplier
2 parents 0405263 + 73f133b commit 20626b7

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

autonomy/chain/tx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(
124124
self.retries = retries or DEFAULT_ON_CHAIN_INTERACT_RETRIES
125125
self.sleep = sleep or DEFAULT_ON_CHAIN_INTERACT_SLEEP
126126

127-
if gas_estimate_multiplier:
127+
if gas_estimate_multiplier is not None:
128128
if gas_estimate_multiplier > 2.5:
129129
logger.warning(f"{gas_estimate_multiplier=} is unusually high.")
130130
if gas_estimate_multiplier <= 0:

tests/test_autonomy/test_chain/test_tx.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,89 @@ def _update_with_gas_estimate_mock(tx: dict) -> dict:
605605

606606
settler.settle()
607607
assert settler.tx_receipt is not None
608+
609+
610+
def test_gas_estimate_multiplier() -> None:
611+
"""Test gas_estimate_multiplier parameter."""
612+
613+
def _tx_builder() -> dict:
614+
return {
615+
"from": "0x123",
616+
"to": "0x456",
617+
"value": 100,
618+
"gas": 100,
619+
}
620+
621+
# Test default multiplier (should be 1.0)
622+
settler = TxSettler(
623+
ledger_api=mock.Mock(
624+
try_get_gas_pricing=lambda **kwargs: {
625+
"maxFeePerGas": 100,
626+
"maxPriorityFeePerGas": 100,
627+
},
628+
update_with_gas_estimate=lambda tx: {**tx, "gas": 100},
629+
),
630+
crypto=mock.Mock(sign_transaction=lambda transaction: transaction),
631+
chain_type=ChainType.LOCAL,
632+
tx_builder=_tx_builder,
633+
)
634+
assert settler.gas_multiplier == 1.0
635+
636+
# Test custom multiplier
637+
settler_with_multiplier = TxSettler(
638+
ledger_api=mock.Mock(
639+
try_get_gas_pricing=lambda **kwargs: {
640+
"maxFeePerGas": 100,
641+
"maxPriorityFeePerGas": 100,
642+
},
643+
update_with_gas_estimate=lambda tx: {**tx, "gas": 100},
644+
),
645+
crypto=mock.Mock(sign_transaction=lambda transaction: transaction),
646+
chain_type=ChainType.LOCAL,
647+
tx_builder=_tx_builder,
648+
gas_estimate_multiplier=1.5,
649+
)
650+
assert settler_with_multiplier.gas_multiplier == 1.5
651+
652+
# Test that gas is multiplied correctly in transaction
653+
settler_with_multiplier.transact(dry_run=False)
654+
assert settler_with_multiplier.tx_dict is not None
655+
assert settler_with_multiplier.tx_dict["gas"] == 150 # 100 * 1.5
656+
657+
# Test invalid multiplier (must be positive)
658+
with pytest.raises(ValueError, match="gas_estimate_multiplier.*must be positive"):
659+
TxSettler(
660+
ledger_api=mock.Mock(),
661+
crypto=mock.Mock(),
662+
chain_type=ChainType.LOCAL,
663+
tx_builder=_tx_builder,
664+
gas_estimate_multiplier=0,
665+
)
666+
667+
with pytest.raises(ValueError, match="gas_estimate_multiplier.*must be positive"):
668+
TxSettler(
669+
ledger_api=mock.Mock(),
670+
crypto=mock.Mock(),
671+
chain_type=ChainType.LOCAL,
672+
tx_builder=_tx_builder,
673+
gas_estimate_multiplier=-1.0,
674+
)
675+
676+
677+
@mock.patch("autonomy.chain.tx.logger")
678+
def test_gas_estimate_multiplier_warning(logger: mock.Mock) -> None:
679+
"""Test that gas_estimate_multiplier warns when > 2.5."""
680+
681+
def _tx_builder() -> dict:
682+
return {"from": "0x123", "to": "0x456", "value": 100}
683+
684+
TxSettler(
685+
ledger_api=mock.Mock(),
686+
crypto=mock.Mock(),
687+
chain_type=ChainType.LOCAL,
688+
tx_builder=_tx_builder,
689+
gas_estimate_multiplier=3.0,
690+
)
691+
692+
logger.warning.assert_called_once() # type: ignore[attr-defined]
693+
assert "unusually high" in logger.warning.call_args[0][0] # type: ignore[attr-defined]

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ commands =
418418
skipsdist = True
419419
skip_install = True
420420
deps =
421-
tomte[safety]==0.4.0
421+
tomte[safety]==0.5.0
422422
typer<0.17.0 # TODO: Update to a version where this is fixed https://github.com/pyupio/safety/issues/784
423423
commands =
424424
safety check -i 37524 -i 38038 -i 37776 -i 38039 -i 39621 -i 40291 -i 39706 -i 41002 -i 51499 -i 67599 -i 70612 -i 82754 # remove 82754 after bumping tomte >0.4.0

0 commit comments

Comments
 (0)