Beispiel #1
0
def initialized_client_app(
        client_app: App,
        monkeypatch: MonkeyPatch,
        keyfiles: None,
        private_keys: List[PrivateKeyHex],
        addresses: List[Address],
        passwords: List[str],
        faucet_private_key: PrivateKeyHex,
        faucet_address: Address,
        web3: Web3,
        wait_for_transaction,
        token_contract: Contract,
        use_tester: bool
):
    i = 0

    fund_account(
        addresses[i],
        WEI_ALLOWANCE,
        REI_ALLOWANCE,
        token_contract,
        web3,
        wait_for_transaction,
        faucet_private_key
    )
    if use_tester:
        ethereum.tester.accounts.append(decode_hex(addresses[i]))
        ethereum.tester.keys.append(decode_hex(private_keys[i]))

    input_sequence = (value for value in [str(i), passwords[i]])
    mock_prompt(monkeypatch, input_sequence)
    client_app.start()

    assert client_app.account.unlocked
    assert client_app.session is not None

    request_original = uSession._request_resource

    def request_patched(self: uSession, method: str, url: str, **kwargs):
        if use_tester:
            self.client.context.web3.testing.mine(1)
        return request_original(self, method, url, **kwargs)
    monkeypatch.setattr(microraiden.client.session.Session, '_request_resource', request_patched)

    if use_tester:
        client_app.session.retry_interval = 0.1

    yield client_app

    client_app.drain(faucet_address, rdn_and_eth=True)
def test_account_load_file_funded(
        keyfiles: None, keyfile_paths: List[str], addresses: List[Address],
        passwords: List[str], web3: Web3, token_address: Address,
        token_contract: Contract, wait_for_transaction,
        faucet_private_key: PrivateKeyHex):
    i = 3
    fund_account(addresses[i], 5, 3, token_contract, web3,
                 wait_for_transaction, faucet_private_key)
    account = Account(keyfile_path=keyfile_paths[i],
                      password=passwords[i],
                      web3=web3,
                      token_address=token_address)

    assert account.wei_balance == 5
    assert account.rei_balance == 3
def test_account_drain(keyfile_paths: List[str],
                       private_keys: List[PrivateKeyHex], passwords: List[str],
                       web3: Web3, token_address: Address,
                       wait_for_transaction, token_contract: Contract,
                       keyfiles: None, addresses: List[Address],
                       faucet_address: Address,
                       faucet_private_key: PrivateKeyHex, use_tester: bool):
    i = 1
    fund_account(addresses[i], WEI_ALLOWANCE, 7, token_contract, web3,
                 wait_for_transaction, faucet_private_key)

    if use_tester:
        ethereum.tester.accounts.append(decode_hex(addresses[i]))
        ethereum.tester.keys.append(decode_hex(private_keys[i]))

    account = Account(keyfile_path=keyfile_paths[i],
                      password=passwords[i],
                      web3=web3,
                      token_address=token_address)
    assert account.unlocked
    assert account.wei_balance > 0
    assert account.rei_balance == 7

    account_rei = token_contract.call().balanceOf(account.address)
    faucet_rei = token_contract.call().balanceOf(faucet_address)

    tx_hash = account.drain_rdn(faucet_address)
    wait_for_transaction(tx_hash)

    account.sync_balances()
    assert account.rei_balance == 0
    assert account.wei_balance > 0
    assert token_contract.call().balanceOf(
        faucet_address) == faucet_rei + account_rei

    account_wei = web3.eth.getBalance(account.address)
    faucet_wei = web3.eth.getBalance(faucet_address)

    tx_hash = account.drain_eth(faucet_address)
    wait_for_transaction(tx_hash)

    account.sync_balances()
    assert account.wei_balance == 0
    assert (web3.eth.getBalance(faucet_address) == faucet_wei + account_wei -
            21000 * NETWORK_CFG.GAS_PRICE)
