def test_http_client_async(docker_services, event_loop) -> AsyncClient: # pylint: disable=redefined-outer-name """Test http_client.is_connected.""" http_client = AsyncClient(commitment=Processed) def check() -> bool: return event_loop.run_until_complete(http_client.is_connected()) docker_services.wait_until_responsive(timeout=15, pause=1, check=check) yield http_client event_loop.run_until_complete(http_client.close())
async def main(): async with AsyncClient("https://api.devnet.solana.com") as client: res = await client.is_connected() print(res) # True # Alternatively, close the client explicitly instead of using a context manager: client = AsyncClient("https://api.devnet.solana.com") # res = await client.is_connected() # res = await client.get_version() res = await client.get_balance("8Lx9U9wdE3afdqih1mCAXy3unJDfzSaXFqAvoLMjhwoD","recent") print(res) # True await client.close()
def async_client(event_loop, solana_test_validator) -> Iterator[AsyncClient]: async_client = AsyncClient(commitment=Confirmed) total_attempts = 10 current_attempt = 0 while not event_loop.run_until_complete(async_client.is_connected()): if current_attempt == total_attempts: raise Exception("Could not connect to test validator") else: current_attempt += 1 time.sleep(1) yield async_client event_loop.run_until_complete(async_client.close())
def test_http_clients(docker_services) -> Clients: """Test http_client.is_connected.""" http_client = Client() async_client = AsyncClient() loop = asyncio.get_event_loop() def check() -> bool: sync_result = http_client.is_connected() async_result = loop.run_until_complete(async_client.is_connected()) return sync_result and async_result docker_services.wait_until_responsive(timeout=15, pause=1, check=check) clients = Clients(sync=http_client, async_=async_client, loop=loop) yield clients clients.loop.run_until_complete(async_client.close())
async def get_all_txs_for_program(program_id: Optional[str]) -> List[Dict]: program_id = program_id if program_id else SYS_PROGRAM_ID solana_client = AsyncClient(RPC_ADDRESS) txs = await solana_client.get_signatures_for_address(account=program_id) hashes = [tx.get("signature") for tx in txs.get("result")] print(f"Retrieved {len(hashes)} txs for program ID {program_id}.") await solana_client.close() return hashes
async def fetch_tx_receipt(tx_hash: str) -> Optional[Dict]: solana_client = AsyncClient(RPC_ADDRESS) tx_info = await solana_client.get_transaction(tx_hash, "base64") print(f"Tx info for {tx_hash}: {json.dumps(tx_info, indent=4)}") await solana_client.close() if is_invalid_tx(tx_info): print(f"Invalid tx hash: {tx_hash}") return tx_info.get("result")
async def get_client(endpoint: str) -> AsyncClient: print(f'Connecting to network at {endpoint}') async_client = AsyncClient(endpoint=endpoint, commitment=Confirmed) total_attempts = 10 current_attempt = 0 while not await async_client.is_connected(): if current_attempt == total_attempts: raise Exception("Could not connect to test validator") else: current_attempt += 1 await asyncio.sleep(1) return async_client
def unit_test_http_client_async() -> AsyncClient: """Async client to be used in unit tests.""" client = AsyncClient(commitment=Processed) return client