def main( private_key: str, state_db: str, web3: Web3, contracts: Dict[str, Contract], start_block: BlockNumber, rdn_per_eth: float, expires_within: BlockNumber, ) -> None: pfs_address = private_key_to_address(private_key) chain_id = ChainID(web3.eth.chainId) database = PFSDatabase(filename=state_db, chain_id=chain_id, pfs_address=pfs_address, sync_start_block=start_block) claim_cost_rdn = calc_claim_cost_rdn(web3, rdn_per_eth) ious = list( get_claimable_ious( database, expires_after=web3.eth.blockNumber, expires_before=BlockNumber(web3.eth.blockNumber + expires_within), claim_cost_rdn=claim_cost_rdn, )) print(f"Found {len(ious)} claimable IOUs") _, failures = claim_ious(ious, claim_cost_rdn, contracts[CONTRACT_ONE_TO_N], web3, database) if failures: sys.exit(1)
def __init__( # pylint: disable=too-many-arguments self, web3: Web3, contracts: Dict[str, Contract], private_key: PrivateKey, db_filename: str, sync_start_block: BlockNumber, required_confirmations: BlockTimeout, poll_interval: float, matrix_servers: Optional[List[str]] = None, ): super().__init__() self.web3 = web3 self.registry_address = contracts[ CONTRACT_TOKEN_NETWORK_REGISTRY].address self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT] self.service_token_address = self.user_deposit_contract.functions.token( ).call() self.chain_id = ChainID(web3.eth.chainId) self.address = private_key_to_address(private_key) self.required_confirmations = required_confirmations self._poll_interval = poll_interval self._is_running = gevent.event.Event() log.info("PFS payment address", address=self.address) self.database = PFSDatabase( filename=db_filename, pfs_address=self.address, sync_start_block=sync_start_block, token_network_registry_address=to_canonical_address( self.registry_address), chain_id=self.chain_id, user_deposit_contract_address=to_canonical_address( self.user_deposit_contract.address), allow_create=True, ) self.blockchain_state = BlockchainState( latest_committed_block=self.database.get_latest_committed_block(), token_network_registry_address=to_canonical_address( self.registry_address), chain_id=self.chain_id, ) self.matrix_listener = MatrixListener( private_key=private_key, chain_id=self.chain_id, device_id=DeviceIDs.PFS, message_received_callback=self.handle_message, servers=matrix_servers, ) self.token_networks = self._load_token_networks() self.updated = gevent.event.Event( ) # set whenever blocks are processed self.startup_finished = gevent.event.AsyncResult() self._init_metrics()
def main( private_key: str, state_db: str, web3: Web3, contracts: Dict[str, Contract], start_block: BlockNumber, rdn_per_eth: float, expires_within: BlockNumber, ) -> None: pfs_address = private_key_to_address(private_key) chain_id = ChainID(int(web3.net.version)) database = PFSDatabase(filename=state_db, chain_id=chain_id, pfs_address=pfs_address) one_to_n_contract = contracts[CONTRACT_ONE_TO_N] claim_cost_eth = 90897 claim_cost_rdn = TokenAmount(int(claim_cost_eth / rdn_per_eth)) ious = list( get_claimable_ious( database, expires_before=web3.eth.blockNumber + expires_within, claim_cost_rdn=claim_cost_rdn, )) print(f"Found {len(ious)} claimable IOUs") _, failures = claim_ious(ious, claim_cost_rdn, one_to_n_contract, web3, database) if failures: sys.exit(1)
def __init__( # pylint: disable=too-many-arguments self, web3: Web3, contracts: Dict[str, Contract], private_key: str, db_filename: str, sync_start_block: BlockNumber = BlockNumber(0), required_confirmations: int = 8, poll_interval: float = 10, matrix_servers: List[str] = None, ): super().__init__() self.web3 = web3 self.registry_address = contracts[ CONTRACT_TOKEN_NETWORK_REGISTRY].address self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT] self.chain_id = ChainID(int(web3.net.version)) self.address = private_key_to_address(private_key) self.required_confirmations = required_confirmations self._poll_interval = poll_interval self._is_running = gevent.event.Event() log.info("PFS payment address", address=self.address) self.blockchain_state = BlockchainState( latest_commited_block=BlockNumber(0), token_network_registry_address=self.registry_address, chain_id=self.chain_id, ) self.database = PFSDatabase( filename=db_filename, pfs_address=self.address, sync_start_block=sync_start_block, token_network_registry_address=self.registry_address, chain_id=self.chain_id, user_deposit_contract_address=self.user_deposit_contract.address, allow_create=True, ) self.matrix_listener = MatrixListener( private_key=private_key, chain_id=self.chain_id, service_room_suffix=PATH_FINDING_BROADCASTING_ROOM, message_received_callback=self.handle_message, address_reachability_changed_callback=self. handle_reachability_change, servers=matrix_servers, ) self.address_to_reachability: Dict[Address, AddressReachability] = dict() self.token_networks = self._load_token_networks()
def __init__( self, web3: Web3, contracts: Dict[str, Contract], private_key: str, db_filename: str, sync_start_block: BlockNumber = BlockNumber(0), required_confirmations: int = 8, poll_interval: float = 10, service_fee: int = 0, ): super().__init__() self.web3 = web3 self.registry_address = contracts[CONTRACT_TOKEN_NETWORK_REGISTRY].address self.sync_start_block = sync_start_block self.required_confirmations = required_confirmations self.poll_interval = poll_interval self.chain_id = ChainID(int(web3.net.version)) self.private_key = private_key self.address = private_key_to_address(private_key) self.service_fee = service_fee self.is_running = gevent.event.Event() self.token_networks: Dict[TokenNetworkAddress, TokenNetwork] = {} self.database = PFSDatabase(filename=db_filename, pfs_address=self.address) self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT] self.last_known_block = 0 self.blockchain_state = BlockchainState( chain_id=self.chain_id, token_network_registry_address=self.registry_address, monitor_contract_address=Address(''), # FIXME latest_known_block=self.sync_start_block, token_network_addresses=[], ) log.info( 'Listening to token network registry', registry_address=self.registry_address, start_block=sync_start_block, ) try: self.matrix_listener = MatrixListener( private_key=private_key, chain_id=self.chain_id, callback=self.handle_message, service_room_suffix=PATH_FINDING_BROADCASTING_ROOM, ) except ConnectionError as e: log.critical('Could not connect to broadcasting system.', exc=e) sys.exit(1)
def __init__( # pylint: disable=too-many-arguments self, web3: Web3, contracts: Dict[str, Contract], private_key: str, db_filename: str, sync_start_block: BlockNumber = BlockNumber(0), required_confirmations: int = 8, poll_interval: float = 10, ): super().__init__() self.web3 = web3 self.registry_address = contracts[CONTRACT_TOKEN_NETWORK_REGISTRY].address self.user_deposit_contract = contracts[CONTRACT_USER_DEPOSIT] self.chain_id = ChainID(int(web3.net.version)) self.address = private_key_to_address(private_key) self._required_confirmations = required_confirmations self._poll_interval = poll_interval self._is_running = gevent.event.Event() self.database = PFSDatabase( filename=db_filename, pfs_address=self.address, sync_start_block=sync_start_block, token_network_registry_address=self.registry_address, chain_id=self.chain_id, user_deposit_contract_address=self.user_deposit_contract.address, allow_create=True, ) self.token_networks = self._load_token_networks() try: self.matrix_listener = MatrixListener( private_key=private_key, chain_id=self.chain_id, callback=self.handle_message, service_room_suffix=PATH_FINDING_BROADCASTING_ROOM, ) except ConnectionError as exc: log.critical("Could not connect to broadcasting system.", exc=exc) sys.exit(1)
def __init__( self, web3: Web3, contract_manager: ContractManager, registry_address: Address, private_key: str, db_filename: str, user_deposit_contract_address: Address, sync_start_block: int = 0, required_confirmations: int = 8, poll_interval: int = 10, service_fee: int = 0, ): """ Creates a new pathfinding service Args: contract_manager: A contract manager token_network_listener: A blockchain listener object token_network_registry_listener: A blockchain listener object for the network registry chain_id: The id of the chain the PFS runs on """ super().__init__() self.web3 = web3 self.contract_manager = contract_manager self.registry_address = registry_address self.sync_start_block = sync_start_block self.required_confirmations = required_confirmations self.poll_interval = poll_interval self.chain_id = int(web3.net.version) self.private_key = private_key self.address = private_key_to_address(private_key) self.service_fee = service_fee self.is_running = gevent.event.Event() self.token_networks: Dict[Address, TokenNetwork] = {} self.token_network_listeners: List[BlockchainListener] = [] self.database = PFSDatabase( filename=db_filename, pfs_address=self.address, ) self.user_deposit_contract = web3.eth.contract( abi=self.contract_manager.get_contract_abi( CONTRACT_USER_DEPOSIT, ), address=user_deposit_contract_address, ) log.info( 'Starting TokenNetworkRegistry Listener', required_confirmations=self.required_confirmations, ) self.token_network_registry_listener = BlockchainListener( web3=web3, contract_manager=self.contract_manager, contract_name=CONTRACT_TOKEN_NETWORK_REGISTRY, contract_address=self.registry_address, required_confirmations=self.required_confirmations, poll_interval=self.poll_interval, sync_start_block=self.sync_start_block, ) log.info( 'Listening to token network registry', registry_address=registry_address, start_block=sync_start_block, ) self._setup_token_networks() try: self.matrix_listener = MatrixListener( private_key=private_key, chain_id=self.chain_id, callback=self.handle_message, service_room_suffix=PATH_FINDING_BROADCASTING_ROOM, ) except ConnectionError as e: log.critical( 'Could not connect to broadcasting system.', exc=e, ) sys.exit(1)