Commit c7f245da authored by confusion's avatar confusion

添加 增加质押记录接口

parent 76336ff9
......@@ -8,8 +8,9 @@ from dependencies import get_current_user, get_fund_collect, get_bill_collect
from exception.db import NotFundError
from model import Response, Page, PageResponse, SortParams, FilterTime
from model.bill import PCFBill, ExchangeBill, BillType, CreatePCFBill, CreateExchangeBill, StakingBill, \
AdjustBill, CreateAdjustBill, UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill
from service.bill import update_bill
AdjustBill, CreateAdjustBill, UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill, \
CreateStakingBill
from service.bill import update_bill, create_staking
from tools.jwt_tools import User
router = APIRouter()
......@@ -18,7 +19,7 @@ router = APIRouter()
@router.post('/pcf/',
response_model=Response[PCFBill],
tags=['新增'],
summary='添加申购赎回账目',
summary='添加[申购/赎回]账目',
description='添加申购赎回账目')
async def create_pcf(
item: CreatePCFBill,
......@@ -111,6 +112,22 @@ async def create_adjust(
return response
@router.post('/staking/',
response_model=Response[StakingBill],
tags=['新增'],
summary='添加质押账目',
description='添加质押账目')
async def create_staking_api(
item: CreateStakingBill,
user: User = Depends(get_current_user),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
bill_collect: AgnosticCollection = Depends(get_bill_collect),
):
staking_bill = await create_staking(item, user.id, fund_collect, bill_collect)
response = Response[StakingBill](data=staking_bill.dict())
return response
# @router.get('/exchange/{fund_id}/',
# response_model=PageResponse[ExchangeBill],
# summary='查询置换记录',
......@@ -221,7 +238,7 @@ async def update_exchange_bill(
@router.put('/staking/{fund_id}/',
tags=['更新'],
response_model=Response[StakingBill],
summary='更新申购赎回调整记录',
summary='更新质押记录',
description='')
async def update_staking_bill(
bill_id: str,
......
......@@ -69,10 +69,10 @@ class ExchangeBill(CreateExchangeBill, BaseCreateModel):
bill_type: BillType = Field(default=BillType.exchange, description='账目类型')
class CreateStaking(BaseModel):
class CreateStakingBill(BaseModel):
fund_id: str = Field(None, description='基金id')
currency: str = Field(default="ETH", description='币种')
volume: float = Field(..., description='数量')
volume: float = Field(default=32, description='数量')
record_time: str = Field(default_factory=lambda: str(datetime.datetime.utcnow().date()), description='记录时间')
remark: str = Field(default="", description="备注")
......
......@@ -6,27 +6,27 @@ from pymongo import ReturnDocument
from exception.db import NotFundError
from model import Response
from model.bill import CreateStaking, StakingBill
from model.bill import CreateStakingBill, StakingBill
DataT = TypeVar('DataT')
async def create_staking(
item: CreateStaking,
item: CreateStakingBill,
user_id,
fund_collect: AgnosticCollection,
bill_collect: AgnosticCollection,
):
query = {"id": item.fund_id, "user_id": user_id}
fund = await fund_collect.find_one(query)
assert fund, NotFundError()
filter_asset = list(filter(lambda x: x["currency"] == item.currency, fund["assets"]))
if filter_asset:
update = {"$inc": {"assets.$.volume": -item.volume}}
await fund_collect.update_one({**query, "assets.currency": item.currency}, update)
else:
update = {"$push": {"assets": {"currency": item.currency, "volume": -item.volume}}}
await fund_collect.update_one(query, update)
# query = {"id": item.fund_id, "user_id": user_id}
# fund = await fund_collect.find_one(query)
# assert fund, NotFundError()
# filter_asset = list(filter(lambda x: x["currency"] == item.currency, fund["assets"]))
# if filter_asset:
# update = {"$inc": {"assets.$.volume": -item.volume}}
# await fund_collect.update_one({**query, "assets.currency": item.currency}, update)
# else:
# update = {"$push": {"assets": {"currency": item.currency, "volume": -item.volume}}}
# await fund_collect.update_one(query, update)
staking_bill = StakingBill(user_id=user_id, **item.dict())
await bill_collect.insert_one(staking_bill.dict())
return staking_bill
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment