Skip to content

Commit e3f221c

Browse files
1yamnesitor
authored andcommitted
Fix: gas estimations + error handling for gas / Aleph token
1 parent 80b8bb0 commit e3f221c

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/aleph/sdk/chains/ethereum.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from eth_keys.exceptions import BadSignature as EthBadSignatureError
1212
from superfluid import Web3FlowInfo
1313
from web3 import Web3
14+
from web3.exceptions import ContractCustomError
1415
from web3.middleware import ExtraDataToPOAMiddleware
1516
from web3.types import TxParams, TxReceipt
1617

@@ -119,22 +120,33 @@ def switch_chain(self, chain: Optional[Chain] = None):
119120
self.connect_chain(chain=chain)
120121

121122
def can_transact(self, tx: TxParams, block=True) -> bool:
122-
balance = self.get_eth_balance()
123+
balance_wei = self.get_eth_balance()
123124
try:
124125
assert self._provider is not None
125126

126127
estimated_gas = self._provider.eth.estimate_gas(tx)
127-
except Exception:
128-
estimated_gas = (
129-
MIN_ETH_BALANCE_WEI # Fallback to MIN_ETH_BALANCE if estimation fails
130-
)
131128

132-
valid = balance > estimated_gas if self.chain else False
129+
gas_price = tx.get("gasPrice", self._provider.eth.gas_price)
130+
131+
if "maxFeePerGas" in tx:
132+
max_fee = tx["maxFeePerGas"]
133+
total_fee_wei = estimated_gas * max_fee
134+
else:
135+
total_fee_wei = estimated_gas * gas_price
136+
137+
total_fee_wei = int(total_fee_wei * 1.2)
138+
139+
except ContractCustomError:
140+
total_fee_wei = MIN_ETH_BALANCE_WEI # Fallback if estimation fails
141+
142+
required_fee_wei = total_fee_wei + (tx.get("value", 0))
143+
144+
valid = balance_wei > required_fee_wei if self.chain else False
133145
if not valid and block:
134146
raise InsufficientFundsError(
135147
token_type=TokenType.GAS,
136-
required_funds=estimated_gas,
137-
available_funds=float(from_wei_token(balance)),
148+
required_funds=float(from_wei_token(required_fee_wei)),
149+
available_funds=float(from_wei_token(balance_wei)),
138150
)
139151
return valid
140152

src/aleph/sdk/connectors/superfluid.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from eth_utils import to_normalized_address
77
from superfluid import CFA_V1, Operation, Web3FlowInfo
8+
from web3.exceptions import ContractCustomError
89

910
from aleph.sdk.evm_utils import (
1011
FlowUpdate,
@@ -52,7 +53,15 @@ def _simulate_create_tx_flow(self, flow: Decimal, block=True) -> bool:
5253
self.account.rpc, self.account._account.key
5354
)
5455
return self.account.can_transact(tx=populated_transaction, block=block)
55-
except Exception:
56+
except ContractCustomError as e:
57+
if getattr(e, "data", None) == "0xea76c9b3":
58+
balance = self.account.get_super_token_balance()
59+
MIN_FLOW_4H = to_wei_token(flow) * Decimal(self.MIN_4_HOURS)
60+
raise InsufficientFundsError(
61+
token_type=TokenType.ALEPH,
62+
required_funds=float(from_wei_token(MIN_FLOW_4H)),
63+
available_funds=float(from_wei_token(balance)),
64+
)
5665
return False
5766

5867
async def _execute_operation_with_account(self, operation: Operation) -> str:
@@ -70,18 +79,8 @@ async def _execute_operation_with_account(self, operation: Operation) -> str:
7079

7180
def can_start_flow(self, flow: Decimal, block=True) -> bool:
7281
"""Check if the account has enough funds to start a Superfluid flow of the given size."""
73-
valid = False
74-
if self._simulate_create_tx_flow(flow=flow, block=block):
75-
balance = self.account.get_super_token_balance()
76-
MIN_FLOW_4H = to_wei_token(flow) * Decimal(self.MIN_4_HOURS)
77-
valid = balance > MIN_FLOW_4H
78-
if not valid and block:
79-
raise InsufficientFundsError(
80-
token_type=TokenType.ALEPH,
81-
required_funds=float(from_wei_token(MIN_FLOW_4H)),
82-
available_funds=float(from_wei_token(balance)),
83-
)
84-
return valid
82+
return self._simulate_create_tx_flow(flow=flow, block=block)
83+
8584

8685
async def create_flow(self, receiver: str, flow: Decimal) -> str:
8786
"""Create a Superfluid flow between two addresses."""

0 commit comments

Comments
 (0)