Beispiel #4
0
def test_app_insufficient_funds_retry(
        monkeypatch: MonkeyPatch, keyfiles: None, client_app: App,
        passwords: List[str], private_keys: List[PrivateKeyHex],
        addresses: List[Address], token_contract: Contract, web3: Web3,
        wait_for_transaction, faucet_private_key: PrivateKeyHex,
        faucet_address: Address, use_tester: bool):
    i = 3
    j = 4
    fund_account(addresses[j], WEI_ALLOWANCE, REI_ALLOWANCE, token_contract,
                 web3, wait_for_transaction, faucet_private_key)
    if use_tester:
        ethereum.tester.accounts.append(decode_hex(addresses[j]))
        ethereum.tester.keys.append(decode_hex(private_keys[j]))
    input_sequence = (value for value in [i, True, j, passwords[j]])
    mock_prompt(monkeypatch, input_sequence)

    client_app.start()

    sweep_account(private_keys[j], faucet_address, token_contract, web3,
                  wait_for_transaction)
Beispiel #5
0
def test_app_drain(monkeypatch: MonkeyPatch, keyfiles: None, client_app: App,
                   passwords: List[str], private_keys: List[PrivateKeyHex],
                   addresses: List[Address], web3: Web3,
                   token_contract: Contract, faucet_address: Address,
                   use_tester: bool, wait_for_transaction,
                   faucet_private_key: PrivateKeyHex):
    i = 6
    j = 3
    fund_account(addresses[i], WEI_ALLOWANCE, REI_ALLOWANCE, token_contract,
                 web3, wait_for_transaction, faucet_private_key)
    fund_account(addresses[j], WEI_ALLOWANCE, REI_ALLOWANCE, token_contract,
                 web3, wait_for_transaction, faucet_private_key)
    if use_tester:
        ethereum.tester.accounts.append(decode_hex(addresses[i]))
        ethereum.tester.keys.append(decode_hex(private_keys[i]))
        ethereum.tester.accounts.append(decode_hex(addresses[j]))
        ethereum.tester.keys.append(decode_hex(private_keys[j]))

    input_sequence = (
        value for value in
        [str(i), passwords[i], False, True,
         str(j), passwords[j], True])
    mock_prompt(monkeypatch, input_sequence)

    client_app.start()

    account_rei = token_contract.call().balanceOf(addresses[i])
    account_wei = web3.eth.getBalance(addresses[i])
    faucet_rei = token_contract.call().balanceOf(faucet_address)
    faucet_wei = web3.eth.getBalance(faucet_address)
    assert account_rei > 0
    assert account_wei > 0

    # Should drain nothing.
    client_app.drain(faucet_address)
    assert token_contract.call().balanceOf(addresses[i]) == account_rei
    assert web3.eth.getBalance(addresses[i]) == account_wei

    with pytest.raises(AssertionError):
        # Address not checksummed.
        client_app.drain(to_normalized_address(faucet_address), rdn=True)

    # Not confirmed.
    client_app.drain(faucet_address, rdn=True)
    assert token_contract.call().balanceOf(addresses[i]) == account_rei
    assert web3.eth.getBalance(addresses[i]) == account_wei

    # Confirmed.
    client_app.drain(faucet_address, rdn=True)
    assert token_contract.call().balanceOf(addresses[i]) == 0
    assert 0 < web3.eth.getBalance(addresses[i]) < account_wei
    assert token_contract.call().balanceOf(
        faucet_address) == faucet_rei + account_rei
    assert web3.eth.getBalance(faucet_address) == faucet_wei

    # Second round, different account.
    client_app.start()

    account_rei = token_contract.call().balanceOf(addresses[j])
    account_wei = web3.eth.getBalance(addresses[j])
    faucet_rei = token_contract.call().balanceOf(faucet_address)
    faucet_wei = web3.eth.getBalance(faucet_address)
    assert account_rei > 0
    assert account_wei > 0

    # Drain both ETH and RDN.
    client_app.drain(faucet_address, rdn_and_eth=True)
    assert token_contract.call().balanceOf(addresses[j]) == 0
    assert web3.eth.getBalance(addresses[j]) == 0
    assert token_contract.call().balanceOf(
        faucet_address) == faucet_rei + account_rei
    assert web3.eth.getBalance(faucet_address) > faucet_wei