# Create Offers This example shows how to: 1. Create currency offers. 2. Retrieve active offers. 3. Match a currency offer to exchange tokens. 4. Cancel an unsettled offer. [](/assets/mt-create-offers-1-empty-form-info.399f4fc313e687c08824419ab0bcdef6c64aaf1816b647258497f15d28a033f2.ac57e6ef.png) Download and expand the [Modular Tutorials](../../../../_code-samples/modular-tutorials/payment-modular-tutorials.zip) archive. **Note:** Without the Modular Tutorial Samples, you will not be able to try the examples that follow. ## Usage To get test accounts: 1. Open `create-offers.html` in a browser. 2. Choose your preferred test network (**Devnet** or **Testnet**). 3. Get test accounts. 1. If you copied the gathered information from another tutorial: 1. Paste the gathered information to the **Result** field. 2. Click **Distribute Account Info**. 2. If you have an existing account seed: 1. Paste the account seed to the **Account 1 Seed** or **Account 2 Seed** field. 2. Click **Get Account 1 from Seed** or **Get Account 2 from Seed**. 3. If you do not have existing accounts: 1. Click **Get New Account 1**. 2. Click **Get New Account 2**. [](/assets/mt-create-offers-2-form-with-account-info.68124deefdb6b9ce5788d4ef59f57727f9477779c24997b9ef1d7ba9b83151df.ac57e6ef.png) You can create and match offers from either account. ## Create Offer To create an offer to exchange XRP for an issued currency: 1. Click **Account 1** or **Account 2**. 2. Enter *XRP* as the Taker Pays **Currency Code**. 3. Enter the Taker Pays **Amount** in drops. For example, *50000000*. 4. Enter the Taker Gets **Currency**. For example, *USD*. 5. Copy the current **Account Address** and paste it in the Taker Gets **Issuer** field. 6. Enter the Taker Gets **Amount**. For example, *50*. 7. Click **Create Offer**. [](/assets/mt-create-offers-3-xrp-for-usd-offer.d0eeb832fda55c41a80f9574b790496f93d66dee4d8b030956d534042ca61590.ac57e6ef.png) ## Get Offers Click **Get Offers** to get a list of offers issued by the corresponding account. [](/assets/mt-create-offers-3-xrp-for-usd-offer.d0eeb832fda55c41a80f9574b790496f93d66dee4d8b030956d534042ca61590.ac57e6ef.png) ## Create a Matching Offer 1. Choose an account other than the Issuer. For example, **Account 2**. 2. Enter *XRP* as the Taker Gets **Currency Code**. 3. Enter the Taker Gets **Amount**. For example, *50000000*. 4. Enter the Taker Pays **Currency Code**, for example *USD*. 5. Enter the Taker Pays **Issuer**. For example, the **Account 1 Address**. 6. Enter the Taker Pays **Amount** For example, *50*. 7. Click **Create Offer**. [](/assets/mt-create-offers-4-matching-offer.d8ae3f9036a7055b34fe4076f79d2f78fc9dc0c1ad1befe0194c71849e5a68e6.ac57e6ef.png) ## Cancel Offer To cancel an existing offer: 1. Enter the sequence number of the offer in the **Offer Sequence** field. To find the sequence number, you can click **Get Offers**, then look for the *Seq* value for the offer you want to cancel. [](/assets/mt-create-offers-5-sequence-number.a13cd752a0743d435fd509c9dc958bf4f8256ab8b061cdb577869d0fd415c113.ac57e6ef.png) 1. Click **Cancel Offer**, then click **Get Offers** to show that the offer has been removed from the list of outstanding offers. [](/assets/mt-create-offers-6-no-offers.3ab0560f51d4a135e7b3c261c3b19afbc6336130d60d69da60acde0cfaaec8ab.ac57e6ef.png) # Code Walkthrough You can download the [Payment Modular Tutorials](/assets/payment-modular-tutorials.a27fd32656123cc667fdfb8949e8c79c60732fc4da4448548774646ed28d5e69.bb35afea.zip) from the source repository for this website. ## create-offer.js The functions in create-offer.html leverage functions from the base module. The functions that follow are solely focused on creating and handling offers. ### Create Offer Connect to the XRP Ledger and get the account wallet. ```javascript async function createOffer() { let net = getNet() const client = new xrpl.Client(net) await client.connect() let results = `===Connected to ${net}, getting wallet....===\n` resultField.value = results const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) ``` Gather the information for what the taker pays, and what the taker gets in return. If the **Currency Code** is *XRP*, the amount is equal to the value in the **Amount** field. Otherwise, the `takerGets` parameter is constructed as an array containing the currency code, issuer address, and the value in the amount field. ```javascript try { if (getCurrencyField.value == 'XRP') { takerGets = getAmountField.value } else { takerGetsString = '{"currency": "' + getCurrencyField.value +'",\n' + '"issuer": "' + getIssuerField.value + '",\n' + '"value": "' + getAmountField.value + '"}' takerGets = JSON.parse(takerGetsString) } ``` The same logic is used to create the value for the `takerPays` parameter. ```javascript if (payCurrencyField.value == 'XRP') { takerPays = xrpl.xrpToDrops(payAmountField.value) } else { takerPaysString = '{"currency": "' + payCurrencyField.value + '",\n' + '"issuer": "' + payIssuerField.value + '",\n' + '"value": "' + payAmountField.value + '"}' takerPays = JSON.parse(takerPaysString) } ``` Define the `OfferCreate` transaction, using the `takerPays` and `takerGets` parameters defined above. ```javascript const prepared = await client.autofill({ "TransactionType": "OfferCreate", "Account": wallet.address, "TakerGets": takerGets, "TakerPays": takerPays }) ``` Sign and send the prepared transaction, and wait for the results. ```javascript const signed = wallet.sign(prepared) const tx = await client.submitAndWait(signed.tx_blob) ``` Request the token balance changes after the transaction. ```javascript results = '\n\n===Offer created===\n\n' + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) resultField.value += results ``` Get the new XRP balance, reflecting the payments and transaction fees. ```javascript xrpBalanceField.value = (await client.getXrpBalance(wallet.address)) ``` ```javascript getOffers() ``` Catch and report any errors, then disconnect from the XRP Ledger. ```javascript } catch (err) { console.error('Error creating offer:', err); results = `\nError: ${err.message}\n` resultField.value += results throw err; // Re-throw the error to be handled by the caller } finally { // Disconnect from the client client.disconnect() }) ``` ### getOffers This function requests a list of all offers posted by an account. Connect to the XRP Ledger and get the Account wallet. ```javascript async function getOffers() { let net = getNet() const client = new xrpl.Client(net) await client.connect() let results = `===Connected to ' + ${net}, getting offers....===\n` const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) resultField.value = results ``` Send a request for all `account_offers` for the specified account address and report the results. ```javascript results += '\n\n*** Offers ***\n' let offers try { offers = await client.request({ method: "account_offers", account: wallet.address, ledger_index: "validated" }) results = JSON.stringify(offers, null, 2) resultField.value += results ``` Catch and report any errors, then disconnect from the XRP Ledger. ```javascript } catch (err) { console.error('Error getting offers:', err); results = `\nError: ${err.message}\n` resultField.value += results throw err; // Re-throw the error to be handled by the caller } finally { client.disconnect() } ``` ### cancelOffer() You can cancel an offer before it is matched with another offer. Connect to the XRP Ledger and get the account wallet. ```javascript async function cancelOffer() { let net = getNet() const client = new xrpl.Client(net) await client.connect() let results = `===Connected to ${net}, canceling offer.===\n` resultField.value = results const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) ``` Prepare the `OfferCancel` transaction, passing the account address of the account that created the offer and the `Sequence` of the offer. ```javascript try { const prepared = await client.autofill({ "TransactionType": "OfferCancel", "Account": wallet.address, "OfferSequence": parseInt(offerSequenceField.value) }) ``` Sign and submit the transaction, then wait for the result. ```javascript const signed = wallet.sign(prepared) const tx = await client.submitAndWait(signed.tx_blob) ``` Report the results. ```javascript results += "\nOffer canceled. Balance changes: \n" + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) resultField.value = results ``` Catch and report any errors, then disconnect from the XRP Ledger. ```javascript } catch (err) { console.error('Error canceling offer:', err); results = `\nError: ${err.message}\n` resultField.value += results throw err; // Re-throw the error to be handled by the caller } finally { client.disconnect() } }// End of cancelOffer() ``` ## create-offer.html ```html