Skip to content

Commit

Permalink
l3staking grpc server development
Browse files Browse the repository at this point in the history
  • Loading branch information
guoshijiang committed Mar 27, 2024
1 parent 50cc635 commit 2da6121
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 0 deletions.
45 changes: 45 additions & 0 deletions l3staking/l3staking_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# encoding=utf-8

import pytz
from l3staking.models import (
Node
)
from services.savour_rpc import l3staking_pb2_grpc, l3staking_pb2
from django.conf import settings

tz = pytz.timezone(settings.TIME_ZONE)


class L3StakingServer(l3staking_pb2_grpc.L3StakingServiceServicer):
def updateStakingNodeIncome(self, request, context) -> l3staking_pb2.StakingNodeRep:
address = str(request.address)
chain_id = str(request.chain_id)
strategy = str(request.strategy)
eth_income = str(request.eth_income)
eth_income_rate = str(request.eth_income_rate)
dp_income = str(request.dp_income)
dp_income_rate = str(request.dp_income_rate)
eth_evil = str(request.eth_evil)
eth_evil_rate = str(request.eth_evil_rate)
dp_evil = str(request.dp_evil)
dp_evil_rate = str(request.dp_evil_rate)
tvl = str(request.tvl)
Node.objects.filter(
address=address,
chain__chain_id=chain_id,
strategy__address=strategy
).update(
eth_income=eth_income,
eth_income_rate=eth_income_rate,
dp_income=dp_income,
dp_income_rate=dp_income_rate,
eth_evil=eth_evil,
eth_evil_rate=eth_evil_rate,
dp_evil=dp_evil,
dp_evil_rate=dp_evil_rate,
tvl=tvl
)
return l3staking_pb2.StakingNodeRep(
code="200",
msg="submit stake node income success",
)
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions l3staking/management/commands/l3staking_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#encoding=utf-8

import grpc
from django.core.management.base import BaseCommand
from concurrent import futures
from services.savour_rpc import l3staking_pb2_grpc
from l3staking.l3staking_server import L3StakingServer


class Command(BaseCommand):
def handle(self, *args, **options):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
l3staking_pb2_grpc.add_L3StakingServiceServicer_to_server(
L3StakingServer(),
server
)
server.add_insecure_port('[::]:50252')
server.start()
print("l3 staking rpc server start")
server.wait_for_termination()
18 changes: 18 additions & 0 deletions l3staking/migrations/0003_stakingstrategy_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.1 on 2024-03-27 13:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('l3staking', '0002_stakingstrategy_chain'),
]

operations = [
migrations.AddField(
model_name='stakingstrategy',
name='address',
field=models.CharField(default='', max_length=100, verbose_name='策略地址'),
),
]
6 changes: 6 additions & 0 deletions l3staking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class StakingStrategy(BaseModel):
unique=False,
verbose_name='质押模块名称'
)
address = models.CharField(
default="",
max_length=100,
unique=False,
verbose_name='策略地址'
)

class Meta:
verbose_name = 'StakingStrategy'
Expand Down
40 changes: 40 additions & 0 deletions services/savour_rpc/l3staking_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by the Protocol Buffers compiler. DO NOT EDIT!
# source: savour_rpc/l3staking.proto
# plugin: grpclib.plugin.main
import abc
import typing

import grpclib.const
import grpclib.client
if typing.TYPE_CHECKING:
import grpclib.server

from services.savour_rpc import l3staking_pb2


class L3StakingServiceBase(abc.ABC):

@abc.abstractmethod
async def updateStakingNodeIncome(self, stream: 'grpclib.server.Stream[l3staking_pb2.StakingNodeReq, l3staking_pb2.StakingNodeRep]') -> None:
pass

def __mapping__(self) -> typing.Dict[str, grpclib.const.Handler]:
return {
'/services.l3staking.L3StakingService/updateStakingNodeIncome': grpclib.const.Handler(
self.updateStakingNodeIncome,
grpclib.const.Cardinality.UNARY_UNARY,
l3staking_pb2.StakingNodeReq,
l3staking_pb2.StakingNodeRep,
),
}


class L3StakingServiceStub:

def __init__(self, channel: grpclib.client.Channel) -> None:
self.updateStakingNodeIncome = grpclib.client.UnaryUnaryMethod(
channel,
'/services.l3staking.L3StakingService/updateStakingNodeIncome',
l3staking_pb2.StakingNodeReq,
l3staking_pb2.StakingNodeRep,
)
228 changes: 228 additions & 0 deletions services/savour_rpc/l3staking_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2da6121

Please sign in to comment.