def test_channel_erc20_create(owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)
    token.transact({"from": owner}).transfer(receiver, 20)

    # Cannot create a channel if tokens were not approved
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)

    assert token_instance.call().balanceOf(uraiden_instance.address) == 0

    # Can create a channel with deposit 0
    uraiden_instance.transact({"from": sender}).createChannel(receiver, 0)

    # Approve token allowance
    token_instance.transact({"from": sender}).approve(uraiden_instance.address, deposit)

    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).createChannel(0x0, deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).createChannel('0x0', deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).createChannel(fake_address, deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).createChannel(receiver, -3)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).createChannel(receiver, MAX_UINT192 + 1)

    # Create channel
    uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)
def test_channel_erc20_create_delegate(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        delegate_instance,
        get_block):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Delegate contract is a trusted contract
    assert uraiden_instance.call().trusted_contracts(delegate_instance.address)

    # Fund delegate with tokens
    token_instance.transact({"from": owner}).transfer(delegate_instance.address, deposit + 100)

    # Create channel through delegate
    txn_hash = delegate_instance.transact({"from": sender}).createChannelERC20(sender, receiver, deposit)

    # Make sure the channel was created between sender and receiver
    open_block_number = get_block(txn_hash)
    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[0] == uraiden_instance.call().getKey(
        sender,
        receiver,
        open_block_number
    )
    assert channel_data[1] == deposit
    assert channel_data[2] == 0
    assert channel_data[3] == 0
def test_channel_erc20_topup_delegate(
        owner,
        uraiden_instance,
        token_instance,
        delegate_instance,
        get_channel):
    deposit = 1000
    deposit_topup = 200
    (sender, receiver, open_block_number) = get_channel(uraiden_instance, token_instance, deposit)[:3]

    # Delegate contract is a trusted contract
    assert uraiden_instance.call().trusted_contracts(delegate_instance.address)

    # Fund delegate with tokens
    token_instance.transact({"from": owner}).transfer(delegate_instance.address, deposit_topup)

    # Top up channel through delegate
    delegate_instance.transact({"from": sender}).topUpERC20(
        sender,
        receiver,
        open_block_number,
        deposit_topup
    )

    # Check channel deposit
    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == deposit + deposit_topup
def test_channel_erc20_event(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        event_handler,
        txn_gas,
        print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver) = get_accounts(2)
    deposit = 1000
    gas_used = 0

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)

    # Approve token allowance
    txn_hash_approve = token_instance.transact({"from": sender}).approve(uraiden_instance.address, deposit)
    gas_used += txn_gas(txn_hash_approve)

    # Create channel
    txn_hash = uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)

    # Check creation event
    ev_handler.add(
        txn_hash,
        uraiden_events['created'],
        checkCreatedEvent(sender, receiver, deposit)
    )
    ev_handler.check()

    print_gas(txn_hash, 'channel_20_create', gas_used)
def test_channel_erc223_event(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        event_handler,
        print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver) = get_accounts(2)
    deposit = 1000
    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)

    txn_hash = token_instance.transact({"from": sender}).transfer(
        uraiden_instance.address,
        1000,
        txdata
    )

    # Check creation event
    ev_handler.add(
        txn_hash,
        uraiden_events['created'],
        checkCreatedEvent(sender, receiver, deposit)
    )
    ev_handler.check()

    print_gas(txn_hash, 'channel_223_create')
def test_channel_erc20_create_bounty_limit(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_block):
    token = token_instance
    (sender, receiver) = get_accounts(2)

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, channel_deposit_bugbounty_limit + 1)

    # Approve token allowance
    token_instance.transact({"from": sender}).approve(
        uraiden_instance.address,
        channel_deposit_bugbounty_limit + 1
    )

    pre_balance = token_instance.call().balanceOf(uraiden_instance.address)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(
            receiver,
            channel_deposit_bugbounty_limit + 1
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(
            receiver,
            channel_deposit_bugbounty_limit + 100
        )

    txn_hash = uraiden_instance.transact({"from": sender}).createChannel(
        receiver,
        channel_deposit_bugbounty_limit
    )

    post_balance = pre_balance + channel_deposit_bugbounty_limit
    assert token_instance.call().balanceOf(uraiden_instance.address) == post_balance
    open_block_number = get_block(txn_hash)

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
def test_channel_erc223_create_bounty_limit(
        get_block,
        owner,
        get_accounts,
        uraiden_instance,
        token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, channel_deposit_bugbounty_limit + 1)

    pre_balance = token_instance.call().balanceOf(uraiden_instance.address)

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 10,
            txdata
        )

    txn_hash = token_instance.transact({"from": sender}).transfer(
        uraiden_instance.address,
        channel_deposit_bugbounty_limit,
        txdata
    )

    post_balance = pre_balance + channel_deposit_bugbounty_limit
    assert token_instance.call().balanceOf(uraiden_instance.address) == post_balance
    open_block_number = get_block(txn_hash)

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
def test_topup_token_fallback_uint_conversion(
    contract_params,
    owner,
    get_accounts,
    uraiden_instance,
    token_instance,
    get_block):
    token = token_instance
    (sender, receiver) = get_accounts(2)

    # Make sure you have a fixture with a supply > 2 ** 192
    supply = contract_params['supply']
    deposit = 100
    top_up_deposit = supply - 100

    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, supply)
    assert token.call().balanceOf(sender) == supply

    # Open a channel with tokenFallback
    txn_hash = token_instance.transact({"from": sender}).transfer(uraiden_instance.address, deposit, txdata)
    open_block_number = get_block(txn_hash)

    assert token.call().balanceOf(uraiden_instance.address) == deposit
    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == deposit

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8)
    top_up_data = bytes.fromhex(top_up_data)

    # TopUp a channel with tokenFallback
    if deposit > 2 ** 192:
        with pytest.raises(tester.TransactionFailed):
            txn_hash = token_instance.transact({"from": sender}).transfer(
                uraiden_instance.address,
                top_up_deposit,
                top_up_data
            )
