mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 19:55:54 +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
|
||||
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)
|
||||
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,
|
||||
ledger_index="validated",
|
||||
)
|
||||
)
|
||||
|
||||
# Verify that the AccountRoot lsfRequireDestTag flag is set
|
||||
flags = account_info.result["account_data"]["Flags"]
|
||||
if flags & 0x00020000 != 0:
|
||||
print(f"Require Destination Tag for account {wallet.classic_address} is enabled.")
|
||||
else:
|
||||
print(f"Require Destination Tag for account {wallet.classic_address} 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.")
|
||||
|
||||
Reference in New Issue
Block a user