Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week08 #58

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/week08/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from src.utils.paths import get_week_paths

assets_dir, homework_dir, lecture_dir = get_week_paths(__file__)
Empty file added src/week08/lecture/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions src/week08/lecture/staking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from opshin.prelude import *


def equal(a: Union[StakingHash, StakingPtr], b: Union[StakingHash, StakingPtr]) -> bool:
juliusfrost marked this conversation as resolved.
Show resolved Hide resolved
result = False
if isinstance(b, StakingHash):
if isinstance(a, StakingHash):
result = b == a
elif isinstance(b, StakingPtr):
if isinstance(a, StakingPtr):
result = b == a
return result


def get_amount(context: ScriptContext, purpose: Rewarding) -> int:
amount = 0
found = False
for staking_cred, a in context.tx_info.wdrl.items():
if equal(staking_cred, purpose.staking_credential):
juliusfrost marked this conversation as resolved.
Show resolved Hide resolved
found = True
amount = a
assert found, "withdrawal not found"
return amount


def paid_to_address(addr: Address, context: ScriptContext) -> int:
juliusfrost marked this conversation as resolved.
Show resolved Hide resolved
paid_amount = 0
for output in context.tx_info.outputs:
if output.address == addr:
paid_amount += output.value.get(b"", {b"": 0}).get(b"", 0)
return paid_amount


def validator(addr: Address, _: None, context: ScriptContext):
purpose = context.purpose
if isinstance(purpose, Certifying):
print("certifying")
elif isinstance(purpose, Rewarding):
amount = get_amount(context, purpose)
paid_amount = paid_to_address(addr, context)
assert 2 * paid_amount >= amount, "insufficient reward sharing"
else:
assert False, "script purpose must be certifying or rewarding"
Empty file added src/week08/tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions src/week08/tests/test_lecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from opshin import compiler

from src.week08 import lecture_dir

python_files = [
"staking.py",
]
script_paths = [str(lecture_dir.joinpath(f)) for f in python_files]


@pytest.mark.parametrize("path", script_paths)
def test_lecture_compile(path):
with open(path, "r") as f:
source_code = f.read()
source_ast = compiler.parse(source_code)
code = compiler.compile(source_ast)
print(code.dumps())