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

feat: Separate the dataclass of the contract from the request Schema of the API #459

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions app/model/blockchain/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
TX_GAS_LIMIT
)
from app.utils.contract_utils import ContractUtils
from app.model.schema import IbetSecurityTokenEscrowApproveTransfer
from app.model.blockchain.tx_params.ibet_security_token_escrow import ApproveTransferParams
from app.exceptions import SendTransactionError, ContractRevertError


Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(self, contract_address: str):
super().__init__(contract_address=contract_address, contract_name="IbetSecurityTokenEscrow")

def approve_transfer(self,
data: IbetSecurityTokenEscrowApproveTransfer,
data: ApproveTransferParams,
tx_from: str,
private_key: str):
"""Approve Transfer"""
Expand Down
53 changes: 29 additions & 24 deletions app/model/blockchain/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,27 @@
ZERO_ADDRESS
)
from app.database import engine
from app.model.blockchain import IbetExchangeInterface
from app.model.blockchain.tx_params.ibet_security_token import (
ApproveTransferParams as IbetSecurityTokenApproveTransfer,
CancelTransferParams as IbetSecurityTokenCancelTransfer
)
from app.model.blockchain.tx_params.ibet_share import (
UpdateParams as IbetShareUpdateParams,
TransferParams as IbetShareTransferParams,
AdditionalIssueParams as IbetShareAdditionalIssueParams,
RedeemParams as IbetShareRedeemParams
)
from app.model.blockchain.tx_params.ibet_straight_bond import (
UpdateParams as IbetStraightBondUpdateParams,
TransferParams as IbetStraightBondTransferParams,
AdditionalIssueParams as IbetStraightBondAdditionalIssueParams,
RedeemParams as IbetStraightBondRedeemParams
)
from app.model.db import (
TokenAttrUpdate,
TokenCache
)
from app.model.schema import (
IbetStraightBondUpdate,
IbetStraightBondTransfer,
IbetStraightBondAdditionalIssue,
IbetStraightBondRedeem,
IbetShareUpdate,
IbetShareTransfer,
IbetShareAdditionalIssue,
IbetShareRedeem,
IbetSecurityTokenApproveTransfer,
IbetSecurityTokenCancelTransfer
)
from app.model.blockchain import IbetExchangeInterface
from app.exceptions import (
SendTransactionError,
ContractRevertError
Expand Down Expand Up @@ -384,7 +388,7 @@ def get(contract_address: str):

@staticmethod
def update(contract_address: str,
data: IbetStraightBondUpdate,
data: IbetStraightBondUpdateParams,
tx_from: str,
private_key: str):
"""Update token"""
Expand Down Expand Up @@ -664,14 +668,15 @@ def update(contract_address: str,
db_session.close()

@staticmethod
def transfer(data: IbetStraightBondTransfer,
def transfer(contract_address: str,
data: IbetStraightBondTransferParams,
tx_from: str,
private_key: str):
"""Transfer ownership"""
try:
bond_contract = ContractUtils.get_contract(
contract_name="IbetStraightBond",
contract_address=data.token_address
contract_address=contract_address
)
_from = data.from_address
_to = data.to_address
Expand Down Expand Up @@ -699,7 +704,7 @@ def transfer(data: IbetStraightBondTransfer,

@staticmethod
def additional_issue(contract_address: str,
data: IbetStraightBondAdditionalIssue,
data: IbetStraightBondAdditionalIssueParams,
tx_from: str,
private_key: str):
"""Additional issue"""
Expand Down Expand Up @@ -750,7 +755,7 @@ def additional_issue(contract_address: str,

@staticmethod
def redeem(contract_address: str,
data: IbetStraightBondRedeem,
data: IbetStraightBondRedeemParams,
tx_from: str,
private_key: str):
"""Redeem a token"""
Expand Down Expand Up @@ -950,7 +955,7 @@ def get(contract_address: str):

@staticmethod
def update(contract_address: str,
data: IbetShareUpdate,
data: IbetShareUpdateParams,
tx_from: str,
private_key: str):
"""Update token"""
Expand Down Expand Up @@ -1211,16 +1216,16 @@ def update(contract_address: str,
finally:
db_session.close()


@staticmethod
def transfer(data: IbetShareTransfer,
def transfer(contract_address: str,
data: IbetShareTransferParams,
tx_from: str,
private_key: str):
"""Transfer ownership"""
try:
share_contract = ContractUtils.get_contract(
contract_name="IbetShare",
contract_address=data.token_address
contract_address=contract_address
)
_from = data.from_address
_to = data.to_address
Expand Down Expand Up @@ -1248,7 +1253,7 @@ def transfer(data: IbetShareTransfer,

@staticmethod
def additional_issue(contract_address: str,
data: IbetShareAdditionalIssue,
data: IbetShareAdditionalIssueParams,
tx_from: str,
private_key: str):
"""Additional issue"""
Expand Down Expand Up @@ -1299,7 +1304,7 @@ def additional_issue(contract_address: str,

@staticmethod
def redeem(contract_address: str,
data: IbetShareRedeem,
data: IbetShareRedeemParams,
tx_from: str,
private_key: str):
"""Redeem a token"""
Expand Down
73 changes: 73 additions & 0 deletions app/model/blockchain/tx_params/ibet_security_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
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 pydantic import (
validator,
BaseModel
)
from web3 import Web3


class TransferParams(BaseModel):
from_address: str
to_address: str
amount: int

@validator("from_address")
def from_address_is_valid_address(cls, v):
if not Web3.isAddress(v):
raise ValueError("from_address is not a valid address")
return v

@validator("to_address")
def to_address_is_valid_address(cls, v):
if not Web3.isAddress(v):
raise ValueError("to_address is not a valid address")
return v


class AdditionalIssueParams(BaseModel):
account_address: str
amount: int

@validator("account_address")
def account_address_is_valid_address(cls, v):
if not Web3.isAddress(v):
raise ValueError("account_address is not a valid address")
return v


class RedeemParams(BaseModel):
account_address: str
amount: int

@validator("account_address")
def account_address_is_valid_address(cls, v):
if not Web3.isAddress(v):
raise ValueError("account_address is not a valid address")
return v


class ApproveTransferParams(BaseModel):
application_id: int
data: str


class CancelTransferParams(BaseModel):
application_id: int
data: str
24 changes: 24 additions & 0 deletions app/model/blockchain/tx_params/ibet_security_token_escrow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
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 pydantic import BaseModel


class ApproveTransferParams(BaseModel):
escrow_id: int
data: str
95 changes: 95 additions & 0 deletions app/model/blockchain/tx_params/ibet_share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
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 typing import (
Optional
)
import math

from pydantic import (
validator,
BaseModel
)
from web3 import Web3

from .ibet_security_token import (
TransferParams as IbetSecurityTokenTransferParams,
AdditionalIssueParams as IbetSecurityTokenAdditionalIssueParams,
RedeemParams as IbetSecurityTokenRedeemParams,
ApproveTransferParams as IbetSecurityTokenApproveTransferParams,
CancelTransferParams as IbetSecurityTokenCancelTransferParams
)


class UpdateParams(BaseModel):
cancellation_date: Optional[str]
dividend_record_date: Optional[str]
dividend_payment_date: Optional[str]
dividends: Optional[float]
tradable_exchange_contract_address: Optional[str]
personal_info_contract_address: Optional[str]
transferable: Optional[bool]
status: Optional[bool]
is_offering: Optional[bool]
contact_information: Optional[str]
privacy_policy: Optional[str]
transfer_approval_required: Optional[bool]
principal_value: Optional[int]
is_canceled: Optional[bool]
memo: Optional[str]

@validator("dividends")
def dividends_13_decimal_places(cls, v):
if v is not None:
float_data = float(v * 10 ** 13)
int_data = int(v * 10 ** 13)
if not math.isclose(int_data, float_data):
raise ValueError("dividends must be rounded to 13 decimal places")
return v

@validator("tradable_exchange_contract_address")
def tradable_exchange_contract_address_is_valid_address(cls, v):
if v is not None and not Web3.isAddress(v):
raise ValueError("tradable_exchange_contract_address is not a valid address")
return v

@validator("personal_info_contract_address")
def personal_info_contract_address_is_valid_address(cls, v):
if v is not None and not Web3.isAddress(v):
raise ValueError("personal_info_contract_address is not a valid address")
return v


class TransferParams(IbetSecurityTokenTransferParams):
pass


class AdditionalIssueParams(IbetSecurityTokenAdditionalIssueParams):
pass


class RedeemParams(IbetSecurityTokenRedeemParams):
pass


class ApproveTransferParams(IbetSecurityTokenApproveTransferParams):
pass


class CancelTransferParams(IbetSecurityTokenCancelTransferParams):
pass
Loading