Skip to content

Commit

Permalink
Merge pull request #545 from BoostryJP/dev-23.9
Browse files Browse the repository at this point in the history
Release v23.9.0
  • Loading branch information
purplesmoke05 authored Sep 28, 2023
2 parents 495d4d7 + 73d6daf commit bf9b5e9
Show file tree
Hide file tree
Showing 229 changed files with 11,477 additions and 8,162 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jobs:
name: 'Lint check (black)'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: psf/black@stable
unit-test-postgres:
name: 'Unit tests (PostgreSQL)'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: run unit test using postgres
run: docker-compose run ibet-prime-postgres
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.9.1
hooks:
- id: black
language_version: python3.11
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ RUN apt-get update -q \
language-pack-ja-base \
language-pack-ja \
git \
libyaml-cpp-dev
libyaml-cpp-dev \
liblzma-dev

# remove unnessesory package files
RUN apt clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand All @@ -55,7 +56,7 @@ RUN . ~/.bash_profile \

# install poetry
RUN . ~/.bash_profile \
&& python -m pip install poetry==1.4.0
&& python -m pip install poetry==1.5.1
RUN . ~/.bash_profile \
&& poetry config virtualenvs.create false

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ibet-Prime

<p>
<img alt="Version" src="https://img.shields.io/badge/version-23.6-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-23.9-blue.svg?cacheSeconds=2592000" />
<img alt="License: Apache--2.0" src="https://img.shields.io/badge/License-Apache--2.0-yellow.svg" />
</p>

Expand Down
2 changes: 1 addition & 1 deletion README_JA.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ibet-Prime

<p>
<img alt="Version" src="https://img.shields.io/badge/version-23.6-blue.svg?cacheSeconds=2592000" />
<img alt="Version" src="https://img.shields.io/badge/version-23.9-blue.svg?cacheSeconds=2592000" />
<img alt="License: Apache--2.0" src="https://img.shields.io/badge/License-Apache--2.0-yellow.svg" />
</p>

Expand Down
28 changes: 24 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
"""
from datetime import datetime

from fastapi import FastAPI, Request, status
from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import ValidationError
from pydantic_core import ArgsKwargs, ErrorDetails
from starlette.exceptions import HTTPException as StarletteHTTPException

from app.exceptions import *
Expand Down Expand Up @@ -59,7 +60,7 @@
app = FastAPI(
title="ibet Prime",
description="Security token management system for ibet network",
version="23.6.0",
version="23.9.0",
contact={"email": "dev@boostry.co.jp"},
license_info={
"name": "Apache 2.0",
Expand Down Expand Up @@ -118,13 +119,32 @@ async def internal_server_error_handler(request: Request, exc: Exception):
)


def convert_errors(
e: ValidationError | RequestValidationError,
) -> list[ErrorDetails]:
new_errors: list[ErrorDetails] = []
for error in e.errors():
# "url" field which Pydantic V2 adds when validation error occurs is not needed for API response.
# https://docs.pydantic.dev/2.1/errors/errors/
if "url" in error.keys():
error.pop("url", None)

# "input" field generated from GET query model_validator is ArgsKwargs instance.
# This cannot be serialized to json as it is, so nested field should be picked.
# https://docs.pydantic.dev/2.1/errors/errors/
if "input" in error.keys() and isinstance(error["input"], ArgsKwargs):
error["input"] = error["input"].kwargs
new_errors.append(error)
return new_errors


# 422:RequestValidationError
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
meta = {"code": 1, "title": "RequestValidationError"}
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"meta": meta, "detail": exc.errors()}),
content=jsonable_encoder({"meta": meta, "detail": convert_errors(exc)}),
)


Expand All @@ -135,7 +155,7 @@ async def query_validation_exception_handler(request: Request, exc: ValidationEr
meta = {"code": 1, "title": "RequestValidationError"}
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"meta": meta, "detail": exc.errors()}),
content=jsonable_encoder({"meta": meta, "detail": convert_errors(exc)}),
)


Expand Down
10 changes: 6 additions & 4 deletions app/model/blockchain/personal_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from eth_keyfile import decode_keyfile_json
from sqlalchemy import select
from sqlalchemy.orm import Session
from web3.exceptions import TimeExhausted

path = os.path.join(os.path.dirname(__file__), "../")
Expand All @@ -44,13 +46,13 @@
class PersonalInfoContract:
"""PersonalInfo contract model"""

def __init__(self, db, issuer_address: str, contract_address=None):
def __init__(self, db: Session, issuer_address: str, contract_address=None):
self.personal_info_contract = ContractUtils.get_contract(
contract_name="PersonalInfo", contract_address=contract_address
)
self.issuer = (
db.query(Account).filter(Account.issuer_address == issuer_address).first()
)
self.issuer = db.scalars(
select(Account).where(Account.issuer_address == issuer_address).limit(1)
).first()

def get_info(self, account_address: str, default_value=None):
"""Get personal information
Expand Down
100 changes: 20 additions & 80 deletions app/model/blockchain/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from decimal import Decimal
from typing import List

