Last updated

Issue a Fungible Token

Anyone can issue various types of tokens in the XRP Ledger, ranging from informal "IOUs" to fiat-backed stablecoins, purely digital fungible and semi-fungible tokens, and more. This tutorial shows the technical steps of creating a token in the ledger. For more information on how XRP Ledger tokens work, see Issued Currencies; for more on the business decisions involved in issuing a stablecoin, see Stablecoin Issuer.

Prerequisites

  • You need two funded XRP Ledger accounts, each with an address, secret key, and some XRP. For this tutorial, you can generate new test credentials as needed.
    • Each address needs enough XRP to satisfy the reserve requirement including the additional reserve for a trust line.
  • You need a connection to the XRP Ledger network. As shown in this tutorial, you can use public servers for testing.
  • You should be familiar with the Getting Started instructions for your preferred client library. This page provides examples for the following:

Example Code

Complete sample code for all of the steps of these tutorials is available under the MIT license.

Steps

1. Get Credentials

To transact on the XRP Ledger, you need an address and secret key, and some XRP. You also need one or more recipients who are willing to hold the tokens you issue: unlike in some other blockchains, in the XRP Ledger you cannot force someone to hold a token they do not want.

The best practice is to use "cold" and "hot" addresses. The cold address is the issuer of the token. The hot address is like a regular user's address that you control. It receives tokens from the cold address, which you can then transfer to other users. A hot address is not strictly necessary, since you could send tokens directly to users from the cold address, but it is good practice for security reasons. In production, you should take extra care of the cold address's cryptographic keys (for example, keeping them offline) because it is much harder to replace a cold address than a hot address.

In this tutorial, the hot address receives the tokens you issue from the cold address. You can get the keys for two addresses using the following interface.

Caution: Ripple provides the Testnet and Devnet for testing purposes only, and sometimes resets the state of these test networks along with all balances. As a precaution, do not use the same addresses on Testnet/Devnet and Mainnet.

When you're building production-ready software, you should use an existing account, and manage your keys using a secure signing configuration.

2. Connect to the Network

You must be connected to the network to submit transactions to it. The following code shows how to connect to a public XRP Ledger Testnet server with a supported client library:

  1. JavaScript
  2. Python
  3. Java
// In browsers, use a <script> tag. In Node.js, uncomment the following line:
// const xrpl = require('xrpl')

// Wrap code in an async function so we can use await
async function main() {

  // Define the network client
  const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
  await client.connect()

  // ... custom code goes here

  // Disconnect when done (If you omit this, Node.js won't end the process)
  await client.disconnect()
}

main()

Note: The JavaScript code samples in this tutorial use the async/await pattern. Since await needs to be used from within an async function, the remaining code samples are written to continue inside the main() function started here. You can also use Promise methods .then() and .catch() instead of async/await if you prefer.

For this tutorial, click the following button to connect:

3. Configure Issuer Settings

First, configure the settings for your cold address (which will become the issuer of your token). Most settings can be reconfigured later, with the following exceptions:

  • Default Ripple: This setting is required so that users can send your token to each other. It's best to enable it before setting up any trust lines or issuing any tokens.
  • Authorized Trust Lines: (Optional) This setting (also called "Require Auth") limits your tokens to being held only by accounts you've explicitly approved. You cannot enable this setting if you already have any trust lines or offers for any token. Note: To use authorized trust lines, you must perform additional steps that are not shown in this tutorial.

Other settings you may want to, optionally, configure for your cold address (issuer):

SettingRecommended ValueSummary
Require Destination TagsEnabled or DisabledEnable if you process withdrawals of your token to outside systems. (For example, your token is a stablecoin.)
Disallow XRPEnabled or DisabledEnable if this address isn't meant to process XRP payments.
Transfer Fee0–1%Charge a percentage fee when users send your token to each other.
Tick Size5Limit the number of decimal places in exchange rates for your token in the decentralized exchange. A tick size of 5-6 reduces churn of almost-equivalent offers and speeds up price discovery compared to the default of 15.
Domain(Your domain name)Set to a domain you own so can verify ownership of the accounts. This can help reduce confusion or impersonation attempts.

You can change these settings later as well.

Note: Many issuing settings apply equally to all tokens issued by an address, regardless of the currency code. If you want to issue multiple types of tokens in the XRP Ledger with different settings, you should use a different address to issue each different token.

