Commit 37e4867e authored by 陈涛's avatar 陈涛

重新计算净值增加调整账目计算

parent c1500f8b
......@@ -12,13 +12,13 @@ from model import Response, Page, PageResponse, SortParams, FilterTime
from model.bill import PCFBill, ExchangeBill, AdjustBill, StakingBill
from model.node import BaseNode
from schema.bill import CreatePCFBill, PCFBillType, CreateExchangeBill, CreateAdjustBill, CreateStakingBill, \
UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill, AllBillType
UpdatePCFBill, UpdateExchangeBill, UpdateStakingBill, UpdateAdjustBill, AllBillType, StakingDirection
from schema.fund import FundStatus
from schema.node import BindNode
from service.beacon import BeaconChaService
from service.bill import update_bill
from service.fund import query_fund_assets_and_nodes, update_fund
from service.nav import query_nav_record
from service.nav import query_nav_record, build_nav_bulk_list
from service.permission import check_permission
from tools.jwt_tools import User
......@@ -60,16 +60,13 @@ async def create_pcf(
# # 如果是赎回 判断余额是否够
# assert assets[create_pcf_bill.currency] + delta_volume >= 0, "余额不足"
assets[create_pcf_bill.currency] += delta_volume
nav_record = await query_nav_record(
nav_collect=nav_collect, fund_id=create_pcf_bill.fund_id,
record_time=create_pcf_bill.record_time, settlement_time=fund_data["settlement_time"]
bulk_list = await build_nav_bulk_list(
nav_collect=nav_collect,
fund_id=create_pcf_bill.fund_id,
record_time=create_pcf_bill.record_time,
settlement_time=fund_data["settlement_time"],
asset_changed={create_pcf_bill.currency: delta_volume}
)
bulk_list = []
for nav in nav_record:
nav["assets"].setdefault(create_pcf_bill.currency, 0)
nav["assets"][create_pcf_bill.currency] += delta_volume
bulk_list.append(UpdateOne({"_id": nav["_id"]}, {"$set": {"assets": nav["assets"]}}))
await update_fund(fund_collect, create_pcf_bill.fund_id, assets=assets)
if bulk_list:
await nav_collect.bulk_write(bulk_list)
......@@ -107,17 +104,16 @@ async def create_exchange(
assets[create_exchange_bill.input_currency] -= create_exchange_bill.input_volume
assets[create_exchange_bill.output_currency] += create_exchange_bill.output_volume
nav_record = await query_nav_record(
nav_collect=nav_collect, fund_id=create_exchange_bill.fund_id,
record_time=create_exchange_bill.record_time, settlement_time=fund_data["settlement_time"]
bulk_list = await build_nav_bulk_list(
nav_collect=nav_collect,
fund_id=create_exchange_bill.fund_id,
record_time=create_exchange_bill.record_time,
settlement_time=fund_data["settlement_time"],
asset_changed={
create_exchange_bill.input_currency: -create_exchange_bill.input_volume,
create_exchange_bill.output_currency: create_exchange_bill.output_volume
}
)
bulk_list = []
for nav in nav_record:
nav["assets"].setdefault(create_exchange_bill.output_currency, 0)
nav["assets"].setdefault(create_exchange_bill.input_currency, 0)
nav["assets"][create_exchange_bill.input_currency] -= create_exchange_bill.input_volume
nav["assets"][create_exchange_bill.output_currency] += create_exchange_bill.output_volume
bulk_list.append(UpdateOne({"_id": nav["_id"]}, {"$set": {"assets": nav["assets"]}}))
await update_fund(fund_collect, create_exchange_bill.fund_id, assets=assets)
if bulk_list:
await nav_collect.bulk_write(bulk_list)
......@@ -158,6 +154,20 @@ async def create_adjust(
adjust_assets[create_adjust_bill.currency] += create_adjust_bill.volume
adjust_assets['fund_share'] += create_adjust_bill.fund_share
bulk_list = await build_nav_bulk_list(
nav_collect=nav_collect,
fund_id=create_adjust_bill.fund_id,
record_time=create_adjust_bill.record_time,
settlement_time=fund_data["settlement_time"],
asset_changed={
create_adjust_bill.currency: create_adjust_bill.volume,
"fund_share": create_adjust_bill.fund_share
}
)
await update_fund(fund_collect, create_adjust_bill.fund_id, adjust_assets=adjust_assets)
if bulk_list:
await nav_collect.bulk_write(bulk_list)
adjust_bill = AdjustBill(user_id=user.id, **create_adjust_bill.dict())
await bill_collect.insert_one(adjust_bill.dict())
response = Response[AdjustBill](data=adjust_bill.dict())
......@@ -189,21 +199,28 @@ async def create_staking_api(
fund_status=FundStatus.active
)
assets, pending_assets, nodes = fund_data["assets"], fund_data["pending_assets"], fund_data["nodes"]
assert assets.get(create_staking_bill.currency, 0) >= create_staking_bill.volume + create_staking_bill.fee, '余额不足'
assets[create_staking_bill.currency] -= create_staking_bill.volume + create_staking_bill.fee
# 防止增加的币种没有 设置默认值
pending_assets.setdefault(create_staking_bill.currency, 0)
pending_assets[create_staking_bill.currency] += create_staking_bill.volume
bind_node = BindNode(**create_staking_bill.dict())
node_detail = await beacon_service.get_validator(index_or_pubkey=bind_node.pub_key)
db_data = BaseNode(**bind_node.dict(), index=node_detail.validator_index)
# 如果已经质押过该节点 直接添加数量
if bind_node.pub_key in nodes:
nodes[bind_node.pub_key]['volume'] += db_data.volume
if create_staking_bill.direction == StakingDirection.bind:
assert assets.get(create_staking_bill.currency, 0) >= create_staking_bill.volume + create_staking_bill.fee, '余额不足'
assets[create_staking_bill.currency] -= create_staking_bill.volume + create_staking_bill.fee
# 防止增加的币种没有 设置默认值
pending_assets.setdefault(create_staking_bill.currency, 0)
pending_assets[create_staking_bill.currency] += create_staking_bill.volume
bind_node = BindNode(**create_staking_bill.dict())
node_detail = await beacon_service.get_validator(index_or_pubkey=bind_node.pub_key)
db_data = BaseNode(**bind_node.dict(), index=node_detail.validator_index)
# 如果已经质押过该节点 直接添加数量
if bind_node.pub_key in nodes:
nodes[bind_node.pub_key]['volume'] += db_data.volume
else:
nodes[bind_node.pub_key] = db_data.dict()
else:
nodes[bind_node.pub_key] = db_data.dict()
assets[create_staking_bill.currency] += create_staking_bill.volume - create_staking_bill.fee
# 检查pending_assets里面的值
for key, value in pending_assets.item():
assets.setdefault(key, 0)
assets[key] += value
pending_assets[key] -= value
del nodes[create_staking_bill.pub_key]
await update_fund(fund_collect, create_staking_bill.fund_id, pending_assets=pending_assets, assets=assets,
nodes=nodes)
......
from pydantic import Field
from model import MyBaseModel
from schema.bill import PCFBillType, BillType, StakingBillStatus
from schema.bill import PCFBillType, BillType, StakingBillStatus, StakingDirection
from tools.time_helper import utc_now_timestamp
......@@ -52,6 +52,7 @@ class StakingBill(MyBaseModel):
remark: str = Field(default="", description="备注")
record_time: int = Field(default_factory=utc_now_timestamp, description='记录时间')
fee: float = Field(0, description="手续费")
direction: StakingDirection = Field(StakingDirection.bind, description="方向")
class AdjustBill(MyBaseModel):
......
......@@ -45,6 +45,11 @@ class StakingBillStatus(str, Enum):
error = 'error'
class StakingDirection(str, Enum):
bind = "bind"
un_bind = "un_bind"
# 创建申购赎回记录
class CreatePCFBill(BaseModel):
fund_id: str = Field(..., description='基金id')
......@@ -80,6 +85,7 @@ class CreateStakingBill(BaseModel):
remark: str = Field(default="", description="备注")
record_time: int = Field(default_factory=lambda: utc_now_timestamp(), description='记录时间')
fee: float = Field(0, description="手续费")
direction: StakingDirection = Field(StakingDirection.bind, description="方向")
class CreateAdjustBill(BaseModel):
......
......@@ -51,6 +51,11 @@ async def calculate_nav(fund_id, calc_time: datetime.datetime = None, beach_serv
amount.setdefault(key, 0)
amount[key] += value
# 查询调整账目资产
for key, value in fund_data['adjust_assets'].items():
amount.setdefault(key, 0)
amount[key] += value
# 查询在途资产
for key, value in fund_data['pending_assets'].items():
amount.setdefault(key, 0)
......
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