Ejemplo n.º 1
0
def userdeposit_maybe_increase_allowance(
    token_proxy: CustomToken,
    userdeposit_proxy: UserDeposit,
    orchestrator_address: Address,
    minimum_allowance: TokenAmount,
    maximum_allowance: TokenAmount,
) -> None:
    """Set the allowance of the corresponding smart contract of
    `userdeposit_proxy` to `required_allowance`.
    """
    given_token_address = token_proxy.address
    user_deposit_token_address = userdeposit_proxy.token_address("latest")

    if user_deposit_token_address != given_token_address:
        raise ValueError(
            f"The allowance for the user deposit contract must be increase on the "
            f"corresponding token. Given token: {to_hex(given_token_address)} "
            f"user deposit token: {to_hex(user_deposit_token_address)}.")

    current_allowance = token_proxy.allowance(
        orchestrator_address, Address(userdeposit_proxy.address), "latest")

    if minimum_allowance > current_allowance:
        # For the RDN token:
        #
        #     To change the approve amount you first have to reduce the addresses`
        #     allowance to zero by calling `approve(_spender, 0)` if it is not
        #     already 0 to mitigate the race condition described here:
        #     https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
        #
        token_proxy.approve(Address(userdeposit_proxy.address), TokenAmount(0))
        token_proxy.approve(Address(userdeposit_proxy.address),
                            maximum_allowance)
Ejemplo n.º 2
0
def userdeposit_maybe_deposit(
    userdeposit_proxy: UserDeposit,
    mint_greenlets: Set[Greenlet],
    target_address: Address,
    minimum_effective_deposit: TokenAmount,
    maximum_funding: TokenAmount,
) -> None:
    """Make a deposit at the given `target_address`.

    The amount of tokens depends on the scenario definition's settings.

    If the target address has a sufficient deposit, this is a no-op.

    TODO: Allow setting max funding parameter, similar to the token `funding_min` setting.
    """
    effective_balance = userdeposit_proxy.effective_balance(
        target_address, "latest")
    current_total_deposit = userdeposit_proxy.get_total_deposit(
        target_address, "latest")

    if maximum_funding < minimum_effective_deposit:
        raise ValueError(
            f"max_funding must be larger than minimum_effective_deposit, "
            f"otherwise the constraint can never be satisfied. Given "
            f"max_funding={maximum_funding} "
            f"minimum_effective_deposit={minimum_effective_deposit}")

    # Only do a deposit if the current effective balance is bellow the minimum.
    # When doing the deposit, top-up to max_funding to save transactions on the
    # next iterations.
    if effective_balance < minimum_effective_deposit:
        topup_amount = maximum_funding - effective_balance
        new_total_deposit = TokenAmount(current_total_deposit + topup_amount)

        # Wait for mint transactions, if necessary
        gevent.joinall(mint_greenlets, raise_error=True)

        userdeposit_proxy.deposit(
            target_address,
            new_total_deposit,
            userdeposit_proxy.client.get_confirmed_blockhash(),
        )
Ejemplo n.º 3
0
    def user_deposit(self, address: Address) -> UserDeposit:
        if not is_binary_address(address):
            raise ValueError("address must be a valid address")

        with self._user_deposit_creation_lock:
            if address not in self.address_to_user_deposit:
                self.address_to_user_deposit[address] = UserDeposit(
                    jsonrpc_client=self.client,
                    user_deposit_address=address,
                    contract_manager=self.contract_manager,
                )

        return self.address_to_user_deposit[address]
Ejemplo n.º 4
0
def check_rdn_deposits(raiden, user_deposit_proxy: UserDeposit):
    """ Check periodically for RDN deposits in the user-deposits contract """
    while True:
        rei_balance = user_deposit_proxy.effective_balance(
            raiden.address, "latest")
        rdn_balance = to_rdn(rei_balance)
        if rei_balance < MIN_REI_THRESHOLD:
            click.secho(
                (f"WARNING\n"
                 f"Your account's RDN balance of {rdn_balance} is below the "
                 f"minimum threshold. Provided that you have either a monitoring "
                 f"service or a path finding service activated, your node is not going "
                 f"to be able to pay those services which may lead to denial of service or "
                 f"loss of funds."),
                fg="red",
            )

        gevent.sleep(CHECK_RDN_MIN_DEPOSIT_INTERVAL)
Ejemplo n.º 5
0
def check_rdn_deposits(
    raiden: "RaidenService", user_deposit_proxy: UserDeposit
) -> None:  # pragma: no unittest
    """ Check periodically for RDN deposits in the user-deposits contract """
    while True:
        rei_balance = user_deposit_proxy.effective_balance(raiden.address, BLOCK_ID_LATEST)
        rdn_balance = to_rdn(rei_balance)
        if rei_balance < MIN_REI_THRESHOLD:
            click.secho(
                (
                    f"WARNING\n"
                    f"Your account's RDN balance deposited in the UserDepositContract of "
                    f"{rdn_balance} is below the minimum threshold {to_rdn(MIN_REI_THRESHOLD)}. "
                    f"Provided that you have either a monitoring service or a path "
                    f"finding service activated, your node is not going to be able to "
                    f"pay those services which may lead to denial of service or loss of funds."
                ),
                fg="red",
            )

        gevent.sleep(CHECK_RDN_MIN_DEPOSIT_INTERVAL)
