AsynchronousΒΆ

Now we have supported the use of asynchronous methods to submit transactions, py-stellar-base gives you the choice, rather than forcing you into always writing async; sync code is easier to write, generally safer, and has many more libraries to choose from.

Server has one parameter is client, here we need to talk about the client parameter, if you do not specify the client, we will use the RequestsClient instance by default, it is a synchronous HTTPClient, you can also specify an asynchronous HTTP Client, for example: AiohttpClient. If you use a synchronous client, then all requests are synchronous, if you use an asynchronous client, then all requests are asynchronous.

The following is an example of send a payment by an asynchronous method, the same example of using the synchronization method can be found here:

 1"""
 2The effect of this example is the same as `payment.py`, but this example is asynchronous.
 3
 4Create, sign, and submit a transaction using Python Stellar SDK.
 5
 6Assumes that you have the following items:
 71. Secret key of a funded account to be the source account
 82. Public key of an existing account as a recipient
 9    These two keys can be created and funded by the friendbot at
10    https://www.stellar.org/laboratory/ under the heading "Quick Start: Test Account"
113. Access to Python Stellar SDK (https://github.com/StellarCN/py-stellar-base) through Python shell.
12
13See: https://developers.stellar.org/docs/start/list-of-operations/#payment
14"""
15import asyncio
16
17from stellar_sdk import Server, Keypair, TransactionBuilder, Network, AiohttpClient
18
19
20def create_account():
21    """To make this script work, create an account on the testnet."""
22    import requests
23    from stellar_sdk import Keypair
24
25    keypair = Keypair.random()
26    url = "https://friendbot.stellar.org"
27    _response = requests.get(url, params={"addr": keypair.public_key})
28    # Check _response.json() in case something goes wrong
29    return keypair
30
31
32# The source account is the account we will be signing and sending from.
33example_keypair = create_account()
34source_secret_key = example_keypair.secret
35
36# Derive Keypair object and public key (that starts with a G) from the secret
37source_keypair = Keypair.from_secret(source_secret_key)
38source_public_key = source_keypair.public_key
39
40# We just send lumen to ourselves in this simple example
41receiver_public_key = example_keypair.public_key
42
43
44async def main():
45    # Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
46    # To use the live network, set the hostname to 'horizon.stellar.org'
47    # When we use the `with` syntax, it automatically releases the resources it occupies.
48    async with Server(
49        horizon_url="https://horizon-testnet.stellar.org", client=AiohttpClient()
50    ) as server:
51        # Transactions require a valid sequence number that is specific to this account.
52        # We can fetch the current sequence number for the source account from Horizon.
53        source_account = await server.load_account(source_public_key)
54
55        base_fee = await server.fetch_base_fee()
56        # we are going to submit the transaction to the test network,
57        # so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
58        # if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
59        transaction = (
60            TransactionBuilder(
61                source_account=source_account,
62                network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
63                base_fee=base_fee,
64            )
65            .add_text_memo("Hello, Stellar!")  # Add a memo
66            # Add a payment operation to the transaction
67            # Send 350.1234567 XLM to receiver
68            # Specify 350.1234567 lumens. Lumens are divisible to seven digits past the decimal.
69            .append_payment_op(receiver_public_key, "350.1234567", "XLM")
70            .set_timeout(30)  # Make this transaction valid for the next 30 seconds only
71            .build()
72        )
73
74        # Sign this transaction with the secret key
75        # NOTE: signing is transaction is network specific. Test network transactions
76        # won't work in the public network. To switch networks, use the Network object
77        # as explained above (look for stellar_sdk.network.Network).
78        transaction.sign(source_keypair)
79
80        # Let's see the XDR (encoded in base64) of the transaction we just built
81        print(transaction.to_xdr())
82
83        # Submit the transaction to the Horizon server.
84        # The Horizon server will then submit the transaction into the network for us.
85        response = await server.submit_transaction(transaction)
86        print(response)
87
88
89if __name__ == "__main__":
90    loop = asyncio.get_event_loop()
91    loop.run_until_complete(main())
92    loop.close()
93    # asyncio.run(main())  # Python 3.7+

The following example helps you listen to multiple endpoints asynchronously.

 1"""
 2See: https://stellar-sdk.readthedocs.io/en/latest/asynchronous.html
 3"""
 4import asyncio
 5
 6from stellar_sdk import AiohttpClient, Server
 7
 8HORIZON_URL = "https://horizon.stellar.org"
 9
10
11async def payments():
12    async with Server(HORIZON_URL, AiohttpClient()) as server:
13        async for payment in server.payments().cursor(cursor="now").stream():
14            print(f"Payment: {payment}")
15
16
17async def effects():
18    async with Server(HORIZON_URL, AiohttpClient()) as server:
19        async for effect in server.effects().cursor(cursor="now").stream():
20            print(f"Effect: {effect}")
21
22
23async def operations():
24    async with Server(HORIZON_URL, AiohttpClient()) as server:
25        async for operation in server.operations().cursor(cursor="now").stream():
26            print(f"Operation: {operation}")
27
28
29async def transactions():
30    async with Server(HORIZON_URL, AiohttpClient()) as server:
31        async for transaction in server.transactions().cursor(cursor="now").stream():
32            print(f"Transaction: {transaction}")
33
34
35async def listen():
36    await asyncio.gather(payments(), effects(), operations(), transactions())
37
38
39if __name__ == "__main__":
40    asyncio.run(listen())