Skip to content

Commit

Permalink
Add referrals limits, tests. Some refactoring (#52)
Browse files Browse the repository at this point in the history
* Add referrals limits, tests. Some refactoring

* Fix referrals limits
  • Loading branch information
cyc60 authored Aug 4, 2022
1 parent bf8ed40 commit 3089b3a
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 100 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Code Quality

on: [pull_request, push]

jobs:
pre-commit:
name: Linting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]
test:
name: Testing
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up python
uses: actions/setup-python@v2
with:
python-version: 3.10.5

# Install poetry
- name: Load cached Poetry installation
uses: actions/cache@v2
with:
path: ~/.local
key: poetry-0
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

# Install dependencies
- name: Install dependencies
run: poetry install --no-interaction --no-root

# Run tests
- name: Run tests
run: poetry run python -m unittest
12 changes: 0 additions & 12 deletions .github/workflows/code-quality.yaml

This file was deleted.

32 changes: 31 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pre-commit = "==2.18.1"
pyinstaller = "==4.10"
types-requests = "==2.27.27"
types-PyYAML = "==6.0.8"
Faker = "==13.14.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
28 changes: 20 additions & 8 deletions stakewise_cli/commands/create_referrals_proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
w3 = Web3()


MAX_SWISE_PER_WEI = 250
MIN_SWISE_PER_WEI = 5


