def test_buy_tokens_ledger_mock(self, ledger_mock): """Tests that we can buy Devise tokens with Ledger nano s HD wallet""" test_client_account = "0xA1C2684B68A98c9636FC22F3B4E4332eF35A2408" ledger = DeviseClient(account=test_client_account, auth_type='ledger') old_tokens_balance = ledger.dvz_balance ether_amount = .5 vrs = ( 28, 2182719146884808318922065288388701482638595064358707342427872633327257261733, 46015294558581125017244029788985539581305398667308472361832920130849890183038) ledger_mock().sign.return_value = vrs ledger_mock().get_account_index.return_value = 0 ledger.w3.eth.estimateGas = MagicMock(return_value=400000) ledger.w3.eth.generateGasPrice = MagicMock(return_value=100000000000) ledger.buy_eth_worth_of_tokens(ether_amount) assert ledger_mock().get_account_index.call_count == 1 ledger_mock().get_account_index.assert_has_calls([call(test_client_account)]) assert ledger_mock().sign.call_count == 1 ledger_mock().sign.assert_has_calls([call( b"\xea\x80\x85\x17Hv\xe8\x00\x83\x07\xa1 \x94\t\x87\xee'By\xc6pu5\xfa\xee\x0e!5\x85\x7f<2\x91\x88\x06\xf0[Y\xd3\xb2\x00\x00\x80", account_index=0)]) currentRate = ledger._token_sale_contract.functions.getCurrentRate().call() new_tokens_balance = ledger.dvz_balance tokens_purchased = ether_amount * currentRate assert new_tokens_balance == old_tokens_balance + tokens_purchased
def test_get_all_bidders(self, client, master_node): """Tests that we can get all the bids in the price auction""" # Add leptons lepton_hash = hashlib.sha1('hello world 1'.encode('utf8')).hexdigest() master_node.add_lepton(lepton_hash, None, 1.5123456789123456789) # client 1 buys and provisions tokens client.buy_eth_worth_of_tokens(2) client.provision(16000) assert client.get_all_bidders() == [] # client 1 leases the blockchain lease_prc = 1000 client.lease_all(lease_prc, 1) assert client.get_all_bidders() == [ {'address': '0xA1C2684B68A98c9636FC22F3B4E4332eF35A2408', 'limit_price': lease_prc, 'requested_seats': 1}] assert client.get_all_bidders(active=True) == [ {'address': '0xA1C2684B68A98c9636FC22F3B4E4332eF35A2408', 'limit_price': lease_prc, 'requested_seats': 1}] # Add another bidder leases the blockchain client2 = DeviseClient(private_key=TEST_KEYS[2]) client2.buy_eth_worth_of_tokens(2) client2.provision(16000) client2.lease_all(lease_prc + 1, 1) assert client.get_all_bidders(active=True) == [ {'address': client.w3.eth.accounts[2], 'limit_price': lease_prc + 1, 'requested_seats': 1}, {'address': '0xA1C2684B68A98c9636FC22F3B4E4332eF35A2408', 'limit_price': lease_prc, 'requested_seats': 1}] # client 2 escrow balances drops below bid price, no longer active client2.withdraw(client2.dvz_balance_escrow) assert client.get_all_bidders(active=True) == [ {'address': '0xA1C2684B68A98c9636FC22F3B4E4332eF35A2408', 'limit_price': lease_prc, 'requested_seats': 1}]
def test_client_call_only(self, client): blank_client = DeviseClient() assert blank_client.price_per_bit_current_term == client.price_per_bit_current_term assert blank_client.rent_per_seat_current_term == client.rent_per_seat_current_term assert blank_client.indicative_rent_per_seat_next_term == client.indicative_rent_per_seat_next_term assert blank_client.total_incremental_usefulness == client.total_incremental_usefulness with raises(AssertionError): blank_client.provision(1000)
def test_create_account(self, _): file_path, address = generate_account() assert os.path.exists(file_path) assert len(address) == 42 assert Web3.toChecksumAddress(address) == address assert file_path.endswith(address.lower() + '.json') client = DeviseClient(account=address) assert client._scan_for_keystore_file(address) == file_path
def test_buy_tokens_keyfile_with_password(self): """Tests that we can buy Devise tokens with ethers using a local encrypted key store file""" key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key_file.json') client_local_keyfile = DeviseClient(key_file=key_path, password='******') old_tokens_balance = client_local_keyfile.dvz_balance ether_amount = .5 client_local_keyfile.buy_eth_worth_of_tokens(ether_amount) currentRate = client_local_keyfile._token_sale_contract.functions.getCurrentRate().call() new_tokens_balance = client_local_keyfile.dvz_balance tokens_purchased = ether_amount * currentRate assert new_tokens_balance == old_tokens_balance + tokens_purchased
def owner_client(): """Devise Client fixture created using an account's private key""" client = DeviseOwner(private_key=TEST_KEYS[0]) net_id = client.w3.version.network if net_id == "1": raise RuntimeError("Cowardly refusing to run tests against MainNet!!") escrow_wallet_account = Web3().eth.accounts[3] client_wallet = DeviseClient(private_key=TEST_KEYS[3]) client_wallet._transact( client_wallet._token_contract.functions.approve(client_wallet._rental_contract.address, int(1e20)), {"from": escrow_wallet_account}) return client
def test_get_client_address(self, client): # current client is not a money account => never provisioned assert client.get_client_address(client.address) is None client.buy_tokens(100000) client.provision(100000) # client is not a current renter assert client.client_summary["current_term_seats"] == 0 # current client is now a money account assert client.get_client_address(client.address) == client.address # getting the money account for a beneficiary works client.designate_beneficiary(client.w3.eth.accounts[2]) assert client.get_client_address(client.w3.eth.accounts[2]) == client.address assert client.get_client_address(client.address) == client.address # once again from the DeviseClient of the beneficiary client_beneficiary = DeviseClient(private_key=TEST_KEYS[2]) assert client_beneficiary.get_client_address(client_beneficiary.address) == client.address
def test_no_contracts(self): try: with raises(RuntimeError): os.environ["ETHEREUM_NETWORK"] = "mainnet" client = DeviseClient() finally: os.environ["ETHEREUM_NETWORK"] = "ganache"
def client(): """Devise Client fixture created using an account's private key""" client = DeviseClient(private_key=TEST_KEYS[5]) net_id = client.w3.version.network if net_id == "1": raise RuntimeError("Cowardly refusing to run tests against MainNet!!") return client
def client_local_keyfile(): """Devise Client fixture created using an account's private key""" key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key_file.json') client = DeviseClient(key_file=key_path) net_id = client.w3.version.network if net_id == "1": raise RuntimeError("Cowardly refusing to run tests against MainNet!!") return client
def client_ledger(): """Devise Client fixture created to use Ledger nano s hardware wallet""" try: client = DeviseClient(account='0x879CEAA289334fE176B95ba341Dc2780ECE4da20', auth_type='ledger') except CommException: return None net_id = client.w3.version.network if net_id == "1": raise RuntimeError("Cowardly refusing to run tests against MainNet!!") return client
def client_local_keyfile(token_wallet_client): """Devise Client fixture created using an account's private key""" key_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'key_file.json') client = DeviseClient(key_file=key_path) # TODO Remove this and replace with real provisioning with ether in the tests token_wallet_client.transfer(client.address, 10000000) net_id = client.w3.version.network if net_id == "1": raise RuntimeError("Cowardly refusing to run tests against MainNet!!") return client
def test_provision_on_behalf_of(self, client): recipient_client = DeviseClient(private_key=TEST_KEYS[2]) sender_dvz_bal = client.dvz_balance prev_bal = recipient_client.dvz_balance client.provision_on_behalf_of(recipient_client.address, 1000) assert client.address != recipient_client.address assert recipient_client.dvz_balance_escrow == 1000 assert recipient_client.dvz_balance == prev_bal assert client.dvz_balance == sender_dvz_bal - 1000 client.provision_on_behalf_of(recipient_client.address, 1) assert recipient_client.dvz_balance_escrow == 1001 assert client.dvz_balance == sender_dvz_bal - 1001 assert recipient_client.client_summary == { 'beneficiary': recipient_client.address, 'client': recipient_client.address, 'current_term_seats': 0, 'dvz_balance': prev_bal, 'dvz_balance_escrow': 1001.0, 'historical_data_access': True, 'indicative_next_term_seats': 0, 'last_term_paid': None, 'power_user': True }
def test_transfer_ether(self, client): client2 = DeviseClient(private_key=TEST_KEYS[2]) prev_bal_recipient = client2.eth_balance client.transfer_ether(client2.address, 1) assert client2.eth_balance == prev_bal_recipient + 1