diff --git a/_code-samples/nft-modular-tutorials/transfer-nfts.js b/_code-samples/nft-modular-tutorials/transfer-nfts.js index 0ad7993186..a003074f1f 100644 --- a/_code-samples/nft-modular-tutorials/transfer-nfts.js +++ b/_code-samples/nft-modular-tutorials/transfer-nfts.js @@ -38,8 +38,6 @@ async function createSellOffer() { transactionJson.Destination = destination; } - console.log("Creating Sell Offer Transaction:", JSON.stringify(transactionJson, null, 2)); - const tx = await client.submitAndWait(transactionJson, { wallet }); results += `\nSell offer created successfully!\nTransaction Hash: ${tx.result.hash}\nEngine Result: ${tx.result.engine_result}`; resultField.value = results; @@ -69,7 +67,6 @@ async function createBuyOffer() { try { // Use the external configureAmount() function let amount = configureAmount(); -console.log("Amount:", amount); // Use the external configureExpiration() function let expiration = configureExpiration(); // This will return a number or an empty string from the original logic @@ -83,12 +80,12 @@ console.log("Amount:", amount); // Only add Amount if it's defined (not undefined or an empty string) if (amount !== undefined && amount !== '') { - transactionJson.Amount = amount; + transactionJson.Amount = amount; } else { - results += "\nError: Amount field is required for a buy offer."; - resultField.value = results; - client.disconnect(); - return; + results += "\nError: Amount field is required for a buy offer."; + resultField.value = results; + client.disconnect(); + return; } if (destinationField.value !== '') { @@ -100,8 +97,6 @@ console.log("Amount:", amount); transactionJson.Expiration = expiration; } - console.log("Buy Offer Transaction JSON:\n" + JSON.stringify(transactionJson, null, 2)); - const tx = await client.submitAndWait(transactionJson, { wallet: wallet }); results += "\n\n=== Sell Offers ===\n"; @@ -166,7 +161,7 @@ async function cancelOffer() { // Submit transaction -------------------------------------------------------- const tx = await client.submitAndWait(transactionJson, { wallet }) - results += "\n\n=== Sell Offers===\n" + results = "\n\n=== Sell Offers===\n" let nftSellOffers try { nftSellOffers = await client.request({ @@ -174,7 +169,7 @@ async function cancelOffer() { nft_id: nftIdField.value }) } catch (err) { - nftSellOffers = "=== No sell offers. ===" + nftSellOffers = '=== No sell offers. ===\n' } results += JSON.stringify(nftSellOffers, null, 2) results += "\n\n=== Buy Offers ===\n" @@ -184,18 +179,20 @@ async function cancelOffer() { method: "nft_buy_offers", nft_id: nftIdField.value }) - results += JSON.stringify(nftBuyOffers, null, 2) + } catch (err) { nftBuyOffers = '=== No buy offers. ===' } + results += JSON.stringify(nftBuyOffers, null, 2) + resultField.value += results // Check transaction results ------------------------------------------------- - results += "\n=== Transaction result:\n" + + results = "\n=== Transaction result:\n" + JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\n=== Balance changes:\n" + + results += "\n\n=== Balance changes:\n" + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - resultField.value = results + resultField.value += results client.disconnect() // End of cancelOffer() } @@ -209,12 +206,12 @@ async function getOffers() { let net = getNet() const client = new xrpl.Client(net) await client.connect() - + let results = '\nConnected. Getting offers...' - resultField.value = results + resultField.value = results // --- Sell Offers --- - results += '\n\n=== Sell Offers ===\n' + results += '\n\n=== Sell Offers ===\n' let nftSellOffers try { nftSellOffers = await client.request({ @@ -225,10 +222,10 @@ async function getOffers() { nftSellOffers = 'No sell offers found for this NFT ID.' } results += JSON.stringify(nftSellOffers, null, 2) - resultField.value = results + resultField.value = results // --- Buy Offers --- - results += '\n\n=== Buy Offers ===\n' + results = '\n\n=== Buy Offers ===\n' let nftBuyOffers try { nftBuyOffers = await client.request({ @@ -240,7 +237,7 @@ async function getOffers() { nftBuyOffers = 'No buy offers found for this NFT ID.' // More descriptive } results += JSON.stringify(nftBuyOffers, null, 2) // Append the JSON string - resultField.value = results // Update the display with buy offers + resultField.value += results // Update the display with buy offers client.disconnect() }// End of getOffers() @@ -253,36 +250,41 @@ async function acceptSellOffer() { const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) let net = getNet() const client = new xrpl.Client(net) - await client.connect() - let results = '\n=== Connected. Accepting sell offer. ===\n\n' - resultField.value = results + try { + await client.connect() + let results = '\n=== Connected. Accepting sell offer. ===\n\n' + resultField.value = results - // Prepare transaction ------------------------------------------------------- - const transactionJson = { - "TransactionType": "NFTokenAcceptOffer", - "Account": wallet.classicAddress, - "NFTokenSellOffer": nftOfferIdField.value, + // Prepare transaction ------------------------------------------------------- + const transactionJson = { + "TransactionType": "NFTokenAcceptOffer", + "Account": wallet.classicAddress, + "NFTokenSellOffer": nftOfferIdField.value, + } + // Submit transaction -------------------------------------------------------- + const tx = await client.submitAndWait(transactionJson, { wallet: wallet }) + const nfts = await client.request({ + method: "account_nfts", + account: wallet.classicAddress + }) + + // Check transaction results ------------------------------------------------- + + xrpBalanceField.value = (await client.getXrpBalance(wallet.address)) + + + results += '=== Transaction result:\n' + results += JSON.stringify(tx.result.meta.TransactionResult, null, 2) + results += '\n=== Balance changes:' + results += JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + results += JSON.stringify(nfts, null, 2) + resultField.value = results + } catch (error) { + console.error('Error accepting sell offer:', error) + resultField.value = `Error: ${error.message || error}` + } finally { + client.disconnect() } - // Submit transaction -------------------------------------------------------- - const tx = await client.submitAndWait(transactionJson, { wallet: wallet }) - const nfts = await client.request({ - method: "account_nfts", - account: wallet.classicAddress - }) - - // Check transaction results ------------------------------------------------- - - xrpBalanceField.value = (await client.getXrpBalance(wallet.address)) - - - results += '=== Transaction result:\n' - results += JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += '\n=== Balance changes:' - results += JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - results += JSON.stringify(nfts, null, 2) - resultField.value = results - - client.disconnect() }// End of acceptSellOffer() // ******************************************************* diff --git a/docs/img/mt-transfer-nfts-6-create-buy-offer.png b/docs/img/mt-transfer-nfts-6-create-buy-offer.png new file mode 100644 index 0000000000..671dbb0482 Binary files /dev/null and b/docs/img/mt-transfer-nfts-6-create-buy-offer.png differ diff --git a/docs/img/mt-transfer-nfts-7-accept-buy-offer.png b/docs/img/mt-transfer-nfts-7-accept-buy-offer.png new file mode 100644 index 0000000000..78241043c3 Binary files /dev/null and b/docs/img/mt-transfer-nfts-7-accept-buy-offer.png differ diff --git a/docs/img/mt-transfer-nfts-8-cancel-offer.png b/docs/img/mt-transfer-nfts-8-cancel-offer.png new file mode 100644 index 0000000000..d7e367eec8 Binary files /dev/null and b/docs/img/mt-transfer-nfts-8-cancel-offer.png differ diff --git a/docs/tutorials/javascript/nfts/transfer-nfts.md b/docs/tutorials/javascript/nfts/transfer-nfts.md index 366bb4be79..831cf3001d 100644 --- a/docs/tutorials/javascript/nfts/transfer-nfts.md +++ b/docs/tutorials/javascript/nfts/transfer-nfts.md @@ -80,24 +80,27 @@ You can offer to buy an NFT from another account. To create an offer to buy an NFT: -1. Enter the **Amount** of your offer. +1. Change to an account other than the NFT owner. +2. Enter the **Amount** of your offer. 1. If paying XRP, enter the **Amount** of XRP in drops (1000000 equals 1 XRP). 2. If paying an issued currency, enter the **Currency**, **Issuer**, and **Amount**. -2. Optionally enter the number of days until **Expiration**. -3. Enter the **NFT ID**. -4. Enter the owner’s account address in the **NFT Owner Address** field. -5. Click **Create Buy Offer**. +3. Optionally enter the number of days until **Expiration**. +4. Enter the **NFT ID**. +5. Enter the owner’s account address in the **NFT Owner Address** field. +6. Click **Create Buy Offer**. -[![NFT Buy Offer](/docs/img/quickstart17.png)](/docs/img/quickstart17.png) +[![NFT Buy Offer](../../../img/mt-transfer-nfts-6-create-buy-offer.png)](../../../img/mt-transfer-nfts-6-create-buy-offer.png) ## Accept a Buy Offer To accept an offer to buy an NFT: -1. Enter the **NFT Offer Index** (the `nft_offer_index` of the NFT buy offer). -3. Click **Accept Buy Offer**. +1. Change to the NFT owner account. +2. Use **Get Offers**, above, if needed, to find the `nft_offer_index`. +3. Enter the `nft_offer_index` in the **NFT Offer ID** field. +4. Click **Accept Buy Offer**. -[![Accept Buy Offer](/docs/img/quickstart18.png)](/docs/img/quickstart18.png) +[![Accept Buy Offer](../../../img/mt-transfer-nfts-7-accept-buy-offer.png)](../../../img/mt-transfer-nfts-7-accept-buy-offer.png) ## Cancel Offer @@ -106,11 +109,11 @@ To cancel a buy or sell offer that you have created: 1. Enter the **NFT Offer Index**. 2. Click **Cancel Offer**. -[![Cancel offer](/docs/img/quickstart20.png)](/docs/img/quickstart20.png) +[![Cancel offer](../../../img/mt-transfer-nfts-8-cancel-offer.png)](../../../img/mt-transfer-nfts-8-cancel-offer.png) # Code Walkthrough -You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/_code-samples/quickstart/js/) archive to try each of the samples in your own browser. +You can download the [NFT Modular Examples](../../../../_code-samples/nft-modular-tutorials/nft-modular-tutorials.zip) archive to try each of the samples in your own browser. ## Create Sell Offer @@ -122,119 +125,89 @@ You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-port async function createSellOffer() { ``` -Connect to the ledger and get the accounts. +Get the account wallet and connect to the XRP Ledger. ```javascript - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - standbyResultField.value = results - await client.connect() - results += '\nConnected. Creating sell offer...' - standbyResultField.value = results -``` + const wallet = xrpl.Wallet.fromSeed(accountSeedField.value); + let results = '\nCreating sell offer...'; + resultField.value = results; -Compute the Expiration Date, if present. The expiration date represents the number of seconds after the Ripple Epoch that the offer should expire. Start with the current date, add the number of days till expiration, then set the `expirationDate` variable to the converted date in Ripple time. - -```javascript - var expirationDate = null - if (standbyExpirationField.value !="") { - var days = standbyExpirationField.value - let d = new Date() - d.setDate(d.getDate() + parseInt(days)) - var expirationDate = xrpl.isoTimeToRippleTime(d) - } -``` - -Define the transaction. A _Flags_ value of 1 indicates that this transaction is a sell offer. - -```javascript - let transactionBlob = { - "TransactionType": "NFTokenCreateOffer", - "Account": standby_wallet.classicAddress, - "NFTokenID": standbyTokenIdField.value, - "Amount": standbyAmountField.value, - "Flags": parseInt(standbyFlagsField.value), - } -``` - -If the Expiration Date is present, append it to the transaction. - -```javascript - if (expirationDate != null) { - transactionBlob.Expiration = expirationDate - } - -``` - -If the Destination field is not empty, append it to the transaction. When the destination is set, only the destination account can buy the NFToken. - -```javascript - if(standbyDestinationField.value !== '') { - transactionBlob.Destination = standbyDestinationField.value - } -``` - -Submit the transaction and wait for the results. - - -```javascript - const tx = await client.submitAndWait(transactionBlob,{wallet: standby_wallet}) - results += '\n\n***Sell Offers***\n' -``` - -Request the list of sell offers for the token. - -```javascript - let nftSellOffers try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: standbyTokenIdField.value}) - } catch (err) { - nftSellOffers = "No sell offers." + const client = new xrpl.Client(getNet()); + await client.connect(); + try { +``` + +If the destination field is populated, capture the value. + +```javascript + const destination = destinationField.value || undefined; +``` + +If the **Expiration** field is populated, configure the expiration date for the sell offer. + +```javascript + const expiration = expirationField.value ? configureExpiration() : undefined; +``` + +Begin constructing the transaction JSON object. + +```javascript + const transactionJson = { + TransactionType: "NFTokenCreateOffer", + Account: wallet.classicAddress, + NFTokenID: nftIdField.value, + Flags: 1, + }; +``` + +Configure the amount. To give the NFT away, set the **Amount** to 0. `configureAmount()` determines whether the currency is XRP or an issued currency, and returns the properly formattted object to add to the JSON transaction object. + +```javascript + const amount = configureAmount(); + if (amount) { // Only add Amount if it's defined + transactionJson.Amount = amount; + } else { + console.warn("Amount is undefined. Sell offer might be invalid."); + resultField.value = results; + } +``` + +If you have an expiration date or specified destination, add them to the JSON transaction object. + +```javascript + if (expiration) { + transactionJson.Expiration = expiration; + } + if (destination) { + transactionJson.Destination = destination; + } +``` + +Submit the transaction, wait for and report the results. + +```javascript + const tx = await client.submitAndWait(transactionJson, { wallet }); + results += `\nSell offer created successfully!\nTransaction Hash: ${tx.result.hash}\nEngine Result: ${tx.result.engine_result}`; + resultField.value = results; +``` + +Disconnect from the XRP Ledger. + +```javascript + } finally { + client.disconnect(); + } +``` + +Catch and report any errors. + +```javascript + } catch (error) { + console.error("Error creating sell offer:", error); + results = `\nError: ${error.message || error}`; + resultField.value = results; } - results += JSON.stringify(nftSellOffers,null,2) -``` - -Request the list of buy offers for the token. - -```javascript - results += '\n\n***Buy Offers***\n' - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: standbyTokenIdField.value }) - results += JSON.stringify(nftBuyOffers,null,2) - } catch (err) { - results += 'No buy offers.' - } -``` - -Report the results of the transaction. - -```javascript - results += '\n\nTransaction result:\n' + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += '\n\nBalance changes:\n' + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) -``` - -Get the current XRP balances for the operational and standby accounts. - -```javascript - operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) - standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) - standbyResultField.value = results -``` - -Disconnect from the ledger. - -```javascript - client.disconnect() }// End of createSellOffer() ``` @@ -248,113 +221,127 @@ Disconnect from the ledger. async function createBuyOffer() { ``` -Get the account and connect to the ledger. +Get the account wallet and connect to the ledger. ```javascript - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - let results = 'Connecting to ' + net + '...' - standbyResultField.value = results - await client.connect() - results = '\nConnected. Creating buy offer...' - standbyResultField.value = results -``` -Prepare the expiration date, if present. - -```javascript - var expirationDate = null - if (standbyExpirationField.value !="") { - var days = standbyExpirationField.value - let d = new Date() - d.setDate(d.getDate() + parseInt(days)) - var expirationDate = xrpl.isoTimeToRippleTime(d) - } + const wallet = xrpl.Wallet.fromSeed(accountSeedField.value); + let net = getNet(); + const client = new xrpl.Client(net); + await client.connect(); + let results = '\n=== Connected. Creating buy offer. ==='; + resultField.value = results; ``` -Define the transaction. Setting the _Flags_ value to _null_ indicates that this is a buy offer. - +Configure the amount and expiration date, if present. ```javascript - const transactionBlob = { - "TransactionType": "NFTokenCreateOffer", - "Account": standby_wallet.classicAddress, - "Owner": standbyOwnerField.value, - "NFTokenID": standbyTokenIdField.value, - "Amount": standbyAmountField.value, - "Flags": null - } -``` -If the expiration date is present, append that to the transaction. - -```javascript - if (expirationDate != null) { - transactionBlob.Expiration = expirationDate - } -``` - -Submit the transaction and wait for the results. - -```javascript - const tx = await client.submitAndWait(transactionBlob,{wallet: standby_wallet}) -``` - -Request the list of sell offers for the token. - -```javascript - results += "\n\n***Sell Offers***\n" - let nftSellOffers try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: standbyTokenIdField.value }) - } catch (err) { - nftSellOffers = "No sell offers." - } - results += JSON.stringify(nftSellOffers,null,2) + // Use the external configureAmount() function + let amount = configureAmount(); + // Use the external configureExpiration() function + let expiration = configureExpiration(); // This will return a number or an empty string from the original logic ``` -Request the list of buy offers for the token. +Start constructing the transactionJson object. ```javascript - results += "\n\n***Buy Offers***\n" - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: standbyTokenIdField.value }) - results += JSON.stringify(nftBuyOffers,null,2) - } catch (err) { - results += "No buy offers." - } + let transactionJson = { + "TransactionType": "NFTokenCreateOffer", + "Account": wallet.classicAddress, + "Owner": nftOwnerField.value, + "NFTokenID": nftIdField.value, + "Flags": 0, // Ensure no tfSellNFToken flag for a buy offer + }; +``` + +Add the configured amount to the transaction. + +```javascript + if (amount !== undefined && amount !== '') { + transactionJson.Amount = amount; + } else { + results += "\nError: Amount field is required for a buy offer."; + resultField.value = results; + client.disconnect(); + return; + } +``` + +Add the **Destination** value, if it is set. + +```javascript + if (destinationField.value !== '') { + transactionJson.Destination = destinationField.value; + } +``` + +Add the Expiration date if it's not an empty string. + +```javascript + if (expiration > 0) { + transactionJson.Expiration = expiration; + } +``` + +Submit the transaction and wait for the results. List the sell offers and buy offers currently available. + +```javascript + const tx = await client.submitAndWait(transactionJson, { wallet: wallet }); + + results += "\n\n=== Sell Offers ===\n"; + let nftSellOffers; + try { + nftSellOffers = await client.request({ + method: "nft_sell_offers", + nft_id: nftIdField.value + }); + } catch (err) { + nftSellOffers = "=== No sell offers. ==="; + } + results += JSON.stringify(nftSellOffers, null, 2); + results += "\n\n=== Buy Offers ===\n"; + let nftBuyOffers; + try { + nftBuyOffers = await client.request({ + method: "nft_buy_offers", + nft_id: nftIdField.value + }); + results += JSON.stringify(nftBuyOffers, null, 2); + } catch (err) { + results += "=== No buy offers. ==="; + } ``` Report the results of the transaction. ```javascript - results += "\n\nTransaction result:\n" + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\n\nBalance changes:\n" + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - standbyResultField.value = results``` + // Check transaction results ------------------------------------------------- + results += "\n\n=== Transaction result:\n" + + JSON.stringify(tx.result.meta.TransactionResult, null, 2); + results += "\n\n=== Balance changes:\n" + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2); + resultField.value = results; +``` -Disconnect from the ledger. +Catch and report any errors. +```javascript + } catch (error) { + console.error('Error creating buy offer:', error); + results += "\n\n=== Error: " + error; + resultField.value = results; +``` -```javascript - client.disconnect() +Disconnect from the XRP Ledger. + +```javascript + } finally { + client.disconnect(); + } }// End of createBuyOffer() ``` ## Cancel Offer - - -// START HERE - - - - ```javascript // ******************************************************* // ******************** Cancel Offer ********************* @@ -363,29 +350,27 @@ Disconnect from the ledger. async function cancelOffer() { ``` -Get the standby address and connect to the ledger. +Get the account wallet and connect to the ledger. ```javascript - const wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) let net = getNet() const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' + standbyResultField.value = results await client.connect() - results += "\nConnected. Cancelling offer..." - standbyResultField.value = results + let results = "\n=== Connected. Cancelling offer. ===" + resultField.value = results ``` +Store the token offer ID in the `NFTokenOffers` array parameter. -Insert the token offer index as a new array. This example destroys one offer at a time. In practice you could implement the function to accept multiple token offer index values and destroy all of the token offers in one operation. - -```javascript - const tokenOfferIDs = [standbyTokenOfferIndexField.value] +```javascript + const tokenOfferIDs = [nftOfferIdField.value] ``` -Define the transaction. +Construct the NFTokenCancelOffer JSON transaction. -```javascript - const transactionBlob = { +```javascript + const transactionJson = { "TransactionType": "NFTokenCancelOffer", "Account": wallet.classicAddress, "NFTokenOffers": tokenOfferIDs @@ -394,55 +379,51 @@ Define the transaction. Submit the transaction and wait for the results. -```javascript - const tx = await client.submitAndWait(transactionBlob,{wallet}) +```javascript + const tx = await client.submitAndWait(transactionJson, { wallet }) ``` -Request the list of sell offers for the token. +List the remaining sell offers and buy offers. -```javascript - results += "\n\n***Sell Offers***\n" +```javascript + results = "\n\n=== Sell Offers===\n" let nftSellOffers try { nftSellOffers = await client.request({ method: "nft_sell_offers", - nft_id: standbyTokenIdField.value + nft_id: nftIdField.value }) } catch (err) { - nftSellOffers = "No sell offers." + nftSellOffers = '=== No sell offers. ===\n' } - results += JSON.stringify(nftSellOffers,null,2) -``` - -Request the list of buy offers for the token. - -```javascript - results += "\n\n***Buy Offers***\n" + results += JSON.stringify(nftSellOffers, null, 2) + results += "\n\n=== Buy Offers ===\n" let nftBuyOffers try { nftBuyOffers = await client.request({ method: "nft_buy_offers", - nft_id: standbyTokenIdField.value + nft_id: nftIdField.value }) - results += JSON.stringify(nftBuyOffers,null,2) } catch (err) { - nftBuyOffers = "No buy offers." - } + nftBuyOffers = '=== No buy offers. ===' + } + results += JSON.stringify(nftBuyOffers, null, 2) + resultField.value += results ``` -Report the transaction results. +Report the transaction results and XRP balance changes. -```javascript - results += "\nTransaction result:\n" + +```javascript + results = "\n=== Transaction result:\n" + JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\nBalance changes:\n" + + results += "\n\n=== Balance changes:\n" + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - standbyResultField.value = results + resultField.value += results ``` -Disconnect from the ledger. +Disconnect from the XRP Ledger. -```javascript +```javascript client.disconnect() // End of cancelOffer() } ``` @@ -458,56 +439,59 @@ Disconnect from the ledger. async function getOffers() { ``` -Connect to the ledger. +Get the account wallet and connect to the ledger. ```javascript - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) +const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) let net = getNet() const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - standbyResultField.value = results await client.connect() - results += '\nConnected. Getting offers...' - standbyResultField.value = results + + let results = '\nConnected. Getting offers...' + resultField.value = results + + // --- Sell Offers --- + results += '\n\n=== Sell Offers ===\n' ``` -Request the list of sell offers for the token. +Prepare and an `nft_sell_offers` request for the selected NFT ID. Catch any errors and report the results. -```javascript - results += '\n\n***Sell Offers***\n' +```javascript let nftSellOffers try { nftSellOffers = await client.request({ method: "nft_sell_offers", - nft_id: standbyTokenIdField.value + nft_id: nftIdField.value }) } catch (err) { - nftSellOffers = 'No sell offers.' + nftSellOffers = 'No sell offers found for this NFT ID.' } - results += JSON.stringify(nftSellOffers,null,2) - standbyResultField.value = results + results += JSON.stringify(nftSellOffers, null, 2) + resultField.value = results ``` -Request the list of buy offers for the token. +Prepare and an `nft_buy_offers` request for the selected NFT ID. Catch any errors and report the results. -```javascript - results += '\n\n***Buy Offers***\n' +```javascript + // --- Buy Offers --- + results = '\n\n=== Buy Offers ===\n' let nftBuyOffers try { nftBuyOffers = await client.request({ method: "nft_buy_offers", - nft_id: standbyTokenIdField.value + nft_id: nftIdField.value }) } catch (err) { - nftBuyOffers = 'No buy offers.' + // Log the actual error for debugging + nftBuyOffers = 'No buy offers found for this NFT ID.' // More descriptive } - results += JSON.stringify(nftBuyOffers,null,2) - standbyResultField.value = results + results += JSON.stringify(nftBuyOffers, null, 2) // Append the JSON string + resultField.value += results // Update the display with buy offers ``` -Disconnect from the ledger. +Disconnect from the XRP Ledger. -```javascript +```javascript client.disconnect() }// End of getOffers() ``` @@ -523,862 +507,484 @@ Disconnect from the ledger. async function acceptSellOffer() { ``` -Get the accounts and connect to the ledger. +Get the account wallet and connect to the ledger. ```javascript - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) let net = getNet() const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - standbyResultField.value = results - await client.connect() - results += '\nConnected. Accepting sell offer...\n\n' - standbyResultField.value = results + try { + await client.connect() + let results = '\n=== Connected. Accepting sell offer. ===\n\n' + resultField.value = results ``` -Define the transaction. +Prepare the transaction JSON object. -```javascript - const transactionBlob = { - "TransactionType": "NFTokenAcceptOffer", - "Account": standby_wallet.classicAddress, - "NFTokenSellOffer": standbyTokenOfferIndexField.value, +```javascript + const transactionJson = { + "TransactionType": "NFTokenAcceptOffer", + "Account": wallet.classicAddress, + "NFTokenSellOffer": nftOfferIdField.value, } ``` Submit the transaction and wait for the results. -```javascript - const tx = await client.submitAndWait(transactionBlob,{wallet: standby_wallet}) +```javascript + const tx = await client.submitAndWait(transactionJson, { wallet: wallet }) ``` -Request the list of NFTs for the standby account. +Get the current NFTs held by the account after the transaction. -```javascript - const nfts = await client.request({ - method: "account_nfts", - account: standby_wallet.classicAddress - }) +```javascript + const nfts = await client.request({ + method: "account_nfts", + account: wallet.classicAddress + }) ``` -Get the balances for both accounts. +Get the new XRP balance after the transaction. -```javascript - standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) - operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) +```javascript + xrpBalanceField.value = (await client.getXrpBalance(wallet.address)) ``` -Report the transaction results. +Report the results. -```javascript - results += 'Transaction result:\n' - results += JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += '\nBalance changes:' - results += JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - results += JSON.stringify(nfts,null,2) - standbyResultField.value = results +```javascript + results += '=== Transaction result:\n' + results += JSON.stringify(tx.result.meta.TransactionResult, null, 2) + results += '\n=== Balance changes:' + results += JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + results += JSON.stringify(nfts, null, 2) + resultField.value += results ``` -Disconnect from the ledger. +Catch and report any errors. -```javascript - client.disconnect() +```javascript + } catch (error) { + console.error('Error accepting sell offer:', error) + resultField.value = `Error: ${error.message || error}` +``` +Disconnect from the XRP Ledger. + +```javascript + } finally { + client.disconnect() + } }// End of acceptSellOffer() -``` -## Accept Buy Offer +### Accept Buy Offer -```javascript +```javascript // ******************************************************* // ******************* Accept Buy Offer ****************** // ******************************************************* - async function acceptBuyOffer() { +``` + +Get the account wallet and connect to the XRP Ledger. + +```javascript +async function acceptBuyOffer() { + const wallet = xrpl.Wallet.fromSeed(accountSeedField.value); + let net = getNet(); + const client = new xrpl.Client(net); + let results = '\n=== Connected. Accepting buy offer. ==='; // Declare results locally + + try { + await client.connect(); + resultField.value = results; ``` -Get the accounts and connect to the ledger. +Create the transactionJson object, passing the account address and the buy offer ID. -```javascript - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - standbyResultField.value = results - await client.connect() - results += '\nConnected. Accepting buy offer...' - standbyResultField.value = results -``` - -Prepare the transaction. - -```javascript - const transactionBlob = { - "TransactionType": "NFTokenAcceptOffer", - "Account": standby_wallet.classicAddress, - "NFTokenBuyOffer": standbyTokenOfferIndexField.value - } +```javascript + const transactionJson = { + "TransactionType": "NFTokenAcceptOffer", + "Account": wallet.classicAddress, + "NFTokenBuyOffer": nftOfferIdField.value + }; ``` Submit the transaction and wait for the results. -```javascript - const tx = await client.submitAndWait(transactionBlob,{wallet: standby_wallet}) +```javascript + const tx = await client.submitAndWait(transactionJson, { wallet: wallet }); ``` +Report the current list of account NFTs after the transaction. -Request the current list of NFTs for the standby account. +```javascript + const nfts = await client.request({ + method: "account_nfts", + account: wallet.classicAddress + }); - -```javascript - const nfts = await client.request({ - method: "account_nfts", - account: standby_wallet.classicAddress - }) - results += JSON.stringify(nfts,null,2) - standbyResultField.value = results + results += JSON.stringify(nfts, null, 2); + resultField.value = results; ``` -Report the transaction result. +Report the result of the transaction and update the XRP Balance field. -```javascript - results += "\n\nTransaction result:\n" + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\nBalance changes:\n" + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) +```javascript + results += "\n\nTransaction result:\n" + + JSON.stringify(tx.result.meta.TransactionResult, null, 2); + results += "\nBalance changes:\n" + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2); + xrpBalanceField.value = (await client.getXrpBalance(wallet.address)); + resultField.value = results; ``` +Catch and report any errors. -Request the XRP balance for both accounts. - - -```javascript - operationalBalanceField.value = - (await client.getXrpBalance(operational_wallet.address)) - standbyBalanceField.value = - (await client.getXrpBalance(standby_wallet.address)) - standbyResultField.value = results +```javascript + } catch (error) { + console.error('Error in acceptBuyOffer:', error); // Log the full error + results = `\n=== Error accepting buy offer: ${error.message || 'Unknown error'} ===`; + resultField.value = results; ``` -Disconnect from the ledger. +Disconnect from the XRP Ledger. -```javascript - client.disconnect() -}// End of acceptBuyOffer() -``` - -## Reciprocal Transactions - -These functions duplicate the functions of the standby account for the operational account. See the corresponding standby account function for walkthrough information. - -```javascript -// ******************************************************* -// *********** Operational Create Sell Offer ************* -// ******************************************************* - -async function oPcreateSellOffer() { - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results += '\nConnected. Creating sell offer...' - operationalResultField.value = results - - //------------------------------------- Prepare Expiration Date - var expirationDate = null - if (operationalExpirationField.value !="") { - var days = operationalExpirationField.value - let d = new Date() - d.setDate(d.getDate() + parseInt(days)) - var expirationDate = xrpl.isoTimeToRippleTime(d) +```javascript + } finally { + if (client && client.isConnected()) { + client.disconnect(); + } } - // Prepare transaction ------------------------------------------------------- - let transactionBlob = { - "TransactionType": "NFTokenCreateOffer", - "Account": operational_wallet.classicAddress, - "NFTokenID": operationalTokenIdField.value, - "Amount": operationalAmountField.value, - "Flags": parseInt(operationalFlagsField.value), - } - if (expirationDate != null) { - transactionBlob.Expiration = expirationDate - } - if(standbyDestinationField.value !== '') { - transactionBlob.Destination = operationalDestinationField.value - } - - // Submit transaction -------------------------------------------------------- - - const tx = await client.submitAndWait(transactionBlob,{wallet: operational_wallet}) - - results += '\n\n***Sell Offers***\n' - - let nftSellOffers - try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftSellOffers = "No sell offers." - } - results += JSON.stringify(nftSellOffers,null,2) - results += '\n\n***Buy Offers***\n' - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: operationalTokenIdField.value - }) - results += JSON.stringify(nftBuyOffers,null,2) - } catch (err) { - results += 'No buy offers.' - } - - // Check transaction results ------------------------------------------------- - results += '\n\nTransaction result:\n' + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += '\n\nBalance changes:\n' + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - operationalBalanceField.value = - (await client.getXrpBalance(operational_wallet.address)) - standbyBalanceField.value = - (await client.getXrpBalance(standby_wallet.address)) - operationalResultField.value = results - - client.disconnect() -} // End of oPcreateSellOffer() - -// ******************************************************* -// ************** Operational Create Buy Offer *********** -// ******************************************************* - -async function oPcreateBuyOffer() { - - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - let results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results = '\nConnected. Creating buy offer...' - operationalResultField.value = results - - //------------------------------------- Prepare Expiration Date - var expirationDate = null - if (operationalExpirationField.value !="") { - var days = operationalExpirationField.value - let d = new Date() - d.setDate(d.getDate() + parseInt(days)) - var expirationDate = xrpl.isoTimeToRippleTime(d) - } - - // Prepare transaction ------------------------------------------------------- - const transactionBlob = { - "TransactionType": "NFTokenCreateOffer", - "Account": operational_wallet.classicAddress, - "Owner": operationalOwnerField.value, - "NFTokenID": operationalTokenIdField.value, - "Amount": operationalAmountField.value, - "Flags": null, - } - if (expirationDate != null) { - transactionBlob.Expiration = expirationDate - } - // Submit transaction -------------------------------------------------------- - const tx = await client.submitAndWait(transactionBlob,{wallet: operational_wallet}) - - results += "\n\n***Sell Offers***\n" - let nftSellOffers - try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftSellOffers = "No sell offers." - } - results += JSON.stringify(nftSellOffers,null,2) - results += "\n\n***Buy Offers***\n" - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - results += "No buy offers." - } - results += JSON.stringify(nftBuyOffers,null,2) - - // Check transaction results ------------------------------------------------- - results += "\n\nTransaction result:\n" + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\n\nBalance changes:\n" + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - operationalResultField.value = results - - client.disconnect() -}// End of oPcreateBuyOffer() - -// ******************************************************* -// ************* Operational Cancel Offer **************** -// ******************************************************* - -async function oPcancelOffer() { - - const wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results += "\nConnected. Cancelling offer..." - operationalResultField.value = results - - const tokenOfferIDs = [operationalTokenOfferIndexField.value] - - // Prepare transaction ------------------------------------------------------- - const transactionBlob = { - "TransactionType": "NFTokenCancelOffer", - "Account": wallet.classicAddress, - "NFTokenOffers": tokenOfferIDs - } - // Submit transaction -------------------------------------------------------- - const tx = await client.submitAndWait(transactionBlob,{wallet}) - - results += "\n\n***Sell Offers***\n" - let nftSellOffers - try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftSellOffers = "No sell offers." - } - results += JSON.stringify(nftSellOffers,null,2) - results += "\n\n***Buy Offers***\n" - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftBuyOffers = "No buy offers." - } - results += JSON.stringify(nftBuyOffers,null,2) - - // Check transaction results ------------------------------------------------- - - results += "\nTransaction result:\n" + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\nBalance changes:\n" + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - operationalResultField.value = results - - client.disconnect() -}// End of oPcancelOffer() - -// ******************************************************* -// **************** Operational Get Offers *************** -// ******************************************************* - -async function oPgetOffers() { -// const standby_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results += '\nConnected. Getting offers...' - - results += '\n\n***Sell Offers***\n' - - let nftSellOffers - try { - nftSellOffers = await client.request({ - method: "nft_sell_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftSellOffers = 'No sell offers.' - } - results += JSON.stringify(nftSellOffers,null,2) - - results += '\n\n***Buy Offers***\n' - let nftBuyOffers - try { - nftBuyOffers = await client.request({ - method: "nft_buy_offers", - nft_id: operationalTokenIdField.value - }) - } catch (err) { - nftBuyOffers = 'No buy offers.' - } - results += JSON.stringify(nftBuyOffers,null,2) - operationalResultField.value = results - - client.disconnect() -}// End of oPgetOffers() - -// ******************************************************* -// *************** Operational Accept Sell Offer ********* -// ******************************************************* - -async function oPacceptSellOffer() { - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results += '\nConnected. Accepting sell offer...\n\n' - operationalResultField.value = results - - // Prepare transaction ------------------------------------------------------- - const transactionBlob = { - "TransactionType": "NFTokenAcceptOffer", - "Account": operational_wallet.classicAddress, - "NFTokenSellOffer": operationalTokenOfferIndexField.value, - } - // Submit transaction -------------------------------------------------------- - const tx = await client.submitAndWait(transactionBlob,{wallet: operational_wallet}) - const nfts = await client.request({ - method: "account_nfts", - account: operational_wallet.classicAddress - }) - - // Check transaction results ------------------------------------------------- - - standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) - operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) - - results += 'Transaction result:\n' - results += JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += '\nBalance changes:' - results += JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - results += JSON.stringify(nfts,null,2) - operationalResultField.value = results - client.disconnect() -}// End of acceptSellOffer() - -// ******************************************************* -// ********* Operational Accept Buy Offer **************** -// ******************************************************* - -async function oPacceptBuyOffer() { - const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) - const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) - let net = getNet() - const client = new xrpl.Client(net) - results = 'Connecting to ' + net + '...' - operationalResultField.value = results - await client.connect() - results += '\nConnected. Accepting buy offer...' - operationalResultField.value = results - - // Prepare transaction ------------------------------------------------------- - const transactionBlob = { - "TransactionType": "NFTokenAcceptOffer", - "Account": operational_wallet.classicAddress, - "NFTokenBuyOffer": operationalTokenOfferIndexField.value - } - // Submit transaction -------------------------------------------------------- - const tx = await client.submitAndWait(transactionBlob,{wallet: operational_wallet}) - const nfts = await client.request({ - method: "account_nfts", - account: operational_wallet.classicAddress - }) - results += JSON.stringify(nfts,null,2) - operationalResultField.value = results - - // Check transaction results ------------------------------------------------- - results += "\n\nTransaction result:\n" + - JSON.stringify(tx.result.meta.TransactionResult, null, 2) - results += "\nBalance changes:\n" + - JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) - operationalBalanceField.value = - (await client.getXrpBalance(operational_wallet.address)) - operationalBalanceField.value = - (await client.getXrpBalance(standby_wallet.address)) - operationalResultField.value = results - client.disconnect() -}// End of acceptBuyOffer() +} // End of acceptBuyOffer() ``` ## 4.transfer-nfts.html -Update the form with fields and buttons to support the new functions. - ```html - - Token Test Harness + + Transfer NFTs - - - - - - - - - - - - - - -

Token Test Harness

+ + + + + + + + + +

Transfer NFTs

- Choose your ledger instance: -    - - -    - - -

- -
- -

- - - - - -
- - + + Choose your ledger instance: + +    + + +    + + +

+
+ + - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Standby Account - - -
-
- Public Key - - -
-
- Private Key - - -
-
- Seed - - -
-
- XRP Balance - - -
-
- Amount - - -
-
- - -
- Currency - - -
NFT URL -
Flags
NFT ID
NFT Offer Index
Owner
Destination
Expiration
Transfer Fee
-

- -

-
- - - - - - -
- -

- -
- -
- -

- -
- -
- -

- -
- -
- -
- -
- -
- -
+
-
- - - -
- - - - - - - - - -
- -

- -
- -
- -

- -
- -
- -

- -
- -
- -
- -
- -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Operational Account - - -
-
- Public Key - - -
-
- Private Key - - -
-
- Seed - - -
-
- XRP Balance - - -
-
- Amount - - -
-
- - - -
- Currency - - -
NFT URL -
Flags
NFT ID
NFT Offer Index
Owner
Destination
Expiration
Transfer Fee
-

- -

-
+
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + +

+ +

+ + + + + + + +
+ + + + + +
+
+ + + + + +
+ + + + + + +
+

+
+ + + + + +
+
+ +
+ + + + + +
+
+    + + +
+ + + + + +
+
+    + + +
+ + + + + +
+
+    + +
+ + + + + +
+ + + + + +
+ + + + + +
+ + + + + +
+

+ +

+
+
+ +
- + + ```