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# The source account is the account we will be signing and sending from.
20source_secret_key = "SBFZCHU5645DOKRWYBXVOXY2ELGJKFRX6VGGPRYUWHQ7PMXXJNDZFMKD"
21
22# Derive Keypair object and public key (that starts with a G) from the secret
23source_keypair = Keypair.from_secret(source_secret_key)
24source_public_key = source_keypair.public_key
25
26receiver_public_key = "GA7YNBW5CBTJZ3ZZOWX3ZNBKD6OE7A7IHUQVWMY62W2ZBG2SGZVOOPVH"
27
28
29async def main():
30    # Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
31    # To use the live network, set the hostname to 'horizon.stellar.org'
32    # When we use the `with` syntax, it automatically releases the resources it occupies.
33    async with Server(
34        horizon_url="https://horizon-testnet.stellar.org", client=AiohttpClient()
35    ) as server:
36        # Transactions require a valid sequence number that is specific to this account.
37        # We can fetch the current sequence number for the source account from Horizon.
38        source_account = await server.load_account(source_public_key)
39
40        base_fee = await server.fetch_base_fee()
41        # we are going to submit the transaction to the test network,
42        # so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
43        # if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
44        transaction = (
45            TransactionBuilder(
46                source_account=source_account,
47                network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
48                base_fee=base_fee,
49            )
50            .add_text_memo("Hello, Stellar!")  # Add a memo
51            # Add a payment operation to the transaction
52            # Send 350.1234567 XLM to receiver
53            # Specify 350.1234567 lumens. Lumens are divisible to seven digits past the decimal.
54            .append_payment_op(receiver_public_key, "350.1234567", "XLM")
55            .set_timeout(30)  # Make this transaction valid for the next 30 seconds only
56            .build()
57        )
58
59        # Sign this transaction with the secret key
60        # NOTE: signing is transaction is network specific. Test network transactions
61        # won't work in the public network. To switch networks, use the Network object
62        # as explained above (look for stellar_sdk.network.Network).
63        transaction.sign(source_keypair)
64
65        # Let's see the XDR (encoded in base64) of the transaction we just built
66        print(transaction.to_xdr())
67
68        # Submit the transaction to the Horizon server.
69        # The Horizon server will then submit the transaction into the network for us.
70        response = await server.submit_transaction(transaction)
71        print(response)
72
73
74if __name__ == "__main__":
75    loop = asyncio.get_event_loop()
76    loop.run_until_complete(main())
77    loop.close()
78    # 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())