def deploy(
        self,
        contract_name: str,
        args=None,
    ):
        if args is None:
            args = list()
        contract_interface = self.contract_manager.get_contract(
            contract_name,
        )

        # Instantiate and deploy contract
        contract = self.web3.eth.contract(
            abi=contract_interface['abi'],
            bytecode=contract_interface['bin'],
        )

        # Get transaction hash from deployed contract
        txhash = self.send_deployment_transaction(contract, args)

        # Get tx receipt to get contract address
        log.debug(
            f'Deploying {contract_name} txHash={encode_hex(txhash)}, '
            f'contracts version {self.contract_manager.contracts_version}',
        )
        receipt = check_succesful_tx(self.web3, txhash, self.wait)
        log.info(
            '{0} address: {1}. Gas used: {2}'.format(
                contract_name,
                receipt['contractAddress'],
                receipt['gasUsed'],
            ),
        )
        return receipt
def standard_token_network_contract(
    web3,
    contracts_manager,
    token_network_registry_contract,
    standard_token_contract,
    contract_deployer_address,
):
    """Return instance of a deployed TokenNetwork for HumanStandardToken."""
    txid = token_network_registry_contract.functions.createERC20TokenNetwork(
        standard_token_contract.address, ).transact(
            {'from': contract_deployer_address})
    tx_receipt = check_succesful_tx(web3, txid)
    assert len(tx_receipt['logs']) == 1
    event_abi = contracts_manager.get_event_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY,
        EVENT_TOKEN_NETWORK_CREATED,
    )
    decoded_event = get_event_data(event_abi, tx_receipt['logs'][0])
    assert decoded_event is not None
    assert is_address(decoded_event['args']['token_address'])
    assert is_address(decoded_event['args']['token_network_address'])
    token_network_address = decoded_event['args']['token_network_address']
    token_network_abi = contracts_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK)
    return web3.eth.contract(abi=token_network_abi,
                             address=token_network_address)
def deprecation_test(
    ctx,
    private_key,
    rpc_provider,
    wait,
    gas_price,
    gas_limit,
):
    """ Turn on the deprecation switch and see channel opening fails """
    setup_ctx(ctx, private_key, rpc_provider, wait, gas_price, gas_limit)
    deployer = ctx.obj['deployer']

    # We deploy the Raiden Network contracts and register a token network
    token_amount = MAX_ETH_CHANNEL_PARTICIPANT * 6
    (
        token_network_registry,
        token_network,
        token_contract,
    ) = deprecation_test_setup(deployer, token_amount)

    log.info('Checking that channels can be opened and deposits can be made.')

    # Check that we can open channels and deposit on behalf of A and B
    # Some arbitrary Ethereum addresses
    A = '0x6AA63296FA94975017244769F00F0c64DB7d7115'
    B = '0xc9a4fad99B6d7D3e48D18d2585470cd8f27FA61e'
    channel_identifier = open_and_deposit(A, B, token_network, deployer)
    log.info('Seding transaction to activate the deprecation switch.')

    # Activate deprecation switch
    assert token_network.functions.safety_deprecation_switch().call() is False
    txhash = token_network.functions.deprecate().transact(
        deployer.transaction, )
    log.debug(f'Deprecation txHash={encode_hex(txhash)}')
    check_succesful_tx(deployer.web3, txhash, deployer.wait)
    assert token_network.functions.safety_deprecation_switch().call() is True

    log.info(
        'Checking that channels cannot be opened anymore and no more deposits are allowed.'
    )

    # Check that we cannot open more channels or deposit
    C = '0x5a23cedB607684118ccf7906dF3e24Efd2964719'
    D = '0x3827B9cDc68f061aa614F1b97E23664ef3b9220A'
    open_and_deposit(C, D, token_network, deployer, channel_identifier, False)

    log.info('Deprecation switch test OK.')
def register_token_network(
    web3: Web3,
    caller: str,
    token_registry_abi: Dict,
    token_registry_address: str,
    token_address: str,
    wait=10,
    gas_limit=4000000,
    gas_price=10,
):
    """Register token with a TokenNetworkRegistry contract."""
    token_network_registry = web3.eth.contract(
        abi=token_registry_abi,
        address=token_registry_address,
    )
    txhash = token_network_registry.functions.createERC20TokenNetwork(
        token_address,
    ).transact(
        {
            'from': caller,
            'gas': gas_limit,
            'gasPrice': gas_price * denoms.gwei,
        },
    )
    log.debug(
        "calling createERC20TokenNetwork(%s) txHash=%s" %
        (
            token_address,
            encode_hex(txhash),
        ),
    )
    (receipt, _) = check_succesful_tx(web3, txhash, wait)

    token_network_address = token_network_registry.functions.token_to_token_networks(
        token_address,
    ).call()
    token_network_address = to_checksum_address(token_network_address)

    print(
        'TokenNetwork address: {0} Gas used: {1}'.format(
            token_network_address,
            receipt['gasUsed'],
        ),
    )
    return token_network_address
