Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add xrpl.js code snippets over to xrpl-py #443

Merged
merged 16 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/get_transaction.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Get Transaction
==========================

.. autofunction:: snippets.get_transaction.async_get_transaction
.. autofunction:: snippets.get_transaction.get_transaction
.. literalinclude:: ../../snippets/get_transaction.py
2 changes: 1 addition & 1 deletion docs/source/partial_payment.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Partial Payment
==========================

.. autofunction:: snippets.partial_payment.async_partial_payment
.. autofunction:: snippets.partial_payment.partial_payment
.. literalinclude:: ../../snippets/partial_payment.py
2 changes: 1 addition & 1 deletion docs/source/paths.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Partial Payment
==========================

.. autofunction:: snippets.paths.async_create_tx_with_paths
.. autofunction:: snippets.paths.create_tx_with_paths
.. literalinclude:: ../../snippets/paths.py
2 changes: 1 addition & 1 deletion docs/source/reliable_transaction_submission.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Reliable Transaction Submission
================================

.. autofunction:: snippets.reliable_transaction_submission.async_send_reliable_tx
.. autofunction:: snippets.reliable_transaction_submission.send_reliable_tx
.. literalinclude:: ../../snippets/reliable_transaction_submission.py
2 changes: 1 addition & 1 deletion docs/source/send_escrow.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Send Escrow
==========================

.. autofunction:: snippets.send_escrow.async_send_escrow
.. autofunction:: snippets.send_escrow.send_escrow
.. literalinclude:: ../../snippets/send_escrow.py
2 changes: 1 addition & 1 deletion docs/source/set_regular_key.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Set Regular Key
==========================

.. autofunction:: snippets.set_regular_key.async_set_regular_key
.. autofunction:: snippets.set_regular_key.set_regular_key
.. literalinclude:: ../../snippets/set_regular_key.py
2 changes: 1 addition & 1 deletion docs/source/snippets.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
XRPL Code Snippets
================
===================

Code snippets to demonstrate basic usage of the xrpl-py library.

Expand Down
68 changes: 33 additions & 35 deletions snippets/get_transaction.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
"""A snippet that walks us through getting a transaction."""
connorjchen marked this conversation as resolved.
Show resolved Hide resolved
from xrpl.asyncio.clients import AsyncWebsocketClient
from xrpl.clients import JsonRpcClient
from xrpl.models.requests import Ledger, Tx


async def async_get_transaction() -> None:
def get_transaction(client: JsonRpcClient) -> None:
connorjchen marked this conversation as resolved.
Show resolved Hide resolved
"""
Async snippet that walks us through getting a transaction.
Sync snippet that walks us through getting a transaction.

Args:
client: The async network client to use to send the request.
client: The network client to use to send the request.

Raises:
Exception: if meta not included in the transaction response.
"""
async with AsyncWebsocketClient("wss://s.altnet.rippletest.net:51233") as client:
await client.open()

ledger_request = Ledger(transactions=True, ledger_index="validated")
ledger_response = await client.request(ledger_request)
print(ledger_response)

transactions = ledger_response.result["ledger"]["transactions"]

if transactions:
tx = await client.request(Tx(transaction=transactions[0]))
print(tx)

# the meta field would be a string(hex)
# when the `binary` parameter is `true` for the `tx` request.
if tx.result["meta"] is None:
raise Exception("meta not included in the response")

# delivered_amount is the amount actually received by the destination account.
# Use this field to determine how much was delivered,
# regardless of whether the transaction is a partial payment.
# https://xrpl.org/transaction-metadata.html#delivered_amount
if type(tx.result["meta"] != "string"):
if "delivered_amount" in tx.result["meta"]:
print("delivered_amount:", tx.result["meta"]["delivered_amount"])
else:
print("delivered_amount: undefined")

await client.close()
ledger_request = Ledger(transactions=True, ledger_index="validated")
ledger_response = client.request(ledger_request)
print(ledger_response)

transactions = ledger_response.result["ledger"]["transactions"]

