How to use the raiden.transfer.views function in raiden

To help you get started, we’ve selected a few raiden examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github raiden-network / raiden / raiden / network / transport / matrix.py View on Github external
def _queueids_to_queues(self) -> QueueIdsToQueues:
        chain_state = views.state_from_raiden(self._raiden_service)
        return views.get_all_messagequeues(chain_state)
github raiden-network / raiden / raiden / network / resolver / client.py View on Github external
token = task.target_state.transfer.token

    request = {
        "token": to_hex(token),
        "secrethash": to_hex(secret_request_event.secrethash),
        "amount": secret_request_event.amount,
        "payment_identifier": secret_request_event.payment_identifier,
        "payment_sender": to_hex(secret_request_event.recipient),
        "expiration": secret_request_event.expiration,
        "payment_recipient": to_hex(raiden.address),
        "chain_id": chain_state.chain_id,
    }

    # loop until we get a valid response from the resolver or until timeout
    while True:
        current_state = views.state_from_raiden(raiden)

        if secret_request_event.expiration < current_state.block_number:
            log.debug(
                "Stopped using resolver, transfer expired", resolver_endpoint=resolver_endpoint
            )
            return False

        response = None

        try:
            # before calling resolver, update block height
            request["chain_height"] = chain_state.block_number
            response = requests.post(resolver_endpoint, json=request)
        except requests.exceptions.RequestException:
            pass
github raiden-network / raiden / raiden / api / python.py View on Github external
if token_address and partner_address:
            channel_state = views.get_channelstate_for(
                chain_state=views.state_from_raiden(self.raiden),
                token_network_registry_address=registry_address,
                token_address=token_address,
                partner_address=partner_address,
            )

            if channel_state:
                result = [channel_state]
            else:
                result = []

        elif token_address:
            result = views.list_channelstate_for_tokennetwork(
                chain_state=views.state_from_raiden(self.raiden),
                token_network_registry_address=registry_address,
                token_address=token_address,
            )

        else:
            result = views.list_all_channelstate(chain_state=views.state_from_raiden(self.raiden))

        return result
