Skip to content

Commit

Permalink
Merge pull request #78 from lidofinance/deposit-formula-improve
Browse files Browse the repository at this point in the history
Deposit formula improve
  • Loading branch information
avsetsin authored Mar 1, 2022
2 parents 580dacb + b727015 commit e0cfec8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
2 changes: 2 additions & 0 deletions scripts/depositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
26 changes: 13 additions & 13 deletions scripts/depositor_utils/depositor_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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.'})
Expand Down
8 changes: 5 additions & 3 deletions scripts/utils/gas_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions tests/test_gas_srategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 1 addition & 15 deletions tests/utils/mock_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit e0cfec8

Please sign in to comment.