Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazabbas committed Jun 27, 2024
1 parent 15ef39e commit 66b275c
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 276 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ repos:
entry: poetry run black .
pass_filenames: false
language: system
- id: mypy
name: mypy
entry: poetry run mypy program_admin/ tests/
- id: pyright
name: pyright
entry: poetry run pyright program_admin/ tests/
pass_filenames: false
language: system
- id: pylint
name: pylint
entry: poetry run pylint program_admin/ tests/
- id: pyflakes
name: pyflakes
entry: poetry run pyflakes program_admin/ tests/
pass_filenames: false
language: system
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
lint:
poetry run isort --profile=black program_admin/ tests/
poetry run black program_admin/ tests/
poetry run mypy program_admin/ tests/
poetry run pylint program_admin/ tests/
poetry run pyright program_admin/ tests/
poetry run pyflakes program_admin/ tests/

install:
poetry install
Expand Down
247 changes: 21 additions & 226 deletions poetry.lock

Large diffs are not rendered by default.

25 changes: 8 additions & 17 deletions program_admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json
import os
import sys
from dataclasses import asdict
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Tuple
from typing import Dict, List, Literal, Optional, Tuple

from loguru import logger
from solana import system_program
Expand All @@ -16,13 +14,7 @@

from program_admin import instructions as pyth_program
from program_admin.keys import load_keypair
from program_admin.parsing import (
parse_account,
parse_overrides_json,
parse_permissions_json,
parse_products_json,
parse_publishers_json,
)
from program_admin.parsing import parse_account
from program_admin.types import (
Network,
PythAuthorityPermissionAccount,
Expand Down Expand Up @@ -72,7 +64,7 @@ def __init__(
key_dir: str,
program_key: str,
commitment: Literal["confirmed", "finalized"],
rpc_endpoint: str = None,
rpc_endpoint: str = "",
):
self.network = network
self.rpc_endpoint = rpc_endpoint or RPC_ENDPOINTS[network]
Expand Down Expand Up @@ -223,7 +215,7 @@ async def send_transaction(

async def sync(
self,
ref_products: ReferenceProduct,
ref_products: Dict[str, ReferenceProduct],
ref_publishers: ReferencePublishers,
ref_permissions: ReferencePermissions,
ref_authority_permissions: ReferenceAuthorityPermissions,
Expand Down Expand Up @@ -252,6 +244,10 @@ async def sync(
await self.send_transaction(
authority_instructions, authority_signers
)
else:
logger.debug(
"Reference data for authority permissions is not defined, skipping..."
)

# Sync mapping accounts
mapping_instructions, mapping_keypairs = await self.sync_mapping_instructions(
Expand Down Expand Up @@ -310,11 +306,6 @@ async def sync(
if send_transactions:
await self.send_transaction(price_instructions, price_keypairs)

else:
logger.debug(
"Reference data for authority permissions is not defined, skipping..."
)

return instructions

async def sync_mapping_instructions(
Expand Down
14 changes: 7 additions & 7 deletions program_admin/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def set_minimum_publishers(funding_key, program_key, price_key, value, outfile):
@click.option("--funding-key", help="Funding key", envvar="FUNDING_KEY")
@click.option("--program-key", help="Pyth program key", envvar="PROGRAM_KEY")
@click.option("--product-key", help="Product account key", envvar="PRODUCT_KEY")
@click.option("--metadata", help="Metadata to add to product", type=dict)
@click.option("--metadata", help="Metadata to add to product", type=str)
@click.option(
"--outfile",
help="File location to write instructions",
Expand All @@ -130,7 +130,9 @@ def update_product_metadata(funding_key, program_key, product_key, metadata, out
funding = PublicKey(funding_key)
program = PublicKey(program_key)
product = PublicKey(product_key)
instruction = instructions.update_product(program, funding, product, metadata)
instruction = instructions.update_product(
program, funding, product, json.loads(metadata)
)

instruction_output = json.dumps(
[
Expand Down Expand Up @@ -448,12 +450,10 @@ def sync(
ref_permissions = parse_permissions_with_overrides(
Path(permissions), Path(overrides), network
)
ref_authority_permissions = None

if authority_permissions:
ref_authority_permissions = parse_authority_permissions_json(
Path(authority_permissions)
)
ref_authority_permissions = parse_authority_permissions_json(
Path(authority_permissions)
)

asyncio.run(
program_admin.sync(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
from .accept import AcceptAccounts, accept
from .propose import ProposeAccounts, propose
from .revert import RevertAccounts, revert

assert AcceptAccounts
assert accept
assert ProposeAccounts
assert propose
assert RevertAccounts
assert revert
16 changes: 9 additions & 7 deletions program_admin/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, TypedDict, Union
from typing import Dict, Generic, List, Literal, Optional, TypedDict, TypeVar, Union

from solana.publickey import PublicKey

Expand Down Expand Up @@ -130,35 +130,37 @@ def __str__(self) -> str:

AccountData = Union[MappingData, ProductData, PriceData, AuthorityPermissionData]

T = TypeVar("T", bound=AccountData)


@dataclass
class PythAccount:
class PythAccount(Generic[T]):
public_key: PublicKey
owner: PublicKey
lamports: int
data: AccountData
data: T

def __str__(self) -> str:
return f"PythAccount(key={str(self.public_key)[0:5]}..., data={self.data})"


@dataclass
class PythMappingAccount(PythAccount):
class PythMappingAccount(PythAccount[MappingData]):
data: MappingData


@dataclass
class PythProductAccount(PythAccount):
class PythProductAccount(PythAccount[ProductData]):
data: ProductData


@dataclass
class PythPriceAccount(PythAccount):
class PythPriceAccount(PythAccount[PriceData]):
data: PriceData


@dataclass
class PythAuthorityPermissionAccount(PythAccount):
class PythAuthorityPermissionAccount(PythAccount[AuthorityPermissionData]):
"""
On-chain authorities permissions account.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ requests = "^2.32.3"
black = { version = "^22.3.0" }
isort = { version = "^5.10.0" }
ipython = "^8.2.0"
mypy = "^0.942"
pylint = "^2.13.0"
types-ujson = "^4.2.0"
pytest = "^7.1.2"
pytest-asyncio = "^0.18.3"
types-requests = "^2.32.0.20240622"
pyright = "^1.1.369"
pyflakes = "^3.2.0"

[tool.poetry.scripts]
program-admin = "program_admin.cli:cli"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_minimum_publishers():
"--price-key",
"6bRsDGmuSfUCND9vZioUbWfB56dkrCqNE8f2DW7eNU5D",
"--value",
20,
"20",
],
)
assert result.exit_code == 0
Expand All @@ -46,7 +46,7 @@ def test_toggle_publisher():
"--publisher-key",
"6bRsDGmuSfUCND9vZioUbWfB56dkrCqNE8f2DW7eNU5E",
"--status",
True,
"true",
],
)

Expand All @@ -72,7 +72,7 @@ def test_update_product():
"--product-key",
"6bRsDGmuSfUCND9vZioUbWfB56dkrCqNE8f2DW7eNU5D",
"--metadata",
{"data": "meta"},
'{"data": "meta"}',
],
)
assert result.exit_code == 0
Expand Down
19 changes: 13 additions & 6 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def oracle():
outfile = "tests/pyth_oracle.so"

try:
response = requests.get(api_url)
response = requests.get(api_url, timeout=300)
response.raise_for_status()
release_info = response.json()

Expand All @@ -97,8 +97,11 @@ async def oracle():
asset_url = asset["browser_download_url"]
break

if not asset_url:
raise Exception(f"Unable to find asset URL for {filename}")

# Download the asset
download_response = requests.get(asset_url)
download_response = requests.get(asset_url, timeout=300)
download_response.raise_for_status()

# Save the file to the specified path
Expand All @@ -107,9 +110,9 @@ async def oracle():

LOGGER.debug(f"File {filename} downloaded successfully to {outfile}.")

except requests.exceptions.RequestException as e:
LOGGER.error(f"An error occurred: {e}")
raise e
except requests.exceptions.RequestException as error:
LOGGER.error(f"An error occurred: {error}")
raise error

yield outfile

Expand Down Expand Up @@ -437,7 +440,11 @@ async def test_sync(

product_accounts = list(program_admin._product_accounts.values())
price_accounts = list(program_admin._price_accounts.values())
authority_permissions = program_admin.authority_permission_account.data
authority_permission_account = program_admin.authority_permission_account
if authority_permission_account:
authority_permissions = authority_permission_account.data
else:
raise Exception("Authority permissions not found")

reference_symbols = ["Crypto.BTC/USD", "Equity.US.AAPL/USD"]

Expand Down

0 comments on commit 66b275c

Please sign in to comment.