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 Keypair, Network, Server, TransactionBuilder
14
15
16def create_account():
17    """To make this script work, create an account on the testnet."""
18    import requests
19
20    from stellar_sdk import Keypair
21
22    keypair = Keypair.random()
23    url = "https://friendbot.stellar.org"
24    _response = requests.get(url, params={"addr": keypair.public_key})
25    # Check _response.json() in case something goes wrong
26    return keypair
27
28
29# The source account is the account we will be signing and sending from.
30example_keypair = create_account()
31source_secret_key = example_keypair.secret
32
33# Derive Keypair object and public key (that starts with a G) from the secret
34source_keypair = Keypair.from_secret(source_secret_key)
35source_public_key = source_keypair.public_key
36
37# We just send lumen to ourselves in this simple example
38receiver_public_key = example_keypair.public_key
39
40# Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
41# To use the live network, set the hostname to 'horizon.stellar.org'
42server = Server(horizon_url="https://horizon-testnet.stellar.org")
43
44# Transactions require a valid sequence number that is specific to this account.
45# We can fetch the current sequence number for the source account from Horizon.
46source_account = server.load_account(source_public_key)
47
48base_fee = server.fetch_base_fee()
49# we are going to submit the transaction to the test network,
50# so network_passphrase is `Network.TESTNET_NETWORK_PASSPHRASE`,
51# if you want to submit to the public network, please use `Network.PUBLIC_NETWORK_PASSPHRASE`.
52transaction = (
53    TransactionBuilder(
54        source_account=source_account,
55        network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
56        base_fee=base_fee,
57    )
58    .add_text_memo("Hello, Stellar!")  # Add a memo
59    # Add a payment operation to the transaction
60    # Send 350.1234567 XLM to receiver
61    # Specify 350.1234567 lumens. Lumens are divisible to seven digits past the decimal.
62    .append_payment_op(receiver_public_key, "350.1234567", "XLM")
63    .set_timeout(30)  # Make this transaction valid for the next 30 seconds only
64    .build()
65)
66
67# Sign this transaction with the secret key
68# NOTE: signing is transaction is network specific. Test network transactions
69# won't work in the public network. To switch networks, use the Network object
70# as explained above (look for stellar_sdk.network.Network).
71transaction.sign(source_keypair)
72
73# Let's see the XDR (encoded in base64) of the transaction we just built
74print(transaction.to_xdr())
75
76# Submit the transaction to the Horizon server.
77# The Horizon server will then submit the transaction into the network for us.
78response = server.submit_transaction(transaction)
79print(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 Asset, Keypair, Network, Server, TransactionBuilder
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)