Developer Interface

Interface

class pyatn_client.Atn(pk_file: str, pw_file: str, http_provider: str = 'https://rpc-test.atnio.net', deposit_strategy: Callable[[int], int] = <function Atn.<lambda>>)[source]

ATN Client Class

Usage:

>>> from pyatn_client import Atn
>>> atn = Atn(pk_file='<path to keystore file>',
>>>           pw_file='<path to password file>'
>>> )
>>> resp = atn.call_dbot_api(dbot_address='0xfd4F504F373f0af5Ff36D9fbe1050E6300699230',
>>>                          uri='/reg',
>>>                          method='POST',
>>>                          data={'theme': '中秋月更圆'})
<Response [200]>
call_dbot_api(dbot_address: str, uri: str, method: str, **requests_kwargs) → requests.models.Response[source]

Send the API’s HTTP request

Channel will be auto created if no channel or be topuped if insufficient balance in channel. The deposit value is determined by deposit_strategy. A signature of balance will be sent to DBot server to pay the price of the API.

Parameters:
  • dbot_address – address of the DBot contract
  • uri – uri of the endpoint
  • method – method of the endpoint
  • requests_kwargs – the other args for http request is same with requests
Returns:

Response object, http response of the API

Return type:

requests.Response

close_channel(dbot_address: str) → None[source]

Close the channel with the DBot

This function will block until the ChannelSettled event received from blockchain. If can not get valid close signature from DBot server, it will close the channel with balance 0 unilaterally. You can change the manual through redefine on_cooperative_close_denied

Parameters:dbot_address – address of the DBot contract
get_channel(dbot_address: str) → pyatn_client.microraiden.client.channel.Channel[source]

Get channel information from blockchain

Parameters:dbot_address – address of the DBot contract
Returns:Channel object, None is no channel
Return type:Channel
get_dbot_channel(dbot_address: str) → pyatn_client.microraiden.client.channel.Channel[source]

Get the channel information from DBot server

DBot server saves the balance proof which send from DBot user. DBot users can get this from DBot server, no need to save by themselves.

Parameters:dbot_address – address of the DBot contract
Returns:Channel object
Return type:pyatn_client.Channel
get_dbot_domain(dbot_address: str) → str[source]

Get the domain of DBot according the address of DBot contract on ATN blockchain.

The DBot server should be accessed on the domain. The domain may contain http:// or https:// protocol prefix.

Parameters:dbot_address – address of the DBot contract
Returns:domain of the DBot
Return type:str
get_dbot_name(dbot_address: str) → str[source]

Get the name of DBot according the address of DBot contract on ATN blockchain.

Parameters:dbot_address – address of the DBot contract
Returns:name of the DBot
Return type:str
get_dbot_owner(dbot_address: str) → str[source]

Get the owner account of DBot contract on ATN blockchain.

Close signature should be signed by the owner of DBot when DBot user want to cooperative close the channel with DBot.

Parameters:dbot_address – address of the DBot contract
Returns:account address of owner
Return type:str
get_price(dbot_address: str, uri: str, method: str) → int[source]

Get the price of a endpoint of the DBot

The unit of price is wei, the smallest unit of ATN. 1ATN = 10^18wei

Parameters:
  • dbot_address – address of the DBot contract
  • uri – uri of the endpoint
  • method – method of the endpoint
Returns:

price of the DBot’s endpoint

Return type:

int

on_cooperative_close_denied(dbot_address: str, response: requests.models.Response = None) → None[source]

Call back function when no valid closing signature received

This function will be called when can not get valid closing signature in method close_channel

Parameters:
  • dbot_address – address of the DBot contract
  • response – response from DBot server when request closing signature
open_channel(dbot_address: str, deposit: int) → pyatn_client.microraiden.client.channel.Channel[source]

Open a channel with the DBot

If a channel with the DBot has exist, the channel will be return directlly. This function will block until the ChannelCreated event received from blockchain.

Parameters:
  • dbot_address – address of the DBot contract
  • deposit – the value of deposit
Returns:

Channel object, None if failed.

Return type:

Channel

set_deposit_strategy(deposit_strategy: Callable[[int], int]) → None[source]

Change deposit strategy.

Channel will be auto created if no channel or be topuped if insufficient balance in channel when call_dbot_api be called. The deposit value is determined by deposit_strategy.

Parameters:deposit_strategy – callable function to determine the deposit value when create and topup channel. If it’s None, disable auto create or topup channel
settle_channel(dbot_address: str) → None[source]

Settle the channel to withdraw your deposit after you close the channel unilaterally

This function should be called after the end of the challenge peroid

Parameters:dbot_address – address of the DBot contract
topup_channel(dbot_address: str, deposit: int) → pyatn_client.microraiden.client.channel.Channel[source]

Topup the channel with the DBot

This function will block until the ChannelToppedUp event received from blockchain.

Parameters:
  • dbot_address – address of the DBot contract
  • deposit – the value of deposit
Returns:

Channel object, None if failed.

Return type:

Channel

uncooperative_close_channel(dbot_address: str, balance: int) → None[source]

Close the channel unilaterally

If DBot server has down, you can close the channel with any balance. DBot server will close the channel if it’s working and detect that you send a wrong balance.

Parameters:
  • dbot_address – address of the DBot contract
  • balance – used balance of the channel
wait_dbot_sync(dbot_address: str, retry_interval: int = 5, retry_times: int = 5) → None[source]

Wait the DBot server to sync the channel info on blockchain

DBot server will sync the channel info on blockchain, and

Parameters:
  • dbot_address – address of the DBot contract
  • retry_interval – interval time for retry, seconds
  • retry_times – how many times to retry

Exceptions

exception pyatn_client.AtnException[source]

Base exception for ATN Client