Merge pull request #1497 from ObiajuluM/patch-1

Added Python code samples for checks
This commit is contained in:
Rome Reginelli
2022-10-17 15:05:23 -07:00
committed by GitHub
4 changed files with 278 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
from xrpl.clients import JsonRpcClient
from xrpl.models import AccountObjects
from xrpl.utils import drops_to_xrp, hex_to_str, ripple_time_to_datetime
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
# Query the ledger for all xrp checks an account has created or received
wallet_addr_to_query = "rPKcw5cXUtREMgsQZqSLkxJTfpwMGg7WcP"
checks_dict = {}
sent_checks = []
received_checks = []
# Build request
req = AccountObjects(account=wallet_addr, ledger_index="validated", type="check")
# Make request and return result
response = client.request(req)
result = response.result
# Parse result
if "account_objects" in result:
account_checks = result["account_objects"]
for check in account_checks:
if isinstance(check["SendMax"], str):
check_data = {}
check_data["sender"] = check["Account"]
check_data["receiver"] = check["Destination"]
if "Expiration" in check:
check_data["expiry_date"] = str(ripple_time_to_datetime(check["Expiration"]))
check_data["amount"] = str(drops_to_xrp(check["SendMax"]))
check_data["check_id"] = check["index"]
if check_data["sender"] == wallet_addr:
sent.append(check_data)
elif check_data["sender"] != wallet_addr:
receive.append(check_data)
# Sort checks
checks_dict["sent"] = sent
checks_dict["receive"] = receive
print(checks_dict)
############################# Query for token checks #############################
# Query the ledger for all token checks an account has created or received
wallet_addr_to_query = "rPKcw5cXUtREMgsQZqSLkxJTfpwMGg7WcP"
checks_dict = {}
sent_dict = []
received_dict = []
# Build request
req = AccountObjects(account=wallet_addr, ledger_index="validated", type="check")
# Make request and return result
response = client.request(req)
result = response.result
# Parse result
if "account_objects" in result:
account_checks = result["account_objects"]
for check in account_checks:
if isinstance(check["SendMax"], dict):
check_data = {}
check_data["sender"] = check["Account"]
check_data["receiver"] = check["Destination"]
if "Expiration" in check:
check_data["expiry_date"] = str(ripple_time_to_datetime(check["Expiration"]))
check_data["token"] = hex_to_str(check["SendMax"]["currency"])
check_data["issuer"] = check["SendMax"]["issuer"]
check_data["amount"] = check["SendMax"]["value"]
check_data["check_id"] = check["index"]
if check_data["sender"] == wallet_addr:
sent.append(check_data)
elif check_data["sender"] != wallet_addr:
receive.append(check_data)
# Sort checks
checks_dict["sent"] = sent
checks_dict["receive"] = receive
print(checks_dict)

View File

@@ -0,0 +1,32 @@
from xrpl.wallet import Wallet, generate_faucet_wallet
from xrpl.clients import JsonRpcClient
from xrpl.models import CheckCancel
from xrpl.transaction import (safe_sign_and_autofill_transaction,
send_reliable_submission)
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
"""Cancel a check"""
# Sender is the check creator or recipient
# If the Check has expired, any address can cancel it
# Check id
check_id = "F944CB379DEE18EFDA7A58A4F81AF1A98C46E54A8B9F2D268F1E26610BC0EB03"
# Create wallet object
sender_wallet = generate_faucet_wallet(client=client)
# Build check cancel transaction
check_txn = CheckCancel(account=sender_wallet.classic_address, check_id=check_id)
# Sign and submit transaction
stxn = safe_sign_and_autofill_transaction(check_txn, sender_wallet, client)
stxn_response = send_reliable_submission(stxn, client)
# Parse response for result
stxn_result = stxn_response.result
# Print result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])

View File

