Skip to main content

SingularityNET Python SDK

Project description

snet-sdk-python

SingularityNET SDK for Python

Package

The package is published in PyPI at the following link:

Package Description
snet.sdk Integrate SingularityNET services seamlessly into Python applications

Getting Started

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

Core concepts

The SingularityNET SDK allows you to make calls to SingularityNET services programmatically from your application. To communicate between clients and services, SingularityNET uses gRPC. To handle payment of services, SingularityNET uses Ethereum state channels. The SingularityNET SDK abstracts and manages state channels with service providers on behalf of the user and handles authentication with the SingularityNET services.

Usage

To call a SingularityNET service, the user must be able to deposit funds (AGIX tokens) to the 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 snet-sdk in your current environment, you can import it into your Python script and create an instance of the base sdk class:

from snet import sdk
config = {
        "private_key": 'YOUR_PRIVATE_WALLET_KEY',
        "eth_rpc_endpoint": f"https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
        "email": "your@email.com",
        "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 is a Python dictionary. See 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 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 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":"test@test.com"  

You can receive these for a given service from the Dapp

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:

service_client = snet_sdk.create_service_client()
service_client.deposit_and_open_channel(123456, 33333)

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:

result = service_client.call_rpc("mul", "Numbers", a=20, b=3)
print(f"Performing 20 * 3: {result}") # Performing 20 * 3: value: 60.0

You can get this code example at https://github.com/singnet/snet-code-examples/tree/python_client/python/client

For more information about gRPC and how to use it with Python, please see:


Development

Installing

Prerequisites


  • Clone the git repository
$ git clone git@github.com:singnet/snet-sdk-python.git
$ cd snet-sdk-python
  • Install the required dependencies
$ pip install -r requirements.txt
  • Install the package in development/editable mode
$ pip install -e .

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snet.sdk-3.3.0.tar.gz (20.0 kB view hashes)

Uploaded Source

Built Distribution

snet.sdk-3.3.0-py3-none-any.whl (24.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page