From 05c8c89590f34af9c01ec38c5da87a996d18de00 Mon Sep 17 00:00:00 2001 From: Pluto <113933026+PlutoNbai@users.noreply.github.com> Date: Fri, 3 May 2024 22:35:50 -0400 Subject: [PATCH] Update README.md --- README.md | 88 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 27ead482..c41e7f61 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,22 @@ [![Chat on discord](https://img.shields.io/badge/join%20-discord-brightgreen.svg)](https://discord.com/invite/KKGhy8ZqzK) ## Table Of Contents + - [Overview](#overview) - [Features](#features) - [Installation](#installation) - [Use Python dotenv (Optional)](#use-python-dotenv) - [Quick Guide](#quick-start-guide-sdk-v2) - 1. [Get SwanHub API Key](#1-get-swanhub-api-key) - 2. [Login to SwanHub](#2-login-into-swanhub-through-sdk) - 3. [Use Swan Payment Contract](#3-connect-to-swan-payment-contract) - 4. [Retrieve CP Hardware Info](#4-retrieve-avaliable-hardware-informaitons) - 5. [Get Job Source URI](#5-get-job_source_uri) - 6. [Esitmate Task Payment](#6-esitmate-payment-amount) - 7. [Create Task](#7-create-task) - 8. [Submit Payment](#8-submit-payment) - 9. [Validate Payment and Delpoy Task](#9-validate-payment-to-deploy-task) - 10. [Follow Up Deployed Task Status (Optional)](#10-follow-up-task-status-optional) + 1. [Get Orchestrator API Key](#1-get-orchestrator-api-key) + 2. [Login to Orchestrator](#2-login-into-Orchestrator-through-sdk) + 3. [Use Swan Payment Contract](#3-connect-to-swan-payment-contract) + 4. [Retrieve CP Hardware Info](#4-retrieve-avaliable-hardware-informaitons) + 5. [Get Job Source URI](#5-get-job_source_uri) + 6. [Esitmate Task Payment](#6-esitmate-payment-amount) + 7. [Create Task](#7-create-task) + 8. [Submit Payment](#8-submit-payment) + 9. [Validate Payment and Delpoy Task](#9-validate-payment-to-deploy-task) + 10. [Follow-Up Deployed Task Status (Optional)](#10-follow-up-task-status-optional) - [Executale Example](#examples) - [Documentation](#documentation) - [Contribution](#contributions) @@ -33,7 +34,7 @@ The PYTHON SWAN SDK is a comprehensive toolkit designed to facilitate seamless i - **API Client Integration**: Streamline your development workflow with our intuitive API client. - **Pre-defined Data Models**: Utilize our structured data models for tasks, directories, and source URIs to enhance your application's reliability and scalability. - **Service Layer Abstractions**: Access complex functionalities through a simplified high-level interface, improving code maintainability. -- **Extensive Documentation**: Access a wealth of information through our comprehensive guides and reference materials located in the `docs/` directory on Github. +- **Extensive Documentation**: Access a wealth of information through our comprehensive guides and reference materials located in the `docs/` directory on GitHub. ## Installation @@ -41,7 +42,7 @@ Setting up the PYTHON SWAN SDK is straightforward. To use Python Swan SDK, use Python 3.8 or later. Earlier versions are not supported. -**Install via PyPI testnet:** +**Install via PyPI:** ```bash pip install swan-sdk @@ -54,26 +55,29 @@ git clone https://github.com/swanchain/orchestrator-sdk.git ``` ## Use Python dotenv -It is recommanded to store your important person information in configuration or as environmental variables. Python dotenv allows loading environment variable from `.env` files for easier access and better security. + +It is recommended to store your important personal information in configuration or as environmental variables. Python dotenv allows loading environment variables from `.env` files for easier access and better security. python-dotenv package: https://pypi.org/project/python-dotenv/ Detailed instructions: https://github.com/swanchain/python-swan-sdk/tree/dev/docs/configuration.md ## Quick Start Guide for Swan SDK + Jump into using the SDK with this quick example: -### 1. Get SwanHub API Key +### 1. Get Orchestrator API Key -To use `swan-sdk` SwanHub API key is required. -- Go to Swan Dashboard: https://orchestrator.swanchain.io/provider-status +To use `swan-sdk`, an Orchestrator API key is required. + +- Go to Orchestrator Dashboard: https://orchestrator.swanchain.io/provider-status - Login through MetaMask. -- Click the user icon on top right. +- Click the user icon on the top right. - Click 'Show API-Key' -> 'New API Key' - Store your API Key safely, do not share with others. -### 2. Login into SwanHub Through SDK +### 2. Login into Orchestrator Through SDK -To use `swan-sdk` you will need to login to SwanHub using API Key. (Wallet login is not supported) +To use `swan-sdk` you will need to login to Orchestrator using API Key. (Wallet login is not supported) ```python from swan import SwanAPI @@ -83,43 +87,48 @@ swan_api = SwanAPI(api_key="") ### 3. Connect to Swan Payment Contract -Payment of SwanHub deployment is paid through Swan Payment Contract. To navigate the contract ABIs. First create a `SwanContract()` instance: +Payment of Orchestrator deployment is paid through the Swan Payment Contract. To navigate the contract ABIs. First create a `SwanContract()` instance: + ```python from swan.contract.swan_contract import SwanContract contract = SwanContract('', swan_api.contract_info) ``` -### 4. Retrieve Avaliable Hardware Informaitons +### 4. Retrieve available hardware information -SwanHub provides selection of Computing Providers with different hardwares. -Use `SwanAPI().get_hardware_config()` to retrieve all avaliable hardwares on SwanHub. +Orchestrator provides a selection of Computing Providers with different hardware. +Use `SwanAPI().get_hardware_config()` to retrieve all available hardware on Orchestrator. Each hardware is stored in `HardwareConfig()` object. + ```python -from swan.object import HardwareConfig +from the swan.object import HardwareConfig ``` -Hardware config contains an unique hardware ID, hardware name, description, hardware type (CPU/GPU), price per hour, avaliable region and current status. +Hardware config contains a unique hardware ID, hardware name, description, hardware type (CPU/GPU), price per hour, available region and current status. -See all avaliable hardware in a python dictionary: -```python +See all available hardware in a Python dictionary: +```python hardwares = swan_api.get_hardware_config() hardwares_info = [hardware.to_dict() for hardware in hardwares if hardware.status == "available"] hardwares_info ``` -`HardwareConfig().status` shows the avalibility of the hardware. -`HardwareConfig().region` is a list of all region this hardware is avaliable in. -Retrieve the hardware with hardware id 0: +`HardwareConfig().status` shows the availability of the hardware. +`HardwareConfig().region` is a list of all regions this hardware is available in. + +Retrieve the hardware with hardware ID 0: + ```python hardwares = swan_api.get_hardware_config() -chosen_hardware = [hardware for hardware in hardwares if hardware.id == 0] +chosen_hardware = [hardware for hardware in hardware if hardware.id == 0] chosen_hardware.to_dict() ``` Sample output: + ``` {'id': 0, 'name': 'C1ae.small', @@ -137,6 +146,7 @@ Sample output: Generate a source URI A demo tetris docker image on GitHub as repo_uri: 'https://github.com/alphaflows/tetris-docker-image.git' + ```python job_source_uri = swan_api.get_source_uri( repo_uri='', @@ -147,8 +157,10 @@ job_source_uri = swan_api.get_source_uri( job_source_uri = job_source_uri['data']['job_source_uri'] ``` -### 6. Esitmate Payment Amount +### 6. Estimate Payment Amount + To estimate the payment required for the deployment. Use `SwanContract().estiamte_payment()` + ```python duration_hour = 1 # or duration you want the deployment to run amount = contract.estimate_payment(chosen_hardware.id, duration_hour) @@ -157,7 +169,8 @@ amount # amount is in wei, 18 decimals ### 7. Create Task -Before paying for the task. First create a task on SwanHub using desired task attributes. +Before paying for the task. First, create a task on Orchestrator using desired task attributes. + ```python import json @@ -179,6 +192,7 @@ print(json.dumps(result, indent=2)) # Print response ``` Sample output: + ``` { "data": { @@ -199,11 +213,12 @@ Sample output: } ``` -The `task['uuid']` will be used in following operations. +The `task['uuid']` will be used in the following operations. ### 8. Submit Payment Use `SwanContract().submit_payment()` to pay for the task. The TX hash is the receipt for the payment. + ```python tx_hash = contract.submit_payment(task_uuid, hardware_id, duration) ``` @@ -211,6 +226,7 @@ tx_hash = contract.submit_payment(task_uuid, hardware_id, duration) ### 9. Validate Payment to Deploy Task Use `SwanAPI().validate_payment()` to validate the payment using TX hash and deploy the task. + ```python swan_api.validate_payment( tx_hash=tx_hash, @@ -218,17 +234,19 @@ swan_api.validate_payment( ) ``` -### 10. Follow up Task Status (Optional) +### 10. Follow-up Task Status (Optional) #### Show results Get the deploy URI to test your task deployment using `SwanAPI().get_real_uri()`. + ```python r = swan_api.get_real_url(task_uuid) print(r) ``` ## Examples + For executable examples consult https://github.com/swanchain/python-swan-sdk/tree/dev/examples ## Documentation