Creating a payment transaction¶
Payment¶
In this example, the destination account must exist. We use synchronous methods to submit transactions here, if you want, you can also use asynchronous methods.
1"""
2Create, sign, and submit a transaction using Python Digital World SDK.
3
4Assumes that you have the following items:
51. Secret key of a funded account to be the source account
62. Public key of an existing account as a recipient
7 These two keys can be created and funded by the faucet at
8 https://lab.digitalworld.global/ under the heading "Quick Start: Test Account"
93. Access to Python Digital World SDK (https://github.com/DigitalWorld/digital-world-py-sdk) through Python shell.
10
11See: https://docs.digitalworld.global/docs/start/list-of-operations/#payment
12"""
13
14from digital_world_sdk import Asset, Keypair, Network, GatewayServer, TransactionBuilder
15
16# The source account is the account we will be signing and sending from.
17# Derive Keypair object and public key (that starts with a G) from the secret
18source_keypair = Keypair.from_secret(
19 "SCDG4ORIDX4QGPMMHQY36KDHHMTJEM4RQ2AWKH3G7AXHTVBJWEV6XOUM"
20)
21source_public_key = source_keypair.public_key
22
23# We are sending the native asset to the receiver account
24receiver_public_key = "GD2JXEFGEO53CNQ22KN2ICOQ2LOASCABQHAIOMLZV265C246PFKKHPYU"
25
26# Configure Digital World SDK to talk to the Gateway instance
27# To use the live network, set the hostname to 'dex.digitalworld.global'
28server = GatewayServer(gateway_url="https://dex-testnet.digitalworld.global")
29
30# Transactions require a valid sequence number that is specific to this account.
31# We can fetch the current sequence number for the source account from Gateway.
32source_account = server.load_account(source_public_key)
33
34base_fee = 100
35# we are going to submit the transaction to the test network,
36# so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
37# if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
38transaction = (
39 TransactionBuilder(
40 source_account=source_account,
41 network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
42 base_fee=base_fee,
43 )
44 .add_text_memo("Hello, Digital World!") # Add a memo
45 # Add a payment operation to the transaction
46 # Send 350.1234567 of the native asset to receiver
47 # The native asset is divisible to seven digits past the decimal.
48 .append_payment_op(receiver_public_key, Asset.native(), "350.1234567")
49 .set_timeout(30) # Make this transaction valid for the next 30 seconds only
50 .build()
51)
52
53# Sign this transaction with the secret key
54# NOTE: signing is transaction is network specific. Test network transactions
55# won't work in the public network. To switch networks, use the Network object
56# as explained above (look for digital_world_sdk.network.Network).
57transaction.sign(source_keypair)
58
59# Let's see the XDR (encoded in base64) of the transaction we just built
60print(transaction.to_xdr())
61
62# Submit the transaction to the Gateway server.
63# The Gateway server will then submit the transaction into the network for us.
64response = server.submit_transaction(transaction)
65print(response)
Path Payment¶
In the example below we’re sending 1000 XLM (at max) from GABJLI6IVBKJ7HIC5NN7HHDCIEW3CMWQ2DWYHREQQUFWSWZ2CDAMZZX4 to GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB. Destination Asset will be GBP issued by GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW. Assets will be exchanged using the following path:
USD issued by GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB
EUR issued by GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL
The path payment will cause the destination address to get 5.5 GBP. It will cost the sender no more than 1000 XLM. In this example there will be 3 exchanges, XLM->USD, USD->EUR, EUR->GBP.
1"""
2A path payment sends an amount of a specific asset to a destination account through a path of offers.
3Since the asset sent (e.g., 450 XLM) can be different from the asset received (e.g, 6 BTC),
4path payments allow for the simultaneous transfer and conversion of currencies.
5
6A Path Payment Strict Send allows a user to specify the amount of the asset to send.
7The amount received will vary based on offers in the order books. If you would like to
8instead specify the amount received, use the Path Payment Strict Receive operation.
9
10See: https://docs.digitalworld.global/docs/start/list-of-operations/#path-payment-strict-send
11See: https://youtu.be/KzlSgSPStz8
12"""
13
14from digital_world_sdk import Asset, Keypair, Network, GatewayServer, TransactionBuilder
15
16server = GatewayServer(gateway_url="https://dex-testnet.digitalworld.global")
17source_keypair = Keypair.from_secret(
18 "SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC"
19)
20
21source_account = server.load_account(account_id=source_keypair.public_key)
22
23path = [
24 Asset("USD", "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"),
25 Asset("EUR", "GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL"),
26]
27transaction = (
28 TransactionBuilder(
29 source_account=source_account,
30 network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
31 base_fee=100,
32 )
33 .append_path_payment_strict_receive_op(
34 destination="GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB",
35 send_asset=Asset.native(),
36 send_max="1000",
37 dest_asset=Asset(
38 "GBP", "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"
39 ),
40 dest_amount="5.50",
41 path=path,
42 )
43 .set_timeout(30)
44 .build()
45)
46transaction.sign(source_keypair)
47response = server.submit_transaction(transaction)