Skip to content

Commit fcaf4d2

Browse files
committed
add topup_account to api
1 parent 44bfd8d commit fcaf4d2

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

convex_api/convex_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ def request_funds(self, amount, account):
123123
raise ValueError(f'request_funds: returned account is not correct {result["address"]}')
124124
return result['amount']
125125

126+
def topup_account(self, account, min_balance=1000000, retry_count=2):
127+
"""
128+
Topup an account from the block chain faucet, so that the balance of the account is above or equal to
129+
the `min_balanace`.
130+
131+
:param number amount: The amount of funds to request
132+
:param Account account: The account to receive funds for
133+
:param number min_balance: Minimum account balance that will allowed before a topup occurs
134+
:Param number retry_count: The number of times the faucet will be called to get above or equal to the `min_balance`
135+
136+
:returns: The amount transfered to the account
137+
138+
"""
139+
140+
request_amount = min(9000000, min_balance)
141+
retry_count = min(5, retry_count)
142+
transfer_amount = 0
143+
while min_balance > self.get_balance(account) and retry_count > 0:
144+
transfer_amount += self.request_funds(request_amount, account)
145+
retry_count -= 1
146+
return transfer_amount
147+
126148
def get_address(self, function_name, address_account):
127149
"""
128150

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from convex_api.account import Account
1212
from convex_api.convex_api import ConvexAPI
1313

14-
from tests.helpers import auto_topup_account
15-
1614
logging.basicConfig(level=logging.DEBUG)
1715
logging.getLogger('urllib3').setLevel(logging.INFO)
1816

@@ -59,5 +57,5 @@ def convex(convex_url):
5957
@pytest.fixture(scope='module')
6058
def other_account(convex):
6159
account = Account.create_new()
62-
auto_topup_account(convex, account)
60+
convex.topup_account(account)
6361
return account

tests/helpers.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/intergration/test_convex_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def test_convex_api_request_funds(convex_url, test_account):
3939
request_amount = convex.request_funds(amount, test_account)
4040
assert(request_amount == amount)
4141

42+
def test_convex_api_topup_account(convex_url):
43+
convex = ConvexAPI(convex_url)
44+
account = Account.create_new()
45+
topup_amount = TEST_FUNDING_AMOUNT
46+
amount = convex.topup_account(account, topup_amount)
47+
assert(amount >= topup_amount)
48+
49+
account = Account.create_new()
50+
amount = convex.topup_account(account)
51+
assert(amount >= 0)
52+
4253
def test_convex_get_account_info(convex_url, test_account):
4354
convex = ConvexAPI(convex_url)
4455
info = convex.get_account_info(test_account)

tests/intergration/test_convex_break.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from eth_utils import add_0x_prefix
1111

1212

13-
from tests.helpers import auto_topup_account
14-
1513
from convex_api.account import Account
1614
from convex_api.convex_api import ConvexAPI
1715
from convex_api.exceptions import ConvexAPIError
@@ -35,7 +33,7 @@ def test_convex_recursion(convex, test_account):
3533
)
3634
)
3735
"""
38-
auto_topup_account(convex, test_account)
36+
convex.topup_account(test_account)
3937
result = convex.send(contract, test_account)
4038
address_list.append(add_0x_prefix(result['value']))
4139
for index in range(0, chain_length):
@@ -81,12 +79,12 @@ def test_schedule_transfer(convex, test_account, other_account):
8179
"""
8280
# (call contract-address (tx-to to-address amount))
8381

84-
auto_topup_account(convex, test_account)
85-
auto_topup_account(convex, other_account)
82+
convex.topup_account(test_account)
83+
convex.topup_account(other_account, 8000000)
8684
result = convex.send(contract, test_account)
8785
contract_address = add_0x_prefix(result['value'])
88-
convex.transfer(contract_address, 8000000, other_account)
89-
auto_topup_account(convex, test_account)
86+
convex.transfer(contract_address, 800000, other_account)
87+
convex.topup_account(test_account)
9088
result = convex.send(f'(call {contract_address} (tx-delay {other_account.address} 1000))', test_account)
9189
print(result)
9290
result = convex.send(f'(call {contract_address} (show-schedule))', test_account)

tests/intergration/test_convex_multi_thread.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from convex_api.convex_api import ConvexAPI
1111
from convex_api.exceptions import ConvexAPIError
1212

13-
from tests.helpers import auto_topup_account
14-
1513
def process_on_convex(convex, test_account):
1614
values = []
1715
inc_values = []
@@ -33,7 +31,7 @@ def test_convex_api_multi_thread(convex_url, test_account):
3331

3432
process_count = 4
3533
convex = ConvexAPI(convex_url)
36-
auto_topup_account(convex, test_account)
34+
convex.topup_account(test_account)
3735
process_list = []
3836
for index in range(process_count):
3937
proc = Process(target=process_on_convex, args=(convex, test_account))

0 commit comments

Comments
 (0)