Migrate code snippets from xrpl.js and xrpl-py

This commit is contained in:
Maria Shodunke
2025-07-07 14:25:58 +01:00
parent be83ee11e9
commit 0b77ba0002
17 changed files with 323 additions and 91 deletions

View File

@@ -12,13 +12,11 @@ import {
const client = new Client('wss://s.altnet.rippletest.net:51233')
void claimPayChannel()
// The snippet walks us through creating and claiming a Payment Channel.
async function claimPayChannel(): Promise<void> {
await client.connect()
// creating wallets as prerequisite
// Creating wallets as prerequisite
const { wallet: wallet1 } = await client.fundWallet()
const { wallet: wallet2 } = await client.fundWallet()
@@ -26,13 +24,13 @@ async function claimPayChannel(): Promise<void> {
console.log(`Balance of ${wallet1.address} is ${await client.getXrpBalance(wallet1.address)} XRP`)
console.log(`Balance of ${wallet2.address} is ${await client.getXrpBalance(wallet2.address)} XRP`)
// create a Payment Channel and submit and wait for tx to be validated
// Create a Payment Channel and submit and wait for tx to be validated
const paymentChannelCreate: PaymentChannelCreate = {
TransactionType: 'PaymentChannelCreate',
Account: wallet1.classicAddress,
Amount: '100',
Amount: '100', // 10 XRP
Destination: wallet2.classicAddress,
SettleDelay: 86400,
SettleDelay: 86400, // 1 day in seconds
PublicKey: wallet1.publicKey,
}
@@ -44,7 +42,7 @@ async function claimPayChannel(): Promise<void> {
console.log("PaymentChannelCreate transaction response:")
console.log(paymentChannelResponse)
// check that the object was actually created
// Check that the object was actually created
const accountObjectsRequest: AccountObjectsRequest = {
command: 'account_objects',
account: wallet1.classicAddress,
@@ -52,23 +50,21 @@ async function claimPayChannel(): Promise<void> {
const accountObjects = (await client.request(accountObjectsRequest)).result
.account_objects
console.log("Account Objects:", accountObjects)
// destination claims the Payment Channel and we see the balances to verify.
// Destination claims the Payment Channel and we see the balances to verify.
const paymentChannelClaim: PaymentChannelClaim = {
Account: wallet2.classicAddress,
TransactionType: 'PaymentChannelClaim',
Channel: hashes.hashPaymentChannel(
wallet1.classicAddress,
wallet2.classicAddress,
paymentChannelResponse.result.Sequence ?? 0,
paymentChannelResponse.result.tx_json.Sequence ?? 0,
),
Amount: '100',
}
console.log("Submitting a PaymentChannelClaim transaction...")
const channelClaimResponse = await client.submit(paymentChannelClaim, {
wallet: wallet1,
})
@@ -81,3 +77,5 @@ async function claimPayChannel(): Promise<void> {
await client.disconnect()
}
void claimPayChannel()

View File

@@ -0,0 +1,87 @@
"""
Create, claim and verify a Payment Channel.
Reference: https://xrpl.org/paychannel.html
"""
from xrpl.clients import JsonRpcClient
from xrpl.models import (
AccountObjects,
PaymentChannelCreate,
PaymentChannelClaim,
)
from xrpl.wallet import generate_faucet_wallet
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."""
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
# Creating wallets as prerequisite
wallet1 = generate_faucet_wallet(client)
wallet2 = generate_faucet_wallet(client)
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")
# 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("Submitting a PaymentChannelCreate transaction...")
payment_channel_response = submit_and_wait(
payment_channel_create,
client,
wallet1
)
print("PaymentChannelCreate transaction response:")
print(payment_channel_response.result)
# 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"]
# 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
if not channel_id:
raise Exception("PayChannel not found in account objects")
print(f"PayChannel ID: {channel_id}")
# 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,
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()