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

🗃️ create table conditions #40

Merged
merged 5 commits into from
Jan 22, 2024
Merged
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
61 changes: 61 additions & 0 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,64 @@ def create_max_calls_per_entrypoint_condition(
db.commit()
db.refresh(db_condition)
return db_condition


def check_max_calls_per_sponsee(db: Session, sponsee_address: str, vault_id: UUID4):
return (
db.query(models.Condition)
.filter(models.Condition.type == ConditionType.MAX_CALLS_PER_SPONSEE)
.filter(models.Condition.sponsee_address == sponsee_address)
.filter(models.Condition.vault_id == vault_id)
.one_or_none()
)


def check_max_calls_per_entrypoint(
db: Session, contract_id: UUID4, entrypoint_id: UUID4, vault_id: UUID4
):
return (
db.query(models.Condition)
.filter(models.Condition.type == ConditionType.MAX_CALLS_PER_ENTRYPOINT)
.filter(models.Condition.contract_id == contract_id)
.filter(models.Condition.entrypoint_id == entrypoint_id)
.filter(models.Condition.vault_id == vault_id)
.one_or_none()
)


def check_conditions(db: Session, datas: schemas.CheckConditions):
print(datas)
sponsee_condition = check_max_calls_per_sponsee(
db, datas.sponsee_address, datas.vault_id
)
entrypoint_condition = check_max_calls_per_entrypoint(
db, datas.contract_id, datas.entrypoint_id, datas.vault_id
)

# No condition registered
if sponsee_condition is None and entrypoint_condition is None:
return True
# One of condition is excedeed
if (
sponsee_condition is not None
and (sponsee_condition.current >= sponsee_condition.max)
) or (
entrypoint_condition is not None
and (entrypoint_condition.current >= entrypoint_condition.max)
):
return False

# Update conditions
# TODO - Rewrite with list
print(sponsee_condition.current, entrypoint_condition)

update_condition(db, sponsee_condition)
update_condition(db, entrypoint_condition)
return True


def update_condition(db: Session, condition: Optional[models.Condition]):
if condition:
db.query(models.Condition).filter(models.Condition.id == condition.id).update(
{"current": condition.current + 1}
)
quentin-burg marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 18 additions & 0 deletions src/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytezos.crypto.encoding import is_address
from .utils import (
ConditionAlreadyExists,
ConditionExceed,
ConditionType,
ContractAlreadyRegistered,
ContractNotFound,
Expand Down Expand Up @@ -301,6 +302,17 @@ async def post_operation(
entrypoint = crud.get_entrypoint(db, str(contract.address), entrypoint_name)
if not entrypoint.is_enabled:
raise EntrypointDisabled()

if not crud.check_conditions(
db,
schemas.CheckConditions(
sponsee_address=call_data.sender_address,
contract_id=contract.id,
entrypoint_id=entrypoint.id,
vault_id=contract.credit_id,
),
):
raise ConditionExceed()
except EntrypointNotFound:
logging.warning(f"Entrypoint {entrypoint_name} is not found")
raise HTTPException(
Expand All @@ -313,6 +325,12 @@ async def post_operation(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"Entrypoint {entrypoint_name} is disabled.",
)
except ConditionExceed:
logging.warning(f"A condition exceed the maximum defined.")
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"A condition exceed the maximum defined.",
)
aguillon marked this conversation as resolved.
Show resolved Hide resolved

try:
# Simulate the operation alone without sending it
Expand Down
7 changes: 7 additions & 0 deletions src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,10 @@ class CreateMaxCallsPerSponseeCondition(BaseModel):
sponsee_address: str
vault_id: UUID4
max: int


class CheckConditions(BaseModel):
sponsee_address: str
contract_id: UUID4
entrypoint_id: UUID4
vault_id: UUID4
4 changes: 4 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class ConditionAlreadyExists(Exception):
pass


class ConditionExceed(Exception):
pass


# -- UTILITY TYPES --
class ConditionType(enum.Enum):
MAX_CALLS_PER_ENTRYPOINT = "MAX_CALLS_PER_ENTRYPOINT"
Expand Down
Loading