if transactions:
tx_request = Tx(transaction=transactions[0])
tx_response = client.request(tx_request)
print(tx_response)

# the meta field would be a string(hex)
# when the `binary` parameter is `true` for the `tx` request.
if tx_response.result["meta"] is None:
raise Exception("meta not included in the response")

# delivered_amount is the amount actually received by the destination account.
# Use this field to determine how much was delivered,
# regardless of whether the transaction is a partial payment.
# https://xrpl.org/transaction-metadata.html#delivered_amount
if type(tx_response.result["meta"] != "string"):
if "delivered_amount" in tx_response.result["meta"]:
print(
"delivered_amount:", tx_response.result["meta"]["delivered_amount"]
)
else:
print("delivered_amount: undefined")


# uncomment the lines below to run the snippet
# import asyncio
# asyncio.run(async_get_transaction())
# client = JsonRpcClient("https://s.altnet.rippletest.net:51234/")
# get_transaction(client)
224 changes: 103 additions & 121 deletions snippets/partial_payment.py
Original file line number Diff line number Diff line change
@@ -1,134 +1,116 @@
"""A snippet that walks us through using a partial payment."""
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.clients import JsonRpcClient
from xrpl.models.amounts import IssuedCurrencyAmount
from xrpl.models.requests import AccountLines
from xrpl.models.transactions import Payment, PaymentFlag, TrustSet
from xrpl.transaction import (
safe_sign_and_autofill_transaction,
send_reliable_submission,
)
from xrpl.wallet import generate_faucet_wallet


async def async_partial_payment() -> None:
def partial_payment(client: JsonRpcClient) -> None:
"""
Async snippet that walks us through using a partial payment.
Sync snippet that walks us through using a partial payment.

Args:
client: The async network client to use to send the request.
client: The network client to use to send the request.
"""
async with AsyncWebsocketClient("wss://s.altnet.rippletest.net:51233") as client:
await client.open()

# creating wallets as prerequisite
wallet1 = await generate_faucet_wallet(client, debug=True)
wallet2 = await generate_faucet_wallet(client, debug=True)

# create a trustline to issue an IOU `FOO` and set limit on it
trust_set_tx = TrustSet(
account=wallet2.classic_address,
limit_amount=IssuedCurrencyAmount(
currency="FOO",
value="10000000000",
issuer=wallet1.classic_address,
),
)

signed_trust_set_tx = await safe_sign_and_autofill_transaction(
trust_set_tx, wallet2, client
)
await send_reliable_submission(signed_trust_set_tx, client)

print("Balances after trustline is claimed:")
print(
(await client.request(AccountLines(account=wallet1.classic_address))).result[
"lines"
]
)
print(
(await client.request(AccountLines(account=wallet2.classic_address))).result[
"lines"
]
)

# initially, the issuer(wallet1) sends an amount to the other account(wallet2)
issue_quantity = "3840"
payment_tx = Payment(
account=wallet1.classic_address,
amount=IssuedCurrencyAmount(
currency="FOO",
value=issue_quantity,
issuer=wallet1.classic_address,
),
destination=wallet2.classic_address,
)

# submit payment
signed_payment_tx = await safe_sign_and_autofill_transaction(
payment_tx, wallet1, client
)
payment_response = await send_reliable_submission(signed_payment_tx, client)
print(payment_response)

print("Balances after wallet1 sends 3840 FOO to wallet2:")
print(
(await client.request(AccountLines(account=wallet1.classic_address))).result[
"lines"
]
)
print(
(await client.request(AccountLines(account=wallet2.classic_address))).result[
"lines"
]
)

# Send money less than the amount specified on 2 conditions:
# 1. Sender has less money than the aamount specified in the payment Tx.
# 2. Sender has the tfPartialPayment flag activated.

