From 0fc587e7776d966560802a3975386b9508d50d34 Mon Sep 17 00:00:00 2001 From: Wo Jake <87929946+wojake@users.noreply.github.com> Date: Sat, 3 Sep 2022 16:59:27 +0000 Subject: [PATCH 1/4] Add Python code sample for Tickets --- .../use-tickets/py/use-tickets.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 content/_code-samples/use-tickets/py/use-tickets.py diff --git a/content/_code-samples/use-tickets/py/use-tickets.py b/content/_code-samples/use-tickets/py/use-tickets.py new file mode 100644 index 0000000000..41a9557579 --- /dev/null +++ b/content/_code-samples/use-tickets/py/use-tickets.py @@ -0,0 +1,61 @@ +from xrpl.clients import JsonRpcClient +from xrpl.models.transactions import TicketCreate, AccountSet +from xrpl.account import get_next_valid_seq_number +from xrpl.transaction import safe_sign_and_submit_transaction +from xrpl.core import keypairs +from xrpl.wallet import Wallet +from xrpl.models.requests.account_info import AccountInfo + +myAddr = "rfaNymVS1fEREj4FpNroXVEcVVMph6k2Mt" +mySeed = "ss3GZzmyEBxvwB3LUvsqfa-------" + +# Derive and initialize wallet +public, private = keypairs.derive_keypair(mySeed) +wallet_from_seed = Wallet(mySeed, 0) + +# Connect to a testnet node +JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/" +client = JsonRpcClient(JSON_RPC_URL) + +# Construct a TicketCreate transaction, 1 ticket created for future use +tx = TicketCreate( + account=myAddr, + fee="10", + ticket_count=1, + sequence=get_next_valid_seq_number(address=myAddr, client=client) +) + +# Sign transaction locally and submit +my_tx_payment_signed = safe_sign_and_submit_transaction(transaction=tx, wallet=wallet_from_seed, client=client) + +# Get ticket Sequence +get_ticket = AccountInfo( + account=myAddr, + ledger_index="current", + strict=True +) + +response = client.request(get_ticket) +ticket_sequence = response.result["account_data"]["Sequence"]-1 + +# Construct Transaction using a Ticket +tx_1 = AccountSet( + account=myAddr, + fee="10", + sequence=0, + last_ledger_sequence=None, + ticket_sequence=ticket_sequence +) + +# Send transaction (w/ Ticket) +result = safe_sign_and_submit_transaction(transaction=tx_1, client=client, wallet=wallet_from_seed) +result = result.result["engine_result"] + +if result == "tesSUCCESS": + print("Transaction successful!") +elif result == "tefMAX_LEDGER": + print("Transaction failed to achieve consensus") +elif result == "unknown": + print("Transaction status unknown") +else: + print(f"Transaction failed with code {result}") From 828f7e282f4b21e4fb9acb9ceb187dfb702cba6d Mon Sep 17 00:00:00 2001 From: Wo Jake <87929946+wojake@users.noreply.github.com> Date: Mon, 5 Sep 2022 11:20:06 +0000 Subject: [PATCH 2/4] Fixup --- .../use-tickets/py/use-tickets.py | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/content/_code-samples/use-tickets/py/use-tickets.py b/content/_code-samples/use-tickets/py/use-tickets.py index 41a9557579..06b6477484 100644 --- a/content/_code-samples/use-tickets/py/use-tickets.py +++ b/content/_code-samples/use-tickets/py/use-tickets.py @@ -1,16 +1,13 @@ from xrpl.clients import JsonRpcClient from xrpl.models.transactions import TicketCreate, AccountSet -from xrpl.account import get_next_valid_seq_number from xrpl.transaction import safe_sign_and_submit_transaction -from xrpl.core import keypairs from xrpl.wallet import Wallet -from xrpl.models.requests.account_info import AccountInfo +from xrpl.models.requests.account_objects import AccountObjects, AccountObjectType -myAddr = "rfaNymVS1fEREj4FpNroXVEcVVMph6k2Mt" -mySeed = "ss3GZzmyEBxvwB3LUvsqfa-------" +myAddr = "rLugkhsRxNHroDVJ29anq9UoTcNQdDjXCR" +mySeed = "sARIIQ06xLZulXC1MBeE---------" # Derive and initialize wallet -public, private = keypairs.derive_keypair(mySeed) wallet_from_seed = Wallet(mySeed, 0) # Connect to a testnet node @@ -20,23 +17,20 @@ client = JsonRpcClient(JSON_RPC_URL) # Construct a TicketCreate transaction, 1 ticket created for future use tx = TicketCreate( account=myAddr, - fee="10", - ticket_count=1, - sequence=get_next_valid_seq_number(address=myAddr, client=client) + ticket_count=1 ) # Sign transaction locally and submit my_tx_payment_signed = safe_sign_and_submit_transaction(transaction=tx, wallet=wallet_from_seed, client=client) -# Get ticket Sequence -get_ticket = AccountInfo( +# Get a Ticket Sequence +get_ticket_sequence = AccountObjects( account=myAddr, - ledger_index="current", - strict=True + type=AccountObjectType.TICKET ) -response = client.request(get_ticket) -ticket_sequence = response.result["account_data"]["Sequence"]-1 +response = client.request(get_ticket_sequence) +ticket_sequence = response.result["account_objects"][0]["TicketSequence"] # Construct Transaction using a Ticket tx_1 = AccountSet( @@ -48,8 +42,8 @@ tx_1 = AccountSet( ) # Send transaction (w/ Ticket) -result = safe_sign_and_submit_transaction(transaction=tx_1, client=client, wallet=wallet_from_seed) -result = result.result["engine_result"] +tx_result = safe_sign_and_submit_transaction(transaction=tx_1, client=client, wallet=wallet_from_seed) +result = tx_result.result["engine_result"] if result == "tesSUCCESS": print("Transaction successful!") From 99e72585f5380b40320de9990159ab6d5ca66fdf Mon Sep 17 00:00:00 2001 From: Wo Jake <87929946+wojake@users.noreply.github.com> Date: Wed, 7 Sep 2022 02:43:00 +0000 Subject: [PATCH 3/4] Use `generate_faucet_wallet` --- .../_code-samples/use-tickets/py/use-tickets.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/content/_code-samples/use-tickets/py/use-tickets.py b/content/_code-samples/use-tickets/py/use-tickets.py index 06b6477484..6533ed176c 100644 --- a/content/_code-samples/use-tickets/py/use-tickets.py +++ b/content/_code-samples/use-tickets/py/use-tickets.py @@ -1,19 +1,17 @@ from xrpl.clients import JsonRpcClient from xrpl.models.transactions import TicketCreate, AccountSet from xrpl.transaction import safe_sign_and_submit_transaction -from xrpl.wallet import Wallet +from xrpl.wallet import Wallet, generate_faucet_wallet from xrpl.models.requests.account_objects import AccountObjects, AccountObjectType -myAddr = "rLugkhsRxNHroDVJ29anq9UoTcNQdDjXCR" -mySeed = "sARIIQ06xLZulXC1MBeE---------" - -# Derive and initialize wallet -wallet_from_seed = Wallet(mySeed, 0) - # Connect to a testnet node JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/" client = JsonRpcClient(JSON_RPC_URL) +# Generate a wallet and request faucet +test_wallet = generate_faucet_wallet(client=client) +myAddr = test_wallet.classic_address + # Construct a TicketCreate transaction, 1 ticket created for future use tx = TicketCreate( account=myAddr, @@ -21,7 +19,7 @@ tx = TicketCreate( ) # Sign transaction locally and submit -my_tx_payment_signed = safe_sign_and_submit_transaction(transaction=tx, wallet=wallet_from_seed, client=client) +my_tx_payment_signed = safe_sign_and_submit_transaction(transaction=tx, wallet=test_wallet, client=client) # Get a Ticket Sequence get_ticket_sequence = AccountObjects( @@ -42,9 +40,10 @@ tx_1 = AccountSet( ) # Send transaction (w/ Ticket) -tx_result = safe_sign_and_submit_transaction(transaction=tx_1, client=client, wallet=wallet_from_seed) +tx_result = safe_sign_and_submit_transaction(transaction=tx_1, client=client, wallet=test_wallet) result = tx_result.result["engine_result"] +print(f"Account: {myAddr}") if result == "tesSUCCESS": print("Transaction successful!") elif result == "tefMAX_LEDGER": From b100af8b61bffa61103b4a774e339ab2fc978ee2 Mon Sep 17 00:00:00 2001 From: Wo Jake <87929946+wojake@users.noreply.github.com> Date: Wed, 28 Sep 2022 08:01:44 +0000 Subject: [PATCH 4/4] New error codes + other changes --- .../use-tickets/py/use-tickets.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/content/_code-samples/use-tickets/py/use-tickets.py b/content/_code-samples/use-tickets/py/use-tickets.py index 6533ed176c..0b66c4109f 100644 --- a/content/_code-samples/use-tickets/py/use-tickets.py +++ b/content/_code-samples/use-tickets/py/use-tickets.py @@ -12,10 +12,10 @@ client = JsonRpcClient(JSON_RPC_URL) test_wallet = generate_faucet_wallet(client=client) myAddr = test_wallet.classic_address -# Construct a TicketCreate transaction, 1 ticket created for future use +# Construct a TicketCreate transaction, 2 ticket created for future use tx = TicketCreate( account=myAddr, - ticket_count=1 + ticket_count=2 ) # Sign transaction locally and submit @@ -28,7 +28,15 @@ get_ticket_sequence = AccountObjects( ) response = client.request(get_ticket_sequence) -ticket_sequence = response.result["account_objects"][0]["TicketSequence"] + +# Since we created 2 Tickets previously, you're able to choose which Ticket you're going to use +ticket1_sequence = response.result["account_objects"][0]["TicketSequence"] +ticket2_sequence = response.result["account_objects"][1]["TicketSequence"] + +print(f"Ticket 1: {ticket1_sequence}\n" + f"Ticket 2: {ticket2_sequence}") + +ticket_sequence = int(input("Pick a ticket sequence to use for your next transaction: ")) # Construct Transaction using a Ticket tx_1 = AccountSet( @@ -46,8 +54,12 @@ result = tx_result.result["engine_result"] print(f"Account: {myAddr}") if result == "tesSUCCESS": print("Transaction successful!") +elif result == "terPRE_TICKET": + print("The provided Ticket Sequence does not exist in the ledger") elif result == "tefMAX_LEDGER": print("Transaction failed to achieve consensus") +elif result == "tefPAST_SEQ": + print("The sequence number is lower than the current sequence number of the account") elif result == "unknown": print("Transaction status unknown") else: