示例#1
0
def test_transferFrom(web3, chain):
    erc20_token, _ = chain.provider.get_or_deploy_contract('ERC20Token')

    t = eth_tester.EthereumTester()
    account2 = t.get_accounts()[1]
    account3 = t.get_accounts()[2]

    with pytest.raises(eth_tester.exceptions.TransactionFailed):
        erc20_token.functions.transferFrom(web3.eth.coinbase, account3, 10).transact({'from': account2})

    set_txn_hash = erc20_token.functions.approve(account2, 100).transact({'from': web3.eth.coinbase})
    chain.wait.for_receipt(set_txn_hash)

    set_txn_hash = erc20_token.functions.transferFrom(web3.eth.coinbase, account3, 10).transact({'from': account2})
    chain.wait.for_receipt(set_txn_hash)

    balance_account1 = erc20_token.functions.balanceOf(web3.eth.coinbase).call()
    balance_account2 = erc20_token.functions.balanceOf(account2).call()
    balance_account3 = erc20_token.functions.balanceOf(account3).call()
    allowance = erc20_token.functions.allowance(web3.eth.coinbase, account2).call()

    assert balance_account1 == 999990
    assert balance_account2 == 0
    assert balance_account3 == 10
    assert allowance == 90
示例#2
0
def test_freeze_balance(web3, chain):
    stable_coin, _ = chain.provider.get_or_deploy_contract('StableCoin')

    t = eth_tester.EthereumTester()
    account2 = t.get_accounts()[1]
    account3 = t.get_accounts()[2]

    set_txn_hash = stable_coin.functions.transfer(account2, 10).transact()
    chain.wait.for_receipt(set_txn_hash)

    set_txn_hash = stable_coin.functions.transfer(account3, 1).transact(
        {'from': account2})
    chain.wait.for_receipt(set_txn_hash)

    set_txn_hash = stable_coin.functions.freezeBalance(account2,
                                                       True).transact()
    chain.wait.for_receipt(set_txn_hash)

    with pytest.raises(eth_tester.exceptions.TransactionFailed):
        stable_coin.functions.transfer(account3,
                                       1).transact({'from': account2})

    set_txn_hash = stable_coin.functions.freezeBalance(account2,
                                                       False).transact()
    chain.wait.for_receipt(set_txn_hash)

    set_txn_hash = stable_coin.functions.transfer(account3, 1).transact(
        {'from': account2})
    chain.wait.for_receipt(set_txn_hash)
示例#3
0
def test_fail_transfer(web3, chain):
    erc20_token, _ = chain.provider.get_or_deploy_contract('ERC20Token')

    t = eth_tester.EthereumTester()
    account2 = t.get_accounts()[1]

    with pytest.raises(eth_tester.exceptions.TransactionFailed):
        erc20_token.functions.transfer(web3.eth.coinbase, 10).transact({'from': account2})
示例#4
0
def test_approve(web3, chain):
    erc20_token, _ = chain.provider.get_or_deploy_contract('ERC20Token')

    t = eth_tester.EthereumTester()
    account2 = t.get_accounts()[1]

    allowance = erc20_token.functions.allowance(web3.eth.coinbase, account2).call()
    assert allowance == 0

    set_txn_hash = erc20_token.functions.approve(account2, 100).transact({'from': web3.eth.coinbase})
    chain.wait.for_receipt(set_txn_hash)

    allowance = erc20_token.functions.allowance(web3.eth.coinbase, account2).call()
    assert allowance == 100
示例#5
0
def w3(signing_account):
    """
    Web3 object connected to the ethereum tester chain
    """
    chain = eth_tester.EthereumTester(eth_tester.PyEVMBackend())
    chain.send_transaction({
        "from": chain.get_accounts()[0],
        "to": signing_account.address,
        "gas": 21000,
        "value": 10_000_000,
    })
    w3 = Web3(EthereumTesterProvider(chain))
    w3.middleware_onion.add(parity_next_nonce)
    signing_middleware.install_signing_middleware(w3, signing_account)
    return w3
