Skip to content

Commit

Permalink
include permissions account in instructions (#42)
Browse files Browse the repository at this point in the history
* delete oracle file from git

* include permissions account in instructions
update tests
download latest oracle for tests

* bump app version, update poetry version

* update solana version

* add pydoc

* run poetry lock

* linting

* remove ecr build

* remove useless if-else
  • Loading branch information
ayazabbas authored Jun 28, 2024
1 parent 622998e commit edf8567
Show file tree
Hide file tree
Showing 16 changed files with 1,081 additions and 1,168 deletions.
2 changes: 1 addition & 1 deletion .github/actions/python-poetry/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inputs:
poetry-version:
required: false
description: Poetry version
default: "1.3.2"
default: "1.8.2"

runs:
using: composite
Expand Down
34 changes: 1 addition & 33 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,9 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python-poetry
- run: sh -c "$(curl -sSfL https://release.solana.com/v1.10.35/install)"
- run: sh -c "$(curl -sSfL https://release.solana.com/v1.18.17/install)"
- run: echo "$HOME/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
- run: poetry run pytest
env:
TEST_MODE: "1"
DEV_MODE: "1"
build-and-push-ecr:
runs-on: ubuntu-latest
needs: [run-tests]
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v2
- uses: aws-actions/[email protected]
with:
role-to-assume: arn:aws:iam::192824654885:role/github-actions-ecr
aws-region: eu-west-2
- uses: docker/login-action@v2
with:
registry: public.ecr.aws
env:
AWS_REGION: us-east-1
- run: docker context create builders
- uses: docker/setup-buildx-action@v2
with:
version: latest
endpoint: builders
- uses: haya14busa/action-cond@v1
id: image_tag
with:
cond: ${{ startsWith(github.ref, 'refs/tags/') }}
if_true: ${{ github.ref_name }}
if_false: ${{ github.sha }}
- uses: docker/build-push-action@v2
with:
push: true
tags: public.ecr.aws/pyth-network/${{ github.event.repository.name }}:${{ steps.image_tag.outputs.value }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
__pycache__
dist
keys
pyth_oracle.so
permissions.json
products.json
publishers.json
test-ledger
.tool-versions

# IntelliJ files
.idea
Expand Down
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
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
lint:
poetry run isort --profile=black program_admin/ tests/
poetry run black program_admin/ tests/
poetry run pyright program_admin/ tests/
poetry run pyflakes program_admin/ tests/

install:
poetry install

test:
poetry run pytest
1,890 changes: 870 additions & 1,020 deletions poetry.lock

Large diffs are not rendered by default.

54 changes: 19 additions & 35 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,10 +215,10 @@ 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: Optional[ReferenceAuthorityPermissions],
ref_authority_permissions: ReferenceAuthorityPermissions,
send_transactions: bool = True,
generate_keys: bool = False,
allocate_price_v2: bool = True,
Expand All @@ -236,6 +228,20 @@ async def sync(
# Fetch program accounts from the network
await self.refresh_program_accounts()

# Sync authority permissions
(
authority_instructions,
authority_signers,
) = await self.sync_authority_permissions_instructions(
ref_authority_permissions
)

if authority_instructions:
instructions.extend(authority_instructions)

if send_transactions:
await self.send_transaction(authority_instructions, authority_signers)

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

if ref_authority_permissions:
# Sync authority permissions
(
authority_instructions,
authority_signers,
) = await self.sync_authority_permissions_instructions(
ref_authority_permissions
)

if authority_instructions:
instructions.extend(authority_instructions)

if send_transactions:
await self.send_transaction(
authority_instructions, authority_signers
)

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
37 changes: 33 additions & 4 deletions program_admin/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solana.transaction import AccountMeta, TransactionInstruction

from program_admin.types import ReferenceAuthorityPermissions
from program_admin.util import encode_product_metadata
from program_admin.util import encode_product_metadata, get_permissions_account

# TODO: Implement add_mapping instruction

Expand Down Expand Up @@ -44,11 +44,16 @@ def init_mapping(
layout = Struct("version" / Int32ul, "command" / Int32sl)
data = layout.build(dict(version=PROGRAM_VERSION, command=COMMAND_INIT_MAPPING))

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=mapping_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand All @@ -71,12 +76,17 @@ def add_product(
layout = Struct("version" / Int32ul, "command" / Int32sl)
data = layout.build(dict(version=PROGRAM_VERSION, command=COMMAND_ADD_PRODUCT))

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=mapping_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=new_product_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand Down Expand Up @@ -124,11 +134,16 @@ def update_product(
data = layout.build(dict(version=PROGRAM_VERSION, command=COMMAND_UPD_PRODUCT))
data_extra = encode_product_metadata(product_metadata)

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data + data_extra,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=product_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand Down Expand Up @@ -162,12 +177,17 @@ def add_price(
)
)

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=product_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=new_price_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand Down Expand Up @@ -222,11 +242,16 @@ def set_minimum_publishers(
)
)

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=price_account_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand Down Expand Up @@ -257,11 +282,16 @@ def toggle_publisher(
)
)

permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

return TransactionInstruction(
data=data,
keys=[
AccountMeta(pubkey=funding_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=price_account_key, is_signer=True, is_writable=True),
AccountMeta(pubkey=permissions_account, is_signer=False, is_writable=True),
],
program_id=program_key,
)
Expand Down Expand Up @@ -306,9 +336,8 @@ def upd_permissions(
)
)

[permissions_account, _bump] = PublicKey.find_program_address(
[AUTHORITY_PERMISSIONS_PDA_SEED],
program_key,
permissions_account = get_permissions_account(
program_key, AUTHORITY_PERMISSIONS_PDA_SEED
)

# Under the BPF upgradeable loader, the program data key is a PDA
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
Loading

0 comments on commit edf8567

Please sign in to comment.