Skip to content

Commit

Permalink
Merge branch 'development' into snyk-fix-d154f8111ff512dd85b44bdc7e8d…
Browse files Browse the repository at this point in the history
…3b1b
  • Loading branch information
sassless authored Oct 15, 2024
2 parents 844fdd4 + e60db51 commit d3b0cc9
Show file tree
Hide file tree
Showing 72 changed files with 6,812 additions and 311 deletions.
286 changes: 227 additions & 59 deletions README.md

Large diffs are not rendered by default.

208 changes: 208 additions & 0 deletions docs/main/account.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
## module: sdk.account

[Link](https://github.com/singnet/snet-sdk-python/blob/master/snet/sdk/account.py) to GitHub

Entities:
1. [TransactionError](#class-transactionerror)
- [\_\_init\_\_](#__init__)
- [\_\_str\_\_](#__str__)
2. [Account](#class-account)
- [\_\_init\_\_](#__init__-1)
- [_get_nonce](#_get_nonce)
- [_get_gas_price](#_get_gas_price)
- [_send_signed_transaction](#_send_signed_transaction)
- [send_transaction](#send_transaction)
- [_parse_receipt](#_parse_receipt)
- [escrow_balance](#escrow_balance)
- [deposit_to_escrow_account](#deposit_to_escrow_account)
- [approve_transfer](#approve_transfer)
- [allowance](#allowance)

### Class `TransactionError`

extends: `Exception`

is extended by: -

#### description

`TransactionError` is a custom exception class that is raised when an Ethereum transaction receipt has a status of 0.
This indicates that the transaction failed. Can provide a custom message. Optionally includes receipt

#### attributes

- `message` (str): The exception message.
- `receipt` (dict): The transaction receipt.

#### methods

#### `__init__`

Initializes the exception with the provided message and receipt.

###### args:

- `message` (str): The exception message.
- `receipt` (dict): The transaction receipt. Defaults to _None_.

###### returns:

- _None_

#### `__str__`

Returns a string representation of the `TransactionError` object.

###### returns:

- A string containing the `message` attribute of the TransactionError object. (str)

### Class `Account`

extends: -

is extended by: -

#### description

`Account` is responsible for managing the Ethereum account associated with the SingularityNET platform.
It provides methods for interacting with the MultiPartyEscrow contract, the SingularityNetToken contract, and
the Ethereum blockchain.

#### attributes

- `config` (dict): The configuration settings for the account. _Note_: In fact, this is the same config
from `SnetSDK`.
- `web3` (Web3): An instance of the `Web3` class for interacting with the Ethereum blockchain.
- `mpe_contract` (MPEContract): An instance of the `MPEContract` class for interacting with
the MultiPartyEscrow contract.
- `token_contract` (Contract): An instance of the `Contract` class from the `web3` library for interacting
with the SingularityNET AGIX Token contract.
- `private_key` (str): The private key associated with the account.
- `signer_private_key` (str): The private key used for signing transactions.
- `address` (str): The Ethereum address associated with the account.
- `signer_address` (str): The Ethereum address used for signing transactions.
- `nonce` (int): The nonce value for the account.

#### methods

#### `__init__`

Initializes a new instance of the `Account` class.

###### args:

- `w3` (Web3): An instance of the `Web3` class.
- `config` (dict): A dictionary containing the configuration settings.
- `mpe_contract` (MPEContract): An instance of the `MPEContract` class.

###### returns:

- _None_

#### `_get_nonce`

Returns the next nonce for a transaction.

###### returns:

- The next nonce for a transaction. (int)

#### `_get_gas_price`

Calculates the gas price for a transaction.

Retrieves the current gas price from the Ethereum network using the web3 and increases it
according to a certain algorithm so that the transaction goes faster.

###### returns:

- The calculated gas price. (int)

#### `_send_signed_transaction`

Sends a signed transaction to the Ethereum blockchain.

Builds a transaction using the given contract function and arguments, signs it with the private key of the account,
and sends it to the Ethereum blockchain.

###### args:

- `contract_fn`: The contract function to be called.
- `*args`: The arguments to pass to the contract function.

###### returns:

- Hash of the sent transaction. (HexStr | str)

#### `send_transaction`

Sends a transaction by calling the given contract function with the provided arguments.

###### args:

- `contract_fn`: The contract function to be called.
- `*args`: The arguments to pass to the contract function.

###### returns:

- The transaction receipt indicating the success or failure of the transaction. (TxReceipt)

#### `_parse_receipt`

Parses the receipt of a transaction and returns the result as a JSON string.

###### args:

- `receipt` (TxReceipt): The receipt of the transaction.
- `event` (Event): The event to process the receipt with.
- `encoder` (JSONEncoder): The JSON encoder to use. Defaults to json.JSONEncoder.

###### returns:

- The result of processing the receipt as a JSON string. (str)

###### raises:

- TransactionError: If the transaction status is 0, indicating a failed transaction.

#### `escrow_balance`

Retrieves the escrow balance for the current account.

###### returns:

- The escrow balance in cogs. (int)

#### `deposit_to_escrow_account`

Deposit the specified amount of AGIX tokens in cogs into the MPE account.

###### args:

- `amount_in_cogs` (int): The amount of AGIX tokens in cogs to deposit.

###### returns:

- The transaction receipt of the transaction. (TxReceipt)

#### `approve_transfer`

Approves a transfer of a specified amount of AGIX tokens in cogs to the MPE contract.

###### args:

- `amount_in_cogs` (int): The amount of AGIX tokens in cogs to approve for transfer.

###### returns:

- The transaction receipt of the transaction. (TxReceipt)

#### `allowance`

Retrieves the allowance of the current account for the MPE contract.

###### returns:

- The allowance in cogs. (int)

53 changes: 53 additions & 0 deletions docs/main/client_lib_generator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## module: sdk.client_lib_generator

[Link](https://github.com/singnet/snet-sdk-python/blob/master/snet/sdk/client_lib_generator.py) to GitHub

Entities:
1. [ClientLibGenerator](#class-clientlibgenerator)
- [\_\_init\_\_](#__init__)
- [generate_client_library](#generate_client_library)

### Class `ClientLibGenerator`

extends: -

is extended by: -

#### description

This class is used to generate client library files for a given service.

#### attributes

- `_metadata_provider` (StorageProvider): An instance of the `StorageProvider` class.
- `org_id` (str): The organization ID of the service.
- `service_id` (str): The service ID.
- `language` (str): The language of the client library. Default is `python`.
- `protodir` (str): The directory where the .proto files are located. Default is `~/.snet`.

#### methods

#### `__init__`

Initializes a new instance of the class. Initializes the attributes by arguments values.

###### args:

- `metadata_provider` (StorageProvider): An instance of the `StorageProvider` class.
- `org_id` (str): The organization ID of the service.
- `service_id` (str): The service ID.

###### returns:

- _None_

#### `generate_client_library`

Generates client library stub files based on specified organization and service ids, including:
- getting service metadata from Registry
- getting archived proto files from IPFS or FileCoin and extracting them
- compiling .proto file to stub file and saving it in a given directory

###### returns:

- _None_
133 changes: 133 additions & 0 deletions docs/main/concurrency_manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
## module: sdk.concurrency_manager

[Link](https://github.com/singnet/snet-sdk-python/blob/master/snet/sdk/concurrency_manager.py) to GitHub

Entities:
1. [ConcurrencyManager](#class-concurrencymanager)
- [\_\_init\_\_](#__init__)
- [concurrent_calls](#concurrent_calls)
- [get_token](#get_token)
- [__get_token](#__get_token)
- [__get_stub_for_get_token](#__get_stub_for_get_token)
- [__get_token_for_amount](#__get_token_for_amount)
- [record_successful_call](#record_successful_call)

### Class `ConcurrencyManager`

extends: -

is extended by: -

#### description

`ConcurrencyManager` provides a mechanism for managing the concurrency of service calls in the SDK.
It ensures that only a certain number of concurrent calls are made and handles the retrieval and management
of tokens for making service calls.

#### attributes

- `__concurrent_calls` (int): The number of concurrent calls allowed.
- `__token` (str): The token used for concurrent calls.
- `__planned_amount` (int): The planned amount for the payment.
- `__used_amount` (int): The amount used for the payment.

#### methods

#### `__init__`

Initializes a new instance of the class.

###### args:

- concurrent_calls (int): The number of concurrent calls allowed.

###### returns:

- _None_

#### `concurrent_calls`

decorator: `@property`

Returns the number of concurrent calls allowed.

###### returns:

- The number of concurrent calls allowed. (int)

#### `get_token`

Retrieves a token for making service calls.

###### args:

- `service_client` (ServiceClient): The service client instance.
- `channel` (PaymentChannel): The payment channel instance.
- `service_call_price` (int): The price of a service call.

###### returns:

- The token for making service calls. (str)

#### `__get_token`

Retrieves a token for a service call.

###### args:

- `service_client` (ServiceClient): The service client instance.
- `channel` (PaymentChannel): The payment channel instance.
- `service_call_price` (int): The price of a service call.
- `new_token` (bool): Whether is needed a new token. Defaults to `False`.

###### returns:

- The token for the service call. (str)

###### raises:

- grpc.RpcError: If an error occurs while retrieving the token.

#### `__get_stub_for_get_token`

Retrieves the gRPC service stub for the TokenServiceStub.

###### args:

- `service_client` (ServiceClient): The service client instance.

###### returns:

- The gRPC service stub for the TokenServiceStub. (ServiceStub)

#### `__get_token_for_amount`

Retrieves a token for a given amount from the token service.

This function retrieves a token for a given amount from the token service. It first retrieves the nonce
from the channel state, then it creates a stub for the token service using the `get_stub_for_get_token`
method. It then imports the `token_service_pb2` module and retrieves the current block number from the
service client's SDK web3 instance. It generates a message using the `solidity_keccak` method from the
`web3.Web3` class and generates signatures using the `generate_signature` method from the service client.
It creates a `TokenRequest` object with the necessary parameters and sends it to the token service using
the `GetToken` method of the stub. Finally, it returns the token reply object containing the token.

###### args:

- `service_client` (ServiceClient): The service client instance.
- `channel` (PaymentChannel): The payment channel instance.
- `amount` (int): The amount for which the token is requested.

###### returns:

- The token reply object containing the token. (Any)

#### `record_successful_call`

Increments the `__used_amount` attribute by 1 to record a successful call.

###### returns:

- _None_


Loading

0 comments on commit d3b0cc9

Please sign in to comment.