The following code sample shows how to send an AccountSet transaction to enable the recommended cold address settings:

  1. JavaScript
  2. Python
  3. Java
  // Configure issuer (cold address) settings ----------------------------------
  const cold_settings_tx = {
    "TransactionType": "AccountSet",
    "Account": cold_wallet.address,
    "TransferRate": 0,
    "TickSize": 5,
    "Domain": "6578616D706C652E636F6D", // "example.com"
    "SetFlag": xrpl.AccountSetAsfFlags.asfDefaultRipple,
    // Using tf flags, we can enable more flags in one transaction
    "Flags": (xrpl.AccountSetTfFlags.tfDisallowXRP |
             xrpl.AccountSetTfFlags.tfRequireDestTag)
  }

  const cst_prepared = await client.autofill(cold_settings_tx)
  const cst_signed = cold_wallet.sign(cst_prepared)
  console.log("Sending cold address AccountSet transaction...")
  const cst_result = await client.submitAndWait(cst_signed.tx_blob)
  if (cst_result.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${cst_signed.hash}`)
  } else {
    throw `Error sending transaction: ${cst_result}`
  }


  
%
(text)
(hexadecimal)
(loading)Sending transaction...

4. Wait for Validation

Most transactions are accepted into the next ledger version after they're submitted, which means it may take 4-7 seconds for a transaction's outcome to be final. You should wait for your earlier transactions to be fully validated before proceeding to the later steps, to avoid unexpected failures from things executing out of order. For more information, see Reliable Transaction Submission.

The code samples in this tutorial use helper functions to wait for validation when submitting a transaction:

Tip: Technically, you can configure the hot address in parallel with configuring the issuer address. For simplicity, this tutorial waits for each transaction one at a time.

Transaction ID:(None)
Latest Validated Ledger Index:(Not connected)
Ledger Index at Time of Submission:(Not submitted)
Transaction LastLedgerSequence:(Not prepared)

5. Configure Hot Address Settings

The hot address does not strictly require any settings changes from the default, but the following are recommended as best practices:

SettingRecommended ValueSummary
Default RippleDisabledLeave this setting disabled. (This is the default.)
Authorized Trust LinesEnabledEnable this setting on the hot address—and never approve any trust lines to the hot address—to prevent accidentally issuing tokens from the wrong address. (Optional, but recommended.)
Require Destination TagsEnabled or DisabledEnable if you process withdrawals of your token to outside systems. (For example, your token is a stablecoin.)
Disallow XRPEnabled or DisabledEnable if this address isn't meant to process XRP payments.
Domain(Your domain name)Set to a domain you own so can verify ownership of the accounts. This can help reduce confusion or impersonation attempts.

The following code sample shows how to send an AccountSet transaction to enable the recommended hot address settings:

  1. JavaScript
  2. Python
  3. Java
  // Configure hot address settings --------------------------------------------

  const hot_settings_tx = {
    "TransactionType": "AccountSet",
    "Account": hot_wallet.address,
    "Domain": "6578616D706C652E636F6D", // "example.com"
    // enable Require Auth so we can't use trust lines that users
    // make to the hot address, even by accident:
    "SetFlag": xrpl.AccountSetAsfFlags.asfRequireAuth,
    "Flags": (xrpl.AccountSetTfFlags.tfDisallowXRP |
              xrpl.AccountSetTfFlags.tfRequireDestTag)
  }

  const hst_prepared = await client.autofill(hot_settings_tx)
  const hst_signed = hot_wallet.sign(hst_prepared)
  console.log("Sending hot address AccountSet transaction...")
  const hst_result = await client.submitAndWait(hst_signed.tx_blob)
  if (hst_result.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${hst_signed.hash}`)
  } else {
    throw `Error sending transaction: ${hst_result.result.meta.TransactionResult}`
  }


  
(text)
(hexadecimal)
(loading)Sending transaction...

6. Wait for Validation

As before, wait for the previous transaction to be validated by consensus before continuing.

Transaction ID:(None)
Latest Validated Ledger Index:(Not connected)
Ledger Index at Time of Submission:(Not submitted)
Transaction LastLedgerSequence:(Not prepared)

7. Create Trust Line from Hot to Cold Address

Before you can receive tokens, you need to create a trust line to the token issuer. This trust line is specific to the currency code of the token you want to issue, such as USD or FOO. You can choose any currency code you want; each issuer's tokens are treated as separate in the XRP Ledger protocol. However, users' balances of tokens with the same currency code can ripple between different issuers if the users enable rippling settings.

The hot address needs a trust line like this before it can receive tokens from the issuer. Similarly, each user who wants to hold your token must also create a trust line¹. Each trust line increases the reserve requirement of the hot address, so you must hold enough spare XRP to pay for the increased requirement. Your reserve requirement goes back down if you remove the trust line.

Tip: A trust line has a "limit" on how much the recipient is willing to hold; others cannot send you more tokens than your specified limit. For community credit systems, you may want to configure limits per individual based on how much you trust that person. For other types and uses of tokens, it is normally OK to set the limit to a very large number.