@click.command(
help="Creates referral proposal and generates a forum post specification"
)
Expand Down Expand Up @@ -51,10 +55,10 @@
type=int,
)
@click.option(
"--referrals-share",
"--referral-share",
required=True,
prompt="Enter referrals share (%), ex. 1.5 = 1.5%",
help="Referrals share (%), ex. 1.5 = 1.5%",
prompt="Enter referral share (%), ex. 1.5 = 1.5%",
help="Referral share (%), ex. 1.5 = 1.5%",
type=decimal.Decimal,
)
@click.option(
Expand All @@ -77,7 +81,7 @@ def create_referrals_proposal(
network: str,
from_block: int,
to_block: int,
referrals_share: decimal.Decimal,
referral_share: decimal.Decimal,
swise_price: decimal.Decimal,
eth_price: decimal.Decimal,
whitelist_path: str,
Expand All @@ -90,10 +94,12 @@ def create_referrals_proposal(
w3 = get_web3_client(network)

ethereum_gql_client = get_ethereum_gql_client(network)
from_date = get_block_timestamp(
from_date: int = get_block_timestamp(
gql_client=ethereum_gql_client, block_number=from_block
)
to_date = get_block_timestamp(gql_client=ethereum_gql_client, block_number=to_block)
to_date: int = get_block_timestamp(
gql_client=ethereum_gql_client, block_number=to_block
)

if not from_date:
raise click.ClickException(
Expand Down Expand Up @@ -134,11 +140,17 @@ def create_referrals_proposal(
for item in referrals_data:
referrer = Web3.toChecksumAddress(item["referrer"])
if referrer not in whitelisted_addresses:
click.secho(
f"Address {referrer} is not in whitelist",
fg="yellow",
)
continue

amount = int(
(int(item["amount"]) * eth_price * referrals_share) / (100 * swise_price)
(int(item["amount"]) * eth_price * referral_share) / (100 * swise_price)
)
amount = min(MAX_SWISE_PER_WEI * int(item["amount"]), amount)
amount = max(MIN_SWISE_PER_WEI * int(item["amount"]), amount)
total_amount += amount
referrals.setdefault(referrer, {}).setdefault(token_address, 0)
referrals[referrer][token_address] += amount
Expand All @@ -158,7 +170,7 @@ def create_referrals_proposal(
swise_price=swise_price,
eth_price=eth_price,
ipfs_url=ipfs_url,
referral_share=referrals_share,
referral_share=referral_share,
)
click.clear()
click.secho(
Expand Down
28 changes: 2 additions & 26 deletions stakewise_cli/commands/sync_db.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import click
from eth_typing import ChecksumAddress
from requests.exceptions import ConnectionError, HTTPError
from web3 import Web3

from stakewise_cli.eth2 import get_beacon_client, validate_mnemonic
from stakewise_cli.eth2 import prompt_beacon_client, validate_mnemonic
from stakewise_cli.networks import (
GNOSIS_CHAIN,
GOERLI,
HARBOUR_GOERLI,
HARBOUR_MAINNET,
MAINNET,
NETWORKS,
)
from stakewise_cli.storages.database import Database, check_db_connection
from stakewise_cli.validators import validate_db_uri, validate_operator_address
Expand Down Expand Up @@ -52,28 +49,7 @@ def sync_db(
) -> None:
check_db_connection(db_url)

while True:
try:
beacon_client = get_beacon_client(network)
genesis = beacon_client.get_genesis()
if genesis["data"]["genesis_fork_version"] != Web3.toHex(
NETWORKS[network]["GENESIS_FORK_VERSION"]
):
click.secho(
"Error: invalid beacon node network",
bold=True,
fg="red",
)
continue
break
except (ConnectionError, HTTPError):
pass

click.secho(
"Error: failed to connect to the ETH2 server with provided URL",
bold=True,
fg="red",
)
beacon_client = prompt_beacon_client(network)

mnemonic = click.prompt(
'Enter your mnemonic separated by spaces (" ")',
Expand Down
28 changes: 1 addition & 27 deletions stakewise_cli/commands/sync_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import click
from eth_typing import ChecksumAddress
from requests.exceptions import ConnectionError, HTTPError
from web3 import Web3

from stakewise_cli.eth2 import get_beacon_client, validate_mnemonic
from stakewise_cli.eth2 import validate_mnemonic
from stakewise_cli.networks import (
GNOSIS_CHAIN,
GOERLI,
HARBOUR_GOERLI,
HARBOUR_MAINNET,
MAINNET,
NETWORKS,
)
from stakewise_cli.storages.local import LocalStorage
from stakewise_cli.validators import validate_operator_address
Expand Down Expand Up @@ -42,29 +39,6 @@
type=click.Path(exists=False, file_okay=False, dir_okay=True),
)
def sync_local(network: str, operator: ChecksumAddress, folder: str) -> None:
while True:
try:
beacon_client = get_beacon_client(network)
genesis = beacon_client.get_genesis()
if genesis["data"]["genesis_fork_version"] != Web3.toHex(
NETWORKS[network]["GENESIS_FORK_VERSION"]
):
click.secho(
"Error: invalid beacon node network",
bold=True,
fg="red",
)
continue
break
except (ConnectionError, HTTPError):
pass

click.secho(
"Error: failed to connect to the ETH2 server with provided URL",
bold=True,
fg="red",
)

mnemonic = click.prompt(
'Enter your mnemonic separated by spaces (" ")',
value_proc=validate_mnemonic,
Expand Down
29 changes: 3 additions & 26 deletions stakewise_cli/commands/sync_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
from eth_typing import ChecksumAddress
from hvac import Client as VaultClient
from hvac.exceptions import InvalidRequest
from requests.exceptions import ConnectionError, HTTPError
from web3 import Web3
from requests.exceptions import ConnectionError

from stakewise_cli.eth2 import get_beacon_client, validate_mnemonic
from stakewise_cli.eth2 import prompt_beacon_client, validate_mnemonic
from stakewise_cli.networks import (
GNOSIS_CHAIN,
GOERLI,
HARBOUR_GOERLI,
HARBOUR_MAINNET,
MAINNET,
NETWORKS,
)
from stakewise_cli.settings import VAULT_VALIDATORS_MOUNT_POINT
from stakewise_cli.storages.vault import Vault
Expand Down Expand Up @@ -65,28 +63,7 @@ def sync_vault(network: str, operator: ChecksumAddress) -> None:
fg="red",
)

while True:
try:
beacon_client = get_beacon_client(network)
genesis = beacon_client.get_genesis()
if genesis["data"]["genesis_fork_version"] != Web3.toHex(
NETWORKS[network]["GENESIS_FORK_VERSION"]
):
click.secho(
"Error: invalid beacon node network",
bold=True,
fg="red",
)
continue
break
except (ConnectionError, HTTPError):
pass

click.secho(
"Error: failed to connect to the ETH2 server with provided URL",
bold=True,
fg="red",
)
beacon_client = prompt_beacon_client(network)

vault_client.secrets.kv.default_kv_version = 1
try:
Expand Down
Loading

0 comments on commit 3089b3a

Please sign in to comment.