Skip to content

Commit 58c2154

Browse files
authored
Merge pull request #112 from singnet/development
Release 6.0.0
2 parents c597647 + e55d112 commit 58c2154

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2520
-1818
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
lint:
2+
@ruff check . --fix
3+
@ruff format .
4+
.PHONY: lint
5+
6+
test:
7+
python -m coverage run -m pytest tests/ -v && \
8+
python -m coverage report
9+
.PHONY: test

README.md

Lines changed: 129 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,76 +19,70 @@ To handle payment of services, SingularityNET uses
1919
[Ethereum state channels](https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/CoreConcepts/SmartContracts/mpe).
2020
The SingularityNET SDK abstracts and manages state channels with service providers on behalf of the user and
2121
handles authentication with the SingularityNET services.
22+
To call a service on a SingularityNET platform, the user must be able to deposit funds (FET tokens) to the
23+
[Multi-Party Escrow](https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/CoreConcepts/SmartContracts/mpe) Smart Contract.
24+
To deposit these tokens or do any other transaction on the Ethereum blockchain.
2225

2326
## Getting Started
2427

2528
These instructions are for the development and use of the SingularityNET SDK for Python.
2629

27-
### Usage
30+
### Installation
2831

29-
To call a service on a SingularityNET platform, the user must be able to deposit funds (FET tokens) to the
30-
[Multi-Party Escrow](https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/CoreConcepts/SmartContracts/mpe) Smart Contract.
31-
To deposit these tokens or do any other transaction on the Ethereum blockchain.
32+
To install SingularityNET Python SDK package run:
33+
34+
```shell
35+
pip install snet-sdk
36+
```
37+
38+
### Config
39+
40+
There are three ways to configure the SDK (set variables):
41+
42+
- `.env` file
43+
44+
```dotenv
45+
SNET_PRIVATE_KEY=12345678...
46+
SNET_ETH_RPC_ENDPOINT=https://mainnet.infura.io/v3/12345678...
47+
SNET_FORCE_UPDATE=False
48+
SNET_LIGHTHOUSE_TOKEN=12345678...
49+
```
50+
51+
- environment variables
52+
53+
- `configure()` function
3254

33-
Once you have installed snet-sdk in your current environment, you can import it into your Python script and create an
34-
instance of the base sdk class:
3555
```python
36-
from snet import sdk
37-
38-
"""
39-
SDK configuration provided by the application provider.
40-
To run the application, replace 'private_key' and 'eth_rpc_endpoint' with your values.
41-
"""
42-
config = sdk.config.Config(
43-
private_key="YOUR_PRIVATE_KEY", # Replace with your Ethereum private key
44-
eth_rpc_endpoint="https://eth-sepolia.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY", # Replace with your Alchemy API key
45-
concurrency=False,
46-
force_update=False
47-
)
56+
from snet.sdk.config import configure
4857

49-
# Initialize the SnetSDK instance
50-
snet_sdk = sdk.SnetSDK(config)
58+
59+
PRIVATE_KEY = "12345678..."
60+
INFURA_KEY = "12345678..."
61+
62+
configure(
63+
private_key=PRIVATE_KEY,
64+
eth_rpc_endpoint=f"https://mainnet.infura.io/v3/{INFURA_KEY}",
65+
force_update=False,
66+
lighthouse_token="12345678...",
67+
)
5168
```
5269

53-
The `config` parameter is an instance of the `Config` class.
54-
See [config.py](https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/SDK/PythonSDK/Documentation/config/)
55-
for a reference.
70+
>Please note that variables in the '.env` file and environment variables must start with "SNET_" and must be uppercase!
5671
5772
#### Config parameters description
5873

