Skip to content

Commit 62934e7

Browse files
committed
fix: add makefile, add linter, apply linter,
1 parent fb11a01 commit 62934e7

38 files changed

+1228
-925
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lint:
2+
@ruff check . --fix
3+
@ruff format .
4+
.PHONY: lint

examples/calculator.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from snet import sdk
22

3-
config = sdk.config.Config(private_key="YOUR_PRIVATE_KEY",
4-
eth_rpc_endpoint=f"https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
5-
concurrency=False,
6-
force_update=False)
7-
8-
operators = {
9-
"+": "add",
10-
"-": "sub",
11-
"*": "mul",
12-
"/": "div"
13-
}
3+
config = sdk.config.Config(
4+
private_key="YOUR_PRIVATE_KEY",
5+
eth_rpc_endpoint="https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
6+
concurrency=False,
7+
force_update=False,
8+
)
9+
10+
operators = {"+": "add", "-": "sub", "*": "mul", "/": "div"}
1411

1512
snet_sdk = sdk.SnetSDK(config)
16-
calc_client = snet_sdk.create_service_client(org_id="26072b8b6a0e448180f8c0e702ab6d2f",
17-
service_id="Exampleservice", group_name="default_group")
13+
calc_client = snet_sdk.create_service_client(
14+
org_id="26072b8b6a0e448180f8c0e702ab6d2f",
15+
service_id="Exampleservice",
16+
group_name="default_group",
17+
)
1818

1919

2020
def parse_expression(expression):
@@ -23,12 +23,16 @@ def parse_expression(expression):
2323
raise Exception(f"Invalid expression '{expression}'. Three items required.")
2424

2525
if elements[1] not in ["+", "-", "*", "/"]:
26-
raise Exception(f"Invalid expression '{expression}'. Operation must be '+' or '-' or '*' or '/'.")
26+
raise Exception(
27+
f"Invalid expression '{expression}'. Operation must be '+' or '-' or '*' or '/'."
28+
)
2729
try:
2830
a = float(elements[0])
2931
b = float(elements[2])
3032
except ValueError:
31-
raise Exception(f"Invalid expression '{expression}'. Operands must be integers or floating point numbers.")
33+
raise Exception(
34+
f"Invalid expression '{expression}'. Operands must be integers or floating point numbers."
35+
)
3236
op = elements[1]
3337

3438
return a, b, op
@@ -54,4 +58,3 @@ def main():
5458

5559
if __name__ == "__main__":
5660
main()
57-

examples/console_app.py

Lines changed: 131 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
transactions and service calls. So, to run the application, you will need to change the values in 'config'.
77
"""
88

9-
109
from snet import sdk
1110

1211

@@ -17,7 +16,7 @@ def list_organizations():
1716
The list is got from the MPE contract using 'get_organization_list'.
1817
"""
1918
print("Organizations:")
20-
print(*map(lambda x: '\t' + x, snet_sdk.get_organization_list()), sep="\n")
19+
print(*map(lambda x: "\t" + x, snet_sdk.get_organization_list()), sep="\n")
2120

2221

2322
def list_services_for_org():
@@ -28,7 +27,10 @@ def list_services_for_org():
2827
"""
2928
org_id = input("Enter organization id: ").strip()
3029
print("Services:")
31-
print(*map(lambda x: '\t' + x, snet_sdk.get_services_list(org_id=org_id)), sep="\n")
30+
print(
31+
*map(lambda x: "\t" + x, snet_sdk.get_services_list(org_id=org_id)),
32+
sep="\n",
33+
)
3234

3335

3436
def create_service_client():
@@ -41,7 +43,9 @@ def create_service_client():
4143
service_id = input("Enter service id: ").strip()
4244
group_name = input("Enter payment group name: ").strip()
4345

44-
service = snet_sdk.create_service_client(org_id=org_id, service_id=service_id, group_name=group_name)
46+
service = snet_sdk.create_service_client(
47+
org_id=org_id, service_id=service_id, group_name=group_name
48+
)
4549
initialized_services.append(service)
4650

4751
global active_service
@@ -57,7 +61,7 @@ def commands_help():
5761
global active_commands
5862
print("Available commands:")
5963
for command in active_commands.items():
60-
print(f'\t{command[0]} - {command[1][1]}')
64+
print(f"\t{command[0]} - {command[1][1]}")
6165

6266

6367
def list_initialized_services():
@@ -94,8 +98,10 @@ def call():
9498
"""
9599
global active_service
96100
if active_service is None:
97-
print("No initialized services!\n"
98-
"Please enter 'service' to go to the service menu and then enter 'add' to add a service.")
101+
print(
102+
"No initialized services!\n"
103+
"Please enter 'service' to go to the service menu and then enter 'add' to add a service."
104+
)
99105
return None
100106

