-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from planetarium/release/0.10.0
0.10.0
- Loading branch information
Showing
27 changed files
with
594 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
common/alembic/versions/90ff6ac09fe5_add_required_level_column.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Add required_level column | ||
Revision ID: 90ff6ac09fe5 | ||
Revises: a2df38682595 | ||
Create Date: 2024-01-22 12:31:38.823682 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '90ff6ac09fe5' | ||
down_revision = 'a2df38682595' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
old_enum = ("INIT", "VALIDATION_REQUEST", "VALID", "REFUNDED_BY_ADMIN", "INVALID", "REFUNDED_BY_BUYER", | ||
"PURCHASE_LIMIT_EXCEED", "TIME_LIMIT", "UNKNOWN") | ||
new_enum = sorted(old_enum + ("REQUIRED_LEVEL",)) | ||
|
||
old_status = sa.Enum(*old_enum, name="receiptstatus") | ||
new_status = sa.Enum(*new_enum, name="receiptstatus") | ||
tmp_status = sa.Enum(*new_enum, name="_receiptstatus") | ||
|
||
receipt_table = sa.sql.table( | ||
"receipt", | ||
sa.Column("status", new_status, nullable=False) | ||
) | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
tmp_status.create(op.get_bind(), checkfirst=False) | ||
op.execute("ALTER TABLE receipt ALTER COLUMN status TYPE _receiptstatus USING status::text::_receiptstatus") | ||
old_status.drop(op.get_bind(), checkfirst=False) | ||
new_status.create(op.get_bind(), checkfirst=False) | ||
op.execute("ALTER TABLE receipt ALTER COLUMN status TYPE receiptstatus USING status::text::receiptstatus") | ||
tmp_status.drop(op.get_bind(), checkfirst=False) | ||
|
||
op.add_column('product', sa.Column('required_level', sa.Integer(), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('product', 'required_level') | ||
|
||
op.execute(receipt_table.update() | ||
.where(receipt_table.c.status.in_(['REQUIRED_LEVEL'])) | ||
.values(status="INVALID") | ||
) | ||
tmp_status.create(op.get_bind(), checkfirst=False) | ||
op.execute("ALTER TABLE receipt ALTER COLUMN status TYPE _receiptstatus USING status::text::_receiptstatus") | ||
new_status.drop(op.get_bind(), checkfirst=False) | ||
old_status.create(op.get_bind(), checkfirst=False) | ||
op.execute("ALTER TABLE receipt ALTER COLUMN status TYPE receiptstatus USING status::text::receiptstatus") | ||
tmp_status.drop(op.get_bind(), checkfirst=False) | ||
# ### end Alembic commands ### |
30 changes: 30 additions & 0 deletions
30
common/alembic/versions/a2df38682595_add_is_free_column.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Add is_free column | ||
Revision ID: a2df38682595 | ||
Revises: 2822c456abc3 | ||
Create Date: 2024-01-18 13:55:23.731546 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'a2df38682595' | ||
down_revision = '2822c456abc3' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('product', sa.Column('is_free', sa.Boolean(), nullable=True, default=False)) | ||
op.execute("UPDATE product SET is_free=FALSE") | ||
op.alter_column("product", "is_free", nullable=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('product', 'is_free') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
|
||
class Address: | ||
def __init__(self, addr: str): | ||
if addr.startswith("0x"): | ||
if len(addr) != 42: | ||
raise ValueError("Address with 0x prefix must have exact 42 chars.") | ||
self.raw = bytes.fromhex(addr[2:]) | ||
else: | ||
if len(addr) != 40: | ||
raise ValueError("Address without 0x prefix must have exact 40 chars.") | ||
self.raw = bytes.fromhex(addr) | ||
|
||
@property | ||
def long_format(self): | ||
return f"0x{self.raw.hex()}" | ||
|
||
@property | ||
def short_format(self): | ||
return self.raw.hex() | ||
|
||
def __eq__(self, other: Address): | ||
return self.raw == other.raw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict, List, Optional, Any | ||
|
||
import bencodex | ||
|
||
from common.lib9c.models.address import Address | ||
|
||
|
||
class Currency: | ||
""" | ||
# Currency | ||
--- | ||
Lib9c Currency model which has ticker, minters, decimal_places, total_supply_trackable. | ||
`minters` will be automatically sanitized to `None` if empty list provided. | ||
""" | ||
|
||
def __init__(self, ticker: str, decimal_places: int, minters: Optional[List[str]] = None, | ||
total_supply_trackable: bool = False): | ||
self.ticker = ticker | ||
self.minters = [Address(x) for x in minters] if minters else None | ||
self.decimal_places = decimal_places | ||
self.total_supply_trackable = total_supply_trackable | ||
|
||
def __eq__(self, other: Currency): | ||
return ( | ||
self.ticker == other.ticker and | ||
self.minters == other.minters and | ||
self.decimal_places == other.decimal_places and | ||
self.total_supply_trackable == other.total_supply_trackable | ||
) | ||
|
||
@classmethod | ||
def NCG(cls): | ||
return cls( | ||
ticker="NCG", | ||
minters=["47d082a115c63e7b58b1532d20e631538eafadde"], | ||
decimal_places=2 | ||
) | ||
|
||
@classmethod | ||
def CRYSTAL(cls): | ||
return cls( | ||
ticker="CRYSTAL", | ||
minters=None, | ||
decimal_places=18 | ||
) | ||
|
||
@property | ||
def plain_value(self) -> Dict[str, Any]: | ||
value = { | ||
"ticker": self.ticker, | ||
"decimalPlaces": chr(self.decimal_places).encode(), | ||
"minters": [x.short_format for x in self.minters] if self.minters else None | ||
} | ||
if self.total_supply_trackable: | ||
value["totalSupplyTrackable"] = True | ||
return value | ||
|
||
@property | ||
def serialized_plain_value(self) -> bytes: | ||
return bencodex.dumps(self.plain_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from decimal import Decimal | ||
from typing import Dict, List, Optional, Any | ||
|
||
import bencodex | ||
|
||
from common.lib9c.models.currency import Currency | ||
|
||
|
||
class FungibleAssetValue: | ||
def __init__(self, currency: Currency, amount: Decimal): | ||
self.currency = currency | ||
self.amount = amount | ||
|
||
def __eq__(self, other: FungibleAssetValue): | ||
return self.currency == other.currency and self.amount == other.amount | ||
|
||
@classmethod | ||
def from_raw_data( | ||
cls, | ||
ticker: str, decimal_places: int, minters: Optional[List[str]] = None, total_supply_trackable: bool = False, | ||
amount: Decimal = Decimal("0") | ||
): | ||
return cls( | ||
Currency(ticker, decimal_places, minters, total_supply_trackable), | ||
amount=amount | ||
) | ||
|
||
@property | ||
def plain_value(self) -> List[Dict[str, Any] | int]: | ||
return [self.currency.plain_value, int(self.amount * max(1, 10 ** self.currency.decimal_places))] | ||
|
||
@property | ||
def serialized_plain_value(self) -> bytes: | ||
return bencodex.dumps(self.plain_value) |
Oops, something went wrong.