Skip to content

Commit

Permalink
Merge pull request #532 from BoostryJP/feature/#529
Browse files Browse the repository at this point in the history
Add token update operation log for audit
  • Loading branch information
YoshihitoAso authored Aug 7, 2023
2 parents 2aabcc0 + b906d47 commit b8d7122
Show file tree
Hide file tree
Showing 18 changed files with 536 additions and 396 deletions.
50 changes: 1 addition & 49 deletions app/model/blockchain/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@
from app.model.blockchain.tx_params.ibet_straight_bond import (
UpdateParams as IbetStraightBondUpdateParams,
)
from app.model.db import (
TokenAttrUpdate,
TokenCache,
TokenType,
UpdateToken,
UpdateTokenTrigger,
)
from app.model.db import TokenAttrUpdate, TokenCache
from app.utils.contract_utils import ContractUtils
from app.utils.web3_utils import Web3Wrapper
from config import CHAIN_ID, TOKEN_CACHE, TOKEN_CACHE_TTL, TX_GAS_LIMIT, ZERO_ADDRESS
Expand Down Expand Up @@ -102,24 +96,6 @@ def record_attr_update(self, db_session: Session):
_token_attr_update.updated_datetime = datetime.utcnow()
db_session.add(_token_attr_update)

def create_history(
self,
db_session: Session,
original_contents: dict,
modified_contents: dict,
token_type: str,
trigger: UpdateTokenTrigger,
):
update_token = UpdateToken()
update_token.token_address = self.token_address
update_token.issuer_address = self.issuer_address
update_token.type = token_type
update_token.arguments = modified_contents
update_token.original_contents = original_contents
update_token.status = 1 # succeeded
update_token.trigger = trigger
db_session.add(update_token)

def create_cache(self, db_session: Session):
token_cache = TokenCache()
token_cache.token_address = self.token_address
Expand Down Expand Up @@ -571,11 +547,6 @@ def update(
self, data: IbetStraightBondUpdateParams, tx_from: str, private_key: str
):
"""Update token"""
if data.dict(exclude_none=True) == {}:
return

original_contents = self.get().__dict__