# Other ways to specify flags are by using Hex code and decimal code.
# eg. For partial payment(tfPartialPayment)
# decimal ->131072, hex -> 0x00020000
partial_payment_tx = Payment(
account=wallet2.classic_address,
amount=IssuedCurrencyAmount(
currency="FOO",
value="4000",
issuer=wallet1.classic_address,
),
destination=wallet1.classic_address,
flags=[PaymentFlag.TF_PARTIAL_PAYMENT],
send_max=IssuedCurrencyAmount(
currency="FOO",
value="1000000",
issuer=wallet1.classic_address,
),
)

# submit payment
signed_partial_payment_tx = await safe_sign_and_autofill_transaction(
partial_payment_tx, wallet2, client
)
partial_payment_response = await send_reliable_submission(
signed_partial_payment_tx, client
)
print(partial_payment_response)

print("Balances after Partial Payment, when wallet2 tried to send 4000 FOOs")
print(
(await client.request(AccountLines(account=wallet1.classic_address))).result[
"lines"
]
)
print(
(await client.request(AccountLines(account=wallet2.classic_address))).result[
"lines"
]
)

await client.close()

# creating wallets as prerequisite
wallet1 = generate_faucet_wallet(client, debug=True)
wallet2 = generate_faucet_wallet(client, debug=True)

# create a trustline to issue an IOU `FOO` and set limit on it
trust_set_tx = TrustSet(
account=wallet2.classic_address,
limit_amount=IssuedCurrencyAmount(
currency="FOO",
value="10000000000",
issuer=wallet1.classic_address,
),
)

signed_trust_set_tx = safe_sign_and_autofill_transaction(
trust_set_tx, wallet2, client
)
send_reliable_submission(signed_trust_set_tx, client)

print("Balances after trustline is claimed:")
print(
(client.request(AccountLines(account=wallet1.classic_address))).result["lines"]
)
print(
(client.request(AccountLines(account=wallet2.classic_address))).result["lines"]
)

# initially, the issuer(wallet1) sends an amount to the other account(wallet2)
issue_quantity = "3840"
payment_tx = Payment(
account=wallet1.classic_address,
amount=IssuedCurrencyAmount(
currency="FOO",
value=issue_quantity,
issuer=wallet1.classic_address,
),
destination=wallet2.classic_address,
)

# submit payment
signed_payment_tx = safe_sign_and_autofill_transaction(payment_tx, wallet1, client)
payment_response = send_reliable_submission(signed_payment_tx, client)
print(payment_response)

print("Balances after wallet1 sends 3840 FOO to wallet2:")
print(
(client.request(AccountLines(account=wallet1.classic_address))).result["lines"]
)
print(
(client.request(AccountLines(account=wallet2.classic_address))).result["lines"]
)

# Send money less than the amount specified on 2 conditions:
# 1. Sender has less money than the aamount specified in the payment Tx.
# 2. Sender has the tfPartialPayment flag activated.

# Other ways to specify flags are by using Hex code and decimal code.
# eg. For partial payment(tfPartialPayment)
# decimal ->131072, hex -> 0x00020000
partial_payment_tx = Payment(
account=wallet2.classic_address,
amount=IssuedCurrencyAmount(
currency="FOO",
value="4000",
issuer=wallet1.classic_address,
),
destination=wallet1.classic_address,
flags=[PaymentFlag.TF_PARTIAL_PAYMENT],
send_max=IssuedCurrencyAmount(
currency="FOO",
value="1000000",
issuer=wallet1.classic_address,
),
)

# submit payment
signed_partial_payment_tx = safe_sign_and_autofill_transaction(
partial_payment_tx, wallet2, client
)
partial_payment_response = send_reliable_submission(
signed_partial_payment_tx, client
)
print(partial_payment_response)

print("Balances after Partial Payment, when wallet2 tried to send 4000 FOOs")
print(
(client.request(AccountLines(account=wallet1.classic_address))).result["lines"]
)
print(
(client.request(AccountLines(account=wallet2.classic_address))).result["lines"]
)


# uncomment the lines below to run the snippet
# import asyncio
# asyncio.run(async_partial_payment())
# client = JsonRpcClient("https://s.altnet.rippletest.net:51234/")
# partial_payment(client)