diff --git a/scripts/depositor.py b/scripts/depositor.py index 418eec6b..c9099153 100644 --- a/scripts/depositor.py +++ b/scripts/depositor.py @@ -14,6 +14,8 @@ def main(): logger.info({'msg': 'Start up metrics service on port: 8080.'}) start_http_server(8080) + flashbot(web3, web3.eth.account.from_key(variables.FLASHBOT_SIGNATURE), FLASHBOTS_RPC[variables.WEB3_CHAIN_ID]) + depositor_bot = DepositorBot() depositor_bot.run_as_daemon() diff --git a/scripts/depositor_utils/depositor_bot.py b/scripts/depositor_utils/depositor_bot.py index 600f352d..bdadda86 100644 --- a/scripts/depositor_utils/depositor_bot.py +++ b/scripts/depositor_utils/depositor_bot.py @@ -4,6 +4,7 @@ from typing import List, Tuple from brownie import web3, Wei, chain +from eth_account import Account from hexbytes import HexBytes from web3.exceptions import BlockNotFound from web3.types import TxParams @@ -256,11 +257,12 @@ def do_deposit(self): logger.info({'msg': 'Creating tx in blockchain.'}) + contract = web3.eth.contract( + address=DepositSecurityModuleInterface.address, + abi=DepositSecurityModuleInterface.abi, + ) + try: - contract = web3.eth.contract( - address=DepositSecurityModuleInterface.address, - abi=DepositSecurityModuleInterface.abi, - ) func = contract.functions.depositBufferedEther( self.deposit_root, self.keys_op_index, @@ -284,31 +286,29 @@ def do_deposit(self): "data": func._encode_transaction_data() } - from eth_account.account import Account - signer = Account.from_key(variables.ACCOUNT.private_key) + signer = Account.from_key(private_key=variables.ACCOUNT.private_key) for i in range(10): # Try to get in next 10 blocks result = web3.flashbots.send_bundle( - [{"signer": signer, "transaction": tx}], + [{"signed_transaction": signer.sign_transaction(tx).rawTransaction}], self._current_block.number + i ) # We are waiting for `self._current_block.number + i` block number and get receipt by tx hash result.wait() rec = result.receipts() - if not rec: - raise Exception('No reception provided') - except Exception as error: - logger.error({'msg': f'Deposit failed.', 'error': str(error)}) - DEPOSIT_FAILURE.inc() - else: + logger.info({'msg': 'Transaction executed.', 'value': { 'blockHash': rec[-1]['blockHash'].hex(), 'blockNumber': rec[-1]['blockNumber'], 'gasUsed': rec[-1]['gasUsed'], 'transactionHash': rec[-1]['transactionHash'].hex(), }}) + except Exception as error: + logger.error({'msg': f'Deposit failed.', 'error': str(error)}) + DEPOSIT_FAILURE.inc() + else: SUCCESS_DEPOSIT.inc() logger.info({'msg': f'Deposit method end. Sleep for 1 minute.'}) diff --git a/scripts/utils/gas_strategy.py b/scripts/utils/gas_strategy.py index 8b727767..321faf7b 100644 --- a/scripts/utils/gas_strategy.py +++ b/scripts/utils/gas_strategy.py @@ -83,11 +83,13 @@ def get_recommended_gas_fee(self, percentiles: Iterable[Tuple[int, int]]) -> flo def get_recommended_buffered_ether_to_deposit(self, gas_fee): """Returns suggested minimum buffered ether to deposit""" - apr = 0.049 # Protocol APR + apr = 0.044 # Protocol APR # ether/14 days : select sum(tr.value)/1e18 from ethereum."transactions" as tr # where tr.to = '\xae7ab96520DE3A18E5e111B5EaAb095312D7fE84' # and tr.block_time >= '2021-12-01' and tr.block_time < '2021-12-15' and tr.value < 600*1e18; - a = 60 # ~ ether/hour + a = 24 # ~ ether/hour + keys_hour = a / 32 p = 32 * 10**18 * apr / 365 / 24 # ~ Profit in hour c = 378300 # wei is constant for every deposit tx that should be paid - return sqrt(c * gas_fee * a / 32 / p) * 32 * 10**18 + multiply_constant = 1.5 # we will get profit with constant from 1 to 2, but the most profitable will be 1.5 + return sqrt(multiply_constant * c * gas_fee * keys_hour / p) * 32 * 10**18 diff --git a/tests/test_gas_srategy.py b/tests/test_gas_srategy.py index 318e740a..498c7e1b 100644 --- a/tests/test_gas_srategy.py +++ b/tests/test_gas_srategy.py @@ -49,7 +49,10 @@ def test_recommended_buffered_ether(): assert 1 < buffered_ether / 10**18 < 100 buffered_ether = gas_fee_strategy.get_recommended_buffered_ether_to_deposit(50 * 10**9) - assert 400 < buffered_ether / 10**18 < 700 + assert 350 < buffered_ether / 10**18 < 400 buffered_ether = gas_fee_strategy.get_recommended_buffered_ether_to_deposit(70 * 10**9) - assert 500 < buffered_ether / 10**18 < 1000 + assert 400 < buffered_ether / 10**18 < 600 + + buffered_ether = gas_fee_strategy.get_recommended_buffered_ether_to_deposit(150 * 10**9) + assert 600 < buffered_ether / 10**18 < 700 diff --git a/tests/utils/mock_provider.py b/tests/utils/mock_provider.py index 75ac95d4..6303a1b2 100644 --- a/tests/utils/mock_provider.py +++ b/tests/utils/mock_provider.py @@ -22,18 +22,4 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse: if result is not None: return result[1] - infura_project_id = os.getenv('WEB3_INFURA_PROJECT_ID') - network = os.getenv('NETWORK') - prov = HTTPProvider(f'https://{network}.infura.io/v3/{infura_project_id}') - request_data = prov.encode_rpc_request(method, params) - raw_response = make_post_request( - prov.endpoint_uri, - request_data, - **prov.get_request_kwargs() - ) - response = prov.decode_rpc_response(raw_response) - - # print(method) - # print(f'({params}, {response}),') - - return response + raise Exception('There is no mock for response')