Add payment channel snippets to docs

This commit is contained in:
Maria Shodunke
2025-07-09 15:28:52 +01:00
parent 0b77ba0002
commit 24f501fda9
3 changed files with 87 additions and 63 deletions

View File

@@ -66,7 +66,7 @@ async function claimPayChannel(): Promise<void> {
console.log("Submitting a PaymentChannelClaim transaction...")
const channelClaimResponse = await client.submit(paymentChannelClaim, {
wallet: wallet1,
wallet: wallet2,
})
console.log("PaymentChannelClaim transaction response:")
console.log(channelClaimResponse)

View File

@@ -13,75 +13,71 @@ from xrpl.transaction import submit, submit_and_wait
from xrpl.account import get_balance
def claim_pay_channel():
"""The snippet walks us through creating and claiming a Payment Channel."""
"""The snippet walks us through creating and claiming a Payment Channel."""
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
# Creating wallets as prerequisite
wallet1 = generate_faucet_wallet(client)
wallet2 = generate_faucet_wallet(client)
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
print("Balances of wallets before Payment Channel is claimed:")
print(f"Balance of {wallet1.address} is {get_balance(wallet1.address, client)} XRP")
print(f"Balance of {wallet2.address} is {get_balance(wallet2.address, client)} XRP")
# Creating wallets as prerequisite
wallet1 = generate_faucet_wallet(client)
wallet2 = generate_faucet_wallet(client)
# Create a Payment Channel and submit and wait for tx to be validated
payment_channel_create = PaymentChannelCreate(
account=wallet1.address,
amount="100", # 10 XRP
destination=wallet2.address,
settle_delay=86400, # 1 day in seconds
public_key=wallet1.public_key,
)
print("Balances of wallets before Payment Channel is claimed:")
print(f"Balance of {wallet1.address} is {get_balance(wallet1.address, client)} XRP")
print(f"Balance of {wallet2.address} is {get_balance(wallet2.address, client)} XRP")
print("Submitting a PaymentChannelCreate transaction...")
payment_channel_response = submit_and_wait(
payment_channel_create,
client,
wallet1
)
print("PaymentChannelCreate transaction response:")
print(payment_channel_response.result)
# Create a Payment Channel and submit and wait for tx to be validated
payment_channel_create = PaymentChannelCreate(
account=wallet1.address,
amount="100", # 10 XRP
destination=wallet2.address,
settle_delay=86400, # 1 day in seconds
public_key=wallet1.public_key,
)
# Check that the object was actually created
account_objects_request = AccountObjects(
account=wallet1.address,
)
account_objects_response = client.request(account_objects_request)
account_objects = account_objects_response.result["account_objects"]
print("Submitting a PaymentChannelCreate transaction...")
payment_channel_response = submit_and_wait(
payment_channel_create,
client,
wallet1
)
print("PaymentChannelCreate transaction response:")
print(payment_channel_response.result)
# Find the PayChannel object to get the correct channel ID
channel_id = None
for obj in account_objects:
if obj["LedgerEntryType"] == "PayChannel":
channel_id = obj["index"]
break
# Check that the object was actually created
account_objects_request = AccountObjects(
account=wallet1.address,
)
account_objects_response = client.request(account_objects_request)
account_objects = account_objects_response.result["account_objects"]
if not channel_id:
raise Exception("PayChannel not found in account objects")
# Find the PayChannel object to get the correct channel ID
channel_id = None
for obj in account_objects:
if obj["LedgerEntryType"] == "PayChannel":
channel_id = obj["index"]
break
print(f"PayChannel ID: {channel_id}")
if not channel_id:
raise Exception("PayChannel not found in account objects")
# Destination claims the Payment Channel and we see the balances to verify.
payment_channel_claim = PaymentChannelClaim(
account=wallet2.address,
channel=channel_id,
amount="100",
)
print(f"PayChannel ID: {channel_id}")
print("Submitting a PaymentChannelClaim transaction...")
channel_claim_response = submit_and_wait(
payment_channel_claim,
client,
wallet1,
)
print("PaymentChannelClaim transaction response:")
print(channel_claim_response.result)
print("Balances of wallets after Payment Channel is claimed:")
print(f"Balance of {wallet1.address} is {get_balance(wallet1.address, client)} XRP")
print(f"Balance of {wallet2.address} is {get_balance(wallet2.address, client)} XRP")
if __name__ == "__main__":
claim_pay_channel()
# Destination claims the Payment Channel and we see the balances to verify.
payment_channel_claim = PaymentChannelClaim(
account=wallet2.address,
channel=channel_id,
amount="100",
)
print("Submitting a PaymentChannelClaim transaction...")
channel_claim_response = submit_and_wait(
payment_channel_claim,
client,
wallet2,
)
print("PaymentChannelClaim transaction response:")
print(channel_claim_response.result)
print("Balances of wallets after Payment Channel is claimed:")
print(f"Balance of {wallet1.address} is {get_balance(wallet1.address, client)} XRP")
print(f"Balance of {wallet2.address} is {get_balance(wallet2.address, client)} XRP")

View File

@@ -62,6 +62,9 @@ The following example shows creation of a payment channel by [submitting](../../
{% admonition type="info" name="Note" %}A payment channel counts as one object toward the payer's [owner reserve](../../../../concepts/accounts/reserves.md#owner-reserves). The owner must keep at least enough XRP to satisfy the reserve after subtracting the XRP allocated to the payment channel.{% /admonition %}
{% tabs %}
{% tab label="JSON-RPC" %}
Request:
```json
@@ -105,6 +108,17 @@ Response:
}
}
```
{% /tab %}
{% tab label="Javascript" %}
{% code-snippet file="/_code-samples/claim-payment-channel/js/claimPayChannel.ts" language="js" from="// Create a Payment" before="// Check" /%}
{% /tab %}
{% tab label="Python" %}
{% code-snippet file="/_code-samples/claim-payment-channel/py/claim_pay_channel.py" language="py" from="# Create a Payment" before="# Check" /%}
{% /tab %}
{% /tabs %}
The immediate response to the `submit` request contains a _provisional_ result with the transaction's identifying `hash` value. The payer should check the transaction's _final_ result in a validated ledger and get the Channel ID from the metadata. This can be done with the `tx` command:
@@ -399,6 +413,9 @@ The payee can do this multiple times, to settle partially while still doing busi
Example of claiming XRP from a channel:
{% tabs %}
{% tab label="JSON-RPC" %}
Request:
```json
@@ -453,6 +470,17 @@ Response:
}
}
```
{% /tab %}
{% tab label="Javascript" %}
{% code-snippet file="/_code-samples/claim-payment-channel/js/claimPayChannel.ts" language="js" from="// Destination claims" before="console.log('Balances of" /%}
{% /tab %}
{% tab label="Python" %}
{% code-snippet file="/_code-samples/claim-payment-channel/py/claim_pay_channel.py" language="py" from="# Destination claims" before="print(\"Balances of" /%}
{% /tab %}
{% /tabs %}
The payee should confirm that this transaction is successful in a validated ledger. For the full details, see [Reliable Transaction Submission](../../../../concepts/transactions/reliable-transaction-submission.md).