-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd8d0be
commit 42cbc81
Showing
8 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,11 @@ | ||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
|
||
from src.validators.endpoints import approve_validators, get_validators | ||
|
||
app = Starlette( | ||
routes=[ | ||
Route('/validators', get_validators, methods=['GET']), | ||
Route('/validators', approve_validators, methods=['POST']), | ||
] | ||
) |
This file contains 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 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 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 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,69 @@ | ||
import json | ||
|
||
from starlette.exceptions import HTTPException | ||
from starlette.requests import Request | ||
from starlette.responses import JSONResponse, Response | ||
from starlette.status import HTTP_400_BAD_REQUEST | ||
from web3.types import Wei | ||
|
||
from src.common.execution import get_oracles | ||
from src.common.utils import MGNO_RATE, WAD | ||
from src.config.networks import GNOSIS | ||
from src.config.settings import DEPOSIT_AMOUNT, settings | ||
from src.validators.database import NetworkValidatorCrud | ||
from src.validators.execution import ( | ||
get_available_validators, | ||
get_latest_network_validator_public_keys, | ||
get_withdrawable_assets, | ||
) | ||
from src.validators.typings import Validator | ||
|
||
|
||
async def get_validators(request: Request) -> Response: | ||
vault_balance, _ = await get_withdrawable_assets() | ||
if settings.network == GNOSIS: | ||
# apply GNO -> mGNO exchange rate | ||
vault_balance = Wei(int(vault_balance * MGNO_RATE // WAD)) | ||
|
||
# calculate number of validators that can be registered | ||
validators_count = vault_balance // DEPOSIT_AMOUNT | ||
if not validators_count: | ||
# not enough balance to register validators | ||
return JSONResponse([]) | ||
|
||
# get latest oracles | ||
oracles = await get_oracles() | ||
|
||
validators_count = min(oracles.validators_approval_batch_limit, validators_count) | ||
|
||
validators: list[Validator] = await get_available_validators( | ||
keystore=request.app.state.keystore, | ||
deposit_data=request.app.state.deposit_data, | ||
count=validators_count, | ||
) | ||
if not validators: | ||
# All validators from `deposit_data` are already registered | ||
return JSONResponse([]) | ||
|
||
# get next validator index for exit signature | ||
latest_public_keys = await get_latest_network_validator_public_keys() | ||
next_validator_index = NetworkValidatorCrud().get_next_validator_index(list(latest_public_keys)) | ||
|
||
return JSONResponse( | ||
[ | ||
{'public_key': validator.public_key, 'index': index} | ||
for index, validator in enumerate(validators, next_validator_index) | ||
] | ||
) | ||
|
||
|
||
def approve_validators(request: Request) -> Response: | ||
# pylint: disable=unused-argument | ||
try: | ||
payload = await request.json() | ||
except json.JSONDecodeError as exc: | ||
raise HTTPException( | ||
status_code=HTTP_400_BAD_REQUEST, detail='invalid_request_body' | ||
) from exc | ||
|
||
return JSONResponse([]) |
This file contains 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