mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 04:05:49 +00:00
Send XRP (py): updates for 1.0.0
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
# Example Credentials ----------------------------------------------------------
|
# Example Credentials ----------------------------------------------------------
|
||||||
from xrpl.wallet import Wallet
|
from xrpl.wallet import Wallet
|
||||||
test_wallet = Wallet(seed="sn3nxiW7v8KXzPzAqzyHXbSSKNuN9")
|
test_wallet = Wallet(seed="sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", sequence=16237283)
|
||||||
print(test_wallet.classic_address) # "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
|
print(test_wallet.classic_address) # "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
|
||||||
|
|
||||||
# Connect ----------------------------------------------------------------------
|
# Connect ----------------------------------------------------------------------
|
||||||
from xrpl.clients import JsonRpcClient
|
import xrpl
|
||||||
testnet_url = "https://s.altnet.rippletest.net:51234"
|
testnet_url = "https://s.altnet.rippletest.net:51234"
|
||||||
client = JsonRpcClient(testnet_url)
|
client = xrpl.clients.JsonRpcClient(testnet_url)
|
||||||
|
|
||||||
# Get credentials from the Testnet Faucet -----------------------------------
|
# Get credentials from the Testnet Faucet -----------------------------------
|
||||||
# For production, instead create a Wallet instance as above
|
# For production, instead create a Wallet instance as above
|
||||||
@@ -16,41 +16,43 @@ from xrpl.wallet import generate_faucet_wallet
|
|||||||
test_wallet = generate_faucet_wallet(client, debug=True)
|
test_wallet = generate_faucet_wallet(client, debug=True)
|
||||||
|
|
||||||
# Prepare transaction ----------------------------------------------------------
|
# Prepare transaction ----------------------------------------------------------
|
||||||
from xrpl.ledger import get_latest_validated_ledger_sequence
|
import xrpl.utils # workaround for https://github.com/XRPLF/xrpl-py/issues/222
|
||||||
current_validated_ledger = get_latest_validated_ledger_sequence(client)
|
my_payment = xrpl.models.transactions.Payment(
|
||||||
min_ledger = current_validated_ledger + 1
|
|
||||||
max_ledger = min_ledger + 20
|
|
||||||
|
|
||||||
from xrpl.models.transactions import Payment
|
|
||||||
from xrpl.utils import xrp_to_drops
|
|
||||||
my_payment = Payment(
|
|
||||||
account=test_wallet.classic_address,
|
account=test_wallet.classic_address,
|
||||||
amount=xrp_to_drops(22),
|
amount=xrpl.utils.xrp_to_drops(22),
|
||||||
destination="rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
|
destination="rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
|
||||||
last_ledger_sequence=max_ledger,
|
|
||||||
sequence=test_wallet.next_sequence_num,
|
|
||||||
fee="10",
|
|
||||||
)
|
)
|
||||||
print("Payment object:", my_payment)
|
print("Payment object:", my_payment)
|
||||||
print(f"Can be validated in ledger range: {min_ledger} - {max_ledger}")
|
|
||||||
|
|
||||||
# Sign transaction -------------------------------------------------------------
|
# Sign transaction -------------------------------------------------------------
|
||||||
import xrpl.transaction
|
signed_tx = xrpl.transaction.safe_sign_and_autofill_transaction(
|
||||||
signed = xrpl.transaction.safe_sign_transaction(my_payment, test_wallet)
|
my_payment, test_wallet, client)
|
||||||
print("Signed transaction blob:", signed)
|
max_ledger = signed_tx.last_ledger_sequence
|
||||||
|
print("Signed transaction:", signed_tx)
|
||||||
|
print("Transaction cost:", xrpl.utils.drops_to_xrp(signed_tx.fee), "XRP")
|
||||||
|
print("Transaction expires after ledger:", max_ledger)
|
||||||
|
|
||||||
# Submit transaction -----------------------------------------------------------
|
# Submit transaction -----------------------------------------------------------
|
||||||
prelim_result = xrpl.transaction.submit_transaction_blob(signed, client)
|
validated_index = xrpl.ledger.get_latest_validated_ledger_sequence(client)
|
||||||
|
min_ledger = validated_index + 1
|
||||||
|
print(f"Can be validated in ledger range: {min_ledger} - {max_ledger}")
|
||||||
|
|
||||||
|
# Tip: you can use xrpl.transaction.send_reliable_submission(signed_tx, client)
|
||||||
|
# to send the transaction and wait for the results to be validated.
|
||||||
|
try:
|
||||||
|
prelim_result = xrpl.transaction.submit_transaction(signed_tx, client)
|
||||||
|
except xrpl.transaction.XRPLReliableSubmissionException as e:
|
||||||
|
exit(f"Submit failed: {e}")
|
||||||
print("Preliminary transaction result:", prelim_result)
|
print("Preliminary transaction result:", prelim_result)
|
||||||
if not prelim_result.is_successful():
|
|
||||||
exit("Submit failed.")
|
|
||||||
tx_id = prelim_result.result["tx_json"]["hash"]
|
tx_id = prelim_result.result["tx_json"]["hash"]
|
||||||
|
|
||||||
# Wait for validation ----------------------------------------------------------
|
# Wait for validation ----------------------------------------------------------
|
||||||
|
# Note: If you used xrpl.transaction.send_reliable_submission, you can skip this
|
||||||
|
# and use the result of that method directly.
|
||||||
from time import sleep
|
from time import sleep
|
||||||
while True:
|
while True:
|
||||||
sleep(1)
|
sleep(1)
|
||||||
current_validated_ledger = get_latest_validated_ledger_sequence(client)
|
validated_ledger = xrpl.ledger.get_latest_validated_ledger_sequence(client)
|
||||||
tx_response = xrpl.transaction.get_transaction_from_hash(tx_id, client)
|
tx_response = xrpl.transaction.get_transaction_from_hash(tx_id, client)
|
||||||
if tx_response.is_successful():
|
if tx_response.is_successful():
|
||||||
if tx_response.result.get("validated"):
|
if tx_response.result.get("validated"):
|
||||||
@@ -58,8 +60,8 @@ while True:
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print(f"Results not yet validated... "
|
print(f"Results not yet validated... "
|
||||||
f"({current_validated_ledger}/{max_ledger})")
|
f"({validated_ledger}/{max_ledger})")
|
||||||
if current_validated_ledger > max_ledger:
|
if validated_ledger > max_ledger:
|
||||||
print("max_ledger has passed. Last tx response:", tx_response)
|
print("max_ledger has passed. Last tx response:", tx_response)
|
||||||
|
|
||||||
# Check transaction results ----------------------------------------------------
|
# Check transaction results ----------------------------------------------------
|
||||||
@@ -70,5 +72,5 @@ metadata = tx_response.result.get("meta", {})
|
|||||||
if metadata.get("TransactionResult"):
|
if metadata.get("TransactionResult"):
|
||||||
print("Result code:", metadata["TransactionResult"])
|
print("Result code:", metadata["TransactionResult"])
|
||||||
if metadata.get("delivered_amount"):
|
if metadata.get("delivered_amount"):
|
||||||
from xrpl.utils import drops_to_xrp
|
print("XRP delivered:", xrpl.utils.drops_to_xrp(
|
||||||
print("XRP delivered:", drops_to_xrp(metadata["delivered_amount"]))
|
metadata["delivered_amount"]))
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ This tutorial explains how to send a simple XRP Payment using ripple-lib for Jav
|
|||||||
To interact with the XRP Ledger, you need to set up a dev environment with the necessary tools. This tutorial provides examples using the following options:
|
To interact with the XRP Ledger, you need to set up a dev environment with the necessary tools. This tutorial provides examples using the following options:
|
||||||
|
|
||||||
- **JavaScript** with the [ripple-lib (RippleAPI) library](https://github.com/ripple/ripple-lib/). See the [RippleAPI Beginners Guide](get-started-with-rippleapi-for-javascript.html) for detailed instructions on getting started.
|
- **JavaScript** with the [ripple-lib (RippleAPI) library](https://github.com/ripple/ripple-lib/). See the [RippleAPI Beginners Guide](get-started-with-rippleapi-for-javascript.html) for detailed instructions on getting started.
|
||||||
- **Python** with the [xrpl-py library](https://xrpl-py.readthedocs.io/). <!--{# TODO: add "get started" link here #}-->
|
- **Python** with the [xrpl-py library](https://xrpl-py.readthedocs.io/). See [Get Started using Python](get-started-using-python.html) for setup steps.
|
||||||
|
|
||||||
|
|
||||||
## Send a Payment on the Test Net
|
## Send a Payment on the Test Net
|
||||||
@@ -283,9 +283,10 @@ console.log(generated.secret) // Example: sp6JS7f14BuwFY8Mw6bTtLKWauoUs
|
|||||||
_Python_
|
_Python_
|
||||||
|
|
||||||
```py
|
```py
|
||||||
from xrpl.core.keypairs import generate_seed
|
|
||||||
from xrpl.wallet import Wallet
|
from xrpl.wallet import Wallet
|
||||||
my_wallet = Wallet(generate_seed()) #TODO: update this with any changes from https://github.com/XRPLF/xrpl-py/pull/194
|
my_wallet = Wallet.create()
|
||||||
|
print(my_wallet.classic_address) # Example: rGCkuB7PBr5tNy68tPEABEtcdno4hE6Y7f
|
||||||
|
print(my_wallet.seed) # Example: sp6JS7f14BuwFY8Mw6bTtLKWauoUs
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|||||||
Reference in New Issue
Block a user