59-
- `private_key`: Your wallet's private key that will be used to pay for calls. Is **required** in config;
74+
- `private_key`: Your wallet's private key that will be used to sign transactions (and pay for calls). Is **required** in config;
75+
- `signer_private_key`: Is equal to `private_key`, if not specified. Your wallet's private key that will be used to pay for calls.
6076
- `eth_rpc_endpoint`: RPC endpoint that is used to access the Ethereum network. Is **required** in config;
6177
> To get your **Alchemy API Key**, follow [this guide](https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/Daemon/alchemy-api/).
62-
6378
- `wallet_index`: The index of the wallet that will be used to pay for calls;
6479
- `ipfs_endpoint`: IPFS endpoint that is used to access IPFS;
6580
- `concurrency`: If set to True, will enable concurrency for the SDK;
6681
- `force_update`: If set to False, will reuse the existing gRPC stubs (if any) instead of downloading proto and regenerating them every time.
67-
- `mpe_contract_address`: The address of the Multi-Party Escrow smart contract;
68-
- `token_contract_address`: The address of the SingularityNET token smart contract;
69-
- `registry_contract_address`: The address of the Registry smart contract;
70-
- `signer_private_key`: The private key of the signer. Used to sign the service call. Equals to `private_key` by default.
71-
72-
#### List organizations and their services
73-
74-
You can use the sdk client instance`s methods get_organization_list() to list all organizations and get_services_list("org_id") to list all services of a given organization.
75-
76-
```python
77-
orgs_list = snet_sdk.get_organization_list()
78-
print(*orgs_list, sep="\n")
79-
# ...
80-
# GoogleOrg3
81-
# 26072b8b6a0e448180f8c0e702ab6d2f
82-
# 43416d873fcb454589900189474b2eaa
83-
# ...
84-
```
85-
86-
```python
87-
org_id = "26072b8b6a0e448180f8c0e702ab6d2f"
88-
services_list = snet_sdk.get_services_list(org_id=org_id)
89-
print(*services_list, sep="\n")
90-
# Exampleservice
91-
```
82+
- `mpe_contract_address`: Custom address of the Multi-Party Escrow smart contract;
83+
- `token_contract_address`: Custom address of ASI (FET) token smart contract;
84+
- `registry_contract_address`: Custom address of the Registry smart contract;
85+
- `lighthouse_token`: Personal Lighthouse storage token to save files into it.
9286

9387
### Calling the service
9488

@@ -99,7 +93,7 @@ organization:
9993
```python
10094
service_client = snet_sdk.create_service_client(org_id="26072b8b6a0e448180f8c0e702ab6d2f",
10195
service_id="Exampleservice",
102-
group_name="default_group")
96+
payment_strategy_type=PaymentStrategyType.PAID_CALL)
10397
```
10498

10599
After executing this code, you should have client libraries created for this service. They are located at the following
@@ -145,6 +139,90 @@ perform other actions related to payment. In this case, the choice of payment st
145139
opening a channel and depositing funds into MPE occurs automatically. For more information on payment, please
146140
visit the [Payment](#payment) section.
147141

142+
## Organization and service managing
143+
144+
### Getting data
145+
146+
Here are useful methods for getting data
147+
148+
```python
149+
from snet.sdk import SnetSDK
150+
151+
sdk = SnetSDK()
152+
org_id = "<ORG_ID>"
153+
service_id = "<SERVICE_ID>"
154+
155+
orgs_list = sdk.get_organization_list()
156+
services_list = sdk.get_services_list(org_id=org_id)
157+
158+
org_metadata = sdk.get_organization_metadata(org_id)
159+
service_metadata = sdk.get_service_metadata(org_id, service_id)
160+
```
161+
162+
### Creating and publishing the service
163+
164+
- create service metadata instance using chaining methods
165+
166+
```python
167+
from snet.sdk import SnetSDK, ServiceMetadata
168+
169+
sdk = SnetSDK()
170+
org_id = "<ORG_ID>"
171+
service_id = "<SERVICE_ID>"
172+
173+
service_metadata = (
174+
ServiceMetadata(
175+
version=1, display_name=service_id, service_type="grpc", tags=["first", "second"]
176+
)
177+
.add_group(free_call_signer_address="0x123456qweasd", endpoints=["https://test-daemon.com"])
178+
.add_contributor("me")
179+
)
180+
```
181+
182+
- publish the service into blockchain including saving `.proto` files and service metadata into storage
183+
184+
```python
185+
sdk.publish_service_comprehensively(
186+
org_id,
187+
service_id,
188+
service_metadata,
189+
proto_dir="<PATH_TO_DIR_WITH_PROTO_FILES>",
190+
storage_type=StorageType.FILECOIN,
191+
)
192+
```
193+
194+
### Updating the service
195+
196+
- fetch and edit service metadata
197+
198+
```python
199+
from snet.sdk import SnetSDK
200+
201+
sdk = SnetSDK()
202+
org_id = "<ORG_ID>"
203+
service_id = "<SERVICE_ID>"
204+
205+
service_metadata = sdk.get_service_metadata(org_id, service_id)
206+
service_metadata.change_description(
207+
short_description="new_short_description", description="new_description"
208+
)
209+
```
210+
211+
- update the service in blockchain including saving `.proto` files and service metadata into storage
212+
213+
```python
214+
sdk.update_service(org_id, service_id, service_metadata)
215+
```
216+
217+
> Please note that the Python SDK provides all the features for interacting with the SNET platform.
218+
> You can use them separately using the following entities:
219+
>
220+
> - sdk.mpe_contract
221+
> - sdk.registry_contract
222+
> - sdk.storage_provider
223+
> - sdk.payment_channel_provider
224+
> - sdk.account
225+
148226
## Payment
149227

150228
When creating a service client, you can select a payment strategy using the `payment_strategy_type` parameter:

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-

0 commit comments

Comments
 (0)