contract = ContractUtils.get_contract(
contract_name=self.contract_name, contract_address=self.token_address
)
Expand Down Expand Up @@ -857,13 +828,6 @@ def update(
db_session = Session(autocommit=False, autoflush=True, bind=engine)
try:
self.record_attr_update(db_session)
self.create_history(
db_session,
original_contents=original_contents,
modified_contents=data.dict(exclude_none=True),
token_type=TokenType.IBET_STRAIGHT_BOND.value,
trigger=UpdateTokenTrigger.UPDATE,
)
self.delete_cache(db_session)
db_session.commit()
except Exception as err:
Expand Down Expand Up @@ -1006,11 +970,6 @@ def get(self):

def update(self, data: IbetShareUpdateParams, tx_from: str, private_key: str):
"""Update token"""
if data.dict(exclude_none=True) == {}:
return

original_contents = self.get().__dict__

contract = ContractUtils.get_contract(
contract_name=self.contract_name, contract_address=self.token_address
)
Expand Down Expand Up @@ -1274,13 +1233,6 @@ def update(self, data: IbetShareUpdateParams, tx_from: str, private_key: str):
db_session = Session(autocommit=False, autoflush=True, bind=engine)
try:
self.record_attr_update(db_session)
self.create_history(
db_session,
original_contents=original_contents,
modified_contents=data.dict(exclude_none=True),
token_type=TokenType.IBET_SHARE.value,
trigger=UpdateTokenTrigger.UPDATE,
)
self.delete_cache(db_session)
db_session.commit()
except Exception as err:
Expand Down
4 changes: 4 additions & 0 deletions app/model/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
from .scheduled_events import ScheduledEvents, ScheduledEventType
from .token import Token, TokenAttrUpdate, TokenCache, TokenType
from .token_holders import TokenHolder, TokenHolderBatchStatus, TokenHoldersList
from .token_update_operation_log import (
TokenUpdateOperationCategory,
TokenUpdateOperationLog,
)
from .transfer_appoval_history import (
TransferApprovalHistory,
TransferApprovalOperationType,
Expand Down
51 changes: 51 additions & 0 deletions app/model/db/token_update_operation_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Copyright BOOSTRY Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
"""
from enum import StrEnum

from sqlalchemy import JSON, BigInteger, String
from sqlalchemy.orm import Mapped, mapped_column

from app.model.db import Base


class TokenUpdateOperationLog(Base):
"""Token Update Operation Log"""

__tablename__ = "token_update_operation_log"

id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
# token address
token_address: Mapped[str] = mapped_column(String(42), index=True, nullable=False)
# issuer address
issuer_address: Mapped[str] = mapped_column(String(42), nullable=False)
# token type(TokenType)
type: Mapped[str] = mapped_column(String(40), nullable=False)
# arguments
arguments: Mapped[dict] = mapped_column(JSON, nullable=False)
# original contents
original_contents: Mapped[dict | None] = mapped_column(JSON, nullable=True)
# operation category(TokenUpdateOperationCategory)
operation_category: Mapped[str] = mapped_column(String(40), nullable=False)


class TokenUpdateOperationCategory(StrEnum):
"""Operation category of update token"""

ISSUE = "Issue"
UPDATE = "Update"
5 changes: 1 addition & 4 deletions app/model/db/update_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ class UpdateToken(Base):
# token address
token_address: Mapped[str | None] = mapped_column(String(42), index=True)
# issuer address
issuer_address: Mapped[str] = mapped_column(String(42), nullable=True)
issuer_address: Mapped[str | None] = mapped_column(String(42), nullable=True)
# token type
type: Mapped[str] = mapped_column(String(40), nullable=False)
# arguments
arguments: Mapped[dict] = mapped_column(JSON, nullable=False)
# original contents
original_contents: Mapped[dict | None] = mapped_column(JSON, nullable=True)
# processing status (pending:0, succeeded:1, failed:2)
status: Mapped[int] = mapped_column(Integer, nullable=False)
# update trigger
Expand All @@ -50,4 +48,3 @@ class UpdateTokenTrigger(StrEnum):
"""Trigger of update token"""

ISSUE = "Issue"
UPDATE = "Update"
8 changes: 4 additions & 4 deletions app/model/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@
ListAllTokenLockEventsQuery,
ListAllTokenLockEventsResponse,
ListAllTokenLockEventsSortItem,
ListTokenHistoryQuery,
ListTokenHistoryResponse,
ListTokenHistorySortItem,
ListTokenOperationLogHistoryQuery,
ListTokenOperationLogHistoryResponse,
TokenAddressResponse,
TokenHistoryResponse,
UpdateTokenTrigger,
TokenUpdateOperationCategory,
)
from .token_holders import (
CreateTokenHoldersListRequest,
Expand Down
18 changes: 9 additions & 9 deletions app/model/schema/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ class ListAllTokenLockEventsQuery:
)


class UpdateTokenTrigger(StrEnum):
"""Trigger of update token"""
class TokenUpdateOperationCategory(StrEnum):
"""Operation category of update token"""

ISSUE = "Issue"
UPDATE = "Update"
Expand All @@ -393,15 +393,15 @@ class ListTokenHistorySortItem(StrEnum):
"""Sort item of token history"""

created = "created"
trigger = "trigger"
operation_category = "operation_category"


@dataclass
class ListTokenHistoryQuery:
class ListTokenOperationLogHistoryQuery:
modified_contents: Optional[str] = Query(
default=None, description="Modified contents query"
)
trigger: Optional[UpdateTokenTrigger] = Query(
operation_category: Optional[TokenUpdateOperationCategory] = Query(
default=None, description="Trigger of change"
)
created_from: Optional[datetime] = Query(
Expand Down Expand Up @@ -490,18 +490,18 @@ class IbetShareResponse(BaseModel):
memo: str


class TokenHistoryResponse(BaseModel):
class TokenOperationLogResponse(BaseModel):
original_contents: dict | None = Field(
default=None, nullable=True, description="original attributes before update"
)
modified_contents: dict = Field(..., description="update attributes")
trigger: UpdateTokenTrigger
operation_category: TokenUpdateOperationCategory
created: datetime


class ListTokenHistoryResponse(BaseModel):
class ListTokenOperationLogHistoryResponse(BaseModel):
result_set: ResultSet
history: list[TokenHistoryResponse] = Field(
history: list[TokenOperationLogResponse] = Field(
default=[], description="token update histories"
)

Expand Down
Loading

0 comments on commit b8d7122

Please sign in to comment.