Commit 839aefa8 authored by 杨明橙's avatar 杨明橙

Merge remote-tracking branch 'origin/main'

parents c77a9751 dc21a972
...@@ -46,10 +46,10 @@ async def create_pcf( ...@@ -46,10 +46,10 @@ async def create_pcf(
permission_role_collect) permission_role_collect)
delta_volume = create_pcf_bill.volume if create_pcf_bill.bill_type == PCFBillType.sub else -create_pcf_bill.volume delta_volume = create_pcf_bill.volume if create_pcf_bill.bill_type == PCFBillType.sub else -create_pcf_bill.volume
assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(fund_collect, assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(
create_pcf_bill.fund_id, fund_collect=fund_collect,
user.id, fund_id=create_pcf_bill.fund_id,
FundStatus.active) fund_status=FundStatus.active)
assets.setdefault(create_pcf_bill.currency, 0) assets.setdefault(create_pcf_bill.currency, 0)
# 如果是赎回 判断余额是否够 # 如果是赎回 判断余额是否够
assert assets[create_pcf_bill.currency] + delta_volume >= 0, "余额不足" assert assets[create_pcf_bill.currency] + delta_volume >= 0, "余额不足"
...@@ -77,10 +77,10 @@ async def create_exchange( ...@@ -77,10 +77,10 @@ async def create_exchange(
create_exchange_bill.fund_id, create_exchange_bill.fund_id,
user.email, permission_user_collect, user.email, permission_user_collect,
permission_role_collect) permission_role_collect)
assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(fund_collect, assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(
create_exchange_bill.fund_id, fund_collect=fund_collect,
user.id, fund_id=create_exchange_bill.fund_id,
FundStatus.active) fund_status=FundStatus.active)
assets.setdefault(create_exchange_bill.output_currency, 0) assets.setdefault(create_exchange_bill.output_currency, 0)
assets.setdefault(create_exchange_bill.input_currency, 0) assets.setdefault(create_exchange_bill.input_currency, 0)
assert assets[ assert assets[
...@@ -116,10 +116,10 @@ async def create_adjust( ...@@ -116,10 +116,10 @@ async def create_adjust(
user.email, permission_user_collect, user.email, permission_user_collect,
permission_role_collect) permission_role_collect)
assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(fund_collect, assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(
create_adjust_bill.fund_id, fund_collect=fund_collect,
user.id, fund_id=create_adjust_bill.fund_id,
FundStatus.active) fund_status=FundStatus.active)
adjust_assets.setdefault(create_adjust_bill.currency, 0) adjust_assets.setdefault(create_adjust_bill.currency, 0)
adjust_assets.setdefault('fund_share', 0) adjust_assets.setdefault('fund_share', 0)
adjust_assets[create_adjust_bill.currency] += create_adjust_bill.volume adjust_assets[create_adjust_bill.currency] += create_adjust_bill.volume
...@@ -152,11 +152,10 @@ async def create_staking_api( ...@@ -152,11 +152,10 @@ async def create_staking_api(
permission_role_collect) permission_role_collect)
assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(fund_collect, assets, adjust_assets, pending_assets, staking_assets, nodes = await query_fund_assets_and_nodes(fund_collect,
user_id=user.id,
fund_id=create_staking_bill.fund_id, fund_id=create_staking_bill.fund_id,
fund_status=FundStatus.active) fund_status=FundStatus.active)
assert assets.get(create_staking_bill.currency, 0) >= create_staking_bill.volume, '余额不足' 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 assets[create_staking_bill.currency] -= create_staking_bill.volume + create_staking_bill.fee
# 防止增加的币种没有 设置默认值 # 防止增加的币种没有 设置默认值
pending_assets.setdefault(create_staking_bill.currency, 0) pending_assets.setdefault(create_staking_bill.currency, 0)
pending_assets[create_staking_bill.currency] += create_staking_bill.volume pending_assets[create_staking_bill.currency] += create_staking_bill.volume
...@@ -203,7 +202,6 @@ async def update_pcf_bill( ...@@ -203,7 +202,6 @@ async def update_pcf_bill(
response = await update_bill( response = await update_bill(
bill_id=bill_id, bill_id=bill_id,
fund_id=fund_id, fund_id=fund_id,
user_id=user.id,
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
...@@ -234,7 +232,6 @@ async def update_exchange_bill( ...@@ -234,7 +232,6 @@ async def update_exchange_bill(
response = await update_bill( response = await update_bill(
bill_id=bill_id, bill_id=bill_id,
fund_id=fund_id, fund_id=fund_id,
user_id=user.id,
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
...@@ -265,7 +262,6 @@ async def update_adjust_bill( ...@@ -265,7 +262,6 @@ async def update_adjust_bill(
response = await update_bill( response = await update_bill(
bill_id=bill_id, bill_id=bill_id,
fund_id=fund_id, fund_id=fund_id,
user_id=user.id,
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
...@@ -296,7 +292,6 @@ async def update_staking_bill( ...@@ -296,7 +292,6 @@ async def update_staking_bill(
response = await update_bill( response = await update_bill(
bill_id=bill_id, bill_id=bill_id,
fund_id=fund_id, fund_id=fund_id,
user_id=user.id,
update_data=update_item, update_data=update_item,
fund_collect=fund_collect, fund_collect=fund_collect,
bill_collect=bill_collect, bill_collect=bill_collect,
...@@ -326,7 +321,7 @@ async def query_bill( ...@@ -326,7 +321,7 @@ async def query_bill(
user.email, permission_user_collect, user.email, permission_user_collect,
permission_role_collect) permission_role_collect)
query = {"fund_id": fund_id, "user_id": user.id, "bill_type": {'$in': query}} query = {"fund_id": fund_id, "bill_type": {'$in': query}}
if filter_time.start_time and filter_time.end_time: if filter_time.start_time and filter_time.end_time:
query.update({'record_time': filter_time.to_mongodb_query()}) query.update({'record_time': filter_time.to_mongodb_query()})
count = await bill_collect.count_documents(query) count = await bill_collect.count_documents(query)
...@@ -357,6 +352,6 @@ async def query_bill( ...@@ -357,6 +352,6 @@ async def query_bill(
user.email, permission_user_collect, user.email, permission_user_collect,
permission_role_collect) permission_role_collect)
query = {"fund_id": fund_id, "user_id": user.id, "bill_type": bill_type, "id": bill_id} query = {"fund_id": fund_id, "bill_type": bill_type, "id": bill_id}
await bill_collect.delete_one(query) await bill_collect.delete_one(query)
return Response() return Response()
import datetime import datetime
from typing import Optional from typing import Optional
import re
from loguru import logger
import pytz import pytz
from fastapi import APIRouter, Depends, Query, Body from fastapi import APIRouter, Depends, Query, Body
from fastapi.background import BackgroundTasks from fastapi.background import BackgroundTasks
...@@ -83,11 +84,15 @@ async def recalculate_nav( ...@@ -83,11 +84,15 @@ async def recalculate_nav(
background_task: BackgroundTasks, background_task: BackgroundTasks,
start: datetime.datetime = Query(..., title="开始时间"), start: datetime.datetime = Query(..., title="开始时间"),
end: datetime.datetime = Query(None, title="结束时间"), end: datetime.datetime = Query(None, title="结束时间"),
fund_collect: AgnosticCollection = Depends(get_fund_collect),
): ):
fund_data = await fund_collect.find_one({"id": fund_id})
end = end or datetime.datetime.utcnow() end = end or datetime.datetime.utcnow()
delta = end - start delta = end - start
for i in range(delta.days + 1): for i in range(delta.days):
date = start + datetime.timedelta(days=i) settlement_time = re.findall(r"(\d+):00", fund_data["settlement_time"])[0]
date = start + datetime.timedelta(days=i, hours=int(settlement_time))
logger.info(f"[重新计算净值]fund_id={fund_id}, date={date}")
background_task.add_task(calculate_nav, fund_id=fund_id, calc_time=date, update_fund=False) background_task.add_task(calculate_nav, fund_id=fund_id, calc_time=date, update_fund=False)
response = Response(data=True) response = Response(data=True)
return response return response
...@@ -51,6 +51,7 @@ class StakingBill(MyBaseModel): ...@@ -51,6 +51,7 @@ class StakingBill(MyBaseModel):
pub_key: str = Field(..., description='节点key') pub_key: str = Field(..., description='节点key')
remark: str = Field(default="", description="备注") remark: str = Field(default="", description="备注")
record_time: int = Field(default_factory=utc_now_timestamp, description='记录时间') record_time: int = Field(default_factory=utc_now_timestamp, description='记录时间')
fee: float = Field(0, description="手续费")
class AdjustBill(MyBaseModel): class AdjustBill(MyBaseModel):
......
...@@ -69,7 +69,18 @@ class Epoch(BaseModel): ...@@ -69,7 +69,18 @@ class Epoch(BaseModel):
finalized: bool = Field(None, title="结果") finalized: bool = Field(None, title="结果")
class NodeGeneral(BaseModel):
cl: float
el: float
total: float
class Rewards(BaseModel): class Rewards(BaseModel):
cl: str total_rewards: NodeGeneral
el: str income_today: NodeGeneral
total: str income1d: NodeGeneral
income7d: NodeGeneral
income31d: NodeGeneral
# apr7d: NodeGeneral
# apr31d: NodeGeneral
# apr365d: NodeGeneral
...@@ -9,7 +9,7 @@ from pydantic import BaseModel, Field ...@@ -9,7 +9,7 @@ from pydantic import BaseModel, Field
from exception.db import NotFundError from exception.db import NotFundError
from exception.http import RequestHttpException from exception.http import RequestHttpException
from schema.beacon import Validator, ValidatorDeposit, ValidatorBlock, ValidatorIncome, Epoch, Rewards from schema.beacon import Validator, ValidatorDeposit, ValidatorBlock, ValidatorIncome, Epoch, Rewards, NodeGeneral
from tools.http_helper import aio_request from tools.http_helper import aio_request
from tools.time_helper import time_str_to_timestamp from tools.time_helper import time_str_to_timestamp
...@@ -180,9 +180,29 @@ class BeaconChaService: ...@@ -180,9 +180,29 @@ class BeaconChaService:
tree = etree.HTML(html) tree = etree.HTML(html)
total_rewards_re = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span')[0] total_rewards_re = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span')[0]
total_rewards_title = total_rewards_re.get("title") total_rewards_title = total_rewards_re.get("title")
cl = re.findall(r'CL: <span>(\+[\d\.]+ ETH)</span>', total_rewards_title)[0] total_list = re.findall(r'<span>(.\d+\.?\d*) ETH</span>', total_rewards_title)
el = re.findall(r'EL: <span>(\+[\d\.]+ ETH)</span>', total_rewards_title)[0] total_total_rewards_text = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span/b')[0].text
total_re = tree.xpath('/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td/span/b')[0] total_total_rewards = re.findall(r".\d+\.?\d*", total_total_rewards_text)[0]
total = total_re.text # income_today
result = Rewards(cl=cl, el=el, total=total) income_today_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span"
income_today_total_path = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[2]/td/span/span/b"
income_today_re, total_income_today = tree.xpath(income_today_xpath)[0], tree.xpath(income_today_total_path)[0].text
income_today_list = re.findall(r"(.\d+\.?\d*) ETH", income_today_re.get("title"))
total_income_today = re.findall(r".\d+\.?\d*", total_income_today)[0]
# income
total_income1d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[1]/b"
total_income7d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[2]/b"
total_income365d_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span/span[3]/b"
total_income1d = re.findall(r".\d+\.?\d*", tree.xpath(total_income1d_xpath)[0].text)[0]
total_income7d = re.findall(r".\d+\.?\d*", tree.xpath(total_income7d_xpath)[0].text)[0]
total_income31d = re.findall(r".\d+\.?\d*", tree.xpath(total_income365d_xpath)[0].text)[0]
income_xpath = "/html/body/main/div[1]/div[2]/div[2]/div/table/tbody/tr[3]/td/span"
income_list = re.findall(r".\d+\.?\d*", tree.xpath(income_xpath)[0].get("title"))
result = Rewards(
total_rewards=NodeGeneral(cl=float(total_list[0]), el=float(total_list[1]), total=float(total_total_rewards)),
income_today=NodeGeneral(cl=float(income_today_list[0]), el=float(income_today_list[1]), total=float(total_income_today)),
income1d=NodeGeneral(cl=float(income_list[0]), el=float(income_list[3]), total=float(total_income1d)),
income7d=NodeGeneral(cl=float(income_list[1]), el=float(income_list[4]), total=float(total_income7d)),
income31d=NodeGeneral(cl=float(income_list[2]), el=float(income_list[5]), total=float(total_income31d)),
)
return result return result
...@@ -47,10 +47,10 @@ async def create_staking( ...@@ -47,10 +47,10 @@ async def create_staking(
async def update_bill( async def update_bill(
bill_id: str, fund_id: str, user_id: str, update_data: [], fund_collect, bill_collect, bill_id: str, fund_id: str, update_data: [], fund_collect, bill_collect,
res_model: Type[DataT] res_model: Type[DataT]
) -> Response[DataT]: ) -> Response[DataT]:
fund = await fund_collect.find_one({'id': fund_id, 'user_id': user_id}) fund = await fund_collect.find_one({'id': fund_id})
assert fund, NotFundError() assert fund, NotFundError()
db_update_data = update_data.dict(exclude_unset=True) db_update_data = update_data.dict(exclude_unset=True)
db_update_data.update({ db_update_data.update({
......
...@@ -85,7 +85,9 @@ async def calculate_nav(fund_id, calc_time: datetime.datetime = None, beach_serv ...@@ -85,7 +85,9 @@ async def calculate_nav(fund_id, calc_time: datetime.datetime = None, beach_serv
nav[symbol] = volume * price_data[symbol] nav[symbol] = volume * price_data[symbol]
logger.info(f'[资产情况] [{fund_id}] {nav}') logger.info(f'[资产情况] [{fund_id}] {nav}')
bill_collect = get_bill_collect(app.state.mongodb_manager) bill_collect = get_bill_collect(app.state.mongodb_manager)
cursor = bill_collect.find({ cursor = bill_collect.find(
{
"fund_id": fund_id,
"bill_type": {"$in": ["adjust", "sub", "redemption"]} "bill_type": {"$in": ["adjust", "sub", "redemption"]}
}) })
result = await cursor.to_list(length=None) result = await cursor.to_list(length=None)
......
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