def test_deploy_script_register(
    web3: Web3,
    channel_participant_deposit_limit: int,
    token_network_deposit_limit: int,
    deployed_raiden_info: DeployedContracts,
    token_address: HexAddress,
) -> None:
    """ Run token register function used in the deployment script

    This checks if register_token_network() works correctly in the happy case,
    to make sure no code dependencies have been changed, affecting the deployment script.
    This does not check however that the cli command works correctly.
    """
    # normal deployment
    gas_limit = 5860000
    deployer = ContractDeployer(web3=web3,
                                private_key=FAUCET_PRIVATE_KEY,
                                gas_limit=gas_limit,
                                gas_price=1,
                                wait=10)

    deployed_contracts_raiden = deployed_raiden_info
    token_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)
    token_registry_address = deployed_contracts_raiden["contracts"][
        CONTRACT_TOKEN_NETWORK_REGISTRY]["address"]
    token_network_address = deployer.register_token_network(
        token_registry_abi=token_registry_abi,
        token_registry_address=token_registry_address,
        token_address=token_address,
        channel_participant_deposit_limit=channel_participant_deposit_limit,
        token_network_deposit_limit=token_network_deposit_limit,
    )
    assert token_network_address is not None
    assert isinstance(token_network_address, str)
def test_deploy_script_register_unexpected_limits(
    web3,
    token_network_deposit_limit,
    channel_participant_deposit_limit,
    token_address,
    deployed_raiden_info,
):
    """ Run token register function used in the deployment script

    without the expected channel participant deposit limit.
    """
    deployer = ContractDeployer(
        web3=web3,
        private_key=FAUCET_PRIVATE_KEY,
        gas_limit=GAS_LIMIT,
        gas_price=1,
        wait=10,
        contracts_version="0.4.0",
    )

    token_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY
    )
    token_registry_address = deployed_raiden_info["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
        "address"
    ]
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=None,
            token_network_deposit_limit=token_network_deposit_limit,
        )
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=channel_participant_deposit_limit,
            token_network_deposit_limit=None,
        )
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=channel_participant_deposit_limit,
            token_network_deposit_limit=token_network_deposit_limit,
        )
def test_deploy_script_register_without_limit(
    token_address: HexAddress,
    deployer_0_4_0: ContractDeployer,
    deployed_raiden_info_0_4_0: DeployedContracts,
) -> None:
    """ Run token register function used in the deployment script

    This checks if register_token_network() works correctly in the happy case for 0.4.0 version,
    to make sure no code dependencies have been changed, affecting the deployment script.
    This does not check however that the cli command works correctly.
    """
    token_registry_abi = deployer_0_4_0.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)
    token_registry_address = deployed_raiden_info_0_4_0["contracts"][
        CONTRACT_TOKEN_NETWORK_REGISTRY]["address"]
    token_network_address = deployer_0_4_0.register_token_network(
        token_registry_abi=token_registry_abi,
        token_registry_address=token_registry_address,
        token_address=token_address,
        channel_participant_deposit_limit=None,
        token_network_deposit_limit=None,
    )
    assert token_network_address is not None
    assert isinstance(token_network_address, str)
def test_deploy_script_register_missing_limits(
    token_network_deposit_limit: int,
    channel_participant_deposit_limit: int,
    deployed_raiden_info: DeployedContracts,
    token_address: HexAddress,
    deployer: ContractDeployer,
) -> None:
    """ Run token register function used in the deployment script

    without the expected channel participant deposit limit.
    """
    token_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)
    token_registry_address = deployed_raiden_info["contracts"][
        CONTRACT_TOKEN_NETWORK_REGISTRY]["address"]
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=None,
            token_network_deposit_limit=token_network_deposit_limit,
        )
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=channel_participant_deposit_limit,
            token_network_deposit_limit=None,
        )
    with pytest.raises(ValueError):
        deployer.register_token_network(
            token_registry_abi=token_registry_abi,
            token_registry_address=token_registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=None,
            token_network_deposit_limit=None,
        )
