Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
PyFund
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈涛
PyFund
Commits
7ab12547
Commit
7ab12547
authored
Mar 23, 2023
by
Confusion-ymc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加节点绑定和解绑
parent
114a8541
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
95 additions
and
37 deletions
+95
-37
fund.py
api/fund.py
+3
-2
node.py
api/node.py
+55
-10
db.py
exception/db.py
+4
-0
fund.py
model/fund.py
+18
-12
node.py
model/node.py
+11
-1
jwt_tools.py
tools/jwt_tools.py
+4
-12
No files found.
api/fund.py
View file @
7ab12547
...
...
@@ -24,12 +24,13 @@ async def create(
fund_collect
:
AgnosticCollection
=
Depends
(
get_fund_collect
)
):
if
create_fund
.
fund_type
==
FundType
.
staking
:
create_model
=
StakingFund
(
**
create_fund
.
dict
(),
**
user
.
dict
())
create_model
=
StakingFund
(
**
create_fund
.
dict
(),
nodes
=
[],
**
user
.
db_save
())
response
=
Response
[
StakingFund
](
data
=
create_model
.
dict
())
else
:
create_model
=
NormalFund
(
**
create_fund
.
dict
(),
**
user
.
d
ict
())
create_model
=
NormalFund
(
**
create_fund
.
dict
(),
**
user
.
d
b_save
())
response
=
Response
[
NormalFund
](
data
=
create_model
.
dict
())
insert_data
=
create_model
.
dict
()
await
fund_collect
.
insert_one
(
insert_data
)
return
response
...
...
api/node.py
View file @
7ab12547
import
pytz
from
apscheduler.triggers
import
interval
from
motor.core
import
AgnosticCollection
import
dependencies
import
service.node
from
exception.db
import
ExistDataError
from
model
import
BaseResponse
from
fastapi
import
APIRouter
,
Depends
from
model.fund
import
FundType
from
model.node
import
CreateNode
,
BaseNode
from
tools.jwt_tools
import
User
router
=
APIRouter
()
# @router.post('/',
# response_model=BaseResponse,
# summary='创建节点刷新任务【测试】',
# description='创建节点刷新任务')
# async def create(
# schedular: dependencies.AsyncIOScheduler = Depends(dependencies.get_schedular)
# ):
# schedular.add_job(service.node.refresh_status, trigger=interval.IntervalTrigger(seconds=5, timezone=pytz.UTC),
# misfire_grace_time=10)
# return BaseResponse(data='创建成功')
@
router
.
post
(
'/'
,
response_model
=
BaseResponse
,
summary
=
'创建节点刷新任务【测试】'
,
description
=
'创建节点刷新任务'
)
async
def
create
(
schedular
:
dependencies
.
AsyncIOScheduler
=
Depends
(
dependencies
.
get_schedular
)
summary
=
'绑定节点'
,
description
=
'绑定节点'
)
async
def
subscribe
(
create_node
:
CreateNode
,
user
:
User
=
Depends
(
dependencies
.
get_current_user
),
fund_collect
:
AgnosticCollection
=
Depends
(
dependencies
.
get_fund_collect
)
):
schedular
.
add_job
(
service
.
node
.
refresh_status
,
trigger
=
interval
.
IntervalTrigger
(
seconds
=
5
,
timezone
=
pytz
.
UTC
),
misfire_grace_time
=
10
)
return
BaseResponse
(
data
=
'创建成功'
)
db_data
=
BaseNode
(
**
create_node
.
dict
())
# 限制staking基金才可绑定节点
query
=
{
'id'
:
create_node
.
fund_id
,
'user_id'
:
user
.
id
,
'fund_type'
:
FundType
.
staking
}
res
=
await
fund_collect
.
update_one
(
{
**
query
,
"nodes"
:
{
"$not"
:
{
"$elemMatch"
:
{
"pub_key"
:
create_node
.
pub_key
}}}},
{
"$push"
:
{
"nodes"
:
db_data
.
dict
()}}
)
if
res
.
raw_result
[
'nModified'
]
==
0
:
raise
ExistDataError
(
message
=
'绑定失败,检查节点是否已存在'
)
return
BaseResponse
(
data
=
'绑定成功'
)
@
router
.
delete
(
'/'
,
response_model
=
BaseResponse
,
summary
=
'解绑节点'
,
description
=
'解绑节点'
)
async
def
unsubscribe
(
create_node
:
CreateNode
,
user
:
User
=
Depends
(
dependencies
.
get_current_user
),
fund_collect
:
AgnosticCollection
=
Depends
(
dependencies
.
get_fund_collect
)
):
db_data
=
BaseNode
(
**
create_node
.
dict
())
query
=
{
'id'
:
create_node
.
fund_id
,
'user_id'
:
user
.
id
,
'fund_type'
:
FundType
.
staking
}
res
=
await
fund_collect
.
update_one
(
{
**
query
,
"nodes"
:
{
"$elemMatch"
:
{
"pub_key"
:
create_node
.
pub_key
}}},
{
"$pull"
:
{
"nodes"
:
{
"pub_key"
:
db_data
.
pub_key
}}}
)
if
res
.
raw_result
[
'nModified'
]
==
0
:
raise
ExistDataError
(
message
=
'解绑失败,检查节点是否存在'
)
return
BaseResponse
(
data
=
'解绑成功'
)
exception/db.py
View file @
7ab12547
...
...
@@ -8,3 +8,7 @@ class NotFundError(MyException):
status
=
status
.
HTTP_404_NOT_FOUND
message
=
'未找到数据'
class
ExistDataError
(
MyException
):
status
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
message
=
'数据已存在'
model/fund.py
View file @
7ab12547
...
...
@@ -4,6 +4,7 @@ from typing import List, Optional
from
pydantic
import
BaseModel
,
Field
from
model
import
BaseCreateModel
from
model.asset
import
NormalAsset
from
model.node
import
BaseNode
...
...
@@ -21,28 +22,33 @@ class BaseFundItem(BaseModel):
# 接口传入模型
# 创建
class
CreateFund
(
BaseFundItem
):
nodes
:
List
[
BaseNode
]
=
Field
(
default
=
[],
description
=
'绑定节点'
)
pass
# nodes: List[BaseNode] = Field(default=[], description='绑定节点')
# 更新
class
UpdateFund
(
BaseModel
):
name
:
Optional
[
str
]
=
Field
(
None
,
description
=
'基金名称'
)
fund_type
:
Optional
[
FundType
]
=
Field
(
default
=
None
,
description
=
'基金类型'
)
base_coin
:
Optional
[
str
]
=
Field
(
None
,
description
=
'基准币种'
)
base_nav
:
Optional
[
float
]
=
Field
(
None
,
description
=
'初始净值'
)
settlement_time
:
Optional
[
str
]
=
Field
(
None
,
description
=
'结算时间'
)
class
Config
:
orm_mode
=
True
# 传入数据库类型 / 接口返回类型
class
NormalFund
(
BaseFundItem
,
BaseCreateModel
):
user_id
:
str
user_email
:
str
assets
:
List
[
NormalAsset
]
=
Field
(
default
=
[],
description
=
'持仓'
)
class
StakingFund
(
BaseFundItem
,
BaseCreateModel
):
user_id
:
str
user_email
:
str
nodes
:
List
[
BaseNode
]
class
UpdateFund
(
BaseModel
):
name
:
Optional
[
str
]
=
Field
(
None
,
description
=
'基金名称'
)
fund_type
:
Optional
[
FundType
]
=
Field
(
default
=
None
,
description
=
'基金类型'
)
base_coin
:
Optional
[
str
]
=
Field
(
None
,
description
=
'基准币种'
)
base_nav
:
Optional
[
float
]
=
Field
(
None
,
description
=
'初始净值'
)
settlement_time
:
Optional
[
str
]
=
Field
(
None
,
description
=
'结算时间'
)
class
Config
:
orm_mode
=
True
assets
:
List
[
NormalAsset
]
=
Field
(
default
=
[],
description
=
'持仓'
)
model/node.py
View file @
7ab12547
from
enum
import
Enum
from
pydantic
import
Field
from
pydantic
import
Field
,
BaseModel
from
model
import
BaseCreateModel
...
...
@@ -14,3 +14,13 @@ class NodeStatus(str, Enum):
class
BaseNode
(
BaseCreateModel
):
pub_key
:
str
=
Field
(
...
,
description
=
'绑定的key'
)
status
:
NodeStatus
=
Field
(
default
=
NodeStatus
.
pending
,
description
=
'状态'
)
currency
:
str
=
Field
(
default
=
'ETH'
,
description
=
'节点需要质押的币种'
)
value
:
float
=
Field
(
default
=
32
,
description
=
'节点需要质押的币种数量'
)
# 接口请求模型 创建
class
CreateNode
(
BaseModel
):
pub_key
:
str
=
Field
(
...
,
description
=
'绑定的key'
)
fund_id
:
str
=
Field
(
...
,
description
=
'绑定基金的ID'
)
currency
:
str
=
Field
(
default
=
'ETH'
,
description
=
'节点需要质押的币种'
)
value
:
float
=
Field
(
default
=
32
,
description
=
'节点需要质押的币种数量'
)
tools/jwt_tools.py
View file @
7ab12547
...
...
@@ -32,20 +32,13 @@ class User(object):
self
.
FoundRole
=
None
self
.
scope
=
None
self
.
amr
=
None
for
k
,
v
in
kwargs
.
items
():
if
hasattr
(
self
,
k
):
self
.
__setattr__
(
k
,
v
)
@
property
def
user_id
(
self
):
return
self
.
id
@
property
def
user_email
(
self
):
return
self
.
email
def
dict
(
self
):
return
self
.
__dict__
def
db_save
(
self
):
return
{
'user_id'
:
self
.
id
,
'user_email'
:
self
.
email
}
async
def
get_identify_key
():
...
...
@@ -77,8 +70,7 @@ def get_current_user(credentials: HTTPAuthorizationCredentials = Security(securi
try
:
assert
credentials
.
scheme
==
'Bearer'
payload
=
decode_token
(
token
)
# options={'verify_signature':False}
user
=
User
()
user
.
__dict__
=
payload
user
=
User
(
**
payload
)
if
not
user
.
id
:
raise
TokenError
(
'错误的Token'
)
return
user
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment