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