示例#6
0
def test_transfer(web3, chain):
    erc20_token, _ = chain.provider.get_or_deploy_contract('ERC20Token')

    t = eth_tester.EthereumTester()
    account2 = t.get_accounts()[1]

    old_balance = erc20_token.functions.balanceOf(account2).call()
    assert old_balance == 0

    set_txn_hash = erc20_token.functions.transfer(account2, 10).transact({'from': web3.eth.coinbase})
    chain.wait.for_receipt(set_txn_hash)

    sender_new_balance = erc20_token.functions.balanceOf(web3.eth.coinbase).call()
    assert sender_new_balance == 999990
    destination_new_balance = erc20_token.functions.balanceOf(account2).call()
    assert destination_new_balance == 10
def chain(pytestconfig):
    """
    The running ethereum tester chain
    Can be used as to manipulate the chain, e.g. chain.mine_block()
    see https://github.com/ethereum/eth-tester for more
    """
    expose_port = pytestconfig.getoption(EXPOSE_RPC_OPTION)
    if expose_port:
        if is_port_in_use(expose_port):
            raise EnvironmentError(
                f"The port {expose_port} is already in use on the machine.")
        # redirect stdout because otherwise the application prints every request and result
        # Get the eth-tester-rpc application
        application = get_application()
        rpc = application.rpc_methods

        # Remove the printing of every rpc replies to avoid flooding the tests outputs
        application = silent_stdout(application)
        application.rpc_methods = rpc

        # Start the server so that we can use rpc on http://localhost:expose_port
        # Silence the logging of every request from the handler to avoid flooding tests outputs
        class NoLoggingRequestHandler(WSGIRequestHandler):
            def log_request(self, code="-", size="-"):
                pass

        server = make_server(
            "localhost",
            int(expose_port),
            application,
            handler_class=NoLoggingRequestHandler,
        )
        spawn(server.serve_forever)

        # yield the eth_tester chain
        yield application.rpc_methods.client
    else:
        yield eth_tester.EthereumTester(eth_tester.PyEVMBackend())

    if pytestconfig.getoption(EXPOSE_RPC_OPTION):
        try:
            server.stop()
        except AttributeError:
            server.shutdown()
def test_upload_video(web3, chain):
    video_sharing, _ = chain.provider.get_or_deploy_contract('VideosSharing')

    t = eth_tester.EthereumTester()
    video_uploader = t.get_accounts()[1]

    index = video_sharing.functions.latest_videos_index(video_uploader).call()
    assert index == 0

    upload_video(video_sharing, chain, video_uploader, b'video-ipfs-path',
                 b"video title")

    index = video_sharing.functions.latest_videos_index(video_uploader).call()
    path = video_sharing.functions.videos_path(video_uploader, 0).call()
    title = video_sharing.functions.videos_title(video_uploader, 0).call()
    assert index == 1
    assert path == b'video-ipfs-path'
    assert title == b"video title"

    upload_video(video_sharing, chain, video_uploader, b'video-ipfs-path2',
                 b"video title2")

    index = video_sharing.functions.latest_videos_index(video_uploader).call()
    path = video_sharing.functions.videos_path(video_uploader, 1).call()
    title = video_sharing.functions.videos_title(video_uploader, 1).call()
    assert index == 2
    assert path == b'video-ipfs-path2'
    assert title == b"video title2"

    events = video_sharing.events.UploadVideo.createFilter(
        fromBlock=0).get_all_entries()

    assert events[0]['args']['_user'] == video_uploader
    assert events[0]['args']['_index'] == 0

    assert events[1]['args']['_user'] == video_uploader
    assert events[1]['args']['_index'] == 1
示例#9
0
def ethereum_tester():
    """Returns an instance of an Ethereum tester"""
    tester = eth_tester.EthereumTester(eth_tester.PyEVMBackend())
    return tester
