Skip to content
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

New deposit strategy #260

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/blockchain/deposit_strategy/gas_price_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class GasPriceCalculator:
_BLOCKS_IN_ONE_DAY = 24 * 60 * 60 // 12
_REQUEST_SIZE = 1024
_PER_KEY_PRICE_ASSUMPTION = Web3.to_wei(100, 'gwei')

def __init__(self, w3: Web3):
self.w3 = w3
Expand All @@ -28,8 +29,8 @@ def is_gas_price_ok(self, module_id: int) -> bool:
current_gas_fee = self._get_pending_base_fee()
GAS_FEE.labels('current_fee', module_id).set(current_gas_fee)

current_buffered_ether = self.w3.lido.lido.get_depositable_ether()
if current_buffered_ether > variables.MAX_BUFFERED_ETHERS:
depositable_ether = self.w3.lido.lido.get_depositable_ether()
if depositable_ether > variables.MAX_BUFFERED_ETHERS:
success = current_gas_fee <= variables.MAX_GAS_FEE
else:
recommended_gas_fee = self._get_recommended_gas_fee()
Expand All @@ -45,6 +46,9 @@ def _get_pending_base_fee(self) -> Wei:
return base_fee_per_gas

def calculate_deposit_recommendation(self, deposit_strategy: BaseDepositStrategy, module_id: int) -> bool:
is_gas_ok = self.is_gas_price_ok(module_id)
logger.info({'msg': 'Calculate gas recommendations.', 'value': is_gas_ok})

possible_keys = deposit_strategy.deposited_keys_amount(module_id)
success = False
if possible_keys < deposit_strategy.DEPOSITABLE_KEYS_THRESHOLD:
Expand All @@ -62,15 +66,20 @@ def calculate_deposit_recommendation(self, deposit_strategy: BaseDepositStrategy
)
base_fee_per_gas = self._get_pending_base_fee()
success = recommended_max_gas >= base_fee_per_gas
if not is_gas_ok or not success:
gas_price_diff = self._get_pending_base_fee() - self._get_recommended_gas_fee()
aprx_waiting_time = gas_price_diff * percentail_in_days # noqa
possible_income = aprx_waiting_time * self.w3.lido.lido.get_depositable_ether() * 0.03
success = is_gas_ok = possible_income >= gas_price_diff + recommended_max_gas
DEPOSIT_AMOUNT_OK.labels(module_id).set(int(success))
return success
return is_gas_ok and success

@staticmethod
def _calculate_recommended_gas_based_on_deposit_amount(deposits_amount: int, module_id: int) -> int:
# For one key recommended gas fee will be around 10
# For 10 keys around 100 gwei. For 20 keys ~ 800 gwei
# ToDo percentiles for all modules?
recommended_max_gas = (deposits_amount ** 3 + 100) * 10 ** 8
recommended_max_gas = (deposits_amount**3 + 100) * 10**8
logger.info({'msg': 'Calculate recommended max gas based on possible deposits.', 'value': recommended_max_gas})
GAS_FEE.labels('based_on_buffer_fee', module_id).set(recommended_max_gas)
return recommended_max_gas
Expand Down
4 changes: 2 additions & 2 deletions src/blockchain/web3_extentions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def send(
variables.MAX_PRIORITY_FEE,
)

gas_limit = self._estimate_gas(transaction, variables.ACCOUNT.address)
gas_limit = self.estimate_gas(transaction, variables.ACCOUNT.address)

transaction_dict = transaction.build_transaction(
TxParams(
Expand Down Expand Up @@ -88,7 +88,7 @@ def send(
return status

@staticmethod
def _estimate_gas(transaction: ContractFunction, account_address: ChecksumAddress) -> int:
def estimate_gas(transaction: ContractFunction, account_address: ChecksumAddress) -> int:
try:
gas = transaction.estimate_gas({'from': account_address})
except ContractLogicError as error:
Expand Down
10 changes: 2 additions & 8 deletions src/bots/depositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ def execute(self, block: BlockData) -> bool:

return True

def _is_mellow_depositable(
self,
module_id: int
) -> bool:
def _is_mellow_depositable(self, module_id: int) -> bool:
if not variables.MELLOW_CONTRACT_ADDRESS:
return False
try:
Expand Down Expand Up @@ -186,9 +183,6 @@ def _deposit_to_module(self, module_id: int) -> bool:
can_deposit = self.w3.lido.deposit_security_module.can_deposit(module_id)
logger.info({'msg': 'Can deposit to module.', 'value': can_deposit})

gas_is_ok = self._gas_price_calculator.is_gas_price_ok(module_id)
logger.info({'msg': 'Calculate gas recommendations.', 'value': gas_is_ok})

strategy, is_mellow = self._select_strategy(module_id)
is_deposit_amount_ok = self._gas_price_calculator.calculate_deposit_recommendation(strategy, module_id)
logger.info({'msg': 'Calculations deposit recommendations.', 'value': is_deposit_amount_ok, 'is_mellow': is_mellow})
Expand All @@ -199,7 +193,7 @@ def _deposit_to_module(self, module_id: int) -> bool:
is_deposit_amount_ok = self._gas_price_calculator.calculate_deposit_recommendation(strategy, module_id)
logger.info({'msg': 'Calculations deposit recommendations.', 'value': is_deposit_amount_ok, 'is_mellow': is_mellow})

if is_depositable and quorum and can_deposit and gas_is_ok and is_deposit_amount_ok:
if is_depositable and quorum and can_deposit and is_deposit_amount_ok:
logger.info({'msg': 'Checks passed. Prepare deposit tx.', 'is_mellow': is_mellow})
success = self.prepare_and_send_tx(module_id, quorum, is_mellow)
if not success and is_mellow:
Expand Down
6 changes: 3 additions & 3 deletions tests/blockchain/web3_extentions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def test_estimate_gas(web3_lido_unit, set_account):
tx = Transaction()

tx.estimate_gas = Mock(return_value=variables.CONTRACT_GAS_LIMIT * 2)
gas_amount = web3_lido_unit.transaction._estimate_gas(tx, variables.ACCOUNT.address)
gas_amount = web3_lido_unit.transaction.estimate_gas(tx, variables.ACCOUNT.address)
assert gas_amount == variables.CONTRACT_GAS_LIMIT

tx.estimate_gas = Mock(return_value=100)
gas_amount = web3_lido_unit.transaction._estimate_gas(tx, variables.ACCOUNT.address)
gas_amount = web3_lido_unit.transaction.estimate_gas(tx, variables.ACCOUNT.address)
assert gas_amount == 130

tx.estimate_gas = Mock(side_effect=ContractLogicError())
gas_amount = web3_lido_unit.transaction._estimate_gas(tx, variables.ACCOUNT.address)
gas_amount = web3_lido_unit.transaction.estimate_gas(tx, variables.ACCOUNT.address)
assert gas_amount == variables.CONTRACT_GAS_LIMIT
Loading