mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 20:25:51 +00:00
Merge pull request #1748 from rikublock/riku/b0079-ms2
[Bounty 0079] Python code samples for require destination tags
This commit is contained in:
16
content/_code-samples/get-started/py/base-async.py
Normal file
16
content/_code-samples/get-started/py/base-async.py
Normal file
@@ -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())
|
||||||
|
|
||||||
@@ -1,48 +1,60 @@
|
|||||||
from xrpl.clients import JsonRpcClient
|
import asyncio
|
||||||
from xrpl.models.transactions import AccountSet, AccountSetFlag
|
|
||||||
from xrpl.transaction import safe_sign_and_autofill_transaction, submit_transaction
|
from xrpl.asyncio.clients import AsyncWebsocketClient
|
||||||
from xrpl.wallet import generate_faucet_wallet
|
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
|
from xrpl.models.requests import AccountInfo
|
||||||
|
from xrpl.models.transactions import (
|
||||||
|
AccountSet,
|
||||||
|
AccountSetFlag,
|
||||||
|
)
|
||||||
|
|
||||||
# Stand-alone code sample for the "Require Destination Tags" tutorial:
|
|
||||||
# https://xrpl.org/require-destination-tags.html
|
|
||||||
|
|
||||||
lsfRequireDestTag = 131072
|
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)
|
||||||
|
|
||||||
# Connect to a testnet node
|
# Send AccountSet transaction -----------------------------------------------
|
||||||
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(
|
tx = AccountSet(
|
||||||
account=myAddr,
|
account=wallet.classic_address,
|
||||||
set_flag=AccountSetFlag.ASF_REQUIRE_DEST
|
set_flag=AccountSetFlag.ASF_REQUIRE_DEST,
|
||||||
)
|
)
|
||||||
print(f"Prepared transaction: {tx}")
|
|
||||||
|
|
||||||
# Sign the transaction
|
# Sign and autofill the transaction (ready to submit)
|
||||||
my_tx_payment_signed = safe_sign_and_autofill_transaction(tx, wallet=test_wallet, client=client)
|
signed_tx = await safe_sign_and_autofill_transaction(tx, wallet, client)
|
||||||
print(f"Transaction Hash: {my_tx_payment_signed.txn_signature}")
|
print("Transaction hash:", signed_tx.get_hash())
|
||||||
|
|
||||||
# Send the transaction to the node
|
# Submit the transaction and wait for response (validated or rejected)
|
||||||
print(f"Enabling Require Destination Tag flag (asfRequireDest) on {myAddr}")
|
print("Submitting transaction...")
|
||||||
submit_tx = submit_transaction(client=client, transaction=my_tx_payment_signed)
|
submit_result = await send_reliable_submission(signed_tx, client)
|
||||||
submit_tx = submit_tx.result["engine_result"]
|
print("Submit result:", submit_result)
|
||||||
print(f"Submit result: {submit_tx}")
|
|
||||||
|
|
||||||
# Verify Account Settings
|
# Confirm Account Settings --------------------------------------------------
|
||||||
get_acc_flag = AccountInfo(
|
print("Requesting account information...")
|
||||||
account=myAddr
|
account_info = await client.request(
|
||||||
|
AccountInfo(
|
||||||
|
account=wallet.classic_address,
|
||||||
|
ledger_index="validated",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
response = client.request(get_acc_flag)
|
|
||||||
|
|
||||||
if response.result['account_data']['Flags'] & lsfRequireDestTag:
|
# Verify that the AccountRoot lsfRequireDestTag flag is set
|
||||||
print("Require Destination Tag is ENABLED.")
|
flags = account_info.result["account_data"]["Flags"]
|
||||||
|
if flags & 0x00020000 != 0:
|
||||||
|
print(f"Require Destination Tag for account {wallet.classic_address} is enabled.")
|
||||||
else:
|
else:
|
||||||
print("Require Destination Tag is DISABLED.")
|
print(f"Require Destination Tag for account {wallet.classic_address} is DISABLED.")
|
||||||
|
|
||||||
|
# End main()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
|
|
||||||
|
|||||||
@@ -19,13 +19,20 @@ This tutorial demonstrates how to enable the Require Destination Tag flag on you
|
|||||||
## Prerequisites
|
## 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 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.
|
- 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:
|
||||||
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.
|
- **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.
|
||||||
|
|
||||||
<!-- Source for this specific tutorial's interactive bits: -->
|
<!-- Source for this specific tutorial's interactive bits: -->
|
||||||
<script type="application/javascript" src="assets/js/tutorials/require-destination-tags.js"></script>
|
<script type="application/javascript" src="assets/js/tutorials/require-destination-tags.js"></script>
|
||||||
|
|
||||||
|
## 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
|
## Steps
|
||||||
{% set n = cycler(* range(1,99)) %}
|
{% set n = cycler(* range(1,99)) %}
|
||||||
@@ -42,8 +49,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):
|
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):
|
||||||
|
|
||||||
|
<!-- MULTICODE_BLOCK_START -->
|
||||||
|
|
||||||
|
_JavaScript_
|
||||||
|
|
||||||
{{ include_code("_code-samples/get-started/js/base.js", language="js") }}
|
{{ include_code("_code-samples/get-started/js/base.js", language="js") }}
|
||||||
|
|
||||||
|
_Python_
|
||||||
|
|
||||||
|
{{ include_code("_code-samples/get-started/py/base-async.py", language="py") }}
|
||||||
|
|
||||||
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|
||||||
For this tutorial, click the following button to connect:
|
For this tutorial, click the following button to connect:
|
||||||
|
|
||||||
{% include '_snippets/interactive-tutorials/connect-step.md' %}
|
{% include '_snippets/interactive-tutorials/connect-step.md' %}
|
||||||
@@ -60,6 +77,10 @@ _JavaScript_
|
|||||||
|
|
||||||
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }}
|
{{ 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") }}
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|
||||||
{{ start_step("Send AccountSet") }}
|
{{ start_step("Send AccountSet") }}
|
||||||
@@ -89,6 +110,10 @@ _JavaScript_
|
|||||||
|
|
||||||
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }}
|
{{ 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()") }}
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user