def transfer_async(self, token_address, amount, target, identifier=None): # pylint: disable=too-many-arguments if not isinstance(amount, (int, long)): raise InvalidAmount('Amount not a number') if amount <= 0: raise InvalidAmount('Amount negative') if identifier is None: identifier = self.create_default_identifier(target, token_address) token_address_bin = safe_address_decode(token_address) target_bin = safe_address_decode(target) if not isaddress( token_address_bin) or token_address_bin not in self.tokens: raise InvalidAddress('token address is not valid.') if not isaddress(target_bin): raise InvalidAddress('target address is not valid.') token_manager = self.raiden.get_manager_by_token_address( token_address_bin) if not token_manager.has_path(self.raiden.address, target_bin): raise NoPathError('No path to address found') transfer_manager = token_manager.transfermanager async_result = transfer_manager.transfer_async( amount, target_bin, identifier=identifier, ) return async_result
def close(self, token_address, partner_address): """ Close a channel opened with `partner_address` for the given `token_address`. """ token_address_bin = safe_address_decode(token_address) partner_address_bin = safe_address_decode(partner_address) if not isaddress( token_address_bin) or token_address_bin not in self.tokens: raise InvalidAddress('token address is not valid.') if not isaddress(partner_address_bin): raise InvalidAddress('partner_address is not valid.') manager = self.raiden.get_manager_by_token_address(token_address_bin) channel = manager.get_channel_by_partner_address(partner_address_bin) first_transfer = None if channel.received_transfers: first_transfer = channel.received_transfers[-1] netting_channel = channel.external_state.netting_channel netting_channel.close( self.raiden.address, first_transfer, ) return channel
def open_channel_with_funding(self, token_address_hex, peer_address_hex, amount, settle_timeout=None, reveal_timeout=None): """Convenience method to open a channel. Args: token_address_hex (str): hex encoded address of the token for the channel. peer_address_hex (str): hex encoded address of the channel peer. amount (int): amount of initial funding of the channel. settle_timeout (int): amount of blocks for the settle time (if None use app defaults). reveal_timeout (int): amount of blocks for the reveal time (if None use app defaults). Return: netting_channel: the (newly opened) netting channel object. """ # Check, if peer is discoverable peer_address = safe_address_decode(peer_address_hex) token_address = safe_address_decode(token_address_hex) try: self._discovery.get(address_decoder(peer_address)) except KeyError: print("Error: peer {} not found in discovery".format( peer_address_hex)) return self._raiden.api.open( token_address, peer_address, settle_timeout=settle_timeout, reveal_timeout=reveal_timeout, ) return self._raiden.api.deposit(token_address, peer_address, amount)
def register_token( self, registry_address_hex: typing.AddressHex, token_address_hex: typing.AddressHex, retry_timeout: typing.NetworkTimeout = DEFAULT_RETRY_TIMEOUT, ) -> TokenNetwork: """ Register a token with the raiden token manager. Args: registry_address: registry address token_address_hex (string): a hex encoded token address. Returns: The token network proxy. """ registry_address = safe_address_decode(registry_address_hex) token_address = safe_address_decode(token_address_hex) registry = self._raiden.chain.token_network_registry(registry_address) token_network_address = registry.add_token(token_address) # Register the channel manager with the raiden registry waiting.wait_for_payment_network( self._raiden, registry.address, token_address, retry_timeout, ) return self._raiden.chain.token_network(token_network_address)
def settle(self, token_address, partner_address): """ Settle a closed channel with `partner_address` for the given `token_address`. """ token_address_bin = safe_address_decode(token_address) partner_address_bin = safe_address_decode(partner_address) if not isaddress( token_address_bin) or token_address_bin not in self.tokens: raise InvalidAddress('token address is not valid.') if not isaddress(partner_address_bin): raise InvalidAddress('partner_address is not valid.') manager = self.raiden.get_manager_by_token_address(token_address_bin) channel = manager.get_channel_by_partner_address(partner_address_bin) if channel.isopen: raise InvalidState('channel is still open.') netting_channel = channel.external_state.netting_channel if (self.raiden.chain.block_number() <= (channel.external_state.closed_block + netting_channel.detail(self.raiden.address)['settle_timeout'])): raise InvalidState('settlement period is not yet over.') netting_channel.settle() return channel
def channel_stats_for(self, token_address_hex, peer_address_hex, pretty=False): """ Collect information about sent and received transfers between yourself and your peer for the given token. Args: token_address_hex (string): hex encoded address of the token peer_address_hex (string): hex encoded address of the peer pretty (boolean): if True, print a json representation instead of returning a dict Returns: stats (dict): collected stats for the channel or None if pretty """ peer_address = safe_address_decode(peer_address_hex) token_address = safe_address_decode(token_address_hex) # Get the token token = self._chain.token(token_address) # Obtain the token manager graph = self._raiden.token_to_channelgraph[token_address] assert graph # Get the channel channel = graph.partneraddress_to_channel[peer_address] assert channel # Collect data stats = dict( transfers=dict( received=[t.transferred_amount for t in channel.received_transfers], sent=[t.transferred_amount for t in channel.sent_transfers], ), channel=(channel if not pretty else hexlify(channel.external_state.netting_channel.address)), lifecycle=dict( opened_at=channel.external_state.opened_block or 'not yet', can_transfer=channel.can_transfer, closed_at=channel.external_state.closed_block or 'not yet', settled_at=channel.external_state.settled_block or 'not yet', ), funding=channel.external_state.netting_channel.detail(), token=dict( our_balance=token.balance_of(self._raiden.address), partner_balance=token.balance_of(peer_address), name=token.proxy.name(), symbol=token.proxy.symbol(), ), ) stats['funding']['our_address'] = hexlify(stats['funding']['our_address']) stats['funding']['partner_address'] = hexlify(stats['funding']['partner_address']) if not pretty: return stats else: print(json.dumps(stats, indent=2, sort_keys=True))
def wait_for_contract(self, contract_address_hex, timeout=None): """ Wait until a contract is mined Args: contract_address_hex (string): hex encoded address of the contract timeout (int): time to wait for the contract to get mined Returns: True if the contract got mined, false otherwise """ contract_address = safe_address_decode(contract_address_hex) start_time = time.time() result = self._raiden.chain.client.eth_getCode(contract_address) current_time = time.time() while len(result) == 0: if timeout and start_time + timeout > current_time: return False result = self._raiden.chain.client.eth_getCode(contract_address) gevent.sleep(0.5) current_time = time.time() return len(result) > 0
def wait_for_contract(self, contract_address_hex, timeout=None): """Wait until a contract is mined Args: contract_address_hex (string): hex encoded address of the contract timeout (int): time to wait for the contract to get mined Returns: True if the contract got mined, false otherwise """ contract_address = safe_address_decode(contract_address_hex) start_time = time.time() result = self._raiden.chain.client.call( 'eth_getCode', contract_address, 'latest', ) current_time = time.time() while result == '0x': if timeout and start_time + timeout > current_time: return False result = self._raiden.chain.client.call( 'eth_getCode', contract_address, 'latest', ) gevent.sleep(0.5) current_time = time.time() return result != '0x'
def wait_for_contract(self, contract_address_hex, timeout=None): """ Wait until a contract is mined Args: contract_address_hex (string): hex encoded address of the contract timeout (int): time to wait for the contract to get mined Returns: True if the contract got mined, false otherwise """ contract_address = safe_address_decode(contract_address_hex) start_time = time.time() result = self._raiden.chain.client.web3.eth.getCode( to_checksum_address(contract_address), ) current_time = time.time() while not result: if timeout and start_time + timeout > current_time: return False result = self._raiden.chain.client.web3.eth.getCode( to_checksum_address(contract_address), ) gevent.sleep(0.5) current_time = time.time() return len(result) > 0
def register_token(self, registry_address_hex, token_address_hex): """ Register a token with the raiden token manager. Args: registry_address: registry address token_address_hex (string): a hex encoded token address. Returns: channel_manager: the channel_manager contract_proxy. """ registry = self._raiden.chain.registry(registry_address_hex) # Add the ERC20 token to the raiden registry token_address = safe_address_decode(token_address_hex) registry.add_token(token_address) # Obtain the channel manager for the token channel_manager = registry.manager_by_token(token_address) # Register the channel manager with the raiden registry waiting.wait_for_payment_network( self._raiden, registry.address, token_address, settings.DEFAULT_EVENTS_POLL_TIMEOUT, ) return channel_manager
def open_channel_with_funding( self, registry_address_hex, token_address_hex, peer_address_hex, total_deposit, settle_timeout=None, reveal_timeout=None, ): """ Convenience method to open a channel. Args: registry_address_hex (str): hex encoded address of the registry for the channel. token_address_hex (str): hex encoded address of the token for the channel. peer_address_hex (str): hex encoded address of the channel peer. total_deposit (int): amount of total funding for the channel. settle_timeout (int): amount of blocks for the settle time (if None use app defaults). reveal_timeout (int): amount of blocks for the reveal time (if None use app defaults). Return: netting_channel: the (newly opened) netting channel object. """ # Check, if peer is discoverable registry_address = safe_address_decode(registry_address_hex) peer_address = safe_address_decode(peer_address_hex) token_address = safe_address_decode(token_address_hex) try: self._discovery.get(peer_address) except KeyError: print('Error: peer {} not found in discovery'.format( peer_address_hex)) return None self._api.channel_open( registry_address, token_address, peer_address, settle_timeout=settle_timeout, reveal_timeout=reveal_timeout, ) return self._api.set_total_channel_deposit( registry_address, token_address, peer_address, total_deposit, )
def open_channel_with_funding( self, registry_address_hex, token_address_hex, peer_address_hex, total_deposit, settle_timeout=None, reveal_timeout=None, ): """ Convenience method to open a channel. Args: registry_address_hex (str): hex encoded address of the registry for the channel. token_address_hex (str): hex encoded address of the token for the channel. peer_address_hex (str): hex encoded address of the channel peer. total_deposit (int): amount of total funding for the channel. settle_timeout (int): amount of blocks for the settle time (if None use app defaults). reveal_timeout (int): amount of blocks for the reveal time (if None use app defaults). Return: netting_channel: the (newly opened) netting channel object. """ # Check, if peer is discoverable registry_address = safe_address_decode(registry_address_hex) peer_address = safe_address_decode(peer_address_hex) token_address = safe_address_decode(token_address_hex) try: self._discovery.get(peer_address) except KeyError: print('Error: peer {} not found in discovery'.format(peer_address_hex)) return None self._api.channel_open( registry_address, token_address, peer_address, settle_timeout=settle_timeout, reveal_timeout=reveal_timeout, ) return self._api.set_total_channel_deposit( registry_address, token_address, peer_address, total_deposit, )
def private_to_account(ctx, privatekey, password): # privatekey is provided in the console, so it's expected to be hexadecimal privatekey = safe_address_decode(privatekey) # cast the values to bytes because it is the expected type in the Crypto library password = bytes(password) privkey = bytes(privatekey) account = Account.new(password, key=privkey) print account.dump()
def private_to_account(ctx, privatekey, password): # privatekey is provided in the console, so it's expected to be hexadecimal privatekey = safe_address_decode(privatekey) # cast the values to bytes because it is the expected type in the Crypto library password = bytes(password) privkey = bytes(privatekey) account = Account.new(password, key=privkey) print(account.dump())
def show_events_for(self, token_address_hex, peer_address_hex): """Find all EVM-EventLogs for a channel. Args: token_address_hex (string): hex encoded address of the token peer_address_hex (string): hex encoded address of the peer Returns: events (list) """ token_address = safe_address_decode(token_address_hex) peer_address = safe_address_decode(peer_address_hex) graph = self._raiden.channelgraphs[token_address] assert graph channel = graph.partneraddress_channel[peer_address] netcontract_address = channel.external_state.netting_channel.address assert len(netcontract_address) netting_channel = self._chain.netting_channel(netcontract_address) return events.netting_channel_events(self._chain.client, netting_channel)
def show_events_for(self, token_address_hex, peer_address_hex): """ Find all EVM-EventLogs for a channel. Args: token_address_hex (string): hex encoded address of the token peer_address_hex (string): hex encoded address of the peer Returns: events (list) """ token_address = safe_address_decode(token_address_hex) peer_address = safe_address_decode(peer_address_hex) graph = self._raiden.token_to_channelgraph[token_address] assert graph channel = graph.partneraddress_to_channel[peer_address] netcontract_address = channel.external_state.netting_channel.address assert netcontract_address netting_channel = self._chain.netting_channel(netcontract_address) return events.netting_channel_events(self._chain.client, netting_channel)
def register_token(self, token_address_hex): """Register a token with the raiden token manager. Args: token_address_hex (string): a hex encoded token address. Returns: channel_manager: the channel_manager contract_proxy. """ # Add the ERC20 token to the raiden registry token_address = safe_address_decode(token_address_hex) self._chain.default_registry.add_token(token_address) # Obtain the channel manager for the token channel_manager = self._chain.manager_by_token(token_address) # Register the channel manager with the raiden registry self._raiden.register_channel_manager(channel_manager.address) return channel_manager
def register_token(self, token_address_hex): """ Register a token with the raiden token manager. Args: token_address_hex (string): a hex encoded token address. Returns: channel_manager: the channel_manager contract_proxy. """ # Add the ERC20 token to the raiden registry token_address = safe_address_decode(token_address_hex) self._raiden.default_registry.add_token(token_address) # Obtain the channel manager for the token channel_manager = self._raiden.default_registry.manager_by_token(token_address) # Register the channel manager with the raiden registry self._raiden.register_channel_manager(channel_manager.address) return channel_manager