diff --git a/.gitignore b/.gitignore index ba0a07e0..572f469c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,3 @@ blockchain/node_modules/ __pycache__/ - -snet_cli/venv/ .idea/ -snet_cli/snet_cli.egg-info/ -snet_cli/snet_cli/resources/contracts/abi -snet_cli/snet_cli/resources/contracts/networks -snet_cli/snet_cli/resources/proto/*.py -snet_cli/build/ -snet_cli/dist/ - -snet_sdk/venv/ -snet_sdk/snet_sdk.egg-info/ -__pycache__ -snet_sdk/snet_sdk/resources/contracts/abi -snet_sdk/snet_sdk/resources/contracts/networks -snet_sdk/build/ -snet_sdk/dist/ diff --git a/README.md b/README.md index 4b025ec5..77976127 100644 --- a/README.md +++ b/README.md @@ -1,124 +1,18 @@ -# snet-cli +# snet-python-monorepo [![CircleCI](https://circleci.com/gh/singnet/snet-cli.svg?style=svg)](https://circleci.com/gh/singnet/snet-cli) -SingularityNET CLI +SingularityNET Python Monorepo -## Getting Started +## Packages +This repository is a monorepo that includes several packages that we publish to PyPI from a shared codebase, specifically: -These instructions are for the development and use of the SingularityNET CLI. -For further details, please check our full [Documentation](http://snet-cli-docs.singularitynet.io/). +|Package |Description | +|----------------------------------------------|---------------------------------------------------------------------| +|[snet-cli](https://pypi.org/project/snet-cli/)|Command line interface to interact with the SingularityNET platform | +|[snet-sdk](https://pypi.org/project/snet-sdk/)|Integrate SingularityNET services seamlessly into Python applications| -### Installing with pip - -#### Install prerequisites - -You should have python with version >= 3.6.5 and pip installed. - -Additionally you should install the following packages: - -* libudev -* libusb 1.0 - -If you use Ubuntu (or any Linux distribution with APT package support) you should do the following: - -```bash -sudo apt-get install libudev-dev libusb-1.0-0-dev -``` - -#### Install snet-cli using pip - -```bash -$ pip3 install snet-cli -``` - - -#### Enabling commands autocomplete -If you want to enable auto completion of commands, you should install the following package -* python-argcomplete - -On ubuntu (or any Linux distribution with APT package support), you should do the following - -```bash -sudo apt install python-argcomplete -``` -After the package is installed, activate autocomplete - -##### for all python commands (which includes snet commands as well) - -```bash -sudo activate-global-python-argcomplete -``` -Note: Changes will not take effect until shell is restarted. - -##### only for snet commands, then you should do the following -```bash -echo 'eval "$(register-python-argcomplete snet)"' >> ~/.bashrc -``` -then -```bash -source ~/.bashrc -``` - -## Commands - -Complete documentation is available [here](http://snet-cli-docs.singularitynet.io/) - - -## Development - -### Installing - -#### Prerequisites - -* [Python 3.6.5](https://www.python.org/downloads/release/python-365/) -* [Node 8+ w/npm](https://nodejs.org/en/download/) - ---- - -* Clone the git repository -```bash -$ git clone git@github.com:singnet/snet-cli.git -$ cd snet-cli -``` - -* Install development/test blockchain dependencies -```bash -$ ./scripts/blockchain install -``` - -* Install the package in development/editable mode -```bash -$ pip3 install -e . -``` - -#### Building Docs - -* Install sphinx, sphinx-argparse and the rtd theme -```bash -$ pip install sphinx -$ pip install sphinx-argparse -$ pip install sphinx-rtd-theme -``` - -* Run the build-docs.sh in the docs directory -```bash -$ cd docs -$ sh build-docs.sh -``` - -The documentation is generated under the docs/build/html folder - -### Release - -This project is published to [PyPI](https://pypi.org/project/snet-cli/). - -### Versioning - -We use [SemVer](http://semver.org/) for versioning. For the versions available, see the -[tags on this repository](https://github.com/singnet/snet-cli/tags). - ## License This project is licensed under the MIT License - see the -[LICENSE](https://github.com/singnet/alpha-daemon/blob/master/LICENSE) file for details. +[LICENSE](https://github.com/singnet/snet-cli/blob/master/LICENSE) file for details. diff --git a/snet_cli/.gitignore b/snet_cli/.gitignore new file mode 100644 index 00000000..3e31273b --- /dev/null +++ b/snet_cli/.gitignore @@ -0,0 +1,7 @@ +venv/ +snet_cli.egg-info/ +snet_cli/resources/contracts/abi +snet_cli/resources/contracts/networks +snet_cli/resources/proto/*.py +build/ +dist/ diff --git a/snet_cli/LICENSE b/snet_cli/LICENSE new file mode 100644 index 00000000..c674cd51 --- /dev/null +++ b/snet_cli/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 SingularityNET + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/snet_cli/README.md b/snet_cli/README.md new file mode 100644 index 00000000..65ee1d07 --- /dev/null +++ b/snet_cli/README.md @@ -0,0 +1,124 @@ +# snet-cli + +[![CircleCI](https://circleci.com/gh/singnet/snet-cli.svg?style=svg)](https://circleci.com/gh/singnet/snet-cli) + +SingularityNET CLI + +## Getting Started + +These instructions are for the development and use of the SingularityNET CLI. +For further details, please check our full [Documentation](http://snet-cli-docs.singularitynet.io/). + +### Installing with pip + +#### Install prerequisites + +You should have python with version >= 3.6.5 and pip installed. + +Additionally you should install the following packages: + +* libudev +* libusb 1.0 + +If you use Ubuntu (or any Linux distribution with APT package support) you should do the following: + +```bash +sudo apt-get install libudev-dev libusb-1.0-0-dev +``` + +#### Install snet-cli using pip + +```bash +$ pip3 install snet-cli +``` + + +#### Enabling commands autocomplete +If you want to enable auto completion of commands, you should install the following package +* python-argcomplete + +On ubuntu (or any Linux distribution with APT package support), you should do the following + +```bash +sudo apt install python-argcomplete +``` +After the package is installed, activate autocomplete + +##### for all python commands (which includes snet commands as well) + +```bash +sudo activate-global-python-argcomplete +``` +Note: Changes will not take effect until shell is restarted. + +##### only for snet commands, then you should do the following +```bash +echo 'eval "$(register-python-argcomplete snet)"' >> ~/.bashrc +``` +then +```bash +source ~/.bashrc +``` + +## Commands + +Complete documentation is available [here](http://snet-cli-docs.singularitynet.io/) + + +## Development + +### Installing + +#### Prerequisites + +* [Python 3.6.5](https://www.python.org/downloads/release/python-365/) +* [Node 8+ w/npm](https://nodejs.org/en/download/) + +--- + +* Clone the git repository +```bash +$ git clone git@github.com:singnet/snet-cli.git +$ cd snet-cli/snet-cli +``` + +* Install development/test blockchain dependencies +```bash +$ ./scripts/blockchain install +``` + +* Install the package in development/editable mode +```bash +$ pip3 install -e . +``` + +#### Building Docs + +* Install sphinx, sphinx-argparse and the rtd theme +```bash +$ pip install sphinx +$ pip install sphinx-argparse +$ pip install sphinx-rtd-theme +``` + +* Run the build-docs.sh in the docs directory +```bash +$ cd docs +$ sh build-docs.sh +``` + +The documentation is generated under the docs/build/html folder + +### Release + +This project is published to [PyPI](https://pypi.org/project/snet-cli/). + +### Versioning + +We use [SemVer](http://semver.org/) for versioning. For the versions available, see the +[tags on this repository](https://github.com/singnet/snet-cli/tags). + +## License + +This project is licensed under the MIT License - see the +[LICENSE](https://github.com/singnet/alpha-daemon/blob/master/LICENSE) file for details. diff --git a/snet_sdk/.gitignore b/snet_sdk/.gitignore new file mode 100644 index 00000000..bbdffa01 --- /dev/null +++ b/snet_sdk/.gitignore @@ -0,0 +1,6 @@ +venv/ +snet_sdk.egg-info/ +snet_sdk/resources/contracts/abi +snet_sdk/resources/contracts/networks +build/ +dist/ diff --git a/snet_sdk/snet_sdk/__init__.py b/snet_sdk/snet_sdk/__init__.py index 5b84d0d1..b780ec27 100644 --- a/snet_sdk/snet_sdk/__init__.py +++ b/snet_sdk/snet_sdk/__init__.py @@ -54,7 +54,6 @@ def __init__( self.registry_contract = get_contract_object(self.web3, "Registry.json") self.account = Account(self.web3, config, self.mpe_contract) - self.block_offset = self._config.get("block_offset", 604800) def create_service_client(self, org_id, service_id, service_stub, group_name="default_group", payment_channel_management_strategy=PaymentChannelManagementStrategy, options={}): service_metadata = self.service_metadata(org_id, service_id) diff --git a/snet_sdk/snet_sdk/payment_channel_management_strategies/default.py b/snet_sdk/snet_sdk/payment_channel_management_strategies/default.py index 60147274..56b5ff99 100644 --- a/snet_sdk/snet_sdk/payment_channel_management_strategies/default.py +++ b/snet_sdk/snet_sdk/payment_channel_management_strategies/default.py @@ -1,6 +1,8 @@ class PaymentChannelManagementStrategy: - def __init__(self, sdk_context): + def __init__(self, sdk_context, block_offset=0, call_allowance=1): self.sdk_context = sdk_context + self.block_offset = block_offset + self.call_allowance = call_allowance def select_channel(self, service_client): account = self.sdk_context.account @@ -12,9 +14,9 @@ def select_channel(self, service_client): default_expiration = service_client.default_channel_expiration() if len(payment_channels) == 0: - if mpe_balance > service_call_price: - return service_client.open_channel(service_call_price, default_expiration + self.sdk_context.block_offset) - return service_client.deposit_and_open_channel(service_call_price, default_expiration + self.sdk_context.block_offset) + if mpe_balance > service_call_price*self.call_allowance: + return service_client.open_channel(service_call_price*self.call_allowance, default_expiration + self.block_offset) + return service_client.deposit_and_open_channel(service_call_price*self.call_allowance - mpe_balance, default_expiration + self.block_offset) funded_valid_channels = [channel for channel in payment_channels if self._has_sufficient_funds(channel, service_call_price) and self._is_valid(channel, default_expiration)] if len(funded_valid_channels): @@ -22,16 +24,16 @@ def select_channel(self, service_client): funded_channels = [channel for channel in payment_channels if self._has_sufficient_funds(channel, service_call_price)] if len(funded_channels): - funded_channels[0].extend_expiration(default_expiration + self.sdk_context.block_offset) + funded_channels[0].extend_expiration(default_expiration + self.block_offset) return funded_channels[0] valid_channels = [channel for channel in payment_channels if self._is_valid(channel, default_expiration)] if len(valid_channels): - valid_channels[0].add_funds(service_call_price) + valid_channels[0].add_funds(service_call_price*self.call_allowance) return valid_channels[0] first_expired_and_unfunded_channel = payment_channels[0] - first_expired_and_unfunded_channel.extend_and_add_funds(default_expiration + self.sdk_context.block_offset, service_call_price) + first_expired_and_unfunded_channel.extend_and_add_funds(default_expiration + self.block_offset, service_call_price*self.call_allowance) return first_expired_and_unfunded_channel