mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 03:35:51 +00:00
migrate from js and py
This commit is contained in:
91
content/_code-samples/partial-payment/js/partialPayment.ts
Normal file
91
content/_code-samples/partial-payment/js/partialPayment.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { Client, Payment, PaymentFlags, TrustSet } from 'xrpl'
|
||||
|
||||
const client = new Client('wss://s.altnet.rippletest.net:51233')
|
||||
|
||||
// This snippet walks us through partial payment.
|
||||
async function partialPayment(): Promise<void> {
|
||||
await client.connect()
|
||||
|
||||
// creating wallets as prerequisite
|
||||
const { wallet: wallet1 } = await client.fundWallet()
|
||||
const { wallet: wallet2 } = await client.fundWallet()
|
||||
|
||||
// create a trustline to issue an IOU `FOO` and set limit on it.
|
||||
const trust_set_tx: TrustSet = {
|
||||
TransactionType: 'TrustSet',
|
||||
Account: wallet2.classicAddress,
|
||||
LimitAmount: {
|
||||
currency: 'FOO',
|
||||
issuer: wallet1.classicAddress,
|
||||
// Value for the new IOU - 10000000000 - is arbitarily chosen.
|
||||
value: '10000000000',
|
||||
},
|
||||
}
|
||||
|
||||
await client.submitAndWait(trust_set_tx, {
|
||||
wallet: wallet2,
|
||||
})
|
||||
|
||||
console.log('Balances after trustline is created')
|
||||
console.log(await client.getBalances(wallet1.classicAddress))
|
||||
console.log(await client.getBalances(wallet2.classicAddress))
|
||||
|
||||
// Initially, the issuer(wallet1) sends an amount to the other account(wallet2)
|
||||
const issue_quantity = '3840'
|
||||
const payment: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: wallet1.classicAddress,
|
||||
Amount: {
|
||||
currency: 'FOO',
|
||||
value: issue_quantity,
|
||||
issuer: wallet1.classicAddress,
|
||||
},
|
||||
Destination: wallet2.classicAddress,
|
||||
}
|
||||
|
||||
// submit payment
|
||||
const initialPayment = await client.submitAndWait(payment, {
|
||||
wallet: wallet1,
|
||||
})
|
||||
console.log(initialPayment)
|
||||
|
||||
console.log('Balances after issuer(wallet1) sends IOU("FOO") to wallet2')
|
||||
console.log(await client.getBalances(wallet1.classicAddress))
|
||||
console.log(await client.getBalances(wallet2.classicAddress))
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
const partialPaymentTx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: wallet2.classicAddress,
|
||||
Amount: {
|
||||
currency: 'FOO',
|
||||
value: '4000',
|
||||
issuer: wallet1.classicAddress,
|
||||
},
|
||||
Destination: wallet1.classicAddress,
|
||||
Flags: PaymentFlags.tfPartialPayment,
|
||||
}
|
||||
|
||||
// submit payment
|
||||
const submitResponse = await client.submitAndWait(partialPaymentTx, {
|
||||
wallet: wallet2,
|
||||
})
|
||||
console.log(submitResponse)
|
||||
|
||||
console.log(
|
||||
"Balances after Partial Payment, when wallet2 tried to send 4000 FOO's",
|
||||
)
|
||||
console.log(await client.getBalances(wallet1.classicAddress))
|
||||
console.log(await client.getBalances(wallet2.classicAddress))
|
||||
|
||||
await client.disconnect()
|
||||
}
|
||||
void partialPayment()
|
||||
96
content/_code-samples/partial-payment/py/partial-payment.py
Normal file
96
content/_code-samples/partial-payment/py/partial-payment.py
Normal file
@@ -0,0 +1,96 @@
|
||||
"""Example of how to handle partial payments"""
|
||||
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 autofill_and_sign, send_reliable_submission
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
# References
|
||||
# - https://xrpl.org/partial-payments.html#partial-payments
|
||||
# - https://xrpl.org/payment.html#payment-flags
|
||||
# - https://xrpl.org/trustset.html#trustset
|
||||
# - https://xrpl.org/account_lines.html#account_lines
|
||||
|
||||
# Create a client to connect to the test network
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
|
||||
# Creating two wallets to send money between
|
||||
wallet1 = generate_faucet_wallet(client, debug=True)
|
||||
wallet2 = generate_faucet_wallet(client, debug=True)
|
||||
|
||||
# Create a TrustSet 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,
|
||||
),
|
||||
)
|
||||
|
||||
# Sign and autofill, then send transaction to the ledger
|
||||
signed_trust_set_tx = autofill_and_sign(trust_set_tx, wallet2, client)
|
||||
send_reliable_submission(signed_trust_set_tx, client)
|
||||
|
||||
# Both balances should be zero since nothing has been sent yet
|
||||
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"])
|
||||
|
||||
# Create a Payment to send 3840 FOO from wallet1 (issuer) to destination (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,
|
||||
)
|
||||
|
||||
# Sign and autofill, then send transaction to the ledger
|
||||
signed_payment_tx = autofill_and_sign(payment_tx, wallet1, client)
|
||||
payment_response = send_reliable_submission(signed_payment_tx, client)
|
||||
print(payment_response)
|
||||
|
||||
# Issuer (wallet1) should have -3840 FOO and destination (wallet2) should have 3840 FOO
|
||||
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 amount 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
|
||||
|
||||
# Create Payment to send 4000 (of 3840) FOO from wallet2 to wallet1
|
||||
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,
|
||||
),
|
||||
)
|
||||
|
||||
# Sign and autofill, then send transaction to the ledger
|
||||
signed_partial_payment_tx = autofill_and_sign(partial_payment_tx, wallet2, client)
|
||||
partial_payment_response = send_reliable_submission(signed_partial_payment_tx, client)
|
||||
print(partial_payment_response)
|
||||
|
||||
# Tried sending 4000 of 3840 FOO -> wallet1 and wallet2 should have 0 FOO
|
||||
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"])
|
||||
Reference in New Issue
Block a user