github raiden-network / raiden / raiden / raiden_service.py View on Github external
def _initialize_channel_fees(self) -> None:
        """ Initializes the fees of all open channels to the latest set values.

        This includes a recalculation of the dynamic rebalancing fees.
        """
        chain_state = views.state_from_raiden(self)
        fee_config: MediationFeeConfig = self.config["mediation_fees"]
        token_addresses = views.get_token_identifiers(
            chain_state=chain_state, token_network_registry_address=self.default_registry.address
        )

        for token_address in token_addresses:
            channels = views.get_channelstate_open(
                chain_state=chain_state,
                token_network_registry_address=self.default_registry.address,
                token_address=token_address,
            )

            for channel in channels:
                # get the flat fee for this network if set, otherwise the default
                flat_fee = fee_config.get_flat_fee(channel.token_address)
                proportional_fee = fee_config.get_proportional_fee(channel.token_address)
                proportional_imbalance_fee = fee_config.get_proportional_imbalance_fee(
                    channel.token_address
github raiden-network / raiden / raiden / message_handler.py View on Github external
def handle_message_refundtransfer(raiden: "RaidenService", message: RefundTransfer) -> None:
        chain_state = views.state_from_raiden(raiden)
        from_transfer = lockedtransfersigned_from_message(message=message)

        role = views.get_transfer_role(
            chain_state=chain_state, secrethash=from_transfer.lock.secrethash
        )

        state_changes: List[StateChange] = []

        if role == "initiator":
            old_secret = views.get_transfer_secret(chain_state, from_transfer.lock.secrethash)
            is_secret_known = old_secret is not None and old_secret != ABSENT_SECRET

            state_changes.append(
                ReceiveTransferCancelRoute(
                    transfer=from_transfer,
                    balance_proof=from_transfer.balance_proof,
github raiden-network / raiden / raiden / connection_manager.py View on Github external
def _open_channels(self) -> bool:
        """ Open channels until there are `self.initial_channel_target`
        channels open. Do nothing if there are enough channels open already.

        Note:
            - This method must be called with the lock held.
        Return:
            - False if no channels could be opened
        """

        open_channels = views.get_channelstate_open(
            chain_state=views.state_from_raiden(self.raiden),
            token_network_registry_address=self.registry_address,
            token_address=self.token_address,
        )
        open_channels = [
            channel_state
            for channel_state in open_channels
            if channel_state.partner_state.address != self.BOOTSTRAP_ADDR
        ]
        funded_channels = [
            channel_state
            for channel_state in open_channels
            if channel_state.our_state.contract_balance >= self._initial_funding_per_partner
        ]
        nonfunded_channels = [
            channel_state
            for channel_state in open_channels
github raiden-network / raiden / raiden / raiden_service.py View on Github external
def connection_manager_for_token_network(
        self, token_network_address: TokenNetworkAddress
    ) -> ConnectionManager:
        if not is_binary_address(token_network_address):
            raise InvalidBinaryAddress("token address is not valid.")

        known_token_networks = views.get_token_network_addresses(
            views.state_from_raiden(self), self.default_registry.address
        )

        if token_network_address not in known_token_networks:
            raise InvalidBinaryAddress("token is not registered.")

        manager = self.tokennetworkaddrs_to_connectionmanagers.get(token_network_address)

        if manager is None:
            manager = ConnectionManager(self, token_network_address)
            self.tokennetworkaddrs_to_connectionmanagers[token_network_address] = manager

        return manager
github raiden-network / raiden / raiden / utils / gas_reserve.py View on Github external
token_network_address
        ).opening_channels_count
        num_opened_channels = len(
            views.get_channelstate_open(chain_state, registry_address, token_address)
        )
        num_closing_channels = len(
            views.get_channelstate_closing(chain_state, registry_address, token_address)
        )
        num_closed_channels = len(
            views.get_channelstate_closed(chain_state, registry_address, token_address)
        )
        num_settling_channels = len(
            views.get_channelstate_settling(chain_state, registry_address, token_address)
        )
        num_settled_channels = len(
            views.get_channelstate_settled(chain_state, registry_address, token_address)
        )

        gas_estimate += _get_required_gas_estimate(
            gas_measurements=measurements,
            opening_channels=num_opening_channels,
            opened_channels=num_opened_channels,
            closing_channels=num_closing_channels,
            closed_channels=num_closed_channels,
            settling_channels=num_settling_channels,
            settled_channels=num_settled_channels,
        )

    return gas_estimate
github raiden-network / raiden / raiden / connection_manager.py View on Github external
def log_open_channels(
    raiden: "RaidenService",
    registry_address: TokenNetworkRegistryAddress,
    token_address: TokenAddress,
    funds: TokenAmount,
) -> None:  # pragma: no unittest
    chain_state = views.state_from_raiden(raiden)
    open_channels = views.get_channelstate_open(
        chain_state=chain_state,
        token_network_registry_address=registry_address,
        token_address=token_address,
    )

    if open_channels:
        sum_deposits = views.get_our_deposits_for_token_network(
            views.state_from_raiden(raiden), registry_address, token_address
        )
        log.debug(
            "connect() called on an already joined token network",
            node=to_checksum_address(raiden.address),
            registry_address=to_checksum_address(registry_address),
            token_address=to_checksum_address(token_address),
            open_channels=len(open_channels),
github raiden-network / raiden / raiden / api / python.py View on Github external
raise InvalidBinaryAddress("Expected binary address format for token in channel close")

        if not all(map(is_binary_address, partner_addresses)):
            raise InvalidBinaryAddress(
                "Expected binary address format for partner in channel close"
            )

        valid_tokens = views.get_token_identifiers(
            chain_state=views.state_from_raiden(self.raiden),
            token_network_registry_address=registry_address,
        )
        if token_address not in valid_tokens:
            raise UnknownTokenAddress("Token address is not known.")

        chain_state = views.state_from_raiden(self.raiden)
        channels_to_close = views.filter_channels_by_partneraddress(
            chain_state=chain_state,
            token_network_registry_address=registry_address,
            token_address=token_address,
            partner_addresses=partner_addresses,
        )

        close_state_changes: List[StateChange] = [
            ActionChannelClose(canonical_identifier=channel_state.canonical_identifier)
            for channel_state in channels_to_close
        ]

        greenlets = set(self.raiden.handle_state_changes(close_state_changes))
        gevent.joinall(greenlets, raise_error=True)

        channel_ids = [channel_state.identifier for channel_state in channels_to_close]