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
87 changes: 87 additions & 0 deletions alembic/versions/c14c4b6f36b3_create_table_conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""create-table-conditions

Revision ID: c14c4b6f36b3
Revises: 0894f3022876
Create Date: 2024-01-11 16:02:26.575786

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "c14c4b6f36b3"
down_revision: Union[str, None] = "0894f3022876"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"conditions",
sa.Column("id", sa.UUID(), nullable=False),
sa.Column(
"type",
sa.Enum(
"MAX_CALLS_PER_ENTRYPOINT",
"MAX_CALLS_PER_SPONSEE",
name="conditiontype",
),
nullable=True,
),
sa.Column(
"sponsee_address",
sa.String(),
sa.CheckConstraint(
"(type = 'MAX_CALLS_PER_SPONSEE') = (sponsee_address IS NOT NULL)",
name="sponsee_address_not_null_constraint",
),
nullable=True,
),
sa.Column(
"contract_id",
sa.UUID(),
sa.CheckConstraint(
"(type = 'MAX_CALLS_PER_ENTRYPOINT') = (contract_id IS NOT NULL)",
name="contract_id_not_null_constraint",
),
nullable=True,
),
sa.Column(
"entrypoint_id",
sa.UUID(),
sa.CheckConstraint(
"(type = 'MAX_CALLS_PER_ENTRYPOINT') = (entrypoint_id IS NOT NULL)",
name="entrypoint_id_not_null_constraint",
),
nullable=True,
),
sa.Column("vault_id", sa.UUID(), nullable=False),
sa.Column("max", sa.Integer(), nullable=False),
sa.Column("current", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["contract_id"],
["contracts.id"],
),
sa.ForeignKeyConstraint(
["entrypoint_id"],
["entrypoints.id"],
),
sa.ForeignKeyConstraint(
["vault_id"],
["credits.id"],
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("conditions")
op.execute("DROP type conditiontype")
# ### end Alembic commands ###
9 changes: 5 additions & 4 deletions demo/staking-contract.mligo
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#import "../permit-cameligo/src/main.mligo" "FA2"

type storage = {
nft_address: address;
staked: (address, nat) big_map;
}
type storage =
{
nft_address : address;
staked : (address, nat) big_map
}

(* We need to provide the address of the NFT's owner so that the transfer can be done by someone
* else (we don't rely on Tezos.get_sender ()) *)
Expand Down
59 changes: 58 additions & 1 deletion src/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String
from sqlalchemy import (
Boolean,
CheckConstraint,
Column,
DateTime,
Enum,
ForeignKey,
Integer,
String,
)
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
import uuid

from src.utils import ConditionType
from .database import Base
import datetime

Expand Down Expand Up @@ -107,3 +118,49 @@ class Operation(Base):

contract = relationship("Contract", back_populates="operations")
entrypoint = relationship("Entrypoint", back_populates="operations")


# ------- CONDITIONS ------- #


class Condition(Base):
__tablename__ = "conditions"

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
type = Column(Enum(ConditionType))
sponsee_address = Column(
String,
quentin-burg marked this conversation as resolved.
Show resolved Hide resolved
CheckConstraint(
"(type = 'MAX_CALLS_PER_SPONSEE') = (sponsee_address IS NOT NULL)",
name="sponsee_address_not_null_constraint",
),
nullable=True,
)
contract_id = Column(
UUID(as_uuid=True),
CheckConstraint(
"(type = 'MAX_CALLS_PER_ENTRYPOINT') = (contract_id IS NOT NULL)",
name="contract_id_not_null_constraint",
),
ForeignKey("contracts.id"),
nullable=True,
)
entrypoint_id = Column(
UUID(as_uuid=True),
CheckConstraint(
"(type = 'MAX_CALLS_PER_ENTRYPOINT') = (entrypoint_id IS NOT NULL)",
name="entrypoint_id_not_null_constraint",
),
ForeignKey("entrypoints.id"),
nullable=True,
)
vault_id = Column(UUID(as_uuid=True), ForeignKey("credits.id"), nullable=False)
max = Column(Integer, nullable=False)
current = Column(Integer, nullable=False)
created_at = Column(
DateTime(timezone=True), default=datetime.datetime.utcnow(), nullable=False
)

contract = relationship("Contract", back_populates="conditions")
entrypoint = relationship("Entrypoint", back_populates="conditions")
vault = relationship("Credits", back_populates="conditions")
9 changes: 9 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -- EXCEPTIONS --


import enum


class UserNotFound(Exception):
pass

Expand Down Expand Up @@ -39,3 +42,9 @@ class NotEnoughFunds(Exception):

class TooManyCallsForThisMonth(Exception):
pass


# -- UTILITY TYPES --
class ConditionType(enum.Enum):
MAX_CALLS_PER_ENTRYPOINT = "MAX_CALLS_PER_ENTRYPOINT"
MAX_CALLS_PER_SPONSEE = "MAX_CALLS_PER_SPONSEE"
quentin-burg marked this conversation as resolved.
Show resolved Hide resolved
Loading