Skip to content

Commit c036b24

Browse files
committed
Update utils module. Remove base64. Add OrderedDict parsing.
1 parent 7e8dfc1 commit c036b24

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

src/sentinel_sdk/modules/lease.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import sentinel_protobuf.sentinel.lease.v1.querier_pb2_grpc as sentinel_subscription_v2_querier_pb2_grpc
66
import sentinel_protobuf.sentinel.subscription.v2.subscription_pb2 as subscription_pb2
77
import sentinel_protobuf.sentinel.lease.v1.msg_pb2 as msg_pb2
8+
from sentinel_protobuf.sentinel.types.v1.price_pb2 import Price
9+
from sentinel_protobuf.sentinel.types.v1.renewal_pb2 import RenewalPricePolicy
810

911
from sentinel_sdk.querier.querier import Querier
1012
from sentinel_sdk.transactor.transactor import Transactor
11-
from sentinel_sdk.types import PageRequest, TxParams, Price, RenewalPricePolicy
13+
from sentinel_sdk.types import PageRequest, TxParams
1214

1315
class LeaseModule(Querier, Transactor):
1416
def __init__(self, channel: grpc.Channel, account, provider_account, client):
@@ -21,25 +23,25 @@ def __init__(self, channel: grpc.Channel, account, provider_account, client):
2123

2224
def EndLease(self, subscription_id: int, tx_params: TxParams = TxParams()):
2325
msg = msg_pb2.MsgEndLeaseRequest(
24-
frm = self._account.address,
26+
frm = self._provider_account.address,
2527
id = subscription_id,
2628
)
2729

2830
return self.transaction([msg], tx_params)
2931

30-
def RenewLease(self, subscription_id: int, hours: int, max_price: Price = Price(), tx_params: TxParams = TxParams()):
32+
def RenewLease(self, subscription_id: int, hours: int, max_price: Price = Price, tx_params: TxParams = TxParams()):
3133
msg = msg_pb2.MsgRenewLeaseRequest(
32-
frm = self._account.address,
34+
frm = self._provider_account.address,
3335
id = subscription_id,
3436
hours = hours,
3537
max_price = max_price,
3638
)
3739

3840
return self.transaction([msg], tx_params)
3941

