diff --git a/content/_code-samples/claim-payment-channel/README.md b/content/_code-samples/claim-payment-channel/README.md index 2a20a5eb5c..7a8b513b7c 100644 --- a/content/_code-samples/claim-payment-channel/README.md +++ b/content/_code-samples/claim-payment-channel/README.md @@ -2,4 +2,4 @@ Create, claim and verify a Payment Channel. -For more context, see [PayChannel](https://xrpl.org/paychannel.html#paychannel). +For more context, see [PayChannel](https://xrpl.org/paychannel.html). diff --git a/content/_code-samples/claim-payment-channel/js/claimPayChannel.ts b/content/_code-samples/claim-payment-channel/js/claimPayChannel.ts index 3ce4572a69..097a8fcf22 100644 --- a/content/_code-samples/claim-payment-channel/js/claimPayChannel.ts +++ b/content/_code-samples/claim-payment-channel/js/claimPayChannel.ts @@ -1,3 +1,7 @@ +/* + * Create, claim and verify a Payment Channel. + * Reference: https://xrpl.org/paychannel.html + */ import { AccountObjectsRequest, Client, @@ -19,8 +23,8 @@ async function claimPayChannel(): Promise { const { wallet: wallet2 } = await client.fundWallet() console.log('Balances of wallets before Payment Channel is claimed:') - 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) + 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 const paymentChannelCreate: PaymentChannelCreate = { @@ -31,11 +35,13 @@ async function claimPayChannel(): Promise { SettleDelay: 86400, PublicKey: wallet1.publicKey, } - + + console.log("Submitting a PaymentChannelCreate transaction...") const paymentChannelResponse = await client.submitAndWait( paymentChannelCreate, { wallet: wallet1 }, ) + console.log("PaymentChannelCreate transaction response:") console.log(paymentChannelResponse) // check that the object was actually created @@ -61,14 +67,17 @@ async function claimPayChannel(): Promise { Amount: '100', } + console.log("Submitting a PaymentChannelClaim transaction...") + const channelClaimResponse = await client.submit(paymentChannelClaim, { wallet: wallet1, }) + console.log("PaymentChannelClaim transaction response:") console.log(channelClaimResponse) console.log('Balances of wallets after Payment Channel is claimed:') - 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) + 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`) await client.disconnect() } diff --git a/content/_code-samples/get-tx/js/getTransaction.ts b/content/_code-samples/get-tx/js/getTransaction.ts index 3e1b9d5c1f..ade7707003 100644 --- a/content/_code-samples/get-tx/js/getTransaction.ts +++ b/content/_code-samples/get-tx/js/getTransaction.ts @@ -1,4 +1,7 @@ - +/* + * Retrieve and display a transaction on the ledger. + * Reference: https://xrpl.org/tx.html + */ import { Client, LedgerResponse, TxResponse } from 'xrpl' const client = new Client('wss://s.altnet.rippletest.net:51233') @@ -18,7 +21,8 @@ async function getTransaction(): Promise { command: 'tx', transaction: transactions[0], }) - console.log("Response from 'tx' request:", tx) + console.log("First transaction in the ledger:") + console.log(tx) // The meta field would be a string(hex) when the `binary` parameter is `true` for the `tx` request. if (tx.result.meta == null) { diff --git a/content/_code-samples/get-tx/py/get_transaction.py b/content/_code-samples/get-tx/py/get_transaction.py index 59f9d6c4f5..1affe70370 100644 --- a/content/_code-samples/get-tx/py/get_transaction.py +++ b/content/_code-samples/get-tx/py/get_transaction.py @@ -24,6 +24,7 @@ if transactions: # Create a Transaction request and have the client call it tx_request = Tx(transaction=transactions[0]) tx_response = client.request(tx_request) + print("First transaction in the ledger:") print(tx_response) else: print("No transactions were found on the ledger!") diff --git a/content/_code-samples/multisigning/js/multisigning.ts b/content/_code-samples/multisigning/js/multisigning.ts index 7cf0cc4804..f4e7a195e7 100644 --- a/content/_code-samples/multisigning/js/multisigning.ts +++ b/content/_code-samples/multisigning/js/multisigning.ts @@ -1,40 +1,82 @@ - -import { Client, LedgerResponse, TxResponse } from 'xrpl' +/* + * Create and submit a SignerListSet and multisign a transaction. + * Reference: https://xrpl.org/multi-signing.html +*/ +import { + multisign, + Client, + AccountSet, + convertStringToHex, + SignerListSet, +} from 'xrpl' const client = new Client('wss://s.altnet.rippletest.net:51233') -async function getTransaction(): Promise { +async function multisigning(): Promise { await client.connect() - const ledger: LedgerResponse = await client.request({ - command: 'ledger', - transactions: true, - ledger_index: 'validated', + /* + * This wallet creation is for demonstration purposes. + * In practice, users generally will not have all keys in one spot, + * hence, users need to implement a way to get signatures. + */ + const { wallet: wallet1 } = await client.fundWallet() + const { wallet: wallet2 } = await client.fundWallet() + const { wallet: walletMaster } = await client.fundWallet() + const signerListSet: SignerListSet = { + TransactionType: 'SignerListSet', + Account: walletMaster.classicAddress, + SignerEntries: [ + { + SignerEntry: { + Account: wallet1.classicAddress, + SignerWeight: 1, + }, + }, + { + SignerEntry: { + Account: wallet2.classicAddress, + SignerWeight: 1, + }, + }, + ], + SignerQuorum: 2, + } + + const signerListResponse = await client.submit(signerListSet, { + wallet: walletMaster, }) - console.log(ledger) + console.log('SignerListSet constructed successfully:') + console.log(signerListResponse) - const transactions = ledger.result.ledger.transactions - if (transactions) { - const tx: TxResponse = await client.request({ - command: 'tx', - transaction: transactions[0], - }) - console.log(tx) + const accountSet: AccountSet = { + TransactionType: 'AccountSet', + Account: walletMaster.classicAddress, + Domain: convertStringToHex('example.com'), + } + const accountSetTx = await client.autofill(accountSet, 2) + console.log('AccountSet transaction is ready to be multisigned:') + console.log(accountSetTx) + const { tx_blob: tx_blob1 } = wallet1.sign(accountSetTx, true) + const { tx_blob: tx_blob2 } = wallet2.sign(accountSetTx, true) + const multisignedTx = multisign([tx_blob1, tx_blob2]) + const submitResponse = await client.submit(multisignedTx) - // The meta field would be a string(hex) when the `binary` parameter is `true` for the `tx` request. - if (tx.result.meta == null) { - throw new Error('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 (typeof tx.result.meta !== 'string') { - console.log('delivered_amount:', tx.result.meta.delivered_amount) + if (submitResponse.result.engine_result === 'tesSUCCESS') { + console.log('The multisigned transaction was accepted by the ledger:') + console.log(submitResponse) + if (submitResponse.result.tx_json.Signers) { + console.log( + `The transaction had ${submitResponse.result.tx_json.Signers.length} signatures`, + ) } + } else { + console.log( + "The multisigned transaction was rejected by rippled. Here's the response from rippled:", + ) + console.log(submitResponse) } await client.disconnect() } -void getTransaction() +void multisigning() \ No newline at end of file diff --git a/content/_code-samples/multisigning/py/multisigning.py b/content/_code-samples/multisigning/py/multisigning.py index 21d4938327..a939421168 100644 --- a/content/_code-samples/multisigning/py/multisigning.py +++ b/content/_code-samples/multisigning/py/multisigning.py @@ -1,4 +1,8 @@ -"""Example of how we can multisign a transaction""" +""" +Example of how we can multisign a transaction. +This will only work with version 2.0.0-beta.0 or later. +Reference: https://xrpl.org/multi-signing.html +""" from xrpl.clients import JsonRpcClient from xrpl.models.requests import SubmitMultisigned from xrpl.models.transactions import AccountSet, SignerEntry, SignerListSet diff --git a/content/_code-samples/partial-payment/js/partialPayment.ts b/content/_code-samples/partial-payment/js/partialPayment.ts index fd50d98c86..ff8a7f4df1 100644 --- a/content/_code-samples/partial-payment/js/partialPayment.ts +++ b/content/_code-samples/partial-payment/js/partialPayment.ts @@ -3,6 +3,7 @@ import { Client, Payment, PaymentFlags, TrustSet } from 'xrpl' const client = new Client('wss://s.altnet.rippletest.net:51233') // This snippet walks us through partial payment. +// Reference: https://xrpl.org/partial-payments.html async function partialPayment(): Promise { await client.connect() @@ -21,14 +22,18 @@ async function partialPayment(): Promise { value: '10000000000', }, } + console.log("Submitting a TrustSet transaction...") - await client.submitAndWait(trust_set_tx, { + const trust_set_res = await client.submitAndWait(trust_set_tx, { wallet: wallet2, }) + console.log("TrustSet transaction response:") + console.log(trust_set_res) + console.log('Balances after trustline is created') - console.log(await client.getBalances(wallet1.classicAddress)) - console.log(await client.getBalances(wallet2.classicAddress)) + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getBalances(wallet1.classicAddress)}`) + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getBalances(wallet2.classicAddress)}`) // Initially, the issuer(wallet1) sends an amount to the other account(wallet2) const issue_quantity = '3840' @@ -47,11 +52,11 @@ async function partialPayment(): Promise { const initialPayment = await client.submitAndWait(payment, { wallet: wallet1, }) - console.log(initialPayment) + console.log("Initial payment response:", 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)) + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getBalances(wallet1.classicAddress)}`) + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getBalances(wallet2.classicAddress)}`) /* * Send money less than the amount specified on 2 conditions: @@ -75,16 +80,17 @@ async function partialPayment(): Promise { } // submit payment + console.log("Submitting a Partial Payment transaction...") const submitResponse = await client.submitAndWait(partialPaymentTx, { wallet: wallet2, }) - console.log(submitResponse) + console.log("Partial Payment response: ", 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)) + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getBalances(wallet1.classicAddress)}`) + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getBalances(wallet2.classicAddress)}`) await client.disconnect() } diff --git a/content/_code-samples/partial-payment/py/partial-payment.py b/content/_code-samples/partial-payment/py/partial-payment.py index 7771a1e1b2..dc10f20e37 100644 --- a/content/_code-samples/partial-payment/py/partial-payment.py +++ b/content/_code-samples/partial-payment/py/partial-payment.py @@ -35,7 +35,9 @@ 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("Balance of ${wallet1.classic_address} is:") print((client.request(AccountLines(account=wallet1.classic_address))).result["lines"]) +print("Balance of ${wallet2.classic_address} is:") print((client.request(AccountLines(account=wallet2.classic_address))).result["lines"]) # Create a Payment to send 3840 FOO from wallet1 (issuer) to destination (wallet2) @@ -53,11 +55,13 @@ payment_tx = Payment( # 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) +print("Initial Payment response: ", 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("Balance of ${wallet1.classic_address} is:") print((client.request(AccountLines(account=wallet1.classic_address))).result["lines"]) +print("Balance of ${wallet2.classic_address} is:") print((client.request(AccountLines(account=wallet2.classic_address))).result["lines"]) # Send money less than the amount specified on 2 conditions: @@ -88,9 +92,11 @@ partial_payment_tx = Payment( # 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) +print("Partial Payment response: ", 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("Balance of ${wallet1.classic_address} is:") print((client.request(AccountLines(account=wallet1.classic_address))).result["lines"]) +print("Balance of ${wallet2.classic_address} is:") print((client.request(AccountLines(account=wallet2.classic_address))).result["lines"]) diff --git a/content/_code-samples/paths/js/paths.ts b/content/_code-samples/paths/js/paths.ts index a6cad669f1..fa4fa59c76 100644 --- a/content/_code-samples/paths/js/paths.ts +++ b/content/_code-samples/paths/js/paths.ts @@ -1,3 +1,7 @@ +/* + * Extract best paths from RipplePathFind to trade with and send a payment using paths. + * Reference: https://xrpl.org/paths.html + */ import { Client, Payment, RipplePathFindResponse } from 'xrpl' const client = new Client('wss://s.altnet.rippletest.net:51233') @@ -26,10 +30,10 @@ async function createTxWithPaths(): Promise { } const resp: RipplePathFindResponse = await client.request(request) - console.log(resp) + console.log("Ripple Path Find response: ", resp) const paths = resp.result.alternatives[0].paths_computed - console.log(paths) + console.log("Computed paths: ", paths) const tx: Payment = { TransactionType: 'Payment', diff --git a/content/_code-samples/reliable-tx-submission/js/reliableTransactionSubmission.ts b/content/_code-samples/reliable-tx-submission/js/reliableTransactionSubmission.ts index a428a5bef1..bfd63d4404 100644 --- a/content/_code-samples/reliable-tx-submission/js/reliableTransactionSubmission.ts +++ b/content/_code-samples/reliable-tx-submission/js/reliableTransactionSubmission.ts @@ -49,10 +49,9 @@ async function sendReliableTx(): Promise { const { wallet: wallet2 } = await client.fundWallet(); console.log("Balances of wallets before Payment tx"); - console.log( - await client.getXrpBalance(wallet1.classicAddress), - await client.getXrpBalance(wallet2.classicAddress) - ); + + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getXrpBalance(wallet1.classicAddress)}XRP`); + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getXrpBalance(wallet2.classicAddress)}XRP`); // create a Payment tx and submit and wait for tx to be validated const payment: Payment = { @@ -62,6 +61,7 @@ async function sendReliableTx(): Promise { Destination: wallet2.classicAddress, }; + console.log("Submitting a Payment transaction...") const paymentResponse = await client.submitAndWait(payment, { wallet: wallet1, }); @@ -74,10 +74,8 @@ async function sendReliableTx(): Promise { console.log("Validated:", txResponse.result.validated); console.log("Balances of wallets after Payment tx:"); - console.log( - await client.getXrpBalance(wallet1.classicAddress), - await client.getXrpBalance(wallet2.classicAddress) - ); + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getXrpBalance(wallet1.classicAddress)}XRP`); + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getXrpBalance(wallet2.classicAddress)}XRP`); await client.disconnect(); } diff --git a/content/_code-samples/set-regular-key/js/setRegularKey.ts b/content/_code-samples/set-regular-key/js/setRegularKey.ts index 2e916defe0..443375d959 100644 --- a/content/_code-samples/set-regular-key/js/setRegularKey.ts +++ b/content/_code-samples/set-regular-key/js/setRegularKey.ts @@ -1,11 +1,11 @@ +/* + * Use `SetRegularKey` to assign a key pair to a wallet and make a payment signed using the regular key wallet. + * Reference: https://xrpl.org/setregularkey.html + */ import { Client, Payment, SetRegularKey } from 'xrpl' const client = new Client('wss://s.altnet.rippletest.net:51233') -/* - * The snippet walks us through an example usage of RegularKey. - * Later, - */ async function setRegularKey(): Promise { await client.connect() const { wallet: wallet1 } = await client.fundWallet() @@ -13,8 +13,8 @@ async function setRegularKey(): Promise { const { wallet: regularKeyWallet } = await client.fundWallet() console.log('Balances before payment') - console.log(await client.getXrpBalance(wallet1.classicAddress)) - console.log(await client.getXrpBalance(wallet2.classicAddress)) + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getXrpBalance(wallet1.classicAddress)}XRP`) + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getXrpBalance(wallet2.classicAddress)}XRP`) // assigns key-pair(regularKeyWallet) to wallet1 using `SetRegularKey`. const tx: SetRegularKey = { @@ -22,6 +22,8 @@ async function setRegularKey(): Promise { Account: wallet1.classicAddress, RegularKey: regularKeyWallet.classicAddress, } + + console.log('Submitting a SetRegularKey transaction...') const response = await client.submitAndWait(tx, { wallet: wallet1, }) @@ -46,8 +48,8 @@ async function setRegularKey(): Promise { console.log('Response for tx signed using Regular Key:') console.log(submitTx) console.log('Balances after payment:') - console.log(await client.getXrpBalance(wallet1.classicAddress)) - console.log(await client.getXrpBalance(wallet2.classicAddress)) + console.log(`Balance of ${wallet1.classicAddress} is ${await client.getXrpBalance(wallet1.classicAddress)}XRP`) + console.log(`Balance of ${wallet2.classicAddress} is ${await client.getXrpBalance(wallet2.classicAddress)}XRP`) await client.disconnect() }