-
Notifications
You must be signed in to change notification settings - Fork 5
Feat: estimated gas based on tx #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1yam
wants to merge
12
commits into
main
Choose a base branch
from
1yam-fix-estimated-gas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
85dcf51
fix: use estimate_gas in can_transact for accurate limit checks
1yam f804f99
feat: implement _simulate_create_tx_flow to estimate gas for specific…
1yam 5c4c543
feat: add can_transact check in _execute_operation_with_account to pr…
1yam f40392c
fix: remove unnecessary can_start_flow check in create_flow
1yam 4ea9924
fix: should use MIN_ETH_BALANCE_WEI instead of MIN_ETH_BALANCE
1yam 59c8c02
fix: ensure _provider exist while using can_transact
1yam ee33e52
fix: return false if error got returned while trying to estimate the …
1yam d8beeeb
Fix: gas estimations + error handling for gas / Aleph token
1yam 4380c74
fix: linting error `hatch`
1yam c5a3562
Feature: gas estimations unit test
1yam 7845ad3
fix: linting
1yam 2865628
fix: mypy cannot assign method
1yam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
from decimal import Decimal | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from aleph_message.models import Chain | ||
from web3.exceptions import ContractCustomError | ||
from web3.types import TxParams | ||
|
||
from aleph.sdk.chains.ethereum import ETHAccount | ||
from aleph.sdk.connectors.superfluid import Superfluid | ||
from aleph.sdk.exceptions import InsufficientFundsError | ||
from aleph.sdk.types import TokenType | ||
|
||
|
||
@pytest.fixture | ||
def mock_eth_account(): | ||
private_key = b"\x01" * 32 | ||
account = ETHAccount( | ||
private_key, | ||
chain=Chain.ETH, | ||
) | ||
account._provider = MagicMock() | ||
account._provider.eth = MagicMock() | ||
account._provider.eth.gas_price = 20_000_000_000 # 20 Gwei | ||
account._provider.eth.estimate_gas = MagicMock( | ||
return_value=100_000 | ||
) # 100k gas units | ||
|
||
# Mock get_eth_balance to return a specific balance | ||
with patch.object(account, "get_eth_balance", return_value=10**18): # 1 ETH | ||
yield account | ||
|
||
|
||
@pytest.fixture | ||
def mock_superfluid(mock_eth_account): | ||
superfluid = Superfluid(mock_eth_account) | ||
superfluid.cfaV1Instance = MagicMock() | ||
superfluid.cfaV1Instance.create_flow = MagicMock() | ||
superfluid.super_token = "0xsupertokenaddress" | ||
superfluid.normalized_address = "0xsenderaddress" | ||
|
||
# Mock the operation | ||
operation = MagicMock() | ||
operation._get_populated_transaction_request = MagicMock( | ||
return_value={"value": 0, "gas": 100000, "gasPrice": 20_000_000_000} | ||
) | ||
superfluid.cfaV1Instance.create_flow.return_value = operation | ||
|
||
return superfluid | ||
|
||
|
||
class TestGasEstimation: | ||
def test_can_transact_with_sufficient_funds(self, mock_eth_account): | ||
tx = TxParams({"to": "0xreceiver", "value": 0}) | ||
|
||
# Should pass with 1 ETH balance against ~0.002 ETH gas cost | ||
assert mock_eth_account.can_transact(tx=tx, block=True) is True | ||
|
||
def test_can_transact_with_insufficient_funds(self, mock_eth_account): | ||
tx = TxParams({"to": "0xreceiver", "value": 0}) | ||
|
||
# Set balance to almost zero | ||
with patch.object(mock_eth_account, "get_eth_balance", return_value=1000): | ||
# Should raise InsufficientFundsError | ||
with pytest.raises(InsufficientFundsError) as exc_info: | ||
mock_eth_account.can_transact(tx=tx, block=True) | ||
|
||
assert exc_info.value.token_type == TokenType.GAS | ||
|
||
def test_can_transact_with_legacy_gas_price(self, mock_eth_account): | ||
tx = TxParams( | ||
{"to": "0xreceiver", "value": 0, "gasPrice": 30_000_000_000} # 30 Gwei | ||
) | ||
|
||
# Should use the tx's gasPrice instead of default | ||
mock_eth_account.can_transact(tx=tx, block=True) | ||
|
||
# It should have used the tx's gasPrice for calculation | ||
mock_eth_account._provider.eth.estimate_gas.assert_called_once() | ||
|
||
def test_can_transact_with_eip1559_gas(self, mock_eth_account): | ||
tx = TxParams( | ||
{"to": "0xreceiver", "value": 0, "maxFeePerGas": 40_000_000_000} # 40 Gwei | ||
) | ||
|
||
# Should use the tx's maxFeePerGas | ||
mock_eth_account.can_transact(tx=tx, block=True) | ||
|
||
# It should have used the tx's maxFeePerGas for calculation | ||
mock_eth_account._provider.eth.estimate_gas.assert_called_once() | ||
|
||
def test_can_transact_with_contract_error(self, mock_eth_account): | ||
tx = TxParams({"to": "0xreceiver", "value": 0}) | ||
|
||
# Make estimate_gas throw a ContractCustomError | ||
mock_eth_account._provider.eth.estimate_gas.side_effect = ContractCustomError( | ||
"error" | ||
) | ||
|
||
# Should fallback to MIN_ETH_BALANCE_WEI | ||
mock_eth_account.can_transact(tx=tx, block=True) | ||
|
||
# It should have called estimate_gas | ||
mock_eth_account._provider.eth.estimate_gas.assert_called_once() | ||
|
||
|
||
class TestSuperfluidFlowEstimation: | ||
@pytest.mark.asyncio | ||
async def test_simulate_create_tx_flow_success( | ||
self, mock_superfluid, mock_eth_account | ||
): | ||
# Patch the can_transact method to simulate a successful transaction | ||
with patch.object(mock_eth_account, "can_transact", return_value=True): | ||
result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) | ||
assert result is True | ||
|
||
# Verify the flow was correctly simulated but not executed | ||
mock_superfluid.cfaV1Instance.create_flow.assert_called_once() | ||
assert "0x0000000000000000000000000000000000000001" in str( | ||
mock_superfluid.cfaV1Instance.create_flow.call_args | ||
) | ||
|
||
@pytest.mark.asyncio | ||
async def test_simulate_create_tx_flow_contract_error( | ||
self, mock_superfluid, mock_eth_account | ||
): | ||
# Setup a contract error code for insufficient deposit | ||
error = ContractCustomError("Insufficient deposit") | ||
error.data = "0xea76c9b3" # This is the specific error code checked in the code | ||
|
||
# Mock can_transact to throw the error | ||
with patch.object(mock_eth_account, "can_transact", side_effect=error): | ||
# Also mock get_super_token_balance for the error case | ||
with patch.object( | ||
mock_eth_account, "get_super_token_balance", return_value=0 | ||
): | ||
# Should raise InsufficientFundsError for ALEPH token | ||
with pytest.raises(InsufficientFundsError) as exc_info: | ||
mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) | ||
|
||
assert exc_info.value.token_type == TokenType.ALEPH | ||
|
||
@pytest.mark.asyncio | ||
async def test_simulate_create_tx_flow_other_error( | ||
self, mock_superfluid, mock_eth_account | ||
): | ||
# Setup a different contract error code | ||
error = ContractCustomError("Other error") | ||
error.data = "0xsomeothercode" | ||
|
||
# Mock can_transact to throw the error | ||
with patch.object(mock_eth_account, "can_transact", side_effect=error): | ||
# Should return False for other errors | ||
result = mock_superfluid._simulate_create_tx_flow(Decimal("0.00000005")) | ||
assert result is False | ||
|
||
@pytest.mark.asyncio | ||
async def test_can_start_flow_uses_simulation(self, mock_superfluid): | ||
# Mock _simulate_create_tx_flow to verify it's called | ||
with patch.object( | ||
mock_superfluid, "_simulate_create_tx_flow", return_value=True | ||
) as mock_simulate: | ||
result = mock_superfluid.can_start_flow(Decimal("0.00000005")) | ||
|
||
assert result is True | ||
mock_simulate.assert_called_once_with( | ||
flow=Decimal("0.00000005"), block=True | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.