To create a trust line, send a TrustSet transaction from the hot address with the following fields:

FieldValue
TransactionType"TrustSet"
AccountThe hot address. (More generally, this is the account that wants to receive the token.)
LimitAmountAn object specifying how much, of which token, from which issuer, you are willing to hold.
LimitAmount.currencyThe currency code of the token.
LimitAmount.issuerThe cold address.
LimitAmount.valueThe maximum amount of the token you are willing to hold.

The following code sample shows how to send a TrustSet transaction from the hot address, trusting the issuing address for a limit of 1 billion FOO:

  1. JavaScript
  2. Python
  3. Java
  // Create trust line from hot to cold address --------------------------------
  const currency_code = "FOO"
  const trust_set_tx = {
    "TransactionType": "TrustSet",
    "Account": hot_wallet.address,
    "LimitAmount": {
      "currency": currency_code,
      "issuer": cold_wallet.address,
      "value": "10000000000" // Large limit, arbitrarily chosen
    }
  }

  const ts_prepared = await client.autofill(trust_set_tx)
  const ts_signed = hot_wallet.sign(ts_prepared)
  console.log("Creating trust line from hot address to issuer...")
  const ts_result = await client.submitAndWait(ts_signed.tx_blob)
  if (ts_result.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${ts_signed.hash}`)
  } else {
    throw `Error sending transaction: ${ts_result.result.meta.TransactionResult}`
  }

    // Create trust line from customer_one to cold address --------------------------------
  const trust_set_tx2 = {
    "TransactionType": "TrustSet",
    "Account": customer_one_wallet.address,
    "LimitAmount": {
      "currency": currency_code,
      "issuer": cold_wallet.address,
      "value": "10000000000" // Large limit, arbitrarily chosen
    }
  }

  const ts_prepared2 = await client.autofill(trust_set_tx2)
  const ts_signed2 = customer_one_wallet.sign(ts_prepared2)
  console.log("Creating trust line from customer_one address to issuer...")
  const ts_result2 = await client.submitAndWait(ts_signed2.tx_blob)
  if (ts_result2.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${ts_signed2.hash}`)
  } else {
    throw `Error sending transaction: ${ts_result2.result.meta.TransactionResult}`
  }


  const trust_set_tx3 = {
    "TransactionType": "TrustSet",
    "Account": customer_two_wallet.address,
    "LimitAmount": {
      "currency": currency_code,
      "issuer": cold_wallet.address,
      "value": "10000000000" // Large limit, arbitrarily chosen
    }
  }

  const ts_prepared3 = await client.autofill(trust_set_tx3)
  const ts_signed3 = customer_two_wallet.sign(ts_prepared3)
  console.log("Creating trust line from customer_two address to issuer...")
  const ts_result3 = await client.submitAndWait(ts_signed3.tx_blob)
  if (ts_result3.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${ts_signed3.hash}`)
  } else {
    throw `Error sending transaction: ${ts_result3.result.meta.TransactionResult}`
  }




  

Note: If you use Authorized Trust Lines, there is an extra step after this one: the cold address must approve the trust line from the hot address. For details of how to do this, see Authorizing Trust Lines.

8. Wait for Validation

As before, wait for the previous transaction to be validated by consensus before continuing.

Transaction ID:(None)
Latest Validated Ledger Index:(Not connected)
Ledger Index at Time of Submission:(Not submitted)
Transaction LastLedgerSequence:(Not prepared)

9. Send Token

Now you can create tokens by sending a Payment transaction from the cold address to the hot address. This transaction should have the following attributes (dot notation indicates nested fields):

FieldValue
TransactionType"Payment"
AccountThe cold address issuing the token.
AmountAn token amount specifying how much of which token to create.
Amount.currencyThe currency code of the token.
Amount.valueDecimal amount of the token to issue, as a string.
Amount.issuerThe cold address issuing the token.
DestinationThe hot address (or other account receiving the token)
PathsOmit this field when issuing tokens.
SendMaxOmit this field when issuing tokens.
DestinationTagAny whole number from 0 to 232-1. You must specify something here if you enabled Require Destination Tags on the hot address.

You can use auto-filled values for all other required fields.

The following code sample shows how to send a Payment transaction to issue 88 FOO from the cold address to the hot address:

  1. JavaScript
  2. Python
  3. Java
  // Send token ----------------------------------------------------------------
  let issue_quantity = "3800"

  const send_token_tx = {
    "TransactionType": "Payment",
    "Account": cold_wallet.address,
    "Amount": {
      "currency": currency_code,
      "value": issue_quantity,
      "issuer": cold_wallet.address
    },
    "Destination": hot_wallet.address,
    "DestinationTag": 1 // Needed since we enabled Require Destination Tags
                        // on the hot account earlier.
  }

  const pay_prepared = await client.autofill(send_token_tx)
  const pay_signed = cold_wallet.sign(pay_prepared)
  console.log(`Cold to hot - Sending ${issue_quantity} ${currency_code} to ${hot_wallet.address}...`)
  const pay_result = await client.submitAndWait(pay_signed.tx_blob)
  if (pay_result.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${pay_signed.hash}`)
  } else {
    console.log(pay_result)
    throw `Error sending transaction: ${pay_result.result.meta.TransactionResult}`
  }


  issue_quantity = "100"
  const send_token_tx2 = {
    "TransactionType": "Payment",
    "Account": hot_wallet.address,
    "Amount": {
      "currency": currency_code,
      "value": issue_quantity,
      "issuer": cold_wallet.address
    },
    "Destination": customer_one_wallet.address,
    "DestinationTag": 1 // Needed since we enabled Require Destination Tags
                        // on the hot account earlier.
  }

  const pay_prepared2 = await client.autofill(send_token_tx2)
  const pay_signed2 = hot_wallet.sign(pay_prepared2)
  console.log(`Hot to customer_one - Sending ${issue_quantity} ${currency_code} to ${customer_one_wallet.address}...`)
  const pay_result2 = await client.submitAndWait(pay_signed2.tx_blob)
  if (pay_result2.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${pay_signed2.hash}`)
  } else {
    console.log(pay_result2)
    throw `Error sending transaction: ${pay_result2.result.meta.TransactionResult}`
  }


  issue_quantity = "12"
  const send_token_tx3 = {
    "TransactionType": "Payment",
    "Account": customer_one_wallet.address,
    "Amount": {
      "currency": currency_code,
      "value": issue_quantity,
      "issuer": cold_wallet.address
    },
    "Destination": customer_two_wallet.address,
    "DestinationTag": 1 // Needed since we enabled Require Destination Tags
                        // on the hot account earlier.
  }

  const pay_prepared3 = await client.autofill(send_token_tx3)
  const pay_signed3 = customer_one_wallet.sign(pay_prepared3)
  console.log(`Customer_one to customer_two - Sending ${issue_quantity} ${currency_code} to ${customer_two_wallet.address}...`)
  const pay_result3 = await client.submitAndWait(pay_signed3.tx_blob)
  if (pay_result3.result.meta.TransactionResult == "tesSUCCESS") {
    console.log(`Transaction succeeded: https://testnet.xrpl.org/transactions/${pay_signed3.hash}`)
  } else {
    console.log(pay_result3)
    throw `Error sending transaction: ${pay_result3.result.meta.TransactionResult}`
  }


  