@@ -0,0 +1,74 @@
from xrpl.clients import JsonRpcClient
from xrpl.models import CheckCash, IssuedCurrencyAmount
from xrpl.transaction import (safe_sign_and_autofill_transaction,
send_reliable_submission)
from xrpl.utils import str_to_hex, xrp_to_drops
from xrpl.wallet import generate_faucet_wallet
# Connect to a network
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
# Cash an xrp check
# Check id
check_id = "F944CB379DEE18EFDA7A58A4F81AF1A98C46E54A8B9F2D268F1E26610BC0EB03"
# Amount to cash
amount = 10.00
# Generate wallet
sender_wallet = generate_faucet_wallet(client=client)
# Build check cash transaction
check_txn = CheckCash(account=sender_wallet.classic_address, check_id=check_id, amount=xrp_to_drops(amount))
# Sign transaction
stxn = safe_sign_and_autofill_transaction(check_txn, sender_wallet, client)
# Submit transaction and wait for result
stxn_response = send_reliable_submission(stxn, client)
# Parse response for result
stxn_result = stxn_response.result
# Print result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])
#################### cash token check #############################
# Cash token check
# Token name
token = "USD"
# Amount of token to deliver
amount = 10.00
# Token issuer address
issuer = generate_faucet_wallet(client=client).classic_address
# Create sender wallet object
sender_wallet = generate_faucet_wallet(client=client)
# Build check cash transaction
check_txn = CheckCash(account=sender_wallet.classic_address, check_id=check_id, amount=IssuedCurrencyAmount(
currency=str_to_hex(token),
issuer=issuer,
value=amount))
# Sign transaction
stxn = safe_sign_and_autofill_transaction(check_txn, sender_wallet, client)
# Submit transaction and wait for result
stxn_response = send_reliable_submission(stxn, client)
# Parse response for result
stxn_result = stxn_response.result
# Print result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])

View File

@@ -0,0 +1,78 @@
from datetime import datetime, timedelta
from xrpl.clients import JsonRpcClient
from xrpl.models import CheckCreate, IssuedCurrencyAmount
from xrpl.transaction import (safe_sign_and_autofill_transaction,
send_reliable_submission)
from xrpl.utils import datetime_to_ripple_time, str_to_hex, xrp_to_drops
from xrpl.wallet import generate_faucet_wallet
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
"""Create a token check"""
check_receiver_addr = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Example: send back to Testnet Faucet
token_name = "USD"
amount_to_deliver = 10.00
token_issuer = "r9CEVt4Cmcjt68ME6GKyhf2DyEGo2rG8AW"
# Set check to expire after 5 days
expiry_date = datetime_to_ripple_time(datetime.now() + timedelta(days=5))
# Generate wallet
sender_wallet = generate_faucet_wallet(client=client)
# Build check create transaction
check_txn = CheckCreate(account=sender_wallet.classic_address, destination=receiver_addr,
send_max=IssuedCurrencyAmount(
currency=str_to_hex(token),
issuer=issuer,
value=amount),
expiration=expiry_date)
# Sign, submit transaction and wait for result
stxn = safe_sign_and_autofill_transaction(check_txn, sender_wallet, client)
stxn_response = send_reliable_submission(stxn, client)
# Parse response for result
stxn_result = stxn_response.result
# Print result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])
############### CREATE XRP CHECK ################################
"""Create xrp check"""
check_receiver_addr = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Example: send back to Testnet Faucet
amount_to_deliver = 10.00
# Set check to expire after 5 days
expiry_date = datetime_to_ripple_time(datetime.now() + timedelta(days=5))
# Generate wallet
sender_wallet = generate_faucet_wallet(client=client)
# Build check create transaction
check_txn = CheckCreate(account=sender_wallet.classic_address,
destination=receiver_addr,
send_max=xrp_to_drops(amount),
expiration=expiry_date)
# Sign, submit transaction and wait for result
stxn = safe_sign_and_autofill_transaction(check_txn, sender_wallet, client)
stxn_response = send_reliable_submission(stxn, client)
# Parse response for result
stxn_result = stxn_response.result
# Print result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])