From e3a8109d263d3cfaeb8fb9e72ea00aa00a16e6d2 Mon Sep 17 00:00:00 2001 From: Riku Date: Fri, 17 Feb 2023 17:12:40 +0100 Subject: [PATCH 1/6] add basic python async getting started example --- content/_code-samples/get-started/py/base.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 content/_code-samples/get-started/py/base.py diff --git a/content/_code-samples/get-started/py/base.py b/content/_code-samples/get-started/py/base.py new file mode 100644 index 0000000000..0b33d9b4e9 --- /dev/null +++ b/content/_code-samples/get-started/py/base.py @@ -0,0 +1,16 @@ +import asyncio +from xrpl.asyncio.clients import AsyncWebsocketClient + + +async def main(): + # Define the network client + async with AsyncWebsocketClient("wss://s.altnet.rippletest.net:51233") as client: + # inside the context the client is open + + # ... custom code goes here + + # after exiting the context, the client is closed + + +asyncio.run(main()) + From 259360735d0022d0d89d916ec5e8ff4022e16e35 Mon Sep 17 00:00:00 2001 From: Riku Date: Fri, 17 Feb 2023 17:14:31 +0100 Subject: [PATCH 2/6] add async python require-dest-tag code example It's now very similar to the js example --- .../py/require-destination-tags.py | 94 ++++++++++--------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/content/_code-samples/require-destination-tags/py/require-destination-tags.py b/content/_code-samples/require-destination-tags/py/require-destination-tags.py index 9787e42ab4..7f71af9abf 100644 --- a/content/_code-samples/require-destination-tags/py/require-destination-tags.py +++ b/content/_code-samples/require-destination-tags/py/require-destination-tags.py @@ -1,48 +1,58 @@ -from xrpl.clients import JsonRpcClient -from xrpl.models.transactions import AccountSet, AccountSetFlag -from xrpl.transaction import safe_sign_and_autofill_transaction, submit_transaction -from xrpl.wallet import generate_faucet_wallet +import asyncio + +from xrpl.asyncio.clients import AsyncWebsocketClient +from xrpl.asyncio.transaction import ( + safe_sign_and_autofill_transaction, + send_reliable_submission, +) +from xrpl.asyncio.wallet import generate_faucet_wallet from xrpl.models.requests import AccountInfo - -# Stand-alone code sample for the "Require Destination Tags" tutorial: -# https://xrpl.org/require-destination-tags.html - -lsfRequireDestTag = 131072 - -# Connect to a testnet node -print("Connecting to Testnet...") -JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/" -client = JsonRpcClient(JSON_RPC_URL) - -# Get credentials from the Testnet Faucet -print("Requesting address from the Testnet faucet...") -test_wallet = generate_faucet_wallet(client=client) -myAddr = test_wallet.classic_address - -# Construct AccountSet transaction -tx = AccountSet( - account=myAddr, - set_flag=AccountSetFlag.ASF_REQUIRE_DEST +from xrpl.models.transactions import ( + AccountSet, + AccountSetFlag, ) -print(f"Prepared transaction: {tx}") -# Sign the transaction -my_tx_payment_signed = safe_sign_and_autofill_transaction(tx, wallet=test_wallet, client=client) -print(f"Transaction Hash: {my_tx_payment_signed.txn_signature}") -# Send the transaction to the node -print(f"Enabling Require Destination Tag flag (asfRequireDest) on {myAddr}") -submit_tx = submit_transaction(client=client, transaction=my_tx_payment_signed) -submit_tx = submit_tx.result["engine_result"] -print(f"Submit result: {submit_tx}") +async def main() -> int: + # Define the network client + async with AsyncWebsocketClient("wss://s.altnet.rippletest.net:51233") as client: + # Get credentials from the Testnet Faucet ----------------------------------- + print("Requesting addresses from the Testnet faucet...") + wallet = await generate_faucet_wallet(client, debug=True) -# Verify Account Settings -get_acc_flag = AccountInfo( - account=myAddr -) -response = client.request(get_acc_flag) + # Send AccountSet transaction ----------------------------------------------- + tx = AccountSet( + account=wallet.classic_address, + set_flag=AccountSetFlag.ASF_REQUIRE_DEST, + ) + + # Sign and autofill the transaction (ready to submit) + signed_tx = await safe_sign_and_autofill_transaction(tx, wallet, client) + print("Transaction hash:", signed_tx.get_hash()) + + # Submit the transaction and wait for response (validated or rejected) + submit_result = await send_reliable_submission(signed_tx, client) + print("Submit result:", submit_result) + + # Confirm Account Settings -------------------------------------------------- + account_info = await client.request( + AccountInfo( + account=wallet.classic_address, + ledger_index="validated", + ) + ) + + # Verify that the AccountRoot lsfRequireDestTag flag is set + flags = account_info.result["account_data"]["Flags"] + if flags & 0x00020000 != 0: + print("Require Destination Tag is enabled.") + else: + print("Require Destination Tag is DISABLED.") + + # End main() + return 0 + + +if __name__ == "__main__": + asyncio.run(main()) -if response.result['account_data']['Flags'] & lsfRequireDestTag: - print("Require Destination Tag is ENABLED.") -else: - print("Require Destination Tag is DISABLED.") From 8ef2befe05dd96d49b98bf2a6cb9e919622c43f2 Mon Sep 17 00:00:00 2001 From: Riku Date: Fri, 17 Feb 2023 17:17:01 +0100 Subject: [PATCH 3/6] update content/tutorials/manage-account-settings/require-destination-tags.md - add python tabs --- .../require-destination-tags.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/content/tutorials/manage-account-settings/require-destination-tags.md b/content/tutorials/manage-account-settings/require-destination-tags.md index bdaf939dee..c591f74f9d 100644 --- a/content/tutorials/manage-account-settings/require-destination-tags.md +++ b/content/tutorials/manage-account-settings/require-destination-tags.md @@ -42,8 +42,18 @@ When you're [building production-ready software](production-readiness.html), you You must be connected to the network to submit transactions to it. The following code shows how to connect to a public XRP Ledger Testnet server a supported [client library](client-libraries.html): + + +_JavaScript_ + {{ include_code("_code-samples/get-started/js/base.js", language="js") }} +_Python_ + +{{ include_code("_code-samples/get-started/py/base.py", language="py") }} + + + For this tutorial, click the following button to connect: {% include '_snippets/interactive-tutorials/connect-step.md' %} @@ -60,6 +70,10 @@ _JavaScript_ {{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }} +_Python_ + +{{ include_code("_code-samples/require-destination-tags/py/require-destination-tags.py", language="py", start_with="# Send AccountSet", end_before="# Confirm Account") }} + {{ start_step("Send AccountSet") }} @@ -89,6 +103,10 @@ _JavaScript_ {{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }} +_Python_ + +{{ include_code("_code-samples/require-destination-tags/py/require-destination-tags.py", language="py", start_with="# Confirm Account", end_before="# End main()") }} + From 787f08f347a9dc672a38e52516a80cfbc160a16f Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 18 Feb 2023 11:13:48 +0100 Subject: [PATCH 4/6] clarify prerequisites and example source --- .../require-destination-tags.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/content/tutorials/manage-account-settings/require-destination-tags.md b/content/tutorials/manage-account-settings/require-destination-tags.md index c591f74f9d..1e5e0fe602 100644 --- a/content/tutorials/manage-account-settings/require-destination-tags.md +++ b/content/tutorials/manage-account-settings/require-destination-tags.md @@ -19,13 +19,20 @@ This tutorial demonstrates how to enable the Require Destination Tag flag on you ## Prerequisites - You need a funded XRP Ledger account, with an address, secret key, and some XRP. For production, you can use the same address and secret consistently. For this tutorial, you can generate new test credentials as needed. -- You need a connection to the XRP Ledger network. - -This page provides examples that use [xrpl.js](get-started-using-javascript.html) in the web browser, so you can read along and use the interactive steps without any setup. +- You need a connection to the XRP Ledger network. As shown in this tutorial, you can use public servers for testing. +- You should be familiar with the Getting Started instructions for your preferred client library. This page provides examples for the following: + - **JavaScript** with the [xrpl.js library](https://github.com/XRPLF/xrpl.js/). See [Get Started Using JavaScript](get-started-using-javascript.html) for setup steps. + - **Python** with the [`xrpl-py` library](https://xrpl-py.readthedocs.io/). See [Get Started using Python](get-started-using-python.html) for setup steps. + - You can also read along and use the interactive steps in your browser without any setup. +## Example Code + +Complete sample code for all the steps of these tutorials is available under the [MIT license](https://github.com/XRPLF/xrpl-dev-portal/blob/master/LICENSE). + +- See [Code Samples: Require Destination Tags](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/require-destination-tags/) in the source repository for this website. ## Steps {% set n = cycler(* range(1,99)) %} From cfc226efb2843fed4bc003d0d9da30916d1bf1c8 Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 4 Mar 2023 11:05:18 +0100 Subject: [PATCH 5/6] rename base-async --- content/_code-samples/get-started/py/{base.py => base-async.py} | 0 .../manage-account-settings/require-destination-tags.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename content/_code-samples/get-started/py/{base.py => base-async.py} (100%) diff --git a/content/_code-samples/get-started/py/base.py b/content/_code-samples/get-started/py/base-async.py similarity index 100% rename from content/_code-samples/get-started/py/base.py rename to content/_code-samples/get-started/py/base-async.py diff --git a/content/tutorials/manage-account-settings/require-destination-tags.md b/content/tutorials/manage-account-settings/require-destination-tags.md index 1e5e0fe602..bf8de00596 100644 --- a/content/tutorials/manage-account-settings/require-destination-tags.md +++ b/content/tutorials/manage-account-settings/require-destination-tags.md @@ -57,7 +57,7 @@ _JavaScript_ _Python_ -{{ include_code("_code-samples/get-started/py/base.py", language="py") }} +{{ include_code("_code-samples/get-started/py/base-async.py", language="py") }} From 68622dbeceb2d8c9003428dc8d46e01cdbe7e8a1 Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 4 Mar 2023 11:18:24 +0100 Subject: [PATCH 6/6] output more info while executing --- .../require-destination-tags/py/require-destination-tags.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/content/_code-samples/require-destination-tags/py/require-destination-tags.py b/content/_code-samples/require-destination-tags/py/require-destination-tags.py index 7f71af9abf..2e0e17ceec 100644 --- a/content/_code-samples/require-destination-tags/py/require-destination-tags.py +++ b/content/_code-samples/require-destination-tags/py/require-destination-tags.py @@ -31,10 +31,12 @@ async def main() -> int: print("Transaction hash:", signed_tx.get_hash()) # Submit the transaction and wait for response (validated or rejected) + print("Submitting transaction...") submit_result = await send_reliable_submission(signed_tx, client) print("Submit result:", submit_result) # Confirm Account Settings -------------------------------------------------- + print("Requesting account information...") account_info = await client.request( AccountInfo( account=wallet.classic_address, @@ -45,9 +47,9 @@ async def main() -> int: # Verify that the AccountRoot lsfRequireDestTag flag is set flags = account_info.result["account_data"]["Flags"] if flags & 0x00020000 != 0: - print("Require Destination Tag is enabled.") + print(f"Require Destination Tag for account {wallet.classic_address} is enabled.") else: - print("Require Destination Tag is DISABLED.") + print(f"Require Destination Tag for account {wallet.classic_address} is DISABLED.") # End main() return 0