|
| 1 | +from decimal import Decimal |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from aleph_message.models import Chain |
| 6 | +from web3.exceptions import ContractCustomError |
| 7 | +from web3.types import TxParams |
| 8 | + |
| 9 | +from aleph.sdk.chains.ethereum import ETHAccount |
| 10 | +from aleph.sdk.connectors.superfluid import Superfluid |
| 11 | +from aleph.sdk.exceptions import InsufficientFundsError |
| 12 | +from aleph.sdk.types import TokenType |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def mock_eth_account(): |
| 17 | + private_key = b"\x01" * 32 |
| 18 | + account = ETHAccount( |
| 19 | + private_key, |
| 20 | + chain=Chain.ETH, |
| 21 | + ) |
| 22 | + account._provider = MagicMock() |
| 23 | + account._provider.eth = MagicMock() |
| 24 | + account._provider.eth.gas_price = 20_000_000_000 # 20 Gwei |
| 25 | + account._provider.eth.estimate_gas = MagicMock( |
| 26 | + return_value=100_000 |
| 27 | + ) # 100k gas units |
| 28 | + |
| 29 | + # Mock get_eth_balance to return a specific balance |
| 30 | + with patch.object(account, "get_eth_balance", return_value=10**18): # 1 ETH |
| 31 | + yield account |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def mock_superfluid(mock_eth_account): |
| 36 | + superfluid = Superfluid(mock_eth_account) |
| 37 | + superfluid.cfaV1Instance = MagicMock() |
| 38 | + superfluid.cfaV1Instance.create_flow = MagicMock() |
| 39 | + superfluid.super_token = "0xsupertokenaddress" |
| 40 | + superfluid.normalized_address = "0xsenderaddress" |
| 41 | + |
| 42 | + # Mock the operation |
| 43 | + operation = MagicMock() |
| 44 | + operation._get_populated_transaction_request = MagicMock( |
| 45 | + return_value={"value": 0, "gas": 100000, "gasPrice": 20_000_000_000} |
| 46 | + ) |
| 47 | + superfluid.cfaV1Instance.create_flow.return_value = operation |
| 48 | + |
| 49 | + return superfluid |
| 50 | + |
| 51 | + |
| 52 | +class TestGasEstimation: |
| 53 | + def test_can_transact_with_sufficient_funds(self, mock_eth_account): |
| 54 | + tx = TxParams({"to": "0xreceiver", "value": 0}) |
| 55 | + |
| 56 | + # Should pass with 1 ETH balance against ~0.002 ETH gas cost |
| 57 | + assert mock_eth_account.can_transact(tx=tx, block=True) is True |
| 58 | + |
| 59 | + def test_can_transact_with_insufficient_funds(self, mock_eth_account): |
| 60 | + tx = TxParams({"to": "0xreceiver", "value": 0}) |
| 61 | + |
| 62 | + # Set balance to almost zero |
| 63 | + with patch.object(mock_eth_account, "get_eth_balance", return_value=1000): |
| 64 | + # Should raise InsufficientFundsError |
| 65 | + with pytest.raises(InsufficientFundsError) as exc_info: |
| 66 | + mock_eth_account.can_transact(tx=tx, block=True) |
| 67 | + |
| 68 | + assert exc_info.value.token_type == TokenType.GAS |
| 69 | + |
| 70 | + def test_can_transact_with_legacy_gas_price(self, mock_eth_account): |
| 71 | + tx = TxParams( |
| 72 | + {"to": "0xreceiver", "value": 0, "gasPrice": 30_000_000_000} # 30 Gwei |
| 73 | + ) |
| 74 | + |
| 75 | + # Should use the tx's gasPrice instead of default |
| 76 | + mock_eth_account.can_transact(tx=tx, block=True) |
| 77 | + |
| 78 | + # It should have used the tx's gasPrice for calculation |
| 79 | + mock_eth_account._provider.eth.estimate_gas.assert_called_once() |
| 80 | + |
| 81 | + def test_can_transact_with_eip1559_gas(self, mock_eth_account): |
| 82 | + tx = TxParams( |
| 83 | + {"to": "0xreceiver", "value": 0, "maxFeePerGas": 40_000_000_000} # 40 Gwei |
| 84 | + ) |
| 85 | + |
| 86 | + # Should use the tx's maxFeePerGas |
| 87 | + mock_eth_account.can_transact(tx=tx, block=True) |
| 88 | + |
| 89 | + # It should have used the tx's maxFeePerGas for calculation |
| 90 | + mock_eth_account._provider.eth.estimate_gas.assert_called_once() |
| 91 | + |
| 92 | + def test_can_transact_with_contract_error(self, mock_eth_account): |
| 93 | + tx = TxParams({"to": "0xreceiver", "value": 0}) |
| 94 | + |
| 95 | + # Make estimate_gas throw a ContractCustomError |
| 96 | + mock_eth_account._provider.eth.estimate_gas.side_effect = ContractCustomError( |
| 97 | + "error" |
| 98 | + ) |
| 99 | + |
| 100 | + # Should fallback to MIN_ETH_BALANCE_WEI |
| 101 | + mock_eth_account.can_transact(tx=tx, block=True) |
| 102 | + |
| 103 | + # It should have called estimate_gas |
| 104 | + mock_eth_account._provider.eth.estimate_gas.assert_called_once() |
| 105 | + |
| 106 | + |
| 107 | +class TestSuperfluidFlowEstimation: |
| 108 | + @pytest.mark.asyncio |
| 109 | + async def test_simulate_create_tx_flow_success( |
| 110 | + self, mock_superfluid, mock_eth_account |
| 111 | + ): |
| 112 | + # Patch the can_transact method to simulate a successful transaction |
| 113 | + with patch.object(mock_eth_account, "can_transact", return_value=True): |
| 114 | + result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) |
| 115 | + assert result is True |
| 116 | + |
| 117 | + # Verify the flow was correctly simulated but not executed |
| 118 | + mock_superfluid.cfaV1Instance.create_flow.assert_called_once() |
| 119 | + assert "0x0000000000000000000000000000000000000001" in str( |
| 120 | + mock_superfluid.cfaV1Instance.create_flow.call_args |
| 121 | + ) |
| 122 | + |
| 123 | + @pytest.mark.asyncio |
| 124 | + async def test_simulate_create_tx_flow_contract_error( |
| 125 | + self, mock_superfluid, mock_eth_account |
| 126 | + ): |
| 127 | + # Setup a contract error code for insufficient deposit |
| 128 | + error = ContractCustomError("Insufficient deposit") |
| 129 | + error.data = "0xea76c9b3" # This is the specific error code checked in the code |
| 130 | + |
| 131 | + # Mock can_transact to throw the error |
| 132 | + with patch.object(mock_eth_account, "can_transact", side_effect=error): |
| 133 | + # Also mock get_super_token_balance for the error case |
| 134 | + with patch.object( |
| 135 | + mock_eth_account, "get_super_token_balance", return_value=0 |
| 136 | + ): |
| 137 | + # Should raise InsufficientFundsError for ALEPH token |
| 138 | + with pytest.raises(InsufficientFundsError) as exc_info: |
| 139 | + mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) |
| 140 | + |
| 141 | + assert exc_info.value.token_type == TokenType.ALEPH |
| 142 | + |
| 143 | + @pytest.mark.asyncio |
| 144 | + async def test_simulate_create_tx_flow_other_error( |
| 145 | + self, mock_superfluid, mock_eth_account |
| 146 | + ): |
| 147 | + # Setup a different contract error code |
| 148 | + error = ContractCustomError("Other error") |
| 149 | + error.data = "0xsomeothercode" |
| 150 | + |
| 151 | + # Mock can_transact to throw the error |
| 152 | + with patch.object(mock_eth_account, "can_transact", side_effect=error): |
| 153 | + # Should return False for other errors |
| 154 | + result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) |
| 155 | + assert result is False |
| 156 | + |
| 157 | + @pytest.mark.asyncio |
| 158 | + async def test_can_start_flow_uses_simulation(self, mock_superfluid): |
| 159 | + # Mock _simulate_create_tx_flow to verify it's called |
| 160 | + with patch.object( |
| 161 | + mock_superfluid, "_simulate_create_tx_flow", return_value=True |
| 162 | + ) as mock_simulate: |
| 163 | + result = mock_superfluid.can_start_flow(Decimal("0.00000005")) |
| 164 | + |
| 165 | + assert result is True |
| 166 | + mock_simulate.assert_called_once_with( |
| 167 | + flow=Decimal("0.00000005"), block=True |
| 168 | + ) |
0 commit comments