Usage with asyncio

You can use AsyncLCDClient to make asynchronous, non-blocking LCD requests. The interface is similar to LCDClient, except the module and wallet API functions must be awaited.

Async module APIs

You can replace your LCDClient instance with AsyncLCDClient inside a coroutine function:

import asyncio
from terra_sdk.client.lcd import AsyncLCDClient

async def main():
    terra = AsyncLCDClient("https://lcd.terra.dev", "columbus-5")
    total_supply = await terra.bank.total()
    print(total_supply)
    await terra.session.close() # you must close the session

asyncio.get_event_loop().run_until_complete(main())

For convenience, you can use the async context manager to automatically teardown the session. Here’s the same code as above, this time using the async with construct.

import asyncio
from terra_sdk.client.lcd import AsyncLCDClient

async def main():
    async with AsyncLCDClient("https://lcd.terra.dev", "columbus-5") as terra:
        total_supply = await terra.bank.total()
        print(total_supply)

asyncio.get_event_loop().run_until_complete(main())

Async wallet API

When creating a wallet with AsyncLCDClient, the wallet’s methods that create LCD requests are also asychronous and therefore must be awaited.

import asyncio
from terra_sdk.client.lcd.api.tx import CreateTxOptions
from terra_sdk.client.lcd import AsyncLCDClient
from terra_sdk.key.mnemonic import MnemonicKey
from terra_sdk.core import Coins

mk = MnemonicKey()
recipient = "terra1..."

async def main():
    async with AsyncLCDClient("https://lcd.terra.dev", "columbus-5") as terra:
        wallet = terra.wallet(mk)
        account_number = await wallet.account_number()
        tx = await wallet.create_and_sign_tx(
            CreateTxOptions(
                msgs=[MsgSend(wallet.key.acc_address, recipient, Coins(uluna=10202))]
            )
        )

asyncio.get_event_loop().run_until_complete(main())

Alternative event loops

The native asyncio event loop can be replaced with an alternative such as uvloop for more performance. For example:

import asyncio
import uvloop

from terra_sdk.client.lcd import AsyncLCDClient

async def main():
    async with AsyncLCDClient("https://lcd.terra.dev", "columbus-5") as terra:
        total_supply = await wallet.bank.total()

uvloop.install()
asyncio.get_event_loop().run_until_complete(main())