Esempio n. 5
0
 def f(initial_amount: int, decimals: int, token_name: str,
       token_symbol: str):
     token_contract = deploy_token_contract(initial_amount, decimals,
                                            token_name, token_symbol)
     txid = token_network_registry_contract.functions.createERC20TokenNetwork(
         token_contract.address, ).transact(
             {'from': contract_deployer_address})
     (tx_receipt, _) = check_succesful_tx(web3, txid)
     assert len(tx_receipt['logs']) == 1
     event_abi = contracts_manager.get_event_abi(
         CONTRACT_TOKEN_NETWORK_REGISTRY,
         EVENT_TOKEN_NETWORK_CREATED,
     )
     decoded_event = get_event_data(event_abi, tx_receipt['logs'][0])
     assert decoded_event is not None
     assert is_address(decoded_event['args']['token_address'])
     assert is_address(decoded_event['args']['token_network_address'])
     token_network_address = decoded_event['args']['token_network_address']
     token_network_abi = contracts_manager.get_contract_abi(
         CONTRACT_TOKEN_NETWORK)
     return web3.eth.contract(abi=token_network_abi,
                              address=token_network_address)
def deprecation_test_setup(deployer, token_amount):
    deployed_contracts = deploy_raiden_contracts(deployer)['contracts']

    token_network_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY, )
    token_network_registry = deployer.web3.eth.contract(
        abi=token_network_registry_abi,
        address=deployed_contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]['address'],
    )

    token_decimals = 18
    multiplier = 10**token_decimals
    token_supply = 10**6 * multiplier
    token_amount = int(token_amount * multiplier)

    deployed_token = deploy_token_contract(
        deployer,
        token_supply,
        token_decimals,
        'TestToken',
        'TTT',
        CONTRACT_CUSTOM_TOKEN,
    )
    token_address = deployed_token[CONTRACT_CUSTOM_TOKEN]
    token_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_CUSTOM_TOKEN)
    token_contract = deployer.web3.eth.contract(
        abi=token_abi,
        address=token_address,
    )

    # Mint some tokens for the owner
    txhash = token_contract.functions.mint(token_amount).transact(
        deployer.transaction, )

    log.debug(f'Minting tokens txHash={encode_hex(txhash)}')
    check_succesful_tx(deployer.web3, txhash, deployer.wait)
    assert token_contract.functions.balanceOf(
        deployer.owner).call() >= token_amount

    abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)
    token_network_address = register_token_network(
        web3=deployer.web3,
        caller=deployer.owner,
        token_registry_abi=abi,
        token_registry_address=deployed_contracts[
            CONTRACT_TOKEN_NETWORK_REGISTRY]['address'],
        token_address=token_address,
        wait=deployer.wait,
    )

    token_network_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK)
    token_network = deployer.web3.eth.contract(
        abi=token_network_abi,
        address=token_network_address,
    )
    log.info(
        f'Registered the token and created a TokenNetwork contract at {token_network_address}.',
    )

    txhash = token_contract.functions.approve(token_network.address,
                                              token_amount).transact(
                                                  deployer.transaction, )
    log.debug(
        f'Approving tokens for the TokenNetwork contract txHash={encode_hex(txhash)}'
    )
    check_succesful_tx(deployer.web3, txhash, deployer.wait)

    assert token_contract.functions.allowance(
        deployer.owner,
        token_network.address,
    ).call() >= token_amount
    log.info(
        f'Approved {token_amount} tokens for the TokenNetwork contract '
        f'from owner {deployer.owner}.', )

    return (token_network_registry, token_network, token_contract)
def open_and_deposit(
    A,
    B,
    token_network,
    deployer,
    channel_identifier=None,
    txn_success_status=True,
):
    try:
        txhash = token_network.functions.openChannel(
            A, B, DEPLOY_SETTLE_TIMEOUT_MIN).transact(deployer.transaction, )
        log.debug(
            f'Opening a channel between {A} and {B} txHash={encode_hex(txhash)}'
        )
        check_succesful_tx(deployer.web3, txhash, deployer.wait)

        # Get the channel identifier
        channel_identifier = token_network.functions.getChannelIdentifier(
            A, B).call()
        success_status = True
    except ValueError as ex:
        success_status = False
        log.info(f'Cannot open a new channel {ex}')

    assert txn_success_status == success_status, \
        f'openChannel txn status is {success_status} instead of {txn_success_status}'

    assert channel_identifier is not None
    try:
        txhash = token_network.functions.setTotalDeposit(
            channel_identifier,
            A,
            int(MAX_ETH_CHANNEL_PARTICIPANT / 2),
            B,
        ).transact(deployer.transaction, )
        log.debug(
            f'Depositing {MAX_ETH_CHANNEL_PARTICIPANT} tokens for {A} in a channel with '
            f'identifier={channel_identifier} and partner= {B} txHash={encode_hex(txhash)}',
        )
        check_succesful_tx(deployer.web3, txhash, deployer.wait)
        success_status = True
    except ValueError as ex:
        success_status = False
        log.info(
            f'Cannot deposit more tokens in channel={channel_identifier}, {ex}'
        )

    assert txn_success_status == success_status, \
        f'setTotalDeposit txn status is {success_status} instead of {txn_success_status}'

    try:
        txhash = token_network.functions.setTotalDeposit(
            channel_identifier,
            B,
            int(MAX_ETH_CHANNEL_PARTICIPANT / 2),
            A,
        ).transact(deployer.transaction, )
        log.debug(
            f'Depositing {MAX_ETH_CHANNEL_PARTICIPANT} tokens for {B} in a channel with '
            f'identifier={channel_identifier} and partner= {A} txHash={encode_hex(txhash)}',
        )
        check_succesful_tx(deployer.web3, txhash, deployer.wait)
        success_status = True
    except ValueError as ex:
        success_status = False
        log.info(
            f'Cannot deposit more tokens in channel={channel_identifier}, {ex}'
        )

    assert txn_success_status == success_status, \
        f'setTotalDeposit txn status is {success_status} instead of {txn_success_status}'

    return channel_identifier