2018-05-25 17:29:07 +00:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
import time
|
|
|
|
from tests import info
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkApi:
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-06-30 12:17:38 +00:00
|
|
|
self.ropsten_url = 'http://api-ropsten.etherscan.io/api?'
|
2018-06-07 15:13:37 +00:00
|
|
|
self.faucet_url = 'http://51.15.45.169:3001/donate'
|
|
|
|
self.chat_bot_url = 'http://offsite.chat:8099'
|
2018-05-25 17:29:07 +00:00
|
|
|
|
|
|
|
def get_transactions(self, address: str) -> dict:
|
2018-06-30 12:17:38 +00:00
|
|
|
method = self.ropsten_url + 'module=account&action=txlist&address=0x%s&sort=desc' % address
|
2018-05-25 17:29:07 +00:00
|
|
|
return requests.request('GET', url=method).json()['result']
|
|
|
|
|
2018-07-03 18:50:18 +00:00
|
|
|
def get_token_transaction(self, address: str) -> dict:
|
|
|
|
method = self.ropsten_url + 'module=account&action=tokentx&address=0x%s&sort=desc' % address
|
|
|
|
return requests.request('GET', url=method).json()['result']
|
|
|
|
|
2018-05-25 17:29:07 +00:00
|
|
|
def is_transaction_successful(self, transaction_hash: str) -> int:
|
2018-06-30 12:17:38 +00:00
|
|
|
method = self.ropsten_url + 'module=transaction&action=getstatus&txhash=%s' % transaction_hash
|
2018-05-25 17:29:07 +00:00
|
|
|
return not int(requests.request('GET', url=method).json()['result']['isError'])
|
|
|
|
|
|
|
|
def get_balance(self, address):
|
2018-06-30 12:17:38 +00:00
|
|
|
method = self.ropsten_url + 'module=account&action=balance&address=0x%s&tag=latest' % address
|
2018-05-25 17:29:07 +00:00
|
|
|
for i in range(5):
|
|
|
|
try:
|
|
|
|
return int(requests.request('GET', method).json()["result"])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2018-06-30 12:17:38 +00:00
|
|
|
def get_latest_block_number(self) -> int:
|
|
|
|
method = self.ropsten_url + 'module=proxy&action=eth_blockNumber'
|
|
|
|
return int(requests.request('GET', url=method).json()['result'], 0)
|
|
|
|
|
2018-05-25 17:29:07 +00:00
|
|
|
def find_transaction_by_hash(self, address: str, transaction_hash: str):
|
|
|
|
transactions = self.get_transactions(address=address)
|
|
|
|
for transaction in transactions:
|
|
|
|
if transaction['hash'] == transaction_hash:
|
|
|
|
info('Transaction is found in Ropsten network')
|
|
|
|
return
|
|
|
|
pytest.fail('Transaction is not found in Ropsten network')
|
|
|
|
|
2018-07-03 18:50:18 +00:00
|
|
|
def find_transaction_by_unique_amount(self, address, amount, token=False, wait_time=600):
|
2018-05-25 17:29:07 +00:00
|
|
|
counter = 0
|
|
|
|
while True:
|
|
|
|
if counter >= wait_time:
|
|
|
|
pytest.fail(
|
|
|
|
'Transaction with amount %s is not found in list of transactions, address is %s' %
|
|
|
|
(amount, address))
|
|
|
|
else:
|
|
|
|
counter += 10
|
|
|
|
time.sleep(10)
|
2018-07-03 18:50:18 +00:00
|
|
|
if token:
|
|
|
|
transactions = self.get_token_transaction(address=address)
|
|
|
|
else:
|
|
|
|
transactions = self.get_transactions(address=address)
|
2018-05-28 13:39:59 +00:00
|
|
|
info('Looking for a transaction with unique amount %s in list of transactions, address is %s' %
|
|
|
|
(amount, address))
|
2018-05-25 17:29:07 +00:00
|
|
|
for transaction in transactions:
|
2018-06-04 13:51:25 +00:00
|
|
|
if float(int(transaction['value']) / 10 ** 18) == float(amount):
|
2018-05-25 17:29:07 +00:00
|
|
|
info('Transaction with unique amount %s is found in list of transactions, address is %s' %
|
|
|
|
(amount, address))
|
|
|
|
return
|
|
|
|
|
|
|
|
def verify_balance_is_updated(self, initial_balance, recipient_address, wait_time=360):
|
|
|
|
counter = 0
|
|
|
|
while True:
|
|
|
|
if counter >= wait_time:
|
|
|
|
pytest.fail('Balance is not changed during %s seconds, funds were not received!' % wait_time)
|
|
|
|
elif initial_balance == self.get_balance(recipient_address):
|
|
|
|
counter += 10
|
|
|
|
time.sleep(10)
|
|
|
|
info('Waiting %s seconds for funds' % counter)
|
|
|
|
else:
|
|
|
|
info('Transaction is received')
|
|
|
|
return
|
|
|
|
|
2018-06-28 18:46:51 +00:00
|
|
|
def verify_balance_is(self, expected_balance: int, recipient_address: str, errors: list):
|
|
|
|
balance = self.get_balance(recipient_address)
|
|
|
|
if balance / 1000000000000000000 != expected_balance:
|
|
|
|
errors.append('Recipients balance is not updated on etherscan')
|
|
|
|
|
2018-05-25 17:29:07 +00:00
|
|
|
def faucet(self, address):
|
2018-06-07 15:13:37 +00:00
|
|
|
return requests.request('GET', '%s/0x%s' % (self.faucet_url, address)).json()
|
2018-05-25 17:29:07 +00:00
|
|
|
|
|
|
|
def get_donate(self, address, wait_time=300):
|
|
|
|
initial_balance = self.get_balance(address)
|
|
|
|
counter = 0
|
|
|
|
if initial_balance < 1000000000000000000:
|
|
|
|
response = self.faucet(address)
|
|
|
|
while True:
|
|
|
|
if counter >= wait_time:
|
|
|
|
pytest.fail("Donation was not received during %s seconds!" % wait_time)
|
|
|
|
elif self.get_balance(address) == initial_balance:
|
|
|
|
counter += 10
|
|
|
|
time.sleep(10)
|
|
|
|
info('Waiting %s seconds for donation' % counter)
|
|
|
|
else:
|
|
|
|
info('Got %s for %s' % (response["amount_eth"], address))
|
|
|
|
return
|
|
|
|
|
2018-03-31 20:05:11 +00:00
|
|
|
def start_chat_bot(self, chat_name: str, messages_number: int, interval: int = 1) -> list:
|
2018-06-07 15:13:37 +00:00
|
|
|
url = '%s/ping/%s?count=%s&interval=%s' % (self.chat_bot_url, chat_name, messages_number, interval)
|
2018-03-31 20:05:11 +00:00
|
|
|
text = requests.request('GET', url).text
|
|
|
|
return [i.split(maxsplit=5)[-1].strip('*') for i in text.splitlines()]
|