示例#10
0
def tester():
    return eth_tester.EthereumTester(eth_tester.PyEVMBackend())
def test_like_video(web3, chain):
    video_sharing, _ = chain.provider.get_or_deploy_contract('VideosSharing')

    t = eth_tester.EthereumTester()
    manager = t.get_accounts()[0]
    video_uploader = t.get_accounts()[1]
    video_liker = t.get_accounts()[2]
    video_liker2 = t.get_accounts()[3]

    transfer_coins(video_sharing, chain, manager, video_liker, 100)
    transfer_coins(video_sharing, chain, manager, video_liker2, 100)
    transfer_coins(video_sharing, chain, manager, video_uploader, 50)
    upload_video(video_sharing, chain, video_uploader, b'video-ipfs-path',
                 b"video title")

    liked = video_sharing.functions.video_has_been_liked(
        video_liker, video_uploader, 0).call()
    assert liked == False
    liked2 = video_sharing.functions.video_has_been_liked(
        video_liker2, video_uploader, 0).call()
    assert liked2 == False
    video_uploader_balance = video_sharing.functions.balanceOf(
        video_uploader).call()
    assert video_uploader_balance == 50
    video_liker_balance = video_sharing.functions.balanceOf(video_liker).call()
    assert video_liker_balance == 100
    video_liker2_balance = video_sharing.functions.balanceOf(
        video_liker2).call()
    assert video_liker2_balance == 100
    aggregate_likes = video_sharing.functions.video_aggregate_likes(
        video_uploader, 0).call()
    assert aggregate_likes == 0

    like_video(video_sharing, chain, video_liker, video_uploader, 0)

    liked = video_sharing.functions.video_has_been_liked(
        video_liker, video_uploader, 0).call()
    assert liked == True
    liked2 = video_sharing.functions.video_has_been_liked(
        video_liker2, video_uploader, 0).call()
    assert liked2 == False
    video_uploader_balance = video_sharing.functions.balanceOf(
        video_uploader).call()
    assert video_uploader_balance == 51
    video_liker_balance = video_sharing.functions.balanceOf(video_liker).call()
    assert video_liker_balance == 99
    video_liker2_balance = video_sharing.functions.balanceOf(
        video_liker2).call()
    assert video_liker2_balance == 100
    aggregate_likes = video_sharing.functions.video_aggregate_likes(
        video_uploader, 0).call()
    assert aggregate_likes == 1

    like_video(video_sharing, chain, video_liker2, video_uploader, 0)

    liked = video_sharing.functions.video_has_been_liked(
        video_liker2, video_uploader, 0).call()
    assert liked == True
    liked2 = video_sharing.functions.video_has_been_liked(
        video_liker2, video_uploader, 0).call()
    assert liked2 == True
    video_uploader_balance = video_sharing.functions.balanceOf(
        video_uploader).call()
    assert video_uploader_balance == 52
    video_liker_balance = video_sharing.functions.balanceOf(video_liker).call()
    assert video_liker_balance == 99
    video_liker2_balance = video_sharing.functions.balanceOf(
        video_liker2).call()
    assert video_liker2_balance == 99
    aggregate_likes = video_sharing.functions.video_aggregate_likes(
        video_uploader, 0).call()
    assert aggregate_likes == 2

    events = video_sharing.events.LikeVideo.createFilter(
        fromBlock=0).get_all_entries()

    assert events[0]['args']['_video_liker'] == video_liker
    assert events[0]['args']['_video_uploader'] == video_uploader
    assert events[0]['args']['_index'] == 0

    assert events[1]['args']['_video_liker'] == video_liker2
    assert events[1]['args']['_video_uploader'] == video_uploader
    assert events[1]['args']['_index'] == 0

    with pytest.raises(eth_tester.exceptions.TransactionFailed):
        like_video(video_sharing, chain, video_liker, video_uploader, 0)
示例#12
0
def chain():
    """
    The running ethereum tester chain
    """
    return eth_tester.EthereumTester(eth_tester.PyEVMBackend())