40-
def StartLease(self, node: str, hours: int, max_price: Price = Price(), renewal: int = RenewalPricePolicy.RENEWAL_PRICE_POLICY_IF_LESSER_OR_EQUAL, tx_params: TxParams = TxParams()):
42+
def StartLease(self, node: str, hours: int, max_price: Price, renewal: int = RenewalPricePolicy.RENEWAL_PRICE_POLICY_IF_LESSER_OR_EQUAL, tx_params: TxParams = TxParams()):
4143
msg = msg_pb2.MsgStartLeaseRequest(
42-
frm = self._account.address,
44+
frm = self._provider_account.address,
4345
node_address = node,
4446
hours = hours,
4547
max_price = max_price,
@@ -50,7 +52,7 @@ def StartLease(self, node: str, hours: int, max_price: Price = Price(), renewal:
5052

5153
def UpdateLease(self, subscription_id: int, renewal: int = RenewalPricePolicy.RENEWAL_PRICE_POLICY_IF_LESSER_OR_EQUAL, tx_params: TxParams = TxParams()):
5254
msg = msg_pb2.MsgUpdateLeaseRequest(
53-
frm = self._account.address,
55+
frm = self._provider_account.address,
5456
id = subscription_id,
5557
renewal_price_policy = renewal,
5658
)

src/sentinel_sdk/modules/node.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
import sentinel_protobuf.sentinel.node.v3.querier_pb2_grpc as sentinel_node_v3_querier_pb2_grpc
1616
import sentinel_protobuf.sentinel.node.v2.msg_pb2 as msg_pb2
1717
import sentinel_protobuf.sentinel.node.v3.msg_pb2 as msg_pb2_3
18+
from sentinel_protobuf.sentinel.types.v1.price_pb2 import Price
1819

1920
from sentinel_sdk.querier.querier import Querier
2021
from sentinel_sdk.transactor.transactor import Transactor
21-
from sentinel_sdk.types import PageRequest, TxParams, NodeType, Price
22+
from sentinel_sdk.types import PageRequest, TxParams, NodeType
2223

2324
from .wireguard import WgKey
2425

@@ -165,7 +166,7 @@ def SubscribeToNode(self, node_address: str, gigabytes: int = 0, hours: int = 0,
165166
return self.transaction([msg], tx_params)
166167
'''
167168

168-
def SubscribeToNode(self, node_address: str, gigabytes: int = 0, hours: int = 0, price: Price = Price(), tx_params: TxParams = TxParams()):
169+
def SubscribeToNode(self, node_address: str, price: Price, gigabytes: int = 0, hours: int = 0, tx_params: TxParams = TxParams()):
169170
msg = msg_pb2_3.MsgStartSessionRequest(
170171
frm = self._account.address,
171172
gigabytes = gigabytes,

src/sentinel_sdk/modules/subscription.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import sentinel_protobuf.sentinel.subscription.v2.subscription_pb2 as subscription_pb2
77
import sentinel_protobuf.sentinel.subscription.v2.msg_pb2 as msg_pb2
88
import sentinel_protobuf.sentinel.subscription.v3.msg_pb2 as msg_pb2_3
9+
from sentinel_protobuf.sentinel.types.v1.renewal_pb2 import RenewalPricePolicy
910

1011
from sentinel_sdk.querier.querier import Querier
1112
from sentinel_sdk.transactor.transactor import Transactor
12-
from sentinel_sdk.types import PageRequest, TxParams, RenewalPricePolicy
13+
from sentinel_sdk.types import PageRequest, TxParams
1314

1415

1516
class SubscriptionModule(Querier, Transactor):

src/sentinel_sdk/types.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,14 @@ def __str__(self):
1818
class NodeType(Enum):
1919
WIREGUARD = 1
2020
V2RAY = 2
21-
OPENVPN = 3
22-
23-
class RenewalPricePolicy(Enum):
24-
RENEWAL_PRICE_POLICY_UNSPECIFIED = 0
25-
RENEWAL_PRICE_POLICY_IF_LESSER = 1
26-
RENEWAL_PRICE_POLICY_IF_LESSER_OR_EQUAL = 2
27-
RENEWAL_PRICE_POLICY_IF_EQUAL = 3
28-
RENEWAL_PRICE_POLICY_IF_NOT_EQUAL = 4
29-
RENEWAL_PRICE_POLICY_IF_GREATER = 5
30-
RENEWAL_PRICE_POLICY_IF_GREATER_OR_EQUAL = 6
31-
RENEWAL_PRICE_POLICY_ALWAYS = 7
21+
OPENVPN = 3
3222

3323
@dataclass
3424
class TxParams:
3525
denom: str = "udvpn"
3626
fee_amount: int = 31415
3727
gas: float = 0
3828
gas_multiplier: float = 1.5
39-
40-
@dataclass
41-
class Price:
42-
denom: str = "udvpn"
43-
base_value: str = "0"
44-
quote_value: str = "0"
45-
4629

4730
class PageRequest:
4831
"""

src/sentinel_sdk/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import json
22
import base64
3+
from collections import OrderedDict
34

45
# TODO: we want to move this method (?)
56
# In the latest version of mospy the response was converted as dict
67
# from google.protobuf.json_format import MessageToDict
78
# https://github.com/ctrl-Felix/mospy/blob/ea07705b6ec4c76cc355b7dc933fae7c09fd8429/src/mospy/clients/GRPCClient.py#L155
89
def search_attribute(tx_response: dict, event_type: str, attribute_key: str) -> dict:
10+
tx_response = ordered_dict_to_dict(tx_response)
911
for event in tx_response.get("txResponse", tx_response).get("events", []):
1012
if event["type"] == event_type:
1113
for attribute in event["attributes"]:
12-
if base64.b64decode(attribute["key"]) == attribute_key.encode():
13-
return json.loads(base64.b64decode(attribute["value"]).decode("utf-8"))
14+
if attribute["key"] == attribute_key:
15+
return json.loads(attribute["value"])
1416
return None
17+
18+
19+
def ordered_dict_to_dict(od):
20+
return {k: (ordered_dict_to_dict(v) if isinstance(v, OrderedDict) else v) for k, v in od.items()}

0 commit comments

Comments
 (0)