|
| 1 | +import { Client, Payment, PaymentFlags, TrustSet } from '../../src' |
| 2 | + |
| 3 | +const client = new Client('wss://s.altnet.rippletest.net:51233') |
| 4 | + |
| 5 | +// This snippet walks us through partial payment. |
| 6 | +async function partialPayment(): Promise<void> { |
| 7 | + await client.connect() |
| 8 | + |
| 9 | + // creating wallets as prerequisite |
| 10 | + const { wallet: wallet1 } = await client.fundWallet(null, { |
| 11 | + usageContext: 'code snippets', |
| 12 | + }) |
| 13 | + const { wallet: wallet2 } = await client.fundWallet(null, { |
| 14 | + usageContext: 'code snippets', |
| 15 | + }) |
| 16 | + |
| 17 | + // create a trustline to issue an IOU `FOO` and set limit on it. |
| 18 | + const trust_set_tx: TrustSet = { |
| 19 | + TransactionType: 'TrustSet', |
| 20 | + Account: wallet2.classicAddress, |
| 21 | + LimitAmount: { |
| 22 | + currency: 'FOO', |
| 23 | + issuer: wallet1.classicAddress, |
| 24 | + // Value for the new IOU - 10000000000 - is arbitarily chosen. |
| 25 | + value: '10000000000', |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + await client.submitAndWait(trust_set_tx, { |
| 30 | + wallet: wallet2, |
| 31 | + }) |
| 32 | + |
| 33 | + console.log('Balances after trustline is created') |
| 34 | + console.log(await client.getBalances(wallet1.classicAddress)) |
| 35 | + console.log(await client.getBalances(wallet2.classicAddress)) |
| 36 | + |
| 37 | + // Initially, the issuer(wallet1) sends an amount to the other account(wallet2) |
| 38 | + const issue_quantity = '3840' |
| 39 | + const payment: Payment = { |
| 40 | + TransactionType: 'Payment', |
| 41 | + Account: wallet1.classicAddress, |
| 42 | + Amount: { |
| 43 | + currency: 'FOO', |
| 44 | + value: issue_quantity, |
| 45 | + issuer: wallet1.classicAddress, |
| 46 | + }, |
| 47 | + Destination: wallet2.classicAddress, |
| 48 | + } |
| 49 | + |
| 50 | + // submit payment |
| 51 | + const initialPayment = await client.submitAndWait(payment, { |
| 52 | + wallet: wallet1, |
| 53 | + }) |
| 54 | + console.log(initialPayment) |
| 55 | + |
| 56 | + console.log('Balances after issuer(wallet1) sends IOU("FOO") to wallet2') |
| 57 | + console.log(await client.getBalances(wallet1.classicAddress)) |
| 58 | + console.log(await client.getBalances(wallet2.classicAddress)) |
| 59 | + |
| 60 | + /* |
| 61 | + * Send money less than the amount specified on 2 conditions: |
| 62 | + * 1. Sender has less money than the aamount specified in the payment Tx. |
| 63 | + * 2. Sender has the tfPartialPayment flag activated. |
| 64 | + * |
| 65 | + * Other ways to specify flags are by using Hex code and decimal code. |
| 66 | + * eg. For partial payment(tfPartialPayment) |
| 67 | + * decimal ->131072, hex -> 0x00020000 |
| 68 | + */ |
| 69 | + const partialPaymentTx: Payment = { |
| 70 | + TransactionType: 'Payment', |
| 71 | + Account: wallet2.classicAddress, |
| 72 | + Amount: { |
| 73 | + currency: 'FOO', |
| 74 | + value: '4000', |
| 75 | + issuer: wallet1.classicAddress, |
| 76 | + }, |
| 77 | + Destination: wallet1.classicAddress, |
| 78 | + Flags: PaymentFlags.tfPartialPayment, |
| 79 | + } |
| 80 | + |
| 81 | + // submit payment |
| 82 | + const submitResponse = await client.submitAndWait(partialPaymentTx, { |
| 83 | + wallet: wallet2, |
| 84 | + }) |
| 85 | + console.log(submitResponse) |
| 86 | + |
| 87 | + console.log( |
| 88 | + "Balances after Partial Payment, when wallet2 tried to send 4000 FOO's", |
| 89 | + ) |
| 90 | + console.log(await client.getBalances(wallet1.classicAddress)) |
| 91 | + console.log(await client.getBalances(wallet2.classicAddress)) |
| 92 | + |
| 93 | + await client.disconnect() |
| 94 | +} |
| 95 | +void partialPayment() |
0 commit comments