def _run_track_transaction(self, **kw): POLLING_INTERVAL = 10 configuration_file_name = kw.get("configuration_file_name") tx_hash = kw.get("tx_hash") time_elapsed = 0 try: configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self._send_status_update( f"Waiting for confirmation of transaction {tx_hash}") while not w3.eth.getTransactionReceipt(tx_hash): time.sleep(POLLING_INTERVAL) time_elapsed += POLLING_INTERVAL self._send_status_update( f"Not confirmed after {time_elapsed} seconds...") self._send_status_update("Transaction confirmed") self._send_redirect( self.reverse_url("funding", configuration_file_name)) except Exception as exc: self._send_error_message(str(exc))
def _run_funding(self, configuration_file: RaidenConfigurationFile): network = configuration_file.network settings = network_settings[network.name] if not network.FAUCET_AVAILABLE: self._send_error_message( f"Can not run automatic funding for {network.capitalized_name}" ) return account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self._send_status_update( f"Obtaining {network.capitalized_name} ETH through faucet") network.fund(account) balance = account.wait_for_ethereum_funds( w3=w3, expected_amount=EthereumAmount(0.01)) self._send_status_update(f"Account funded with {balance.formatted}") if settings.service_token.mintable: service_token = Erc20Token.find_by_ticker( settings.service_token.ticker, settings.network) self._send_status_update(f"Minting {service_token.ticker}") mint_tokens(w3, account, service_token) if settings.transfer_token.mintable: transfer_token = Erc20Token.find_by_ticker( settings.transfer_token.ticker, settings.network) self._send_status_update(f"Minting {transfer_token.ticker}") mint_tokens(w3, account, transfer_token)
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) if get_passphrase() is None: self.render( "account_unlock.html", keystore_file_path=configuration_file.account. keystore_file_path, return_to=f"/swap/{configuration_file_name}/{token_ticker}", ) return w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker, configuration_file.network.name) swap_amounts = SwapAmounts.from_settings(self.installer_settings) if token_ticker == self.installer_settings.service_token.ticker: swap_amount = swap_amounts.service_token elif token_ticker == self.installer_settings.transfer_token.ticker: swap_amount = swap_amounts.transfer_token self.render( "swap.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, swap_amount=swap_amount, )
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker, configuration_file.network.name) network = configuration_file.network settings = network_settings[network.name] swap_amounts = SwapAmounts.from_settings(settings) if token_ticker == settings.service_token.ticker: swap_amount_1 = swap_amounts.service_token_1 swap_amount_2 = swap_amounts.service_token_2 swap_amount_3 = swap_amounts.service_token_3 elif token_ticker == settings.transfer_token.ticker: swap_amount_1 = swap_amounts.transfer_token_1 swap_amount_2 = swap_amounts.transfer_token_2 swap_amount_3 = swap_amounts.transfer_token_3 self.render( "swap.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, swap_amount_1=swap_amount_1, swap_amount_2=swap_amount_2, swap_amount_3=swap_amount_3, )
def get(self, configuration_file_name): # Returns the highest estimate of ETH needed to get required service token amount configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) required = RequiredAmounts.for_network(configuration_file.network.name) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) highest_cost = 0 for exchange in (kyber, uniswap): exchange_costs = exchange.calculate_transaction_costs( required.service_token, account) if not exchange_costs: continue total_cost = exchange_costs["total"].as_wei highest_cost = max(highest_cost, total_cost) estimated_cost = EthereumAmount(Wei(highest_cost)) self.render_json({ "dex_swap_RDN": { "as_wei": estimated_cost.as_wei, "formatted": estimated_cost.formatted, } })
def test_funding(self, ws_client, config, test_account, unlocked, settings): data = { "method": "fund", "configuration_file_name": config.file_name, } ws_client.write_message(json.dumps(data)) for _ in range(2): message = json.loads((yield ws_client.read_message())) assert message["type"] == "status-update" message = json.loads((yield ws_client.read_message())) assert message["type"] == "next-step" for _ in range(3): message = json.loads((yield ws_client.read_message())) assert message["type"] == "status-update" message = json.loads((yield ws_client.read_message())) assert message["type"] == "next-step" message = json.loads((yield ws_client.read_message())) assert message["type"] == "redirect" assert message["redirect_url"] == f"/launch/{config.file_name}" w3 = make_web3_provider(config.ethereum_client_rpc_endpoint, test_account) assert check_balances(w3, test_account, settings, lambda x: x > 0) empty_account(w3, test_account)
def post(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account try_unlock(account) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) ex_currency_amt = json_decode(self.request.body) exchange = Exchange.get_by_name(ex_currency_amt["exchange"])(w3=w3) currency = Erc20Token.find_by_ticker(ex_currency_amt["currency"], configuration_file.network) token_amount = TokenAmount(ex_currency_amt["target_amount"], currency) try: exchange_costs = exchange.calculate_transaction_costs( token_amount, account) total_cost = exchange_costs["total"] self.render_json({ "exchange": exchange.name, "currency": currency.ticker, "target_amount": ex_currency_amt["target_amount"], "as_wei": total_cost.as_wei, "formatted": total_cost.formatted, "utc_seconds": int(time.time()), }) except ExchangeError as ex: log.error("There was an error preparing the exchange", exc_info=ex) self.set_status( status_code=409, reason=str(ex), )
def setUp(self): assert INFURA_PROJECT_ID Account.DEFAULT_KEYSTORE_FOLDER = Path(tempfile.gettempdir()) self.account = Account.create("test_raiden_integration") self.network = Network.get_by_name(self.__class__.NETWORK_NAME) self.infura = Infura.make(self.network, INFURA_PROJECT_ID) self.w3 = make_web3_provider(self.infura.url, self.account)
def setUp(self): assert INFURA_PROJECT_ID self.account = Account.create(TESTING_KEYSTORE_FOLDER, "test_raiden_integration") self.network = Network.get_by_name(self.__class__.NETWORK_NAME) self.infura = Infura.make(self.network, INFURA_PROJECT_ID) self.w3 = make_web3_provider(self.infura.url, self.account)
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename(configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account ) current_balance = configuration_file.account.get_ethereum_balance(w3) self.render("launch.html", configuration_file=configuration_file, balance=current_balance)
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_action_swap_kyber(): account = prompt_account_selection() ethereum_rpc_endpoint = prompt_ethereum_rpc_endpoint_selection() w3 = make_web3_provider(ethereum_rpc_endpoint.url, account) kyber = Exchange.get_by_name("kyber")(w3=w3) amount = Wei(5 * (10**18)) kyber.buy_tokens(account, TokenAmount(amount, Erc20Token.find_by_ticker("RDN"))) return main_prompt()
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account try_unlock(account) web3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self.render_json({ "gas_price": web3.eth.generateGasPrice(), "block_number": web3.eth.blockNumber, "utc_seconds": int(time.time()), })
def test_locked_funding(self, ws_client, config, test_account, settings): data = { "method": "fund", "configuration_file_name": config.file_name, } ws_client.write_message(json.dumps(data)) message = json.loads((yield ws_client.read_message())) assert message["type"] == "error-message" w3 = make_web3_provider(config.ethereum_client_rpc_endpoint, test_account) assert check_balances(w3, test_account, settings, lambda x: x == 0)
def _run_track_transaction(self, **kw): configuration_file_name = kw.get("configuration_file_name") tx_hash = kw.get("tx_hash") time_start = time.time() try: configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) configuration_file._initial_funding_txhash = tx_hash configuration_file.save() account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self._send_txhash_message( ["Waiting for confirmation of transaction"], tx_hash=tx_hash) transaction_found = False while (not transaction_found) and (time.time() - time_start < WEB3_TIMEOUT): try: tx_receipt = w3.eth.waitForTransactionReceipt( decode_hex(tx_hash), timeout=WEB3_TIMEOUT) assert tx_receipt.get("blockNumber", 0) > 0 transaction_found = True except TimeExhausted: pass if not transaction_found: self._send_status_update([ f"Not confirmed after {int(time.time() - time_start)} seconds!" ], icon="error") self._send_txhash_message( "Funding took too long! " "Click the link below and restart the wizard, " "once it was confirmed:", tx_hash=tx_hash, ) time.sleep(10) sys.exit(1) else: configuration_file._initial_funding_txhash = None configuration_file.save() self._send_status_update("Transaction confirmed") service_token = configuration_file.settings.service_token self._send_redirect( self.reverse_url("swap", configuration_file.file_name, service_token.ticker)) except Exception as exc: self._send_error_message(str(exc))
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 _run_udc_deposit(self, **kw): try: configuration_file_name = kw.get("configuration_file_name") configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) except (ValueError, KeyError, TypeError) as exc: self._send_error_message(f"Invalid request: {exc}") return try: settings = self.installer_settings required = RequiredAmounts.from_settings(settings) swap_amounts = SwapAmounts.from_settings(settings) service_token = Erc20Token.find_by_ticker( required.service_token.ticker, settings.network) account = configuration_file.account try_unlock(account) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) service_token_balance = get_token_balance(w3, account, service_token) service_token_deposited = get_token_deposit( w3, account, service_token) if service_token_deposited < required.service_token: swap_amount = swap_amounts.service_token if service_token_balance >= swap_amount: deposit = swap_amount - service_token_deposited else: deposit = service_token_balance self._deposit_to_udc(w3, account, service_token, deposit) else: self._send_status_update( f"Service token deposited at UDC: {service_token_deposited.formatted} is enough" ) time.sleep(5) transfer_token = Erc20Token.find_by_ticker( required.transfer_token.ticker, settings.network) transfer_token_balance = get_token_balance(w3, account, transfer_token) self._redirect_transfer_swap(configuration_file, transfer_token_balance, required) except (json.decoder.JSONDecodeError, KeyError, ExchangeError, ValueError) as exc: self._redirect_after_swap_error(exc, configuration_file.file_name, service_token.ticker)
def get(self, exchange_name, configuration_file_name, token_ticker): exchange_class = Exchange.get_by_name(exchange_name) configuration_file = RaidenConfigurationFile.get_by_filename(configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account ) self.render( "swap.html", exchange=exchange_class(w3=w3), configuration_file=configuration_file, balance=configuration_file.account.get_ethereum_balance(w3), token=Erc20Token.find_by_ticker(token_ticker), )
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) network = configuration_file.network.name account = configuration_file.account try_unlock(account) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) required = RequiredAmounts.for_network(network) service_token = Erc20Token.find_by_ticker( required.service_token.ticker, network) transfer_token = Erc20Token.find_by_ticker( required.transfer_token.ticker, network) service_token_balance = get_total_token_owned( w3=w3, account=configuration_file.account, token=service_token) transfer_token_balance = get_token_balance( w3=w3, account=configuration_file.account, token=transfer_token) eth_balance = configuration_file.account.get_ethereum_balance(w3) def serialize_balance(balance_amount): return ({ "as_wei": balance_amount.as_wei, "formatted": balance_amount.formatted } if balance_amount else None) self.render_json({ "url": self.reverse_url("api-configuration-detail", configuration_file.file_name), "file_name": configuration_file.file_name, "account_page_url": self.reverse_url("account", configuration_file.file_name), "account": configuration_file.account.address, "network": configuration_file.network.name, "balance": { "ETH": serialize_balance(eth_balance), "service_token": serialize_balance(service_token_balance), "transfer_token": serialize_balance(transfer_token_balance), }, "_initial_funding_txhash": configuration_file._initial_funding_txhash, })
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename(configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account ) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker) self.render( "swap_options.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, )
def provide_liquidity(infura, create_account, uniswap): account = create_account() w3 = make_web3_provider(infura.url, account) fund_account(account, w3) tx_hash = mint_tokens(w3, account, WIZ_TOKEN) wait_for_transaction(w3, tx_hash) current_rate = uniswap.get_current_rate(TokenAmount(1000, WIZ_TOKEN)) router_proxy = w3.eth.contract( abi=uniswap_contracts.UNISWAP_ROUTER02_ABI, address=Uniswap.ROUTER02_ADDRESS, ) addLiquidity(w3, account, router_proxy, current_rate) yield removeLiquidity(w3, account, router_proxy) empty_account(w3, account)
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) keystore_path = configuration_file.configuration_data["keystore-path"] filename = "" for file in glob(f"{keystore_path}/UTC--*"): file_path = Path(file) if file_path.is_file(): keystore_content = json.loads(file_path.read_text()) if (to_checksum_address(keystore_content["address"]) == configuration_file.account.address): filename = os.path.basename(file) break w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) required = RequiredAmounts.for_network(configuration_file.network.name) eth_balance = configuration_file.account.get_ethereum_balance(w3) log.info(f"Checking balance {eth_balance} > {required.eth}") if eth_balance < required.eth: log.info( f"funding tx {configuration_file._initial_funding_txhash}") if configuration_file._initial_funding_txhash is not None: return self.render( "account.html", configuration_file=configuration_file, keystore=filename, ) else: configuration_file._initial_funding_txhash = None configuration_file.save() if PASSPHRASE is not None: self.render("account.html", configuration_file=configuration_file, keystore=filename) else: self.render( "account_unlock.html", keystore_file_path=configuration_file.account. keystore_file_path, return_to=f"/account/{configuration_file_name}", )
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) service_token = Erc20Token.find_by_ticker( SERVICE_TOKEN_REQUIRED.ticker) transfer_token = Erc20Token.find_by_ticker( TRANSFER_TOKEN_REQUIRED.ticker) service_token_balance = get_total_token_owned( w3=w3, account=configuration_file.account, token=service_token) transfer_token_balance = get_token_balance( w3=w3, account=configuration_file.account, token=transfer_token) eth_balance = configuration_file.account.get_ethereum_balance(w3) def serialize_balance(balance_amount): return ({ "as_wei": balance_amount.as_wei, "formatted": balance_amount.formatted } if balance_amount else None) self.render_json({ "url": self.reverse_url("api-configuration-detail", configuration_file.file_name), "file_name": configuration_file.file_name, "account_page_url": self.reverse_url("account", configuration_file.file_name), "funding_page_url": self.reverse_url("funding", configuration_file.file_name), "account": configuration_file.account.address, "network": configuration_file.network.name, "balance": { "ETH": serialize_balance(eth_balance), "service_token": serialize_balance(service_token_balance), "transfer_token": serialize_balance(transfer_token_balance), }, })
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) current_balance = configuration_file.account.get_ethereum_balance(w3) if PASSPHRASE is not None: self.render("launch.html", configuration_file=configuration_file, balance=current_balance) else: self.render( "account_unlock.html", keystore_file_path=configuration_file.account. keystore_file_path, return_to=f"/launch/{configuration_file_name}", )
def get(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) if get_passphrase() is None: self.render( "account_unlock.html", keystore_file_path=configuration_file.account. keystore_file_path, return_to=f"/account/{configuration_file_name}", ) return keystore_path = configuration_file.configuration_data["keystore-path"] filename = "" for file in glob(f"{keystore_path}/UTC--*"): file_path = Path(file) if file_path.is_file(): keystore_content = json.loads(file_path.read_text()) if (to_canonical_address(keystore_content["address"]) == configuration_file.account.address): filename = os.path.basename(file) break w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) required = RequiredAmounts.from_settings(self.installer_settings) eth_balance = configuration_file.account.get_ethereum_balance(w3) log.info(f"funding tx {configuration_file._initial_funding_txhash}") log.info(f"Checking balance {eth_balance} >= {required.eth}") if eth_balance >= required.eth: configuration_file._initial_funding_txhash = None configuration_file.save() self.render( "account.html", configuration_file=configuration_file, keystore=filename, ramp_api_key=RAMP_API_KEY, )
def _run_track_transaction(self, **kw): try: configuration_file_name = kw.get("configuration_file_name") tx_hash = kw.get("tx_hash") configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) except Exception as exc: self._send_error_message(str(exc)) return configuration_file._initial_funding_txhash = tx_hash configuration_file.save() account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self._send_txhash_message(["Waiting for confirmation of transaction"], tx_hash=tx_hash) try: wait_for_transaction(w3, decode_hex(tx_hash)) except TransactionTimeoutError: self._send_status_update( [f"Not confirmed after {WEB3_TIMEOUT} seconds!"], icon="error") self._send_txhash_message( "Funding took too long! " "Click the link below and restart the wizard, " "once it was confirmed:", tx_hash=tx_hash, ) time.sleep(10) sys.exit(1) else: configuration_file._initial_funding_txhash = None configuration_file.save() self._send_status_update("Transaction confirmed") service_token = configuration_file.settings.service_token self._send_redirect( self.reverse_url("swap", configuration_file.file_name, service_token.ticker))
def post(self, configuration_file_name): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) ex_currency_amt = json_decode(self.request.body) exchange = Exchange.get_by_name(ex_currency_amt["exchange"])(w3=w3) currency = Erc20Token.find_by_ticker(ex_currency_amt["currency"], configuration_file.network) token_amount = TokenAmount(ex_currency_amt["target_amount"], currency) exchange_costs = exchange.calculate_transaction_costs( token_amount, account) total_cost = exchange_costs["total"] self.render_json({ "exchange": exchange.name, "currency": currency.ticker, "target_amount": ex_currency_amt["target_amount"], "as_wei": total_cost.as_wei, "formatted": total_cost.formatted, "utc_seconds": int(time.time()), })
def run_action_account_fund(): account = prompt_account_selection() network = prompt_network_selection() ethereum_rpc_endpoint = prompt_ethereum_rpc_endpoint_selection( network=network) w3 = make_web3_provider(ethereum_rpc_endpoint.url, account) current_balance = account.get_ethereum_balance(w3) needed_funds = max( 0, network.MINIMUM_ETHEREUM_BALANCE_REQUIRED - current_balance) if needed_funds > 0: if network.FAUCET_AVAILABLE: try: print( f"Attempting to add funds to {account.address} on {network.capitalized_name}" ) network.fund(account) except FundingError as exc: print(f"Failed: {exc}") else: print(f"Insufficience funds. Current balance is {current_balance}") print( f"Please send at least {needed_funds} to 0x{account.address} on " f"{network.capitalized_name}") time_remaining = 60 polling_interval = 1 while (current_balance < network.MINIMUM_ETHEREUM_BALANCE_REQUIRED and time_remaining > 0): time.sleep(polling_interval) print(f"Waiting for {time_remaining}s...") time_remaining -= polling_interval current_balance = account.get_ethereum_balance(w3) print("Balance is now", account.get_ethereum_balance(w3)) return main_prompt()
def _run_track_transaction(self, **kw): POLLING_INTERVAL = 10 configuration_file_name = kw.get("configuration_file_name") tx_hash = kw.get("tx_hash") time_elapsed = 0 try: configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) self._send_txhash_message( ["Waiting for confirmation of transaction"], tx_hash=tx_hash) transaction_found = False iteration_cycle = 0 while not transaction_found and iteration_cycle < 30: try: w3.eth.getTransactionReceipt(tx_hash) transaction_found = True except Exception: time.sleep(2) while not w3.eth.getTransactionReceipt(tx_hash): time.sleep(POLLING_INTERVAL) time_elapsed += POLLING_INTERVAL self._send_status_update( f"Not confirmed after {time_elapsed} seconds...") self._send_status_update("Transaction confirmed") service_token = configuration_file.settings.service_token self._send_redirect( self.reverse_url("swap", configuration_file.file_name, service_token.ticker)) except Exception as exc: self._send_error_message(str(exc))
def uniswap(test_account, infura): w3 = make_web3_provider(infura.url, test_account) return Uniswap(w3)