from sqlalchemy import desc
from sqlalchemy import delete, desc, select
from sqlalchemy.exc import IntegrityError as SAIntegrityError
from sqlalchemy.orm import Session
from web3.datastructures import AttributeDict
Expand All @@ -33,23 +33,11 @@
from app.model.blockchain import IbetExchangeInterface
from app.model.blockchain.tx_params.ibet_security_token import (
AdditionalIssueParams as IbetSecurityTokenAdditionalIssueParams,
)
from app.model.blockchain.tx_params.ibet_security_token import (
ApproveTransferParams as IbetSecurityTokenApproveTransfer,
)
from app.model.blockchain.tx_params.ibet_security_token import (
CancelTransferParams as IbetSecurityTokenCancelTransfer,
)
from app.model.blockchain.tx_params.ibet_security_token import (
ForceUnlockParams as IbetSecurityTokenForceUnlockParams,
)
from app.model.blockchain.tx_params.ibet_security_token import (
LockParams as IbetSecurityTokenLockParams,
)
from app.model.blockchain.tx_params.ibet_security_token import (
RedeemParams as IbetSecurityTokenRedeemParams,
)
from app.model.blockchain.tx_params.ibet_security_token import (
TransferParams as IbetSecurityTokenTransferParams,
)
from app.model.blockchain.tx_params.ibet_share import (
Expand All @@ -58,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 @@ -95,12 +77,12 @@ def __init__(

def check_attr_update(self, db_session: Session, base_datetime: datetime):
is_updated = False
_token_attr_update = (
db_session.query(TokenAttrUpdate)
.filter(TokenAttrUpdate.token_address == self.token_address)
_token_attr_update = db_session.scalars(
select(TokenAttrUpdate)
.where(TokenAttrUpdate.token_address == self.token_address)
.order_by(desc(TokenAttrUpdate.id))
.first()
)
.limit(1)
).first()
if (
_token_attr_update is not None
and _token_attr_update.updated_datetime > base_datetime
Expand All @@ -114,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 All @@ -143,9 +107,9 @@ def create_cache(self, db_session: Session):
db_session.merge(token_cache)

def delete_cache(self, db_session: Session):
db_session.query(TokenCache).filter(
TokenCache.token_address == self.token_address
).delete()
db_session.execute(
delete(TokenCache).where(TokenCache.token_address == self.token_address)
)

def get_account_balance(self, account_address: str):
"""Get account balance"""
Expand Down Expand Up @@ -470,11 +434,11 @@ def get(self):

# When using the cache
if TOKEN_CACHE:
token_cache: TokenCache | None = (
db_session.query(TokenCache)
.filter(TokenCache.token_address == self.token_address)
.first()
)
token_cache: TokenCache | None = db_session.scalars(
select(TokenCache)
.where(TokenCache.token_address == self.token_address)
.limit(1)
).first()
if token_cache is not None:
is_updated = self.check_attr_update(
db_session=db_session, base_datetime=token_cache.cached_datetime
Expand Down Expand Up @@ -583,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 @@ -869,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 @@ -924,11 +876,11 @@ def get(self):

# When using the cache
if TOKEN_CACHE:
token_cache: TokenCache | None = (
db_session.query(TokenCache)
.filter(TokenCache.token_address == self.token_address)
.first()
)
token_cache: TokenCache | None = db_session.scalars(
select(TokenCache)
.where(TokenCache.token_address == self.token_address)
.limit(1)
).first()
if token_cache is not None:
is_updated = self.check_attr_update(
db_session=db_session, base_datetime=token_cache.cached_datetime
Expand Down Expand Up @@ -1018,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 @@ -1286,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
Loading

0 comments on commit bf9b5e9

Please sign in to comment.