10. Wait for Validation

As before, wait for the previous transaction to be validated by consensus before continuing.

Transaction ID:(None)
Latest Validated Ledger Index:(Not connected)
Ledger Index at Time of Submission:(Not submitted)
Transaction LastLedgerSequence:(Not prepared)

11. Confirm Token Balances

You can check the balances of your token from the perspective of either the token issuer or the hot address. Tokens issued in the XRP Ledger always have balances that sum to 0: negative from the perspective of the issuer and positive from the perspective of the holder.

Use the account_lines method to look up the balances from the perspective of the holder. This lists each trust line along with its limit, balance, and settings.

Use the gateway_balances method to look up balances from the perspective of a token issuer. This provides a sum of all tokens issued by a given address.

Tip: Since the XRP Ledger is fully public, you can check the balances of any account at any time without needing any cryptographic keys.

The following code sample shows how to use both methods:

  1. JavaScript
  2. Python
  3. Java
  // Check balances ------------------------------------------------------------
  console.log("Getting hot address balances...")
  const hot_balances = await client.request({
    command: "account_lines",
    account: hot_wallet.address,
    ledger_index: "validated"
  })
  console.log(hot_balances.result)

  console.log("Getting cold address balances...")
  const cold_balances = await client.request({
    command: "gateway_balances",
    account: cold_wallet.address,
    ledger_index: "validated",
    hotwallet: [hot_wallet.address]
  })
  console.log(JSON.stringify(cold_balances.result, null, 2))

  client.disconnect()
} 

Next Steps

Now that you've created the token, you can explore how it fits into features of the XRP Ledger:

  • Send tokens from the hot address to other users.
  • Trade it in the decentralized exchange.
  • Monitor for incoming payments of your token.
  • Create an xrp-ledger.toml file and set up domain verification for your token's issuer.
  • Learn about other features of XRP Ledger tokens.

Footnotes

¹ Users can hold your token without explicitly creating a trust line if they buy your token in the decentralized exchange. Buying a token in the exchange automatically creates the necessary trust lines. This is only possible if someone is selling your token in the decentralized exchange.