def test_topup_token_fallback_uint_conversion(contract_params, owner,
                                              get_accounts, uraiden_instance,
                                              token_instance, get_block):
    token = token_instance
    (sender, receiver) = get_accounts(2)

    # Make sure you have a fixture with a supply > 2 ** 192
    supply = contract_params['supply']
    deposit = 100
    top_up_deposit = supply - 100

    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, supply)
    assert token.call().balanceOf(sender) == supply

    # Open a channel with tokenFallback
    txn_hash = token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, deposit, txdata)
    open_block_number = get_block(txn_hash)

    assert token.call().balanceOf(uraiden_instance.address) == deposit
    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[1] == deposit

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(
        8)
    top_up_data = bytes.fromhex(top_up_data)

    # TopUp a channel with tokenFallback
    if deposit > 2**192:
        with pytest.raises(tester.TransactionFailed):
            txn_hash = token_instance.transact({
                "from": sender
            }).transfer(uraiden_instance.address, top_up_deposit, top_up_data)
def test_delegate_remove_trusted_contract(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        delegate_instance):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Fund delegate with tokens
    token_instance.transact({"from": owner}).transfer(delegate_instance.address, deposit * 3)

    # Create channel through delegate
    delegate_instance.transact({"from": sender}).createChannelERC20(sender, receiver, deposit)

    # Remove trusted contract
    uraiden_instance.transact({"from": owner}).removeTrustedContracts([
        delegate_instance.address
    ])

    # Delegate create channel should fail now
    with pytest.raises(tester.TransactionFailed):
        delegate_instance.transact({"from": sender}).createChannelERC20(sender, receiver, deposit)
def test_channel_topup_223_bounty_limit(
    get_accounts,
    owner,
    uraiden_instance,
    token_instance,
    get_channel):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 1
    channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8)
    top_up_data = bytes.fromhex(top_up_data)

    # See how many tokens we need to reach channel_deposit_bugbounty_limit
    added_deposit = channel_deposit_bugbounty_limit - channel_deposit

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, added_deposit + 1)

    pre_balance = token.call().balanceOf(uraiden_instance.address)

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            added_deposit + 1,
            top_up_data
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            added_deposit + 20,
            top_up_data
        )

    token_instance.transact({"from": sender}).transfer(
        uraiden_instance.address,
        added_deposit,
        top_up_data
    )

    post_balance = pre_balance + added_deposit
    assert token_instance.call().balanceOf(uraiden_instance.address) == post_balance

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
def test_channel_topup_223_bounty_limit(get_accounts, owner, uraiden_instance,
                                        token_instance, get_channel):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 1
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(
        8)
    top_up_data = bytes.fromhex(top_up_data)

    # See how many tokens we need to reach channel_deposit_bugbounty_limit
    added_deposit = channel_deposit_bugbounty_limit - channel_deposit

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, added_deposit + 1)

    pre_balance = token.call().balanceOf(uraiden_instance.address)

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, added_deposit + 1, top_up_data)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, added_deposit + 20, top_up_data)

    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, added_deposit, top_up_data)

    post_balance = pre_balance + added_deposit
    assert token_instance.call().balanceOf(
        uraiden_instance.address) == post_balance

    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
def test_channel_erc223_create(owner, get_accounts, uraiden_instance,
                               token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    deposit = 1000
    txdata = bytes.fromhex(receiver[2:].zfill(40))
    txdata_fake = txdata[1:]

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)
    token.transact({"from": owner}).transfer(receiver, 20)

    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(0x0, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(fake_address, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, -2, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, MAX_UINT256 + 1, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(empty_address, deposit, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, deposit, bytearray(10))
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, deposit, txdata_fake)

    # tokenFallback only callable by token
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': C
        }).tokenFallback(sender, 10, txdata)

    assert token_instance.call().balanceOf(uraiden_instance.address) == 0

    # Deposit 0 is possible now
    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, 0, txdata)

    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, deposit, txdata)
def test_channel_erc223_create(owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    deposit = 1000
    txdata = bytes.fromhex(sender[2:] + receiver[2:])
    txdata_fake = txdata[1:]

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)
    token.transact({"from": owner}).transfer(receiver, 20)

    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(0x0, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(fake_address, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(uraiden_instance.address, -2, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            MAX_UINT256 + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(empty_address, deposit, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            deposit,
            encode_hex(bytearray(10))
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            deposit,
            txdata_fake
        )

    # tokenFallback only callable by token
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': C}).tokenFallback(sender, 10, txdata)

    assert token_instance.call().balanceOf(uraiden_instance.address) == 0

    # Deposit 0 is possible now
    token_instance.transact({"from": sender}).transfer(uraiden_instance.address, 0, txdata)

    token_instance.transact({"from": sender}).transfer(uraiden_instance.address, deposit, txdata)