101107
method_name = input("Enter method name: ")
@@ -135,8 +141,10 @@ def print_service_info():
135141
"""
136142
global active_service
137143
if active_service is None:
138-
print("No initialized services!\n"
139-
"Please enter 'service' to go to the service menu and then enter 'add' to add a service.")
144+
print(
145+
"No initialized services!\n"
146+
"Please enter 'service' to go to the service menu and then enter 'add' to add a service."
147+
)
140148
return None
141149
print(active_service.get_services_and_messages_info_as_pretty_string())
142150

@@ -146,7 +154,9 @@ def balance():
146154
The function, which is called when the user enters the command 'balance' in the main menu.
147155
Prints the balances of FET and MPE. It gets the balances using 'balance_of' and 'escrow_balance'.
148156
"""
149-
account_balance = snet_sdk.account.token_contract.functions.balanceOf(snet_sdk.account.address).call()
157+
account_balance = snet_sdk.account.token_contract.functions.balanceOf(
158+
snet_sdk.account.address
159+
).call()
150160
escrow_balance = snet_sdk.account.escrow_balance()
151161

152162
print(f"FET balance: {account_balance}")
@@ -168,8 +178,10 @@ def block_number():
168178
Prints the current block number. It gets the block number using 'get_current_block_number'.
169179
"""
170180
if active_service is None:
171-
print("No initialized services!\n"
172-
"Please enter 'service' to go to the service menu and then enter 'add' to add a service.")
181+
print(
182+
"No initialized services!\n"
183+
"Please enter 'service' to go to the service menu and then enter 'add' to add a service."
184+
)
173185
return None
174186
print("Current block number: ", active_service.get_current_block_number())
175187

@@ -183,13 +195,18 @@ def update_channels():
183195
to work, so there is a warning for the user about this at the beginning.
184196
"""
185197
if active_service is None:
186-
print("No initialized services!\n"
187-
"Please enter 'service' to go to the service menu and then enter 'add' to add a service.")
198+
print(
199+
"No initialized services!\n"
200+
"Please enter 'service' to go to the service menu and then enter 'add' to add a service."
201+
)
188202
return None
189203

190-
is_continue = input("""Updating the channel list makes sense if the channel data has changed through other entry points.
204+
is_continue = (
205+
input("""Updating the channel list makes sense if the channel data has changed through other entry points.
191206
This procedure may take several minutes.
192-
Continue? (y/n): """).strip() == 'y'
207+
Continue? (y/n): """).strip()
208+
== "y"
209+
)
193210
if not is_continue:
194211
return None
195212

@@ -200,7 +217,14 @@ def update_channels():
200217
for service in initialized_services:
201218
load_channels = service.load_open_channels()
202219
for channel in load_channels:
203-
channels.append((channel, service.org_id, service.service_id, service.group['group_name']))
220+
channels.append(
221+
(
222+
channel,
223+
service.org_id,
224+
service.service_id,
225+
service.group["group_name"],
226+
)
227+
)
204228

205229
print("Channels updated! Enter 'list' to print the updated list.")
206230

@@ -214,13 +238,19 @@ def open_channel():
214238
"""
215239
global active_service
216240
global channels
217-
additions = False
218241
if active_service is None:
219-
print("No initialized services! The channel can only be opened for the service!\n"
220-
"Please enter 'service' to go to the service menu and then enter 'add' to add a service.")
242+
print(
243+
"No initialized services! The channel can only be opened for the service!\n"
244+
"Please enter 'service' to go to the service menu and then enter 'add' to add a service."
245+
)
221246
return None
222247
else:
223-
is_continue = input("The new channel will be opened for the active service. Continue? (y/n): ").strip() == 'y'
248+
is_continue = (
249+
input(
250+
"The new channel will be opened for the active service. Continue? (y/n): "
251+
).strip()
252+
== "y"
253+
)
224254
if not is_continue:
225255
return None
226256

@@ -230,7 +260,12 @@ def open_channel():
230260
is_deposit = False
231261
if balance < amount:
232262
print(f"Insufficient balance!\n\tCurrent MPE balance: {balance}\n\tAmount to put: {amount}")
233-
is_deposit = input("Would you like to deposit needed amount of FET tokens in advance? (y/n): ").strip() == 'y'
263+
is_deposit = (
264+
input(
265+
"Would you like to deposit needed amount of FET tokens in advance? (y/n): "
266+
).strip()
267+
== "y"
268+
)
234269
if not is_deposit:
235270
print("Channel is not opened!")
236271
return None
@@ -241,7 +276,14 @@ def open_channel():
241276
channel = active_service.open_channel(amount=amount, expiration=expiration)
242277
else:
243278
channel = active_service.deposit_and_open_channel(amount=amount, expiration=expiration)
244-
channels.append((channel, active_service.org_id, active_service.service_id, active_service.group['group_name']))
279+
channels.append(
280+
(
281+
channel,
282+
active_service.org_id,
283+
active_service.service_id,
284+
active_service.group["group_name"],
285+
)
286+
)
245287

246288

247289
def list_channels():
@@ -253,12 +295,14 @@ def list_channels():
253295
print("ORGANIZATION_ID SERVICE_ID GROUP_NAME CHANNEL_ID AMOUNT EXPIRATION")
254296
for channel in channels:
255297
channel[0].sync_state()
256-
print(channel[1],
257-
channel[2],
258-
channel[3],
259-
channel[0].channel_id,
260-
channel[0].state['available_amount'],
261-
channel[0].state['expiration'])
298+
print(
299+
channel[1],
300+
channel[2],
301+
channel[3],
302+
channel[0].channel_id,
303+
channel[0].state["available_amount"],
304+
channel[0].state["expiration"],
305+
)
262306

263307

264308
def add_funds():
@@ -271,7 +315,9 @@ def add_funds():
271315
exists = False
272316
for channel in channels:
273317
if channel[0].channel_id == channel_id:
274-
amount = int(input("Enter amount of FET tokens in cogs to add to the channel: ").strip())
318+
amount = int(
319+
input("Enter amount of FET tokens in cogs to add to the channel: ").strip()
320+
)
275321
channel[0].add_funds(amount)
276322
exists = True
277323
if not exists:
@@ -304,10 +350,12 @@ def extend_expiration():
304350
SDK configuration that is configured by the application provider.
305351
To run the application you need to change the 'private_key', 'eth_rpc_endpoint' and 'identity_name' values.
306352
"""
307-
config = sdk.config.Config(private_key="YOUR_PRIVATE_KEY",
308-
eth_rpc_endpoint=f"https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
309-
concurrency=False,
310-
force_update=False)
353+
config = sdk.config.Config(
354+
private_key="YOUR_PRIVATE_KEY",
355+
eth_rpc_endpoint="https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
356+
concurrency=False,
357+
force_update=False,
358+
)
311359

