Skip to content

Commit bf931bd

Browse files
committed
fix: linting
1 parent 0a8039b commit bf931bd

File tree

1 file changed

+66
-53
lines changed

1 file changed

+66
-53
lines changed

tests/unit/test_gas_estimation.py

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import pytest
21
from decimal import Decimal
32
from unittest.mock import MagicMock, patch
43

4+
import pytest
55
from aleph_message.models import Chain
6-
from web3.types import TxParams
76
from web3.exceptions import ContractCustomError
7+
from web3.types import TxParams
88

99
from aleph.sdk.chains.ethereum import ETHAccount
10-
from aleph.sdk.exceptions import InsufficientFundsError
11-
from aleph.sdk.evm_utils import MIN_ETH_BALANCE_WEI
1210
from aleph.sdk.connectors.superfluid import Superfluid
11+
from aleph.sdk.exceptions import InsufficientFundsError
1312
from aleph.sdk.types import TokenType
1413

1514

@@ -23,11 +22,13 @@ def mock_eth_account():
2322
account._provider = MagicMock()
2423
account._provider.eth = MagicMock()
2524
account._provider.eth.gas_price = 20_000_000_000 # 20 Gwei
26-
account._provider.eth.estimate_gas = MagicMock(return_value=100_000) # 100k gas units
27-
25+
account._provider.eth.estimate_gas = MagicMock(
26+
return_value=100_000
27+
) # 100k gas units
28+
2829
# Mock get_eth_balance to return a specific balance
2930
account.get_eth_balance = MagicMock(return_value=10**18) # 1 ETH
30-
31+
3132
return account
3233

3334

@@ -38,120 +39,132 @@ def mock_superfluid(mock_eth_account):
3839
superfluid.cfaV1Instance.create_flow = MagicMock()
3940
superfluid.super_token = "0xsupertokenaddress"
4041
superfluid.normalized_address = "0xsenderaddress"
41-
42+
4243
# Mock the operation
4344
operation = MagicMock()
4445
operation._get_populated_transaction_request = MagicMock(
4546
return_value={"value": 0, "gas": 100000, "gasPrice": 20_000_000_000}
4647
)
4748
superfluid.cfaV1Instance.create_flow.return_value = operation
48-
49+
4950
return superfluid
5051

5152

5253
class TestGasEstimation:
5354
def test_can_transact_with_sufficient_funds(self, mock_eth_account):
5455
tx = TxParams({"to": "0xreceiver", "value": 0})
55-
56+
5657
# Should pass with 1 ETH balance against ~0.002 ETH gas cost
5758
assert mock_eth_account.can_transact(tx=tx, block=True) is True
58-
59+
5960
def test_can_transact_with_insufficient_funds(self, mock_eth_account):
6061
tx = TxParams({"to": "0xreceiver", "value": 0})
61-
62+
6263
# Set balance to almost zero
6364
mock_eth_account.get_eth_balance = MagicMock(return_value=1000)
64-
65+
6566
# Should raise InsufficientFundsError
6667
with pytest.raises(InsufficientFundsError) as exc_info:
6768
mock_eth_account.can_transact(tx=tx, block=True)
68-
69+
6970
assert exc_info.value.token_type == TokenType.GAS
70-
71+
7172
def test_can_transact_with_legacy_gas_price(self, mock_eth_account):
72-
tx = TxParams({
73-
"to": "0xreceiver",
74-
"value": 0,
75-
"gasPrice": 30_000_000_000 # 30 Gwei
76-
})
77-
73+
tx = TxParams(
74+
{"to": "0xreceiver", "value": 0, "gasPrice": 30_000_000_000} # 30 Gwei
75+
)
76+
7877
# Should use the tx's gasPrice instead of default
7978
mock_eth_account.can_transact(tx=tx, block=True)
80-
79+
8180
# It should have used the tx's gasPrice for calculation
8281
mock_eth_account._provider.eth.estimate_gas.assert_called_once()
83-
82+
8483
def test_can_transact_with_eip1559_gas(self, mock_eth_account):
85-
tx = TxParams({
86-
"to": "0xreceiver",
87-
"value": 0,
88-
"maxFeePerGas": 40_000_000_000 # 40 Gwei
89-
})
90-
84+
tx = TxParams(
85+
{"to": "0xreceiver", "value": 0, "maxFeePerGas": 40_000_000_000} # 40 Gwei
86+
)
87+
9188
# Should use the tx's maxFeePerGas
9289
mock_eth_account.can_transact(tx=tx, block=True)
93-
90+
9491
# It should have used the tx's maxFeePerGas for calculation
9592
mock_eth_account._provider.eth.estimate_gas.assert_called_once()
96-
93+
9794
def test_can_transact_with_contract_error(self, mock_eth_account):
9895
tx = TxParams({"to": "0xreceiver", "value": 0})
99-
96+
10097
# Make estimate_gas throw a ContractCustomError
101-
mock_eth_account._provider.eth.estimate_gas.side_effect = ContractCustomError("error")
102-
98+
mock_eth_account._provider.eth.estimate_gas.side_effect = ContractCustomError(
99+
"error"
100+
)
101+
103102
# Should fallback to MIN_ETH_BALANCE_WEI
104103
mock_eth_account.can_transact(tx=tx, block=True)
105-
104+
106105
# It should have called estimate_gas
107106
mock_eth_account._provider.eth.estimate_gas.assert_called_once()
108107