Ejemplo n.º 6
0
def deploy_smoketest_contracts(
    client: JSONRPCClient,
    chain_id: ChainID,
    contract_manager: ContractManager,
    token_address: AddressHex,
) -> Dict[str, Address]:
    if client.eth_node is EthClient.GETH:
        client.web3.geth.personal.unlockAccount(client.web3.eth.accounts[0],
                                                DEFAULT_PASSPHRASE)
    elif client.eth_node is EthClient.PARITY:
        client.web3.parity.personal.unlockAccount(client.web3.eth.accounts[0],
                                                  DEFAULT_PASSPHRASE)

    contract_proxy, _ = client.deploy_single_contract(
        contract_name=CONTRACT_SECRET_REGISTRY,
        contract=contract_manager.get_contract(CONTRACT_SECRET_REGISTRY),
        constructor_parameters=None,
    )
    secret_registry_address = Address(
        to_canonical_address(contract_proxy.address))

    secret_registry_constructor_arguments = (
        to_checksum_address(secret_registry_address),
        chain_id,
        TEST_SETTLE_TIMEOUT_MIN,
        TEST_SETTLE_TIMEOUT_MAX,
        UINT256_MAX,
    )

    contract_proxy, _ = client.deploy_single_contract(
        contract_name=CONTRACT_TOKEN_NETWORK_REGISTRY,
        contract=contract_manager.get_contract(
            CONTRACT_TOKEN_NETWORK_REGISTRY),
        constructor_parameters=secret_registry_constructor_arguments,
    )
    token_network_registry_address = Address(
        to_canonical_address(contract_proxy.address))

    service_registry_constructor_arguments = (
        token_address,
        EMPTY_ADDRESS,
        int(500e18),
        6,
        5,
        180 * SECONDS_PER_DAY,
        1000,
        200 * SECONDS_PER_DAY,
    )
    service_registry_contract, _ = client.deploy_single_contract(
        contract_name=CONTRACT_SERVICE_REGISTRY,
        contract=contract_manager.get_contract(CONTRACT_SERVICE_REGISTRY),
        constructor_parameters=service_registry_constructor_arguments,
    )
    service_registry_address = Address(
        to_canonical_address(service_registry_contract.address))

    user_deposit_contract, _ = client.deploy_single_contract(
        contract_name=CONTRACT_USER_DEPOSIT,
        contract=contract_manager.get_contract(CONTRACT_USER_DEPOSIT),
        constructor_parameters=(token_address, UINT256_MAX),
    )
    user_deposit_address = Address(
        to_canonical_address(user_deposit_contract.address))

    monitoring_service_contract, _ = client.deploy_single_contract(
        contract_name=CONTRACT_MONITORING_SERVICE,
        contract=contract_manager.get_contract(CONTRACT_MONITORING_SERVICE),
        constructor_parameters=(
            token_address,
            service_registry_address,
            user_deposit_address,
            token_network_registry_address,
        ),
    )
    monitoring_service_address = Address(
        to_canonical_address(monitoring_service_contract.address))

    one_to_n_contract, _ = client.deploy_single_contract(
        contract_name=CONTRACT_ONE_TO_N,
        contract=contract_manager.get_contract(CONTRACT_ONE_TO_N),
        constructor_parameters=(user_deposit_address, chain_id,
                                service_registry_address),
    )
    one_to_n_address = Address(to_canonical_address(one_to_n_contract.address))

    proxy_manager = ProxyManager(
        rpc_client=client,
        contract_manager=contract_manager,
        metadata=ProxyManagerMetadata(
            token_network_registry_deployed_at=GENESIS_BLOCK_NUMBER,
            filters_start_at=GENESIS_BLOCK_NUMBER,
        ),
    )
    user_deposit_proxy = UserDeposit(
        jsonrpc_client=client,
        user_deposit_address=UserDepositAddress(
            to_canonical_address(user_deposit_contract.address)),
        contract_manager=contract_manager,
        proxy_manager=proxy_manager,
        block_identifier=BLOCK_ID_LATEST,
    )
    transaction_hash = user_deposit_proxy.init(
        monitoring_service_address=MonitoringServiceAddress(
            monitoring_service_address),
        one_to_n_address=OneToNAddress(one_to_n_address),
        given_block_identifier=BLOCK_ID_LATEST,
    )
    assert is_tx_hash_bytes(transaction_hash)

    addresses = {
        CONTRACT_SECRET_REGISTRY: secret_registry_address,
        CONTRACT_TOKEN_NETWORK_REGISTRY: token_network_registry_address,
        CONTRACT_SERVICE_REGISTRY: service_registry_address,
        CONTRACT_USER_DEPOSIT: user_deposit_address,
        CONTRACT_MONITORING_SERVICE: monitoring_service_address,
        CONTRACT_ONE_TO_N: one_to_n_address,
    }

    return addresses