312360
snet_sdk = sdk.SnetSDK(config) # the 'SnetSDK' instance
313361
initialized_services = [] # the list of initialized service clients
@@ -319,42 +367,72 @@ def extend_expiration():
319367
"""
320368
commands = {
321369
"main": {
322-
"organizations": (list_organizations, "print a list of organization ids from Registry"),
323-
"services": (list_services_for_org, "print a list of service ids for an organization from Registry"),
324-
"balance": (balance, "print the account balance and the escrow balance"),
370+
"organizations": (
371+
list_organizations,
372+
"print a list of organization ids from Registry",
373+
),
374+
"services": (
375+
list_services_for_org,
376+
"print a list of service ids for an organization from Registry",
377+
),
378+
"balance": (
379+
balance,
380+
"print the account balance and the escrow balance",
381+
),
325382
"deposit": (deposit, "deposit FET tokens into MPE"),
326383
"block": (block_number, "print the current block number"),
327384
"service": (lambda: None, "go to the services menu"),
328385
"channel": (lambda: None, "go to the channels menu"),
329-
"help": (commands_help, "print a list of available commands in the main menu"),
330-
"exit": (lambda: exit(0), "exit the application")
386+
"help": (
387+
commands_help,
388+
"print a list of available commands in the main menu",
389+
),
390+
"exit": (lambda: exit(0), "exit the application"),
331391
},
332-
333392
"service": {
334-
"add": (create_service_client,
335-
"create a new service client. If it the first time, the new service becomes active"),
393+
"add": (
394+
create_service_client,
395+
"create a new service client. If it the first time, the new service becomes active",
396+
),
336397
"use": (switch_service, "switch the active service"),
337398
"call": (call, "call the active service method"),
338-
"info": (print_service_info, "output services, methods and messages in a service"),
339-
"list": (list_initialized_services, "print a list of initialized services"),
340-
"help": (commands_help, "print a list of available commands in the services menu"),
399+
"info": (
400+
print_service_info,
401+
"output services, methods and messages in a service",
402+
),
403+
"list": (
404+
list_initialized_services,
405+
"print a list of initialized services",
406+
),
407+
"help": (
408+
commands_help,
409+
"print a list of available commands in the services menu",
410+
),
341411
"back": (lambda: None, "return to the main menu"),
342-
"exit": (lambda: exit(0), "exit the application")
412+
"exit": (lambda: exit(0), "exit the application"),
343413
},
344-
345414
"channel": {
346-
"update": (update_channels, "update a list of initialized payment channels"),
415+
"update": (
416+
update_channels,
417+
"update a list of initialized payment channels",
418+
),
347419
"list": (list_channels, "print a list of initialized payment channels"),
348420
"open": (open_channel, "open a new payment channel"),
349421
"add-funds": (add_funds, "add funds to a channel"),
350-
"extend-expiration": (extend_expiration, "extend expiration of a channel"),
351-
"help": (commands_help, "print a list of available commands in the channels menu"),
422+
"extend-expiration": (
423+
extend_expiration,
424+
"extend expiration of a channel",
425+
),
426+
"help": (
427+
commands_help,
428+
"print a list of available commands in the channels menu",
429+
),
352430
"back": (lambda: None, "return to the main menu"),
353-
"exit": (lambda: exit(0), "exit the application")
354-
}
431+
"exit": (lambda: exit(0), "exit the application"),
432+
},
355433
}
356434

357-
active_commands: dict = commands["main"] # the list of available commands in the active menu
435+
active_commands: dict = commands["main"] # the list of available commands in the active menu
358436

359437

360438
def main():

0 commit comments

Comments
 (0)