def _run_launch(self, **kw): configuration_file_name = kw.get("configuration_file_name") configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) network_name = configuration_file.network.name raiden_client = RaidenClient.get_client(network_name) required = RequiredAmounts.for_network(network_name) if not raiden_client.is_installed: self._send_status_update( f"Downloading and installing raiden {raiden_client.release}") raiden_client.install() self._send_status_update("Installation complete") account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) service_token = Erc20Token.find_by_ticker( required.service_token.ticker, network_name) service_token_balance = get_token_balance(w3=w3, account=account, token=service_token) service_token_in_deposit = get_token_deposit(w3=w3, account=account, token=service_token) if service_token_balance.as_wei and service_token_in_deposit < required.service_token: self._send_status_update( f"Making deposit of {service_token_balance.formatted} for Raiden Services" ) deposit_service_tokens(w3=w3, account=account, token=service_token, amount=service_token_balance.as_wei) service_token_deposited = get_token_deposit(w3=w3, account=account, token=service_token) self._send_status_update( f"Amount deposited at UDC: {service_token_deposited.formatted}" ) self._send_status_update( "Launching Raiden, this might take a couple of minutes, do not close the browser" ) if not raiden_client.is_running: raiden_client.launch(configuration_file) try: raiden_client.wait_for_web_ui_ready( status_callback=lambda stat: log.info(str(stat))) self._send_task_complete("Raiden is ready!") self._send_redirect(raiden_client.WEB_UI_INDEX_URL) except (RaidenClientError, RuntimeError) as exc: self._send_error_message(f"Raiden process failed to start: {exc}") raiden_client.kill()
def _run_launch(self, **kw): configuration_file_name = kw.get("configuration_file_name") configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) if not RAIDEN_CLIENT.is_installed: self._send_status_update( f"Downloading and installing raiden {RAIDEN_CLIENT.release}") RAIDEN_CLIENT.install() self._send_status_update("Installation complete") account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) service_token = Erc20Token.find_by_ticker( settings.service_token.ticker) service_token_balance = get_token_balance(w3=w3, account=account, token=service_token) service_token_in_deposit = get_token_deposit(w3=w3, account=account, token=service_token) if service_token_balance.as_wei and service_token_in_deposit < SERVICE_TOKEN_REQUIRED: self._send_status_update( f"Making deposit of {service_token_balance.formatted} for Raiden Services" ) deposit_service_tokens(w3=w3, account=account, token=service_token, amount=service_token_balance.as_wei) service_token_deposited = get_token_deposit(w3=w3, account=account, token=service_token) self._send_status_update( f"Amount deposited at UDC: {service_token_deposited.formatted}" ) self._send_status_update( "Launching Raiden, this might take a couple of minutes, do not close the browser" ) if not RAIDEN_CLIENT.is_running: RAIDEN_CLIENT.launch(configuration_file) try: RAIDEN_CLIENT.wait_for_web_ui_ready() self._send_task_complete("Raiden is ready!") self._send_redirect(RAIDEN_CLIENT.WEB_UI_INDEX_URL) except (RaidenClientError, RuntimeError) as exc: self._send_error_message(f"Raiden process failed to start: {exc}") RAIDEN_CLIENT.kill()
def _deposit(self): network = self._get_network() w3 = self._get_web3() ethereum_balance = self.account.get_ethereum_balance(w3=w3) log.debug(f"Ethereum Balance: {ethereum_balance.formatted}") if not ethereum_balance.as_wei: network.fund(self.account) token = Erc20Token.find_by_ticker(settings.service_token.ticker) token_balance = get_token_balance(w3, self.account, token) log.debug(f"Token Balance: {token_balance.formatted}") if not token_balance.as_wei: mint_tokens(w3, self.account, token) token_balance = get_token_balance(w3, self.account, token) log.debug(f"Tokens minted. New Balance: {token_balance.formatted}") deposit_service_tokens(w3, self.account, token, token_balance.as_wei)
def test_udc_deposit(self): self.network.fund(self.account) self.account.wait_for_ethereum_funds(self.w3, EthereumAmount(0.01)) tx_hash = mint_tokens(self.w3, self.account, self.svt_token) wait_for_transaction(self.w3, tx_hash) amount = get_token_balance(self.w3, self.account, self.svt_token) tx_hash = deposit_service_tokens(self.w3, self.account, self.svt_token, amount.as_wei) wait_for_transaction(self.w3, tx_hash) deposited = get_token_deposit(self.w3, self.account, self.svt_token) self.assertEqual(deposited, amount)
def _deposit_to_udc(self, w3, account, service_token, deposit_amount): self._send_status_update( f"Making deposit of {deposit_amount.formatted} to the " "User Deposit Contract") self._send_status_update(f"This might take a few minutes") tx_hash = deposit_service_tokens( w3=w3, account=account, token=service_token, amount=deposit_amount.as_wei, ) wait_for_transaction(w3, tx_hash) service_token_deposited = get_token_deposit(w3=w3, account=account, token=service_token) self._send_status_update( f"Total amount deposited at UDC: {service_token_deposited.formatted}" )
def _run_swap(self, **kw): try: configuration_file_name = kw.get("configuration_file_name") exchange_name = kw["exchange"] token_amount = kw["amount"] token_ticker = kw["token"] except (ValueError, KeyError, TypeError) as exc: self._send_error_message(f"Invalid request: {exc}") return try: configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) network_name = configuration_file.network.name form = TokenExchangeForm({ "network": [network_name], "exchange": [exchange_name], "token_amount": [token_amount], "token_ticker": [token_ticker], }) if form.validate(): account = configuration_file.account try_unlock(account) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) token = Erc20Token.find_by_ticker(form.data["token_ticker"], network_name) token_amount = TokenAmount(Wei(form.data["token_amount"]), token) exchange = Exchange.get_by_name(form.data["exchange"])(w3=w3) self._send_status_update(f"Starting swap at {exchange.name}") costs = exchange.calculate_transaction_costs( token_amount, account) needed_funds = costs["total"] exchange_rate = costs["exchange_rate"] balance_before_swap = account.get_ethereum_balance(w3) if needed_funds > balance_before_swap: raise ValueError(( f"Not enough ETH. {balance_before_swap.formatted} available, but " f"{needed_funds.formatted} needed")) self._send_status_update( (f"Best exchange rate found at {exchange.name}: " f"{exchange_rate} / {token_amount.ticker}")) self._send_status_update( f"Trying to acquire {token_amount} at this rate") transaction_receipt = exchange.buy_tokens( account, token_amount, costs) wait_for_transaction(w3, transaction_receipt) token_balance = get_token_balance(w3, account, token) balance_after_swap = account.get_ethereum_balance(w3) actual_total_costs = balance_before_swap - balance_after_swap self._send_status_update( f"Swap complete. {token_balance.formatted} available") self._send_status_update(f"Actual costs: {actual_total_costs}") required = RequiredAmounts.for_network(network_name) service_token = Erc20Token.find_by_ticker( required.service_token.ticker, network_name) service_token_balance = get_token_balance( w3, account, service_token) total_service_token_balance = get_total_token_owned( w3, account, service_token) transfer_token = Erc20Token.find_by_ticker( required.transfer_token.ticker, network_name) transfer_token_balance = get_token_balance( w3, account, transfer_token) if total_service_token_balance < required.service_token: raise ExchangeError("Exchange was not successful") elif service_token_balance.as_wei > 0: self._send_status_update( f"Making deposit of {service_token_balance.formatted} to the User Deposit Contract" ) self._send_status_update(f"This might take a few minutes") transaction_receipt = deposit_service_tokens( w3=w3, account=account, token=service_token, amount=service_token_balance.as_wei, ) wait_for_transaction(w3, transaction_receipt) service_token_deposited = get_token_deposit( w3=w3, account=account, token=service_token) self._send_status_update( f"Total amount deposited at UDC: {service_token_deposited.formatted}" ) if transfer_token_balance < required.transfer_token: redirect_url = self.reverse_url( "swap", configuration_file.file_name, transfer_token.ticker) next_page = "Moving on to exchanging DAI ..." else: redirect_url = self.reverse_url( "launch", configuration_file.file_name) next_page = "You are ready to launch Raiden! ..." self._send_summary( ["Congratulations! Swap Successful!", next_page], icon=token_ticker) time.sleep(5) self._send_redirect(redirect_url) else: for key, error_list in form.errors.items(): error_message = f"{key}: {'/'.join(error_list)}" self._send_error_message(error_message) except (json.decoder.JSONDecodeError, KeyError, ExchangeError, ValueError) as exc: self._send_error_message(str(exc)) redirect_url = self.reverse_url("swap", configuration_file.file_name, token_ticker) next_page = f"Try again to exchange {token_ticker}..." self._send_summary(["Transaction failed", str(exc), next_page], icon="error") time.sleep(5) self._send_redirect(redirect_url)