AsynchronousΒΆ
Now we have supported the use of asynchronous methods to submit transactions, digital-world-py-sdk 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 Digital World 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 faucet at
10 https://lab.digitalworld.global/ under the heading "Quick Start: Test Account"
113. Access to Python Digital World SDK (https://github.com/DigitalWorld/digital-world-py-sdk) through Python shell.
12
13See: https://docs.digitalworld.global/docs/start/list-of-operations/#payment
14"""
15
16import asyncio
17
18from digital_world_sdk import (
19 AiohttpClient,
20 Asset,
21 Keypair,
22 Network,
23 GatewayServerAsync,
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 the native asset to the receiver account
35receiver_public_key = "GD2JXEFGEO53CNQ22KN2ICOQ2LOASCABQHAIOMLZV265C246PFKKHPYU"
36
37
38async def main():
39 # Configure Digital World SDK to talk to the Gateway instance
40 # To use the live network, set the hostname to 'dex.digitalworld.global'
41 # When we use the `with` syntax, it automatically releases the resources it occupies.
42 async with GatewayServerAsync(
43 gateway_url="https://dex-testnet.digitalworld.global", 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 Gateway.
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, Digital World!") # Add a memo
60 # Add a payment operation to the transaction
61 # Send 350.1234567 of the native asset to receiver
62 # The native asset is 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 digital_world_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 Gateway server.
78 # The Gateway 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://digital-world-sdk.docs.digitalworld.global/en/latest/asynchronous.html
3"""
4
5import asyncio
6
7from digital_world_sdk import AiohttpClient, GatewayServerAsync
8
9GATEWAY_URL = "https://dex.digitalworld.global"
10
11
12async def payments():
13 async with GatewayServerAsync(GATEWAY_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 GatewayServerAsync(GATEWAY_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 GatewayServerAsync(GATEWAY_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 GatewayServerAsync(GATEWAY_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())