109108

110109
class TestSuperfluidFlowEstimation:
111110
@pytest.mark.asyncio
112-
async def test_simulate_create_tx_flow_success(self, mock_superfluid, mock_eth_account):
111+
async def test_simulate_create_tx_flow_success(
112+
self, mock_superfluid, mock_eth_account
113+
):
113114
# Patch the can_transact method to simulate a successful transaction
114-
with patch.object(mock_eth_account, 'can_transact', return_value=True):
115+
with patch.object(mock_eth_account, "can_transact", return_value=True):
115116
result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005"))
116117
assert result is True
117-
118+
118119
# Verify the flow was correctly simulated but not executed
119120
mock_superfluid.cfaV1Instance.create_flow.assert_called_once()
120-
assert "0x0000000000000000000000000000000000000001" in str(mock_superfluid.cfaV1Instance.create_flow.call_args)
121-
121+
assert "0x0000000000000000000000000000000000000001" in str(
122+
mock_superfluid.cfaV1Instance.create_flow.call_args
123+
)
124+
122125
@pytest.mark.asyncio
123-
async def test_simulate_create_tx_flow_contract_error(self, mock_superfluid, mock_eth_account):
126+
async def test_simulate_create_tx_flow_contract_error(
127+
self, mock_superfluid, mock_eth_account
128+
):
124129
# Setup a contract error code for insufficient deposit
125130
error = ContractCustomError("Insufficient deposit")
126131
error.data = "0xea76c9b3" # This is the specific error code checked in the code
127-
132+
128133
# Mock can_transact to throw the error
129-
with patch.object(mock_eth_account, 'can_transact', side_effect=error):
134+
with patch.object(mock_eth_account, "can_transact", side_effect=error):
130135
# Also mock get_super_token_balance for the error case
131-
with patch.object(mock_eth_account, 'get_super_token_balance', return_value=0):
136+
with patch.object(
137+
mock_eth_account, "get_super_token_balance", return_value=0
138+
):
132139
# Should raise InsufficientFundsError for ALEPH token
133140
with pytest.raises(InsufficientFundsError) as exc_info:
134141
mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005"))
135-
142+
136143
assert exc_info.value.token_type == TokenType.ALEPH
137-
144+
138145
@pytest.mark.asyncio
139-
async def test_simulate_create_tx_flow_other_error(self, mock_superfluid, mock_eth_account):
146+
async def test_simulate_create_tx_flow_other_error(
147+
self, mock_superfluid, mock_eth_account
148+
):
140149
# Setup a different contract error code
141150
error = ContractCustomError("Other error")
142151
error.data = "0xsomeothercode"
143-
152+
144153
# Mock can_transact to throw the error
145-
with patch.object(mock_eth_account, 'can_transact', side_effect=error):
154+
with patch.object(mock_eth_account, "can_transact", side_effect=error):
146155
# Should return False for other errors
147156
result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005"))
148157
assert result is False
149-
158+
150159
@pytest.mark.asyncio
151160
async def test_can_start_flow_uses_simulation(self, mock_superfluid):
152161
# Mock _simulate_create_tx_flow to verify it's called
153-
with patch.object(mock_superfluid, '_simulate_create_tx_flow', return_value=True) as mock_simulate:
162+
with patch.object(
163+
mock_superfluid, "_simulate_create_tx_flow", return_value=True
164+
) as mock_simulate:
154165
result = mock_superfluid.can_start_flow(Decimal("0.00000005"))
155-
166+
156167
assert result is True
157-
mock_simulate.assert_called_once_with(flow=Decimal("0.00000005"), block=True)
168+
mock_simulate.assert_called_once_with(
169+
flow=Decimal("0.00000005"), block=True
170+
)

0 commit comments

Comments
 (0)