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.
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"""
15
16import asyncio
17
18from stellar_sdk import (
19 AiohttpClient,
20 Asset,
21 Keypair,
22 Network,
23 ServerAsync,
24 TransactionBuilder,
25)
26
27# The source account is the account we will be signing and sending from.
28# Derive Keypair object and public key (that starts with a G) from the secret
29source_keypair = Keypair.from_secret(
30 "SCDG4ORIDX4QGPMMHQY36KDHHMTJEM4RQ2AWKH3G7AXHTVBJWEV6XOUM"
31)
32source_public_key = source_keypair.public_key
33
34# We are sending lumen to the receiver account
35receiver_public_key = "GD2JXEFGEO53CNQ22KN2ICOQ2LOASCABQHAIOMLZV265C246PFKKHPYU"
36
37
38async def main():
39 # Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
40 # To use the live network, set the hostname to 'horizon.stellar.org'
41 # When we use the `with` syntax, it automatically releases the resources it occupies.
42 async with ServerAsync(
43 horizon_url="https://horizon-testnet.stellar.org", client=AiohttpClient()
44 ) as server:
45 # Transactions require a valid sequence number that is specific to this account.
46 # We can fetch the current sequence number for the source account from Horizon.
47 source_account = await server.load_account(source_public_key)
48
49 base_fee = 100
50 # we are going to submit the transaction to the test network,
51 # so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
52 # if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
53 transaction = (
54 TransactionBuilder(
55 source_account=source_account,
56 network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
57 base_fee=base_fee,
58 )
59 .add_text_memo("Hello, Stellar!") # Add a memo
60 # Add a payment operation to the transaction
61 # Send 350.1234567 XLM to receiver
62 # Specify 350.1234567 lumens. Lumens are divisible to seven digits past the decimal.
63 .append_payment_op(receiver_public_key, Asset.native(), "350.1234567")
64 .set_timeout(30) # Make this transaction valid for the next 30 seconds only
65 .build()
66 )
67
68 # Sign this transaction with the secret key
69 # NOTE: signing is transaction is network specific. Test network transactions
70 # won't work in the public network. To switch networks, use the Network object
71 # as explained above (look for stellar_sdk.network.Network).
72 transaction.sign(source_keypair)
73
74 # Let's see the XDR (encoded in base64) of the transaction we just built
75 print(transaction.to_xdr())
76
77 # Submit the transaction to the Horizon server.
78 # The Horizon server will then submit the transaction into the network for us.
79 response = await server.submit_transaction(transaction)
80 print(response)
81
82
83if __name__ == "__main__":
84 asyncio.run(main())
The following example helps you listen to multiple endpoints asynchronously.
1"""
2See: https://stellar-sdk.readthedocs.io/en/latest/asynchronous.html
3"""
4
5import asyncio
6
7from stellar_sdk import AiohttpClient, ServerAsync
8
9HORIZON_URL = "https://horizon.stellar.org"
10
11
12async def payments():
13 async with ServerAsync(HORIZON_URL, AiohttpClient()) as server:
14 async for payment in server.payments().cursor(cursor="now").stream():
15 print(f"Payment: {payment}")
16
17
18async def effects():
19 async with ServerAsync(HORIZON_URL, AiohttpClient()) as server:
20 async for effect in server.effects().cursor(cursor="now").stream():
21 print(f"Effect: {effect}")
22
23
24async def operations():
25 async with ServerAsync(HORIZON_URL, AiohttpClient()) as server:
26 async for operation in server.operations().cursor(cursor="now").stream():
27 print(f"Operation: {operation}")
28
29
30async def transactions():
31 async with ServerAsync(HORIZON_URL, AiohttpClient()) as server:
32 async for transaction in server.transactions().cursor(cursor="now").stream():
33 print(f"Transaction: {transaction}")
34
35
36async def listen():
37 await asyncio.gather(payments(), effects(), operations(), transactions())
38
39
40if __name__ == "__main__":
41 asyncio.run(listen())