Skip to content

Commit

Permalink
Release 3.3.0 (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim Bredin <[email protected]>
Co-authored-by: Deralden <[email protected]>
Co-authored-by: PAY2109 <[email protected]>
  • Loading branch information
4 people authored Jun 20, 2024
1 parent ee61b40 commit 75a599b
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 447 deletions.
77 changes: 48 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The package is published in PyPI at the following link:

|Package |Description |
|----------------------------------------------|---------------------------------------------------------------------|
|[snet-sdk](https://pypi.org/project/snet.sdk/)|Integrate SingularityNET services seamlessly into Python applications|
|[snet.sdk](https://pypi.org/project/snet.sdk/)|Integrate SingularityNET services seamlessly into Python applications|

## Getting Started

Expand All @@ -23,53 +23,72 @@ The SingularityNET SDK abstracts and manages state channels with service provide

### Usage

To call a SingularityNET service, the user must be able to deposit funds (AGI tokens) to the [Multi-Party Escrow](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/) Smart Contract.
To call a SingularityNET service, the user must be able to deposit funds (AGIX tokens) to the [Multi-Party Escrow](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/) Smart Contract.
To deposit these tokens or do any other transaction on the Ethereum blockchain, the user must possess an Ethereum identity with available Ether.

Once you have installed the snet-sdk in your current environment and it's in your PYTHONPATH, you should import it and create an instance of the base sdk class:

Once you have installed snet-sdk in your current environment, you can import it into your Python script and create an instance of the base sdk class:
```python
from snet import sdk
from config import config
config = {
"private_key": 'YOUR_PRIVATE_WALLET_KEY',
"eth_rpc_endpoint": f"https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
"email": "[email protected]",
"free_call_auth_token-bin":"f5533eb0f01f0d45239c11b411bdfd4221fd3b125e4250db1f7bc044466108bc10ce95ab62ae224b6578b68d0ce337b4ec36e4b9dfbe6653e04973107813cbc01c",
"free-call-token-expiry-block":19690819,
"concurrency": False,
"org_id": "organization_id",
"service_id": "id_of_the_service",
"group_name": "default_group",
"identity_name": "local_name_for_that_identity",
"identity_type": "key",
"network": "sepolia",
"force_update": False
}

snet_sdk = sdk.SnetSDK(config)
```

The `config` parameter must be a Python dictionary.
See [test_sdk_client.py.sample](https://github.com/singnet/snet-cli/blob/master/packages/sdk/testcases/functional_tests/test_sdk_client.py) for a sample configuration file.
The `config` parameter is a Python dictionary.
See [test_sdk_client.py](https://github.com/singnet/snet-sdk-python/blob/master/testcases/functional_tests/test_sdk_client.py) for a reference.
#### Config options description

private_key: Your wallet's private key that will be used to pay for calls. Is **required** to make a call;
eth_rpc_endpoint: RPC endpoint that is used to access the Ethereum network. Is **required** to make a call;
email: Your email;
"free_call_auth_token-bin" and "free-call-token-expiry-block": Are used to make free calls. See more on that below;
org_id: ID of the organization that owns the service you want to call. Is **required** to make a call;
service_id: ID of the service you want to call. Is **required** to make a call;
identity_name: Name that will be used locally to save your wallet settings. You can check your identities in the `~/.snet/config` file;
identity_type: Type of your wallet authentication. Note that snet-sdk currently supports only "key" identity_type;
network: You can set the Ethereum network that will be used to make a call;
force_update: If set to False, will reuse the existing gRPC stubs (if any) instead of downloading proto and regenerating them every time.

After executing this code, you should have client libraries created for this service. They are located in the following path: ~/.snet/org_id/service_id/python/

Note: Currently you can only save files to ~/.snet/
After executing this code, you should have client libraries created for this service. They are located at the following path: `~/.snet/org_id/service_id/python/`

Note: Currently you can only save files to `~/.snet/`. We will fix this in the future.

##### Free call configuration

If you want to use free call you need to add below mwntioned attributes in config file.
If you want to use a free call you need to add these attributes to the config dictionary:
```
"free_call_auth_token-bin":"f2548d27ffd319b9c05918eeac15ebab934e5cfcd68e1ec3db2b92765",
"free-call-token-expiry-block":172800,
"email":"[email protected]"
```
You can download this config for a given service from [Dapp]([https://beta.singularitynet.io/)

Now, the instance of the sdk can be used to create service client instances.
Continuing from the previous code this is an example using `Exampleservicee` from the `26072b8b6a0e448180f8c0e702ab6d2f` organization:
You can receive these for a given service from the [Dapp](https://beta.singularitynet.io/)
#### Calling the service
Now, the instance of the sdk can be used to create the service client instances.
Continuing from the previous code this is an example using `Exampleservice` from the `26072b8b6a0e448180f8c0e702ab6d2f` organization:

```python
import example_service_pb2_grpc

org_id = "26072b8b6a0e448180f8c0e702ab6d2f"
service_id = "Exampleservice"
group_name="default_group"

service_client = snet_sdk.create_service_client(org_id, service_id, group_name)
service_client = snet_sdk.create_service_client()
service_client.deposit_and_open_channel(123456, 33333)
```

Note that `org_id` and `service_id` must be passed to `config`.

The generated service_client instance can be used to call methods provided by the service.
To call these methods, you need to use the call_rpc method, passing into it the names of the method and data object, as well as the data itself (What specific data needs to be passed can be seen in the .proto file).
Continuing from the previous code, this is an example using example-service from the snet organization:

`deposit_and_open_channel()` function deposits the specified amount of AGIX tokens in cogs into an MPE smart contract and opens a payment channel.
The instance of service_client that has been generated can be utilized to invoke the methods that the service offers.
To do this, use the the call_rpc method. This method needs the names of the method and data object, along with the data itself, to be passed into it.
The specific data that needs to be passed can be found in the .proto file. Building upon the previously written code, here’s an example that uses the *Exampleservice* from the *26072b8b6a0e448180f8c0e702ab6d2f* organization:
```python
result = service_client.call_rpc("mul", "Numbers", a=20, b=3)
print(f"Performing 20 * 3: {result}") # Performing 20 * 3: value: 60.0
Expand Down Expand Up @@ -112,4 +131,4 @@ $ pip install -e .
## License

This project is licensed under the MIT License - see the
[LICENSE](https://github.com/singnet/snet-sdk-python/blob/master/LICENSE) file for details.
[LICENSE](https://github.com/singnet/snet-sdk-python/blob/master/LICENSE) file for details.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ argcomplete==3.1.2
grpcio-health-checking==1.59.0
jsonschema==4.0.0
eth-account==0.9.0
snet-cli==2.1.2
snet-cli==2.1.3
snet.contracts==0.1.1
46 changes: 41 additions & 5 deletions snet/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,48 @@ def __init__(self, config, metadata_provider=None):
self.registry_contract = get_contract_object(self.web3, "Registry", _registry_contract_address)

self.account = Account(self.web3, config, self.mpe_contract)

sdk = SDKCommand(Config(), args=Arguments(config['org_id'], config['service_id']))
sdk.generate_client_library()

def create_service_client(self, org_id, service_id, group_name=None,
payment_channel_management_strategy=None, options=None, concurrent_calls=1):
global_config = Config(sdk_config=config)
self.setup_config(global_config)
sdk = SDKCommand(global_config, args=Arguments(config['org_id'], config['service_id']))
force_update = config.get('force_update', False)

if force_update:
sdk.generate_client_library()
else:
path_to_pb_files = self.get_path_to_pb_files(config['org_id'], config['service_id'])
pb_2_file_name = find_file_by_keyword(path_to_pb_files, keyword="pb2.py")
pb_2_grpc_file_name = find_file_by_keyword(path_to_pb_files, keyword="pb2_grpc.py")
if not pb_2_file_name or not pb_2_grpc_file_name:
sdk.generate_client_library()

def setup_config(self, config: Config) -> None:
out_f = sys.stdout
network = self._config.get("network", None)
identity_name = self._config.get("identity_name", None)
# Checking for an empty network
if network and config["session"]["network"] != network:
config.set_session_network(network, out_f)
if identity_name:
self.set_session_identity(identity_name, config, out_f)
elif len(config.get_all_identities_names()) > 0:
if "identity" not in config["session"] or config["session"]["identity"] == "":
raise Exception("identity_name is not passed or selected")

def set_session_identity(self, identity_name: str, config: Config, out_f):
if identity_name not in config.get_all_identities_names():
identity = config.setup_identity()
config.add_identity(identity_name, identity, out_f)
config.set_session_identity(identity_name, out_f)
elif config["session"]["identity"] != identity_name:
config.set_session_identity(identity_name, out_f)

def create_service_client(self, payment_channel_management_strategy=None,
options=None, concurrent_calls=1):
org_id = self._config.get("org_id")
service_id = self._config.get("service_id")
group_name = self._config.get("group_name", "default_group")

if payment_channel_management_strategy is None:
payment_channel_management_strategy = DefaultPaymentStrategy(concurrent_calls)
if options is None:
Expand Down
166 changes: 0 additions & 166 deletions testcases/functional_tests/examples_service_pb2.py

This file was deleted.

Loading

0 comments on commit 75a599b

Please sign in to comment.