Skip to content

Commit 70bc105

Browse files
committed
add better docs
1 parent 289f6ba commit 70bc105

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

convex_api/convex_api.py

+72-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def create_account(self, account=None, sequence_retry_count=20):
5454
5555
:param `Account` account: :class:`.Account` object that you whish to use as the signing account.
5656
The :class:`.Account` object contains the public/private keys to access and submit commands
57-
on the convex network. The address property will be changed by this method.
57+
on the convex network.
5858
5959
If no object given, then this method will automatically create a new :class:`.Account` object.
6060
:type account: Account, optional
@@ -64,7 +64,7 @@ def create_account(self, account=None, sequence_retry_count=20):
6464
create accounts on the same node, then we will get sequence errors.
6565
:type sequence_retry_count: int, optional
6666
67-
:returns: A new :class:`.Account` object, or the current supplied :class:`.Account` object with a new `address` property set
67+
:returns: A new :class:`.Account` object, or copy of the :class:`.Account` object with a new `address` property value set
6868
6969
7070
.. code-block:: python
@@ -127,18 +127,86 @@ def create_account(self, account=None, sequence_retry_count=20):
127127
def load_account(self, name, account):
128128
"""
129129
130-
Load an account using the account name. If successfull return the account address
130+
Load an account using the account name. If successfull return the :class:`.Account` object with the address set.
131+
132+
This is a Query operation, so no convex tokens are used in loading the account.
133+
134+
:param str name: name of the account to load
135+
:param Account account: :class:`.Account` object to import
136+
137+
:results: :class:`.Account` object with the address and name set, if not found then return None
138+
139+
140+
.. code-block:: python
141+
142+
>>> # Create a new account with new public/priavte keys and address
143+
>>> import_account = Account.import_from_file('my_account.pem', 'secret')
144+
>>> account = convex.load_account('my_account, import_account)
145+
>>> print(account.name)
146+
my_account
147+
>>> print(account.address)
148+
930
131149
132150
"""
133151
address = self.resolve_account_name(name)
134152
if address:
135153
new_account = Account.import_from_account(account, address=address, name=name)
136154
return new_account
137155

156+
def setup_account(self, name, import_account):
157+
"""
158+
159+
Convenience method to create or load an account based on the account name.
160+
If the account name cannot be found then account will be created and account name registered,
161+
if the name is found, then account and it's address with that name will be loaded.
162+
163+
:param str name: name of the account to create or load
164+
:param Account account: :class:`.Account` object to import
165+
166+
:results: :class:`.Account` object with the address and name set, if not found then return None
167+
168+
**Note** This method calls the :meth:`.topup_account` method to get enougth funds to register an account name.
169+
170+
171+
.. code-block:: python
172+
173+
>>> import_account = Account.import_from_file('my_account.pem', 'secret')
174+
>>> # create or load the account named 'my_account'
175+
>>> account = convex.setup_account('my_account', import_account)
176+
>>> print(account.name)
177+
my_account
178+
179+
"""
180+
if self.resolve_account_name(name):
181+
account = self.load_account(name, import_account)
182+
else:
183+
account = self.create_account(account=import_account)
184+
self.topup_account(account)
185+
self.register_account_name(name, account)
186+
self.topup_account(account)
187+
return account
188+
138189
def register_account_name(self, name, account):
139190
"""
140191
141-
Register an account with the given address, with a name.
192+
Register an account address with an account name.
193+
194+
This call will submit to the CNS (Convex Name Service), a name in the format
195+
"`account.<your_name>`". You need to have some convex balance in your account, and
196+
a valid account address.
197+
198+
:param str name: name of the account to register
199+
:param Account account: :class:`.Account` object to register the account name
200+
201+
202+
>>> # create a new account
203+
>>> account = convex.create_account()
204+
>>> # add some convex tokens to the account
205+
>>> convex.topup_account(account)
206+
10000000
207+
>>> account = convex.register_account('my_new_account', account)
208+
>>> print(account.name)
209+
my_new_account
142210
143211
"""
144212
if account.address:

tests/intergration/test_account_api.py

+10
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ def test_account_name(convex_url, test_account_info):
4444
assert(account.name)
4545
assert(account.name == TEST_ACCOUNT_NAME)
4646
assert(convex.resolve_account_name(TEST_ACCOUNT_NAME) == account.address)
47+
48+
49+
def test_account_setup_account(convex_url, test_account_info):
50+
convex = ConvexAPI(convex_url)
51+
import_account = Account.import_from_bytes(test_account_info['private_bytes'])
52+
account = convex.setup_account(TEST_ACCOUNT_NAME, import_account)
53+
assert(account.address)
54+
assert(account.name)
55+
assert(account.name == TEST_ACCOUNT_NAME)
56+
assert(convex.resolve_account_name(TEST_ACCOUNT_NAME) == account.address)

0 commit comments

Comments
 (0)