# Trade fungible tokens in the XRPL Decentralized Exchange

In this tutorial you'll learn how to trade XRP fungible tokens in the Decentralized Exchange (DEX) with Ripple Custody.

## Learning objectives

By the end of this tutorial, you should be able to:

- Create an offer for fungible tokens on the DEX.
- Match an offer on the DEX to complete a trade.


## How Ripple Custody trades tokens on the DEX

Ripple Custody trades on the DEX by creating an **immediate-or-cancel** or **fill-or-kill** offer, both of which must immediately match an offer that already exists on the DEX.

For example:

- A seller account creates an offer of 1 XRP for 10 tokens.
- A token holder account creates an offer of 11 tokens for 1 XRP.


The image below shows this trade:

![](/assets/xrpl-ft-flow.80e93fb53630fc940ca776e6d442a8079c66b119215cebaf0342b97b7120e510.53c01680.png)

## Trade tokens — flow

The workflow to trade fungible tokens on the DEX is as follows:

1. **Create a seller offer** — Create an offer from a seller account on the DEX.
2. **Create a matching buyer offer** — Create an offer from another account that matches the seller offer.
3. **View the transaction details** — Check the details of the transaction and associated transfer.


## Trade tokens — instructions

### Prerequisites

Before you begin, we recommend you read the [Issue fungible tokens on the XRP Ledger](/products/custody/v1.15/resources/tutorials/xrpl-ft-issue) tutorial.

For this tutorial, you will need:

- A fungible token to trade. In this tutorial we'll use a token asset called **KBO**.
- An external **Seller** account, with a balance of XRP native currency.
- A Ripple Custody token **Holder** account, with a balance of XRP native currency and some **KBO** tokens issued from an issuer account.
For more information on how to create and hold tokens in an account, see the [Issue fungible tokens on the XRP Ledger](/products/custody/v1.15/resources/tutorials/xrpl-ft-issue) tutorial.
- A trust line between the holder account and the account that issued the tokens. This can be the same as the seller account, but it can also be a different account. For more information, see [Link the issuer and holder accounts with a trust line](/products/custody/v1.15/resources/tutorials/xrpl-ft-issue#link-the-issuer-and-holder-accounts-with-a-trust-line).


Tip
For more information on how to:

- Create and fund Ripple Custody accounts, see [Create and fund accounts](#create-and-fund-accounts).
- Fund external accounts, see [Fund external accounts](#fund-external-accounts).


### Create a seller offer

Create an offer on the DEX of 1 XRP for 10 KBO from a seller account. Since this is an external account, you need to create the offer using `OfferCreate` from the seller environment.

For more information, see Trade in the Decentralized Exchange in the XRPL documentation.

The offer is now available on the XRP Ledger:

![](/assets/xrpl-ft-offercreate.d764647afad0386b63a03b9411be2824b272845572d59509c77f63e938f04122.53c01680.png)

### Create a matching buyer offer

Create a matching offer in Ripple Custody from **Holder** using a transaction order. The offer is 11 KBO for 1 XRP.

To create an offer:

- With the UI, see [Ripple Custody UI > Create an offer to trade on the DEX](/products/custody/v1.15/ui/accounting/tokens/xrpl#create-an-offer-to-trade-on-the-dex).
The transaction order details are as follows:
![](/assets/xrpl-ft-offercreate2.5782f500f1a44b9d355d93c5c014cc4bb9bbc6a2b646a26d150ef13a13f23cff.53c01680.png)
- With the API, see [Ripple Custody API > Create an offer to trade on the DEX](/products/custody/v1.15/api/accounting/tokens/xrpl#create-an-offer-to-trade-on-the-dex).
The payload details are as follows:

```json Payload example
{
  "payload": {
    "id": "58129d8f-3ff4-4696-8cab-0d649634fbde",
    "ledgerId": "xrpl-testnet-august-2024",
    "accountId": "29ef6ffe-e24e-474f-ad00-17cd2e016b6b",
    "parameters": {
      "operation": {
        "type": "OfferCreate",
        "flags": [
          "tfImmediateOrCancel"
        ],
        "takerGets": {
          "amount": "11000000000000000000000000000000000000000000000000000000000000000000000000000000000",
          "currency": {
            "type": "TickerId",
            "tickerId": "13aac11d-7cfe-481a-8082-8e663eee60bd"
          }
        },
        "takerPays": {
          "amount": "1000000"
        }
      },
      "memos": [],
      "feeStrategy": {
        "priority": "Low",
        "type": "Priority"
      },
      "type": "XRPL"
    },
    "description": null,
    "customProperties": {},
    "type": "v0_CreateTransactionOrder"
  }
}
```


Since the offer of 11 KBO is higher than the amount sought by the seller, the offer matches successfully and the trade goes ahead.

### View the transaction details

To see details of the confirmed transaction and associated objects in Ripple Custody:

- With the UI, see [Ripple Custody UI > View transactions](/products/custody/v1.15/ui/accounting/transactions/view)
- With the API, see [Ripple Custody API > View transactions](/products/custody/v1.15/api/accounting/transactions/view)


The transaction details are as follows:

![](/assets/xrpl-ft-sell-transaction.9f07d854e384456057a162247a789596ecbe60518eaf4779023fd3d105e6e21c.53c01680.png)

With the transfer of 1 XRP:

![](/assets/xrpl-ft-sell-transfer.5b614442d88b3ed9425959374f1d8f5c6358a086024463d467b3871d273a2343.53c01680.png)

The ledger shows the transaction executed successfully:

![](/assets/xrpl-ft-sell-ledger.fe398e5161a6a6023568c5ff02d559e4c056f7c61af9697d3c5cf4cbff983776.53c01680.png)

## Further reading

### XRPL documentation

Trade in the Decentralized Exchange

### Ripple Custody documentation

#### Issue XRPL fungible tokens

- [Tutorials > Issue fungible tokens on the XRP Ledger](/products/custody/v1.15/resources/tutorials/xrpl-ft-issue)


#### Create and fund accounts

- [Ripple Custody UI > Create accounts](/products/custody/v1.15/ui/accounting-entities/account/create)
- [Ripple Custody API > Create accounts](/products/custody/v1.15/api/accounting-entities/account/create)
- [Ripple Custody UI > Receive assets](/products/custody/v1.15/ui/accounting/receive-assets)
- [Ripple Custody API > Receive assets](/products/custody/v1.15/api/accounting/receive-assets)


#### Fund external accounts

- [Ripple Custody UI > Send assets](/products/custody/v1.15/ui/accounting/send-assets)
- [Ripple Custody API > Send assets](/products/custody/v1.15/api/accounting/send-assets)


#### View account balances

- [Ripple Custody UI > Check account balances](/products/custody/v1.15/ui/accounting-entities/account/view#check-account-balances)
- [Ripple Custody API > Check account balances](/products/custody/v1.15/api/accounting-entities/account/balances#check-account-balances)