Example #1
0
def fund_account(
        address: str,
        eth_allowance: int,
        token_allowance: int,
        token_contract: Contract,
        web3: Web3,
        wait_for_transaction,
        faucet_private_key: str,
):
    log.info('Funding account {}'.format(address))
    tx = create_signed_transaction(
        faucet_private_key,
        web3,
        to=address,
        value=eth_allowance
    )
    tx_hash = web3.eth.sendRawTransaction(tx)
    wait_for_transaction(tx_hash)

    if token_allowance > 0:
        tx = create_signed_contract_transaction(
            faucet_private_key,
            token_contract,
            'transfer',
            [
                address,
                token_allowance
            ]
        )
        tx_hash = web3.eth.sendRawTransaction(tx)
        wait_for_transaction(tx_hash)
Example #2
0
def test_wait_for_transaction(
        web3: Web3,
        patched_contract,
        receiver_address: str,
        faucet_private_key: str,
):
    tx = create_signed_transaction(
        faucet_private_key,
        web3,
        to=receiver_address
    )
    tx_hash = web3.eth.sendRawTransaction(tx)
    tx_receipt = wait_for_transaction(web3, tx_hash)
    assert tx_receipt
def test_cheating_client(
        doggo_proxy,
        web3,
        session: Session,
        wait_for_transaction,
        http_doggo_url: str,
        faucet_private_key: str,
        faucet_address: str,
        receiver_privkey: str,
        receiver_address: str
):
    balance = web3.eth.getBalance(doggo_proxy.channel_manager.receiver)
    assert balance > 0
    # remove all receiver's eth
    tx = create_signed_transaction(
        receiver_privkey,
        web3,
        faucet_address,
        balance - NETWORK_CFG.GAS_PRICE * NETWORK_CFG.POT_GAS_LIMIT
    )
    tx_hash = web3.eth.sendRawTransaction(tx)
    wait_for_transaction(tx_hash)
    response = session.get(http_doggo_url)
    # proxy is expected to return 502 - it has no funds
    assert response.status_code == 502
    tx = create_signed_transaction(
        faucet_private_key,
        web3,
        receiver_address,
        balance - NETWORK_CFG.GAS_PRICE * NETWORK_CFG.POT_GAS_LIMIT
    )
    tx_hash = web3.eth.sendRawTransaction(tx)
    wait_for_transaction(tx_hash)
    response = session.get(http_doggo_url)
    # now it should proceed normally
    assert response.status_code == 200
Example #4
0
def sweep_account(
        private_key: str,
        faucet_address: str,
        token_contract: Contract,
        web3: Web3,
        wait_for_transaction
):
    address = privkey_to_addr(private_key)
    log.info('Sweeping account {}'.format(address))
    token_balance = token_contract.call().balanceOf(address)
    if token_balance > 0:
        tx = create_signed_contract_transaction(
            private_key,
            token_contract,
            'transfer',
            [
                faucet_address,
                token_balance
            ]
        )
        try:
            tx_hash = web3.eth.sendRawTransaction(tx)
        except ValueError as e:
            if e.args[0]['message'].startswith('Insufficient funds.'):
                pass
            else:
                raise
        else:
            wait_for_transaction(tx_hash)
            assert token_contract.call().balanceOf(address) == 0

    balance = web3.eth.getBalance(address)
    if balance < NETWORK_CFG.POT_GAS_LIMIT * NETWORK_CFG.GAS_PRICE:
        return
    tx = create_signed_transaction(
        private_key,
        web3,
        to=faucet_address,
        value=balance - NETWORK_CFG.POT_GAS_LIMIT * NETWORK_CFG.GAS_PRICE,
        gas_limit=NETWORK_CFG.POT_GAS_LIMIT
    )
    tx_hash = web3.eth.sendRawTransaction(tx)
    wait_for_transaction(tx_hash)
    assert web3.eth.getBalance(address) == 0, (
        'Sweeping of account {} (private key {}) failed.'.format(address, private_key)
    )
Example #5
0
def test_create_signed_transaction():
    # Mock to simulate the example from EIP 155.
    # https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids
    class Web3Mock:
        class VersionMock:
            network = 1

        class EthMock:
            class ContractMock:
                def _prepare_transaction(self, *args, **kwargs):
                    return {'data': b''}

            defaultAccount = web3_empty

            def contract(self, *args, **kwargs):
                return self.ContractMock()

            def getTransactionCount(self, *args, **kwargs):
                return 9

            def getCode(self, *args, **kwargs):
                """Need to implement this to fake contract existence check"""
                return '0x123456789abcdef'

        version = VersionMock()
        eth = EthMock()

    # TODO: replace with proper mock
    web3 = Web3Mock()
    private_key = '0x4646464646464646464646464646464646464646464646464646464646464646'
    address = '0x3535353535353535353535353535353535353535'

    tx = create_signed_transaction(
        private_key,
        web3,
        to=address,
        value=10**18,
        gas_limit=21000,
        gas_price=20 * denoms.gwei
    )
    tx_expected = \
        '0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0' \
        'b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620' \
        'aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83'
    assert tx == tx_expected