def main(keystore_file: str, contract_version: str, password: str, output: str, rpc_url: str):
    web3 = Web3(HTTPProvider(rpc_url, request_kwargs={'timeout': 60}))
    web3.middleware_onion.inject(geth_poa_middleware, layer=0)

    with open(keystore_file, 'r') as keystore:
        encrypted_key = keystore.read()
        private_key = web3.eth.account.decrypt(encrypted_key, password)
        account = Account.privateKeyToAccount(private_key)

    if private_key is None:
        print('No private key found')
        exit(1)

    owner = account.address

    if web3.eth.getBalance(owner) == 0:
        print('Account with insuficient funds.')
        exit(1)

    print(f'Deploying contracts {contract_version} on behalf of {owner}')

    deployer = ContractDeployer(
        web3=web3,
        private_key=private_key,
        gas_limit=GAS_LIMIT,
        gas_price=GAS_PRICE,
        wait=120,
        contracts_version=contract_version,
    )

    print('Deploying Raiden contracts')
    try:
        deployed_contracts_info = deployer.deploy_raiden_contracts(
            max_num_of_token_networks=UNLIMITED,
            settle_timeout_min=SETTLE_TIMEOUT_MIN,
            settle_timeout_max=SETTLE_TIMEOUT_MAX,
            reuse_secret_registry_from_deploy_file=None
        )
    except Exception as err:
        print(f'Raiden Contract Deployment failed: {err}')
        exit(1)

    deployed_contracts = {
        contract_name: info['address']
        for contract_name, info in deployed_contracts_info['contracts'].items()
    }

    try:
        deployer.store_and_verify_deployment_info_raiden(
            deployed_contracts_info=deployed_contracts_info
        )
    except Exception as err:
        print(f'Contract verification failed: {err}')
        exit(1)

    token_network_registry_address = deployed_contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]
    print(f'Registry contract deployed @ {token_network_registry_address}')

    print('Deploying TTT contract')
    try:
        ttt_abi, ttt_token_address = deploy_token(deployer)
    except Exception as err:
        print(f'TTT deployment failed: {err}')
        exit(1)

    print(f'Deployed TTT contract @ {ttt_token_address}')

    print('Registering TTT')
    try:
        deployer.register_token_network(
            token_registry_abi=ttt_abi,
            token_registry_address=token_network_registry_address,
            token_address=ttt_token_address,
            channel_participant_deposit_limit=UNLIMITED,
            token_network_deposit_limit=UNLIMITED,
        )
    except Exception as err:
        print(f'Registration of TTT failed: {err}')
        exit(1)

    print('Deploying SVT contract')
    try:
        svt_abi, svt_token_address = deploy_token(deployer, 'ServiceToken', 'SVT')
    except Exception as err:
        print(f'SVT Deployment failed: {err}')
        exit(1)

    print(f'Deployed SVT contract @ {svt_token_address}')

    print('Deploying Raiden service contracts')
    try:
        deployed_service_contracts_info = deployer.deploy_service_contracts(
            token_address=svt_token_address,
            user_deposit_whole_balance_limit=UNLIMITED,
            service_registry_controller=owner,
            initial_service_deposit_price=INITIAL_SERVICE_DEPOSIT_PRICE,
            service_deposit_bump_numerator=SERVICE_DEPOSIT_BUMP_NUMERATOR,
            service_deposit_bump_denominator=SERVICE_DEPOSIT_BUMP_DENOMINATOR,
            decay_constant=SERVICE_DEPOSIT_DECAY_CONSTANT,
            min_price=SERVICE_DEPOSIT_MIN_PRICE,
            registration_duration=SERVICE_REGISTRATION_DURATION,
            token_network_registry_address=token_network_registry_address
        )
    except Exception as err:
        print(f'Service contract deployment failed: {err}')
        exit(1)

    try:
        deployer.store_and_verify_deployment_info_services(
            deployed_contracts_info=deployed_service_contracts_info,
            token_address=svt_token_address,
            user_deposit_whole_balance_limit=UNLIMITED,
            token_network_registry_address=token_network_registry_address,
        )
    except Exception as err:
        print(f'Service contract verification failed: {err}')
        exit(1)

    if os.path.exists("user_deposit_address"):
        os.remove("user_deposit_address")

    print(f'Storing deployment info to {output}')

    try:
        with open(output, 'w+') as address_file:
            contracts_info = deployed_service_contracts_info['contracts']
            user_deposit_address = contracts_info[CONTRACT_USER_DEPOSIT]['address']
            one_to_n_address = contracts_info[CONTRACT_ONE_TO_N]['address']
            address_file.write(f'#!/bin/sh\n')
            address_file.write(f'export TTT_TOKEN_ADDRESS={ttt_token_address}\n')
            address_file.write(f'export SVT_TOKEN_ADDRESS={svt_token_address}\n')
            address_file.write(f'export USER_DEPOSIT_ADDRESS={user_deposit_address}\n')
            address_file.write(f'export TOKEN_NETWORK_REGISTRY_ADDRESS={token_network_registry_address}\n')
            address_file.write(f'export TOKEN_NETWORK_REGISTRY_ADDRESS={token_network_registry_address}\n')
            address_file.write(f'export ONE_TO_N_ADDRESS={one_to_n_address}\n')
            address_file.close()
    except Exception as err:
        print(f'Writing environment variables failed: {err}')

    print('done')