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 Stellar 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 friendbot at
 8    https://www.stellar.org/laboratory/ under the heading "Quick Start: Test Account"
 93. Access to Python Stellar SDK (https://github.com/StellarCN/py-stellar-base) through Python shell.
10
11See: https://developers.stellar.org/docs/start/list-of-operations/#payment
12"""
13from stellar_sdk import Server, Keypair, TransactionBuilder, Network
14
15# The source account is the account we will be signing and sending from.
16source_secret_key = "SBFZCHU5645DOKRWYBXVOXY2ELGJKFRX6VGGPRYUWHQ7PMXXJNDZFMKD"
17
18# Derive Keypair object and public key (that starts with a G) from the secret
19source_keypair = Keypair.from_secret(source_secret_key)
20source_public_key = source_keypair.public_key
21
22receiver_public_key = "GA7YNBW5CBTJZ3ZZOWX3ZNBKD6OE7A7IHUQVWMY62W2ZBG2SGZVOOPVH"
23
24# Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
25# To use the live network, set the hostname to 'horizon.stellar.org'
26server = Server(horizon_url="https://horizon-testnet.stellar.org")
27
28# Transactions require a valid sequence number that is specific to this account.
29# We can fetch the current sequence number for the source account from Horizon.
30source_account = server.load_account(source_public_key)
31
32base_fee = server.fetch_base_fee()
33# we are going to submit the transaction to the test network,
34# so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
35# if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
36transaction = (
37    TransactionBuilder(
38        source_account=source_account,
39        network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
40        base_fee=base_fee,
41    )
42    .add_text_memo("Hello, Stellar!")  # Add a memo
43    # Add a payment operation to the transaction
44    # Send 350.1234567 XLM to receiver
45    # Specify 350.1234567 lumens. Lumens are divisible to seven digits past the decimal.
46    .append_payment_op(receiver_public_key, "350.1234567", "XLM")
47    .set_timeout(30)  # Make this transaction valid for the next 30 seconds only
48    .build()
49)
50
51# Sign this transaction with the secret key
52# NOTE: signing is transaction is network specific. Test network transactions
53# won't work in the public network. To switch networks, use the Network object
54# as explained above (look for stellar_sdk.network.Network).
55transaction.sign(source_keypair)
56
57# Let's see the XDR (encoded in base64) of the transaction we just built
58print(transaction.to_xdr())
59
60# Submit the transaction to the Horizon server.
61# The Horizon server will then submit the transaction into the network for us.
62response = server.submit_transaction(transaction)
63print(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://developers.stellar.org/docs/start/list-of-operations/#path-payment-strict-send
11See: https://youtu.be/KzlSgSPStz8
12"""
13from stellar_sdk import Keypair, Server, TransactionBuilder, Network, Asset
14
15server = Server(horizon_url="https://horizon-testnet.stellar.org")
16source_keypair = Keypair.from_secret(
17    "SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC"
18)
19
20source_account = server.load_account(account_id=source_keypair.public_key)
21
22path = [
23    Asset("USD", "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"),
24    Asset("EUR", "GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL"),
25]
26transaction = (
27    TransactionBuilder(
28        source_account=source_account,
29        network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
30        base_fee=100,
31    )
32    .append_path_payment_strict_receive_op(
33        destination="GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB",
34        send_code="XLM",
35        send_issuer=None,
36        send_max="1000",
37        dest_code="GBP",
38        dest_issuer="GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW",
39        dest_amount="5.50",
40        path=path,
41    )
42    .set_timeout(30)
43    .build()
44)
45transaction.sign(source_keypair)
46response = server.submit_transaction(transaction)