Skip to content

Commit 4bf0676

Browse files
committed
docs: update readme
1 parent 28d7aea commit 4bf0676

File tree

2 files changed

+134
-56
lines changed

2 files changed

+134
-56
lines changed

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:

snet/sdk/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ def create_service_client(
109109

110110
group = self._get_service_group(org_id, service_id, service_metadata, group_name)
111111

112-
service_stubs = self.get_service_stub(lib_generator)
112+
service_stubs = self._get_service_stub(lib_generator)
113113

114-
pb2_module = self.get_module_by_keyword("pb2.py", lib_generator)
114+
pb2_module = self._get_module_by_keyword("pb2.py", lib_generator)
115115
_service_client = ServiceClient(
116116
org_id,
117117
service_id,
@@ -142,9 +142,9 @@ def _enhance_service_metadata(self, org_id, service_id):
142142

143143
return service_metadata
144144

145-
def get_service_stub(self, lib_generator: ClientLibGenerator) -> list[ServiceStub]:
145+
def _get_service_stub(self, lib_generator: ClientLibGenerator) -> list[ServiceStub]:
146146
path_to_pb_files = str(lib_generator.proto_dir)
147-
module_name = self.get_module_by_keyword("pb2_grpc.py", lib_generator)
147+
module_name = self._get_module_by_keyword("pb2_grpc.py", lib_generator)
148148
sys.path.append(path_to_pb_files)
149149
try:
150150
grpc_file = importlib.import_module(module_name)
@@ -158,7 +158,7 @@ def get_service_stub(self, lib_generator: ClientLibGenerator) -> list[ServiceStu
158158
except Exception as e:
159159
raise Exception(f"Error importing module: {e}")
160160

161-
def get_module_by_keyword(self, keyword: str, lib_generator: ClientLibGenerator) -> ModuleName:
161+
def _get_module_by_keyword(self, keyword: str, lib_generator: ClientLibGenerator) -> ModuleName:
162162
path_to_pb_files = lib_generator.proto_dir
163163
file_name = find_file_by_keyword(path_to_pb_files, keyword, exclude=["training"])
164164
module_name = os.path.splitext(file_name)[